Sfoglia il codice sorgente

util.Application generates OnQuit event before terminating the app

leonsal 8 anni fa
parent
commit
13178b7eb7
1 ha cambiato i file con 20 aggiunte e 2 eliminazioni
  1. 20 2
      util/application/application.go

+ 20 - 2
util/application/application.go

@@ -70,6 +70,10 @@ const OnBeforeRender = "util.application.OnBeforeRender"
 // OnAfterRender is the event generated by Application just after rendering the scene/gui
 const OnAfterRender = "util.application.OnAfterRender"
 
+// OnQuit is the event generated by Application when the user tries to close the window
+// or the Quit() method is called.
+const OnQuit = "util.application.OnQuit"
+
 // appInstance contains the pointer to the single Application instance
 var appInstance *Application
 
@@ -421,7 +425,19 @@ func (app *Application) Run() error {
 	app.frameTime = time.Now()
 
 	// Render loop
-	for !app.win.ShouldClose() {
+	for true {
+		// If was requested to terminate the application by trying to close the window
+		// or by calling Quit(), dispatch OnQuit event for subscribers.
+		// If no subscriber cancelled the event, terminates the application.
+		if app.win.ShouldClose() {
+			canceled := app.Dispatch(OnQuit, nil)
+			if canceled {
+				app.win.SetShouldClose(false)
+			} else {
+				break
+			}
+		}
+
 		// Starts measuring this frame
 		app.frameRater.Start()
 
@@ -521,7 +537,9 @@ func (app *Application) OpenDefaultAudioDevice() error {
 	return nil
 }
 
-// Quit ends the application
+// Quit requests to terminate the application
+// Application will dispatch OnQuit events to registered subscriber which
+// can cancel the process by calling CancelDispatch().
 func (app *Application) Quit() {
 
 	app.win.SetShouldClose(true)