From d7528a8338a2b0f6c81063a2d16c2f716ac41168 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Mon, 11 Dec 2023 15:00:58 -0600 Subject: [PATCH] app: [macOS] use NSNotificationCenter to receive app events Notifications don't require a list of windows nor an app delegate. Signed-off-by: Elias Naur --- app/os_macos.go | 14 -------------- app/os_macos.m | 21 +++++++++++++++------ 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/app/os_macos.go b/app/os_macos.go index b7842c33..e4d7b517 100644 --- a/app/os_macos.go +++ b/app/os_macos.go @@ -851,20 +851,6 @@ func gio_onWindowed(view C.CFTypeRef) { w.ProcessEvent(ConfigEvent{Config: w.config}) } -//export gio_onAppHide -func gio_onAppHide() { - for _, w := range viewMap { - w.setStage(StagePaused) - } -} - -//export gio_onAppShow -func gio_onAppShow() { - for _, w := range viewMap { - w.setStage(StageRunning) - } -} - //export gio_onFinishLaunching func gio_onFinishLaunching() { close(launched) diff --git a/app/os_macos.m b/app/os_macos.m index 71d28afa..92a126bc 100644 --- a/app/os_macos.m +++ b/app/os_macos.m @@ -191,6 +191,12 @@ static void handleMouse(NSView *view, NSEvent *event, int typ, CGFloat dx, CGFlo r = [self convertRect:r toView:nil]; return [[self window] convertRectToScreen:r]; } +- (void)applicationWillUnhide:(NSNotification *)notification { + gio_onShow((__bridge CFTypeRef)self); +} +- (void)applicationDidHide:(NSNotification *)notification { + gio_onHide((__bridge CFTypeRef)self); +} @end // Delegates are weakly referenced from their peers. Nothing @@ -374,6 +380,15 @@ CFTypeRef gio_createView(void) { GioView* view = [[GioView alloc] initWithFrame:frame]; view.wantsLayer = YES; view.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize; + + [[NSNotificationCenter defaultCenter] addObserver:view + selector:@selector(applicationWillUnhide:) + name:NSApplicationWillUnhideNotification + object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:view + selector:@selector(applicationDidHide:) + name:NSApplicationDidHideNotification + object:nil]; return CFBridgingRetain(view); } } @@ -384,12 +399,6 @@ CFTypeRef gio_createView(void) { [NSApp activateIgnoringOtherApps:YES]; gio_onFinishLaunching(); } -- (void)applicationDidHide:(NSNotification *)aNotification { - gio_onAppHide(); -} -- (void)applicationWillUnhide:(NSNotification *)notification { - gio_onAppShow(); -} @end void gio_main() {