From 6f15275e223cd71b329d653c566ccceb849adf5f Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Mon, 8 Jun 2020 17:54:06 +0200 Subject: [PATCH] app/internal/window: [macOS] retain NSWindow instance Updates (maybe fixes) #130 Signed-off-by: Elias Naur --- app/internal/window/os_macos.go | 18 +++++++++++++++--- app/internal/window/os_macos.m | 21 ++++++++++++--------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/app/internal/window/os_macos.go b/app/internal/window/os_macos.go index 9d95e942..224bb972 100644 --- a/app/internal/window/os_macos.go +++ b/app/internal/window/os_macos.go @@ -40,7 +40,9 @@ __attribute__ ((visibility ("hidden"))) CFTypeRef gio_readClipboard(void); __attribute__ ((visibility ("hidden"))) void gio_writeClipboard(unichar *chars, NSUInteger length); __attribute__ ((visibility ("hidden"))) void gio_setNeedsDisplay(CFTypeRef viewRef); __attribute__ ((visibility ("hidden"))) void gio_appTerminate(void); -__attribute__ ((visibility ("hidden"))) NSPoint gio_createWindow(CFTypeRef viewRef, const char *title, CGFloat width, CGFloat height, NSPoint topLeft); +__attribute__ ((visibility ("hidden"))) CFTypeRef gio_createWindow(CFTypeRef viewRef, const char *title, CGFloat width, CGFloat height); +__attribute__ ((visibility ("hidden"))) void gio_makeKeyAndOrderFront(CFTypeRef windowRef); +__attribute__ ((visibility ("hidden"))) NSPoint gio_cascadeTopLeftFromPoint(CFTypeRef windowRef, NSPoint topLeft); */ import "C" @@ -51,6 +53,7 @@ func init() { type window struct { view C.CFTypeRef + window C.CFTypeRef w Callbacks stage system.Stage displayLink *displayLink @@ -252,8 +255,10 @@ func gio_onClose(view C.CFTypeRef) { w.displayLink.Close() deleteView(view) w.w.Event(system.DestroyEvent{}) + C.CFRelease(w.view) w.view = 0 - C.CFRelease(view) + C.CFRelease(w.window) + w.window = 0 if len(viewMap) == 0 { C.gio_appTerminate() } @@ -311,7 +316,14 @@ func NewWindow(win Callbacks, opts *Options) error { errch <- nil win.SetDriver(w) w.w = win - nextTopLeft = C.gio_createWindow(w.view, title, C.CGFloat(width), C.CGFloat(height), nextTopLeft) + w.window = C.gio_createWindow(w.view, title, C.CGFloat(width), C.CGFloat(height)) + if nextTopLeft.x == 0 && nextTopLeft.y == 0 { + // cascadeTopLeftFromPoint treats (0, 0) as a no-op, + // and just returns the offset we need for the first window. + nextTopLeft = C.gio_cascadeTopLeftFromPoint(w.window, nextTopLeft) + } + nextTopLeft = C.gio_cascadeTopLeftFromPoint(w.window, nextTopLeft) + C.gio_makeKeyAndOrderFront(w.window) }) return <-errch } diff --git a/app/internal/window/os_macos.m b/app/internal/window/os_macos.m index 942da09a..9ff15408 100644 --- a/app/internal/window/os_macos.m +++ b/app/internal/window/os_macos.m @@ -114,7 +114,17 @@ void gio_setDisplayLinkDisplay(CFTypeRef dl, uint64_t did) { CVDisplayLinkSetCurrentCGDisplay((CVDisplayLinkRef)dl, (CGDirectDisplayID)did); } -NSPoint gio_createWindow(CFTypeRef viewRef, const char *title, CGFloat width, CGFloat height, NSPoint topLeft) { +NSPoint gio_cascadeTopLeftFromPoint(CFTypeRef windowRef, NSPoint topLeft) { + NSWindow *window = (__bridge NSWindow *)windowRef; + return [window cascadeTopLeftFromPoint:topLeft]; +} + +void gio_makeKeyAndOrderFront(CFTypeRef windowRef) { + NSWindow *window = (__bridge NSWindow *)windowRef; + [window makeKeyAndOrderFront:nil]; +} + +CFTypeRef gio_createWindow(CFTypeRef viewRef, const char *title, CGFloat width, CGFloat height) { @autoreleasepool { NSRect rect = NSMakeRect(0, 0, width, height); NSUInteger styleMask = NSTitledWindowMask | @@ -131,16 +141,9 @@ NSPoint gio_createWindow(CFTypeRef viewRef, const char *title, CGFloat width, CG NSView *view = (__bridge NSView *)viewRef; [window setContentView:view]; [window makeFirstResponder:view]; - if (topLeft.x == 0 && topLeft.y == 0) { - // cascadeTopLeftFromPoint treats (0, 0) as a no-op, - // and just returns the offset we need for the first window. - topLeft = [window cascadeTopLeftFromPoint:topLeft]; - } - topLeft = [window cascadeTopLeftFromPoint:topLeft]; window.releasedWhenClosed = NO; window.delegate = globalWindowDel; - [window makeKeyAndOrderFront:nil]; - return topLeft; + return (__bridge_retained CFTypeRef)window; } }