mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-07-14 00:42:17 +08:00
- 关闭应用前拦截 Wails 退出事件,前端弹出确认退出、保存退出和取消三按钮\n- 保存退出支持外部 SQL 文件和已保存查询,未命名临时查询保留草稿恢复语义\n- 补充退出保护后端测试、前端保存目标测试和多语言文案
81 lines
2.2 KiB
Go
81 lines
2.2 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
|
|
"GoNavi-Wails/internal/connection"
|
|
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
|
|
)
|
|
|
|
const applicationBeforeCloseRequestEvent = "app:before-close-request"
|
|
|
|
var (
|
|
emitApplicationBeforeCloseRequest = wailsRuntime.EventsEmit
|
|
quitApplicationRuntime = wailsRuntime.Quit
|
|
)
|
|
|
|
// NewBeforeCloseHandler exposes the Wails close guard without binding the
|
|
// lifecycle callback itself as a frontend RPC method.
|
|
func NewBeforeCloseHandler(a *App) func(context.Context) bool {
|
|
return func(ctx context.Context) bool {
|
|
if a == nil {
|
|
return false
|
|
}
|
|
return a.beforeClose(ctx)
|
|
}
|
|
}
|
|
|
|
func (a *App) beforeClose(ctx context.Context) bool {
|
|
a.applicationQuitMu.Lock()
|
|
if a.allowApplicationQuit {
|
|
a.allowApplicationQuit = false
|
|
a.applicationQuitPromptInFlight = false
|
|
a.applicationQuitMu.Unlock()
|
|
return false
|
|
}
|
|
if a.applicationQuitPromptInFlight {
|
|
a.applicationQuitMu.Unlock()
|
|
return true
|
|
}
|
|
a.applicationQuitPromptInFlight = true
|
|
a.applicationQuitMu.Unlock()
|
|
|
|
emitCtx := ctx
|
|
if emitCtx == nil {
|
|
emitCtx = a.ctx
|
|
}
|
|
if emitCtx != nil {
|
|
emitApplicationBeforeCloseRequest(emitCtx, applicationBeforeCloseRequestEvent)
|
|
}
|
|
return true
|
|
}
|
|
|
|
// CancelApplicationQuit resets a previously intercepted close request after
|
|
// the frontend dialog is cancelled or cannot complete a save.
|
|
func (a *App) CancelApplicationQuit() connection.QueryResult {
|
|
if a == nil {
|
|
return connection.QueryResult{Success: false, Message: "application is not initialized"}
|
|
}
|
|
a.applicationQuitMu.Lock()
|
|
a.applicationQuitPromptInFlight = false
|
|
a.allowApplicationQuit = false
|
|
a.applicationQuitMu.Unlock()
|
|
return connection.QueryResult{Success: true}
|
|
}
|
|
|
|
// ForceQuitApplication is called only after the frontend has confirmed that
|
|
// discarding or saving pending SQL edits is acceptable.
|
|
func (a *App) ForceQuitApplication() connection.QueryResult {
|
|
if a == nil {
|
|
return connection.QueryResult{Success: false, Message: "application is not initialized"}
|
|
}
|
|
a.applicationQuitMu.Lock()
|
|
a.allowApplicationQuit = true
|
|
a.applicationQuitPromptInFlight = false
|
|
a.applicationQuitMu.Unlock()
|
|
if a.ctx != nil {
|
|
quitApplicationRuntime(a.ctx)
|
|
}
|
|
return connection.QueryResult{Success: true}
|
|
}
|