Add option to prevent application shutdown

This update allows the application shutdown to be cancelled. It implements the shouldQuit function, which can prevent the application from quitting if it returns false. Additional checks have been added to ensure cleanup and quit processes are performed only if required. The darwin delegate and linux and windows applications were modified to include this new functionality.
This commit is contained in:
Lea Anthony
2024-01-17 20:45:30 +11:00
parent ab22ea1f27
commit 861ddea1b2
10 changed files with 66 additions and 18 deletions
+15 -6
View File
@@ -297,6 +297,7 @@ type App struct {
// Shutdown
performingShutdown bool
// Shutdown tasks are run when the application is shutting down.
// They are run in the order they are added and run on the main thread.
// The application option `OnShutdown` is run first.
@@ -617,12 +618,11 @@ func (a *App) OnShutdown(f func()) {
a.shutdownTasks = append(a.shutdownTasks, f)
}
func (a *App) Quit() {
func (a *App) cleanup() {
if a.performingShutdown {
return
}
a.performingShutdown = true
// Run the shutdown tasks
for _, shutdownTask := range a.shutdownTasks {
InvokeSync(shutdownTask)
}
@@ -639,13 +639,15 @@ func (a *App) Quit() {
}
a.systemTrays = nil
a.systemTraysLock.Unlock()
if a.impl != nil {
a.impl.destroy()
a.impl = nil
}
})
}
func (a *App) Quit() {
if a.impl != nil {
InvokeSync(a.impl.destroy)
}
}
func (a *App) SetIcon(icon []byte) {
if a.impl != nil {
a.impl.setIcon(icon)
@@ -867,3 +869,10 @@ func (a *App) Environment() EnvironmentInfo {
Debug: a.isDebugMode,
}
}
func (a *App) shouldQuit() bool {
if a.options.ShouldQuit != nil {
return a.options.ShouldQuit()
}
return true
}