Files
MyGolandRelease/internal/app/unsaved.go
T
zerorigin a80c0f886a MyGoland 首次提交:Go 轻量 IDE
- 编辑器:多标签、语法高亮、gopls 全套(补全/诊断/跳转/重命名/代码动作/内联提示/签名大纲)
- 调试:Delve DAP(断点/条件断点/变量树/表达式求值/goroutine 列表)
- 测试:结果树、覆盖率行着色、失败跳源码、-run 精确运行、行号可跑标记
- Git:状态着色、选择性暂存提交、pull/push、历史并排对比
- 本地历史 + 自动快照 + 崩溃恢复、会话/光标恢复
- 全局搜索替换(预览+确认,GBK 安全)、多终端、gopls 崩溃自愈
- 设计文档与验证记录见 docs/plans/
2026-11-04 16:37:05 +08:00

148 lines
2.9 KiB
Go

package app
import (
"fmt"
"github.com/egoist/mygo"
)
// 未保存对话框的三个选项。
const (
choiceSave = iota
choiceDiscard
choiceCancel
)
// dirtyDocs 返回有未保存改动的文件,须在主线程调用。
func (a *App) dirtyDocs() []*Doc {
var out []*Doc
for _, d := range a.docs {
if d.buf.Modified() {
out = append(out, d)
}
}
return out
}
// askUnsaved 询问如何处理未保存的文件,只在 goroutine 中调用。
func (a *App) askUnsaved(n int, detail string) int {
res, err := mygo.Dialog.Message(mygo.MessageOptions{
Parent: a.win,
Type: mygo.MessageWarning,
Title: "未保存的更改",
Message: fmt.Sprintf("有 %d 个文件尚未保存。", n),
Detail: detail,
Buttons: []string{"保存全部", "不保存", "取消"},
DefaultButton: 0,
CancelButton: 2,
})
if err != nil {
return choiceCancel
}
switch res.Button {
case 0:
return choiceSave
case 1:
return choiceDiscard
}
return choiceCancel
}
// saveDocs 写回一组文件,全部成功才返回 true。
func (a *App) saveDocs(docs []*Doc) bool {
for _, d := range docs {
if !d.buf.Modified() {
continue
}
if err := writeDocFile(d); err != nil {
a.alert("保存失败", fmt.Sprintf("%s\n%s", d.path, err))
return false
}
}
if a.win != nil {
a.win.Update(a.syncTitle)
}
return true
}
// exitAfterConfirm 退出前询问,确认后再执行 proceed。
func (a *App) exitAfterConfirm(dirty []*Doc, proceed func()) {
ok := false
switch a.askUnsaved(len(dirty), "退出前需要处理这些文件。") {
case choiceSave:
ok = a.saveDocs(dirty)
case choiceDiscard:
ok = true
}
a.win.Update(func() {
a.exitPending = false
if ok {
proceed()
}
})
}
// guardClose 在窗口关闭前拦截未保存的改动。
func (a *App) guardClose(e *mygo.CloseEvent) {
if a.allowExit {
return
}
// 已经有确认流程在跑,先拦住,避免弹两次。
if a.exitPending {
e.PreventDefault()
return
}
dirty := a.dirtyDocs()
if len(dirty) == 0 {
return
}
e.PreventDefault()
a.exitPending = true
go a.exitAfterConfirm(dirty, func() {
a.allowExit = true
a.win.Close()
})
}
// guardQuit 在应用退出前拦截未保存的改动。
func (a *App) guardQuit(e *mygo.QuitEvent) {
if a.allowExit {
return
}
if a.exitPending {
e.PreventDefault()
return
}
dirty := a.dirtyDocs()
if len(dirty) == 0 {
return
}
e.PreventDefault()
a.exitPending = true
go a.exitAfterConfirm(dirty, func() {
a.allowExit = true
mygo.App.Exit(0)
})
}
// switchRoot 带未保存检查地切换工作区。
func (a *App) switchRoot(abs string) {
dirty := a.dirtyDocs()
if len(dirty) == 0 {
a.setRoot(abs)
return
}
go func() {
ok := false
switch a.askUnsaved(len(dirty), "切换工作区会关闭当前打开的文件。") {
case choiceSave:
ok = a.saveDocs(dirty)
case choiceDiscard:
ok = true
}
if ok {
a.win.Update(func() { a.setRoot(abs) })
}
}()
}