summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPietro Gagliardi <[email protected]>2015-12-12 12:01:31 -0500
committerPietro Gagliardi <[email protected]>2015-12-12 12:01:31 -0500
commita1375fb7769bb882d638f7e628193f31e27f440c (patch)
tree9a4e968f46a1e7f1d58aa8b675105fbfc6f8b8e7
parenteb3a833decbeffa79b9112570e091e231a50f7fd (diff)
Added OnShouldQuit().
-rw-r--r--main.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/main.go b/main.go
index 2406f1e..ce66591 100644
--- a/main.go
+++ b/main.go
@@ -11,12 +11,17 @@ import (
// #include "ui.h"
// extern void doQueued(void *);
+// extern int doOnShouldQuit(void *);
// /* I forgot how dumb cgo is... ./main.go:73: cannot use _Cgo_ptr(_Cfpvar_fp_doQueued) (type unsafe.Pointer) as type *[0]byte in argument to _Cfunc_uiQueueMain */
// /* I'm pretty sure this worked before... */
// static inline void realQueueMain(void *x)
// {
// uiQueueMain(doQueued, x);
// }
+// static inline int realOnShouldQuit(void)
+// {
+// uiOnShouldQuit(doOnShouldQuit, NULL);
+// }
import "C"
// Main initializes package ui, runs f to set up the program,
@@ -45,6 +50,8 @@ func start(errchan chan error, f func()) {
C.uiFreeInitError(estr)
return
}
+ // set up OnShouldQuit()
+ C.realOnShouldQuit()
QueueMain(f)
C.uiMain()
errchan <- nil
@@ -95,3 +102,27 @@ func doQueued(nn unsafe.Pointer) {
f()
}
+
+// no need to lock this; this API is only safe on the main thread
+var shouldQuitFunc func() bool
+
+// OnShouldQuit schedules f to be exeucted when the OS wants
+// the program to quit or when a Quit menu item has been clicked.
+// Only one function may be registered at a time. If the function
+// returns true, Quit will be called. If the function returns false, or
+// if OnShouldQuit is never called. Quit will not be called and the
+// OS will be told that the program needs to continue running.
+func OnShouldQuit(f func() bool) {
+ shouldQuitFunc = f
+}
+
+//export doOnShouldQuit
+func doOnShouldQuit(unused unsafe.Pointer) C.int {
+ if shouldQuitFunc == nil {
+ return 0
+ }
+ if shouldQuitFunc() {
+ return 1
+ }
+ return 0
+}