diff --git a/darwin/init.m b/darwin/init.m index 3893be08..2a977261 100644 --- a/darwin/init.m +++ b/darwin/init.m @@ -17,15 +17,27 @@ static BOOL canQuit = NO; // yes that's right folks: DO ABSOLUTELY NOTHING. // the magic is [NSApp run] will just... stop. + // well let's not do nothing; let's actually quit our graceful way + NSEvent *e; + // for debugging NSLog(@"in terminate:"); if (!canQuit) complain("call to [NSApp terminate:] when not ready to terminate"); - // TODO move the body of uiQuit() here - // TODO make uiQuit() just call terminate: - // TODO update the above comment + [NSApp stop:NSApp]; + // stop: won't register until another event has passed; let's synthesize one + e = [NSEvent otherEventWithType:NSApplicationDefined + location:NSZeroPoint + modifierFlags:0 + timestamp:[[NSProcessInfo processInfo] systemUptime] + windowNumber:0 + context:[NSGraphicsContext currentContext] + subtype:0 + data1:0 + data2:0]; + [NSApp postEvent:e atStart:NO]; // let pending events take priority (this is what PostQuitMessage() on Windows does so we have to do it here too for parity; thanks to mikeash in irc.freenode.net/#macdev for confirming that this parameter should indeed be NO) } @end @@ -88,3 +100,14 @@ void uiUninit(void) void uiFreeInitError(const char *err) { } + +void uiMain(void) +{ + [NSApp run]; +} + +void uiQuit(void) +{ + canQuit = YES; + [NSApp terminate:NSApp]; +} diff --git a/darwin/main.m b/darwin/main.m index 58b23739..1c41cf34 100644 --- a/darwin/main.m +++ b/darwin/main.m @@ -1,25 +1,2 @@ // 6 april 2015 #import "uipriv_darwin.h" - -void uiMain(void) -{ - [NSApp run]; -} - -void uiQuit(void) -{ - NSEvent *e; - - [NSApp stop:NSApp]; - // stop: won't register until another event has passed; let's synthesize one - e = [NSEvent otherEventWithType:NSApplicationDefined - location:NSZeroPoint - modifierFlags:0 - timestamp:[[NSProcessInfo processInfo] systemUptime] - windowNumber:0 - context:[NSGraphicsContext currentContext] - subtype:0 - data1:0 - data2:0]; - [NSApp postEvent:e atStart:NO]; // let pending events take priority (this is what PostQuitMessage() on Windows does so we have to do it here too for parity; thanks to mikeash in irc.freenode.net/#macdev for confirming that this parameter should indeed be NO) -} diff --git a/test/main.c b/test/main.c index e519e829..5f28bdf8 100644 --- a/test/main.c +++ b/test/main.c @@ -23,6 +23,7 @@ int onClosing(uiWindow *w, void *data) int onShouldQuit(void *data) { printf("in onShouldQuit()\n"); + uiControlDestroy(uiControl(data)); return uiMenuItemChecked(shouldQuitItem); } @@ -57,12 +58,12 @@ int main(int argc, char *argv[]) if (!nomenus) initMenus(); - uiOnShouldQuit(onShouldQuit, NULL); - w = newWindow("Main Window", 320, 240, 1); uiWindowOnClosing(w, onClosing, NULL); printf("main window %p\n", w); + uiOnShouldQuit(onShouldQuit, w); + mainBox = newHorizontalBox(); uiWindowSetChild(w, uiControl(mainBox));