Ver código fonte

Improved Application implementations

Daniel Salvadori 6 anos atrás
pai
commit
7bdc844766
3 arquivos alterados com 13 adições e 11 exclusões
  1. 1 0
      app/app-browser.go
  2. 8 11
      app/app-desktop.go
  3. 4 0
      app/doc.go

+ 1 - 0
app/app-browser.go

@@ -79,6 +79,7 @@ func (app *Application) Run(update func(renderer *renderer.Renderer, deltaTime t
 		if !app.exit {
 			app.cbid = js.Global().Call("requestAnimationFrame", tick)
 		} else {
+			app.Dispatch(OnExit, nil)
 			done <- true // Write to done channel to exit the app
 		}
 		return nil

+ 8 - 11
app/app-desktop.go

@@ -22,10 +22,6 @@ const (
 	height = 600
 )
 
-// OnExit is the event generated by Application when the user
-// tries to close the window or the Exit() method is called.
-const OnExit = "app.OnExit"
-
 // Application
 type Application struct {
 	window.IWindow                    // Embedded GlfwWindow
@@ -74,14 +70,15 @@ func (app *Application) Run(update func(renderer *renderer.Renderer, deltaTime t
 	// Set up recurring calls to user's update function
 	for true {
 		// If Exit() was called or there was an attempt to close the window dispatch OnExit event for subscribers.
-		// If no subscriber cancelled the event, terminates the application.
+		// If no subscriber cancelled the event, terminate the application.
 		if app.IWindow.(*window.GlfwWindow).ShouldClose() {
-			canceled := app.Dispatch(OnExit, nil) // TODO implement the same in app-browser
-			if canceled {
-				app.IWindow.(*window.GlfwWindow).SetShouldClose(false)
-			} else {
-				break
-			}
+			app.Dispatch(OnExit, nil)
+			// TODO allow for cancelling exit e.g. showing dialog asking the user if he/she wants to save changes
+			// if exit was cancelled {
+			//     app.IWindow.(*window.GlfwWindow).SetShouldClose(false)
+			// } else {
+			break
+			// }
 		}
 		// Update frame start and frame delta
 		now := time.Now()

+ 4 - 0
app/doc.go

@@ -4,3 +4,7 @@
 
 // Package app implements a cross-platform G3N app.
 package app
+
+// OnExit is the event generated by Application when the user
+// tries to close the window (desktop) or the Exit() method is called.
+const OnExit = "app.OnExit"