Added OnShouldQuit().

This commit is contained in:
Pietro Gagliardi 2015-12-12 12:01:31 -05:00
parent eb3a833dec
commit a1375fb776
1 changed files with 31 additions and 0 deletions

31
main.go
View File

@ -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
}