v0.7.2 → v0.7.4 两个补丁版本。逐文件 diff 后确认无影响本项目的破坏性变更: paintBlock 的签名改动只影响自定义 decor,本项目没有实现。 - fileops.go:删除弹窗用上新的 DestructiveButtons,把「永久删除」标出来 - view.go:补注释说明 NoScrollPastEnd 在 v0.7.4 下仍然必要 - 评估报告记录升级验证过程与滚动条行为变化 验证:build/vet/gofmt 全绿,lsp 与 app 包 -count=1 全绿(含真 gopls 与 右键菜单的 ui.Tester 用例);另编了 v0.7.2 二进制做像素级对照,差异仅在 滚动条滑块是否常驻。
307 lines
7.7 KiB
Go
307 lines
7.7 KiB
Go
package app
|
||
|
||
import (
|
||
"fmt"
|
||
"os"
|
||
"path/filepath"
|
||
"sort"
|
||
|
||
"github.com/egoist/mygo"
|
||
)
|
||
|
||
// selected 判断路径是否被选中。
|
||
func (a *App) selected(path string) bool { return a.sel != nil && a.sel[path] }
|
||
|
||
// selectOnly 只选中这一项(普通点击)。
|
||
func (a *App) selectOnly(path string) {
|
||
a.sel = map[string]bool{path: true}
|
||
a.selAnchor = path
|
||
}
|
||
|
||
// toggleSelect 切换某一项的选中状态(Ctrl 点击)。
|
||
func (a *App) toggleSelect(path string) {
|
||
if a.sel == nil {
|
||
a.sel = map[string]bool{}
|
||
}
|
||
if a.sel[path] {
|
||
delete(a.sel, path)
|
||
} else {
|
||
a.sel[path] = true
|
||
}
|
||
a.selAnchor = path
|
||
}
|
||
|
||
// selectRange 选中从基准项到当前项之间的全部(Shift 点击)。
|
||
// 区间按「树里当前可见的顺序」算,折叠着的子树不参与。
|
||
func (a *App) selectRange(path string) {
|
||
all := a.visiblePaths()
|
||
from, to := -1, -1
|
||
for i, p := range all {
|
||
if samePath(p, a.selAnchor) {
|
||
from = i
|
||
}
|
||
if samePath(p, path) {
|
||
to = i
|
||
}
|
||
}
|
||
if from < 0 || to < 0 {
|
||
a.selectOnly(path)
|
||
return
|
||
}
|
||
if from > to {
|
||
from, to = to, from
|
||
}
|
||
a.sel = make(map[string]bool, to-from+1)
|
||
for _, p := range all[from : to+1] {
|
||
a.sel[p] = true
|
||
}
|
||
}
|
||
|
||
// visiblePaths 按当前展开状态深度优先列出树上可见的项。
|
||
func (a *App) visiblePaths() []string {
|
||
var out []string
|
||
var walk func(dir string)
|
||
walk = func(dir string) {
|
||
for _, e := range a.entries(dir) {
|
||
p := filepath.Join(dir, e.Name())
|
||
if e.IsDir() {
|
||
if skipDir(e.Name()) {
|
||
continue
|
||
}
|
||
out = append(out, p)
|
||
if *a.openFlag(p) {
|
||
walk(p)
|
||
}
|
||
continue
|
||
}
|
||
if skipFile(e.Name()) {
|
||
continue
|
||
}
|
||
out = append(out, p)
|
||
}
|
||
}
|
||
if a.root != "" {
|
||
walk(a.root)
|
||
}
|
||
return out
|
||
}
|
||
|
||
// targetsFor 返回一次操作要作用的路径:右键的那一行如果在选中集合里,
|
||
// 就作用于整个集合,否则只作用于它自己。
|
||
func (a *App) targetsFor(path string) []string {
|
||
if a.sel[path] && len(a.sel) > 0 {
|
||
out := make([]string, 0, len(a.sel))
|
||
for p := range a.sel {
|
||
out = append(out, p)
|
||
}
|
||
sort.Strings(out)
|
||
return out
|
||
}
|
||
return []string{path}
|
||
}
|
||
|
||
// treeMenu 是文件树上的一行的右键菜单。
|
||
func (a *App) treeEntries(path string, isDir bool) []menuEntry {
|
||
dir := path
|
||
if !isDir {
|
||
dir = filepath.Dir(path)
|
||
}
|
||
targets := a.targetsFor(path)
|
||
var out []menuEntry
|
||
|
||
out = append(out,
|
||
menuEntry{label: "新建文件", action: func() { a.beginNew(dir, false) }},
|
||
menuEntry{label: "新建文件夹", action: func() { a.beginNew(dir, true) }},
|
||
menuEntry{divider: true},
|
||
)
|
||
|
||
if !isDir && len(targets) == 1 {
|
||
out = append(out, menuEntry{label: "打开", action: func() { go a.OpenFile(path) }})
|
||
}
|
||
// 多选时不做重命名:一次改一个更安全。
|
||
if len(targets) == 1 {
|
||
out = append(out, menuEntry{label: "重命名…", action: func() { a.BeginRename(path, isDir) }})
|
||
}
|
||
// 历史入口放在这里:右键一个文件看「它改过什么」是最自然的时机。
|
||
if !isDir || len(targets) == 1 {
|
||
out = append(out, menuEntry{divider: true})
|
||
out = append(out, menuEntry{
|
||
label: "Git 历史…",
|
||
action: func() { a.OpenGitLog(path, isDir) },
|
||
})
|
||
out = append(out, menuEntry{
|
||
label: "本地历史…",
|
||
action: func() { a.OpenHistoryAt(path, isDir) },
|
||
})
|
||
}
|
||
out = append(out, menuEntry{
|
||
label: "在资源管理器中打开",
|
||
action: func() { mygo.Shell.ShowItemInFolder(path) },
|
||
})
|
||
out = append(out, menuEntry{divider: true})
|
||
|
||
del := "删除"
|
||
if len(targets) > 1 {
|
||
del = fmt.Sprintf("删除这 %d 项", len(targets))
|
||
}
|
||
out = append(out, menuEntry{label: del, action: func() { a.deletePaths(targets) }})
|
||
out = append(out, menuEntry{divider: true})
|
||
out = append(out, menuEntry{label: "刷新文件树", action: func() { a.Refresh() }})
|
||
return out
|
||
}
|
||
|
||
// deletePaths 删除文件或目录。
|
||
//
|
||
// 默认走回收站(可撤销),另给「永久删除」。默认必须是可逆的那个:
|
||
// 删目录是递归的,误点一下整包源码就没了,而回收站只是多一步清空。
|
||
func (a *App) deletePaths(paths []string) {
|
||
if len(paths) == 0 || a.win == nil {
|
||
return
|
||
}
|
||
|
||
hasDir := false
|
||
for _, p := range paths {
|
||
if st, err := os.Stat(p); err == nil && st.IsDir() {
|
||
hasDir = true
|
||
break
|
||
}
|
||
}
|
||
|
||
what := "项目"
|
||
if len(paths) == 1 {
|
||
what = filepath.Base(paths[0])
|
||
}
|
||
var msg string
|
||
switch {
|
||
case len(paths) == 1 && hasDir:
|
||
msg = fmt.Sprintf("确定删除目录 %s?\n目录会连同里面的内容一起删除。", what)
|
||
case len(paths) == 1:
|
||
msg = fmt.Sprintf("确定删除 %s?", what)
|
||
case hasDir:
|
||
msg = fmt.Sprintf("确定删除选中的 %d 项?\n其中目录会连同里面的内容一起删除。", len(paths))
|
||
default:
|
||
msg = fmt.Sprintf("确定删除选中的 %d 项?", len(paths))
|
||
}
|
||
msg += "\n\n移到回收站可以恢复;永久删除不可恢复。"
|
||
|
||
const (
|
||
btnTrash = 0
|
||
btnPermanent = 1
|
||
btnCancel = 2
|
||
)
|
||
res, err := mygo.Dialog.Message(mygo.MessageOptions{
|
||
Parent: a.win,
|
||
Type: mygo.MessageWarning,
|
||
Title: "删除",
|
||
Message: msg,
|
||
Buttons: []string{"移到回收站", "永久删除", "取消"},
|
||
DefaultButton: btnTrash,
|
||
CancelButton: btnCancel,
|
||
// 告诉系统哪个按钮是不可逆的:macOS 11+ 会把它单独摆放,
|
||
// 并且不让回车键顺手激活它。Windows 的 task dialog 没有这个概念,
|
||
// 传了也不生效——所以在 Windows 上「默认按钮是回收站」才是保险的那道。
|
||
DestructiveButtons: []int{btnPermanent},
|
||
})
|
||
if err != nil || res.Button == btnCancel {
|
||
return
|
||
}
|
||
|
||
switch res.Button {
|
||
case btnTrash:
|
||
if err := moveToTrash(paths); err != nil {
|
||
// 失败就停手,绝不悄悄退化成永久删除。
|
||
a.alert("移到回收站失败", err.Error()+"\n\n可以重试,或改用「永久删除」。")
|
||
return
|
||
}
|
||
case btnPermanent:
|
||
for _, p := range paths {
|
||
if err := os.RemoveAll(p); err != nil {
|
||
a.appendOut(err.Error())
|
||
a.panel = true
|
||
}
|
||
}
|
||
default:
|
||
return
|
||
}
|
||
|
||
// 被删掉的文件如果开着,标签也要跟着关,否则编辑器指向一个不存在的路径。
|
||
a.win.Update(func() {
|
||
var closed []string
|
||
for _, d := range a.docs {
|
||
if underAny(d.path, paths) {
|
||
closed = append(closed, d.path)
|
||
}
|
||
}
|
||
for _, p := range closed {
|
||
a.removeDoc(p)
|
||
}
|
||
})
|
||
|
||
a.sel = nil
|
||
a.selAnchor = ""
|
||
a.Refresh()
|
||
a.win.Update(func() {})
|
||
}
|
||
|
||
// underAny 判断 path 是否等于或位于 paths 里某一项之下。
|
||
func underAny(path string, paths []string) bool {
|
||
clean := filepath.Clean(path)
|
||
for _, p := range paths {
|
||
base := filepath.Clean(p)
|
||
if samePath(clean, base) {
|
||
return true
|
||
}
|
||
if len(clean) > len(base) && samePath(clean[:len(base)], base) {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
// addEntries 是侧栏「+」按钮的菜单:在当前选中目录下新建。
|
||
func (a *App) addEntries() []menuEntry {
|
||
dir := a.root
|
||
if len(a.sel) == 1 {
|
||
for p := range a.sel {
|
||
if st, err := os.Stat(p); err == nil {
|
||
if st.IsDir() {
|
||
dir = p
|
||
} else {
|
||
dir = filepath.Dir(p)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return []menuEntry{
|
||
{label: "新建文件", action: func() { a.beginNew(dir, false) }},
|
||
{label: "新建文件夹", action: func() { a.beginNew(dir, true) }},
|
||
}
|
||
}
|
||
|
||
// ExpandAll 展开文件树里所有目录,深度上限防止超大仓库卡死。
|
||
func (a *App) ExpandAll() {
|
||
if a.root == "" {
|
||
return
|
||
}
|
||
if a.win == nil {
|
||
a.expandDir(a.root, 0)
|
||
return
|
||
}
|
||
a.win.Update(func() { a.expandDir(a.root, 0) })
|
||
}
|
||
|
||
// expandDir 递归把目录标记为展开。
|
||
func (a *App) expandDir(dir string, depth int) {
|
||
if depth > 10 {
|
||
return
|
||
}
|
||
for _, e := range a.entries(dir) {
|
||
if !e.IsDir() || skipDir(e.Name()) {
|
||
continue
|
||
}
|
||
p := filepath.Join(dir, e.Name())
|
||
*a.openFlag(p) = true
|
||
a.expandDir(p, depth+1)
|
||
}
|
||
}
|