From a1375fb7769bb882d638f7e628193f31e27f440c Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 12 Dec 2015 12:01:31 -0500 Subject: [PATCH] Added OnShouldQuit(). --- main.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) 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 +}