Refactored application shutdown process to streamline app termination.

The application shutdown process has been significantly reworked to be more efficient and robust. The refactored code removes the event listener for the 'ApplicationTerminate' event. Instead, an in-process flag is added to the 'Quit' method to prevent recursive calls. Additionally, an optional 'OnShutdown' function variable is introduced to allow custom cleanup operations upon app termination.
This commit is contained in:
Lea Anthony 2024-01-09 18:19:09 +11:00
commit 68b12d4fff
No known key found for this signature in database
GPG key ID: 33DAF7BB90A58405
5 changed files with 53 additions and 54 deletions

View file

@ -177,6 +177,5 @@ export const EventTypes = {
WindowDPIChanged: "common:WindowDPIChanged",
WindowFilesDropped: "common:WindowFilesDropped",
ThemeChanged: "common:ThemeChanged",
ApplicationTerminate: "common:ApplicationTerminate",
},
};

View file

@ -177,6 +177,5 @@ export declare const EventTypes: {
WindowDPIChanged: string,
WindowFilesDropped: string,
ThemeChanged: string,
ApplicationTerminate: string,
},
};

View file

@ -122,11 +122,6 @@ func New(appOptions Options) *App {
result.keyBindings = processKeyBindingOptions(result.options.KeyBindings)
}
// Handle the terminate event
result.On(events.Common.ApplicationTerminate, func(e *Event) {
result.Quit()
})
return result
}
@ -291,8 +286,11 @@ type App struct {
// Keybindings
keyBindings map[string]func(window *WebviewWindow)
//
shutdownOnce sync.Once
// OnShutdown is called when the application is about to quit.
// This is useful for cleanup tasks.
// The shutdown process blocks until this function returns
OnShutdown func()
performingShutdown bool
}
func (a *App) init() {
@ -602,6 +600,13 @@ func (a *App) CurrentWindow() *WebviewWindow {
}
func (a *App) Quit() {
if a.performingShutdown {
return
}
a.performingShutdown = true
if a.OnShutdown != nil {
a.OnShutdown()
}
InvokeSync(func() {
a.windowsLock.RLock()
for _, window := range a.windows {

View file

@ -6,52 +6,50 @@ type WindowEventType uint
var Common = newCommonEvents()
type commonEvents struct {
ApplicationStarted ApplicationEventType
WindowMaximise WindowEventType
WindowUnMaximise WindowEventType
WindowFullscreen WindowEventType
WindowUnFullscreen WindowEventType
WindowRestore WindowEventType
WindowMinimise WindowEventType
WindowUnMinimise WindowEventType
WindowClosing WindowEventType
WindowZoom WindowEventType
WindowZoomIn WindowEventType
WindowZoomOut WindowEventType
WindowZoomReset WindowEventType
WindowFocus WindowEventType
WindowLostFocus WindowEventType
WindowShow WindowEventType
WindowHide WindowEventType
WindowDPIChanged WindowEventType
WindowFilesDropped WindowEventType
ThemeChanged ApplicationEventType
ApplicationTerminate ApplicationEventType
ApplicationStarted ApplicationEventType
WindowMaximise WindowEventType
WindowUnMaximise WindowEventType
WindowFullscreen WindowEventType
WindowUnFullscreen WindowEventType
WindowRestore WindowEventType
WindowMinimise WindowEventType
WindowUnMinimise WindowEventType
WindowClosing WindowEventType
WindowZoom WindowEventType
WindowZoomIn WindowEventType
WindowZoomOut WindowEventType
WindowZoomReset WindowEventType
WindowFocus WindowEventType
WindowLostFocus WindowEventType
WindowShow WindowEventType
WindowHide WindowEventType
WindowDPIChanged WindowEventType
WindowFilesDropped WindowEventType
ThemeChanged ApplicationEventType
}
func newCommonEvents() commonEvents {
return commonEvents{
ApplicationStarted: 1174,
WindowMaximise: 1175,
WindowUnMaximise: 1176,
WindowFullscreen: 1177,
WindowUnFullscreen: 1178,
WindowRestore: 1179,
WindowMinimise: 1180,
WindowUnMinimise: 1181,
WindowClosing: 1182,
WindowZoom: 1183,
WindowZoomIn: 1184,
WindowZoomOut: 1185,
WindowZoomReset: 1186,
WindowFocus: 1187,
WindowLostFocus: 1188,
WindowShow: 1189,
WindowHide: 1190,
WindowDPIChanged: 1191,
WindowFilesDropped: 1192,
ThemeChanged: 1193,
ApplicationTerminate: 1194,
ApplicationStarted: 1174,
WindowMaximise: 1175,
WindowUnMaximise: 1176,
WindowFullscreen: 1177,
WindowUnFullscreen: 1178,
WindowRestore: 1179,
WindowMinimise: 1180,
WindowUnMinimise: 1181,
WindowClosing: 1182,
WindowZoom: 1183,
WindowZoomIn: 1184,
WindowZoomOut: 1185,
WindowZoomReset: 1186,
WindowFocus: 1187,
WindowLostFocus: 1188,
WindowShow: 1189,
WindowHide: 1190,
WindowDPIChanged: 1191,
WindowFilesDropped: 1192,
ThemeChanged: 1193,
}
}
@ -560,5 +558,4 @@ var eventToJS = map[uint]string{
1191: "common:WindowDPIChanged",
1192: "common:WindowFilesDropped",
1193: "common:ThemeChanged",
1194: "common:ApplicationTerminate",
}

View file

@ -167,5 +167,4 @@ common:WindowShow
common:WindowHide
common:WindowDPIChanged
common:WindowFilesDropped
common:ThemeChanged
common:ApplicationTerminate
common:ThemeChanged