From 4b77530c654896b429cfbe6ae397c99ad1f7260b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 4 Jul 2019 09:56:59 +0530 Subject: [PATCH] Store a reference to the glfw window pointer in the NSWindow class as well --- glfw/cocoa_window.m | 64 +++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/glfw/cocoa_window.m b/glfw/cocoa_window.m index f05b4fe9c..b58a45529 100644 --- a/glfw/cocoa_window.m +++ b/glfw/cocoa_window.m @@ -390,9 +390,7 @@ static NSUInteger translateKeyToModifierFlag(int key) static const NSRange kEmptyRange = { NSNotFound, 0 }; -//------------------------------------------------------------------------ -// Delegate for window related notifications -//------------------------------------------------------------------------ +// Delegate for window related notifications {{{ @interface GLFWWindowDelegate : NSObject { @@ -533,12 +531,9 @@ static const NSRange kEmptyRange = { NSNotFound, 0 }; } -@end +@end // }}} - -//------------------------------------------------------------------------ -// Delegate for application related notifications -//------------------------------------------------------------------------ +// Delegate for application related notifications {{{ @interface GLFWApplicationDelegate : NSObject @end @@ -605,11 +600,9 @@ static GLFWapplicationshouldhandlereopenfun handle_reopen_callback = NULL; } @end +// }}} - -//------------------------------------------------------------------------ -// Content view class for the GLFW window -//------------------------------------------------------------------------ +// Content view class for the GLFW window {{{ @interface GLFWContentView : NSView { @@ -619,6 +612,7 @@ static GLFWapplicationshouldhandlereopenfun handle_reopen_callback = NULL; NSRect markedRect; } +- (void) removeGLFWWindow; - (instancetype)initWithGlfwWindow:(_GLFWwindow *)initWindow; @end @@ -1205,17 +1199,40 @@ void _glfwPlatformUpdateIMEState(_GLFWwindow *w, int which, int a, int b, int c, } @end +// }}} +// GLFW Window class {{{ -//------------------------------------------------------------------------ -// GLFW window class -//------------------------------------------------------------------------ +@interface GLFWWindow : NSWindow { + _GLFWwindow* glfw_window; +} -@interface GLFWWindow : NSWindow {} +- (instancetype)initWithGlfwWindow:(NSRect)contentRect + styleMask:(NSWindowStyleMask)style + backing:(NSBackingStoreType)backingStoreType + initWindow:(_GLFWwindow *)initWindow; + +- (void) removeGLFWWindow; @end @implementation GLFWWindow +- (instancetype)initWithGlfwWindow:(NSRect)contentRect + styleMask:(NSWindowStyleMask)style + backing:(NSBackingStoreType)backingStoreType + initWindow:(_GLFWwindow *)initWindow +{ + self = [super initWithContentRect:contentRect styleMask:style backing:backingStoreType defer:NO]; + if (self != nil) glfw_window = initWindow; + return self; +} + +- (void) removeGLFWWindow +{ + glfw_window = NULL; +} + + - (BOOL)canBecomeKeyWindow { // Required for NSWindowStyleMaskBorderless windows @@ -1229,18 +1246,13 @@ void _glfwPlatformUpdateIMEState(_GLFWwindow *w, int which, int a, int b, int c, - (void)toggleFullScreen:(nullable id)sender { - GLFWContentView *view = [self contentView]; - if (view) - { - _GLFWwindow *window = [view glfwWindow]; - if (window && window->ns.toggleFullscreenCallback && window->ns.toggleFullscreenCallback((GLFWwindow*)window) == 1) + if (glfw_window && glfw_window->ns.toggleFullscreenCallback && glfw_window->ns.toggleFullscreenCallback((GLFWwindow*)glfw_window) == 1) return; - } [super toggleFullScreen:sender]; } @end - +// }}} // Set up the menu bar (manually) // This is nasty, nasty stuff -- calls to undocumented semi-private APIs that @@ -1421,10 +1433,11 @@ static bool createNativeWindow(_GLFWwindow* window, contentRect = NSMakeRect(0, 0, wndconfig->width, wndconfig->height); window->ns.object = [[GLFWWindow alloc] - initWithContentRect:contentRect + initWithGlfwWindow:contentRect styleMask:getStyleMask(window) backing:NSBackingStoreBuffered - defer:NO]; + initWindow:window + ]; if (window->ns.object == nil) { @@ -1555,6 +1568,7 @@ void _glfwPlatformDestroyWindow(_GLFWwindow* window) [window->ns.view release]; window->ns.view = nil; + [window->ns.object removeGLFWWindow]; [window->ns.object close]; window->ns.object = nil; }