From 80e4922b5e78f8ce4ca5d98ffeeb4aea9ebf8dec Mon Sep 17 00:00:00 2001 From: Sergey Tikhomirov Date: Mon, 5 Jun 2017 00:26:55 +0300 Subject: [PATCH] Cocoa: Hide cursor instead of using blank image When cursor isn't in normal mode and should be hidden, use [NSCursor hide] method instead of setting it to blank image. This should prevent situations when hidden cursor becomes visible after system notification was shown. Fixes #971. Closes #1028. --- src/cocoa_init.m | 3 --- src/cocoa_platform.h | 1 - src/cocoa_window.m | 38 +++++++++++++++++++++++--------------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/cocoa_init.m b/src/cocoa_init.m index ba5ddb62..dfc35b6f 100644 --- a/src/cocoa_init.m +++ b/src/cocoa_init.m @@ -352,9 +352,6 @@ void _glfwPlatformTerminate(void) _glfw.ns.listener = nil; } - [_glfw.ns.cursor release]; - _glfw.ns.cursor = nil; - free(_glfw.ns.clipboardString); _glfwTerminateNSGL(); diff --git a/src/cocoa_platform.h b/src/cocoa_platform.h index b8e0bc98..95223f34 100644 --- a/src/cocoa_platform.h +++ b/src/cocoa_platform.h @@ -102,7 +102,6 @@ typedef struct _GLFWlibraryNS CGEventSourceRef eventSource; id delegate; id autoreleasePool; - id cursor; TISInputSourceRef inputSource; IOHIDManagerRef hidManager; id unicodeData; diff --git a/src/cocoa_window.m b/src/cocoa_window.m index 4c27c517..41fdb4c8 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -88,19 +88,35 @@ static GLFWbool cursorInClientArea(_GLFWwindow* window) return [window->ns.view mouse:pos inRect:[window->ns.view frame]]; } +// Updates cursor visibility +// +static void setCursorVisibility(_GLFWwindow* window, BOOL makeVisible) +{ + static BOOL isCursorVisible = YES; + + if (makeVisible && !isCursorVisible) + [NSCursor unhide]; + else if (!makeVisible && isCursorVisible) + [NSCursor hide]; + + isCursorVisible = makeVisible; +} + // Updates the cursor image according to its cursor mode // static void updateCursorImage(_GLFWwindow* window) { if (window->cursorMode == GLFW_CURSOR_NORMAL) { + setCursorVisibility(window, YES); + if (window->cursor) [(NSCursor*) window->cursor->ns.object set]; else [[NSCursor arrowCursor] set]; } else - [(NSCursor*) _glfw.ns.cursor set]; + setCursorVisibility(window, NO); } // Transforms the specified y-coordinate between the CG display and NS screen @@ -363,20 +379,6 @@ static const NSRange kEmptyRange = { NSNotFound, 0 }; @implementation GLFWContentView -+ (void)initialize -{ - if (self == [GLFWContentView class]) - { - if (_glfw.ns.cursor == nil) - { - NSImage* data = [[NSImage alloc] initWithSize:NSMakeSize(16, 16)]; - _glfw.ns.cursor = [[NSCursor alloc] initWithImage:data - hotSpot:NSZeroPoint]; - [data release]; - } - } -} - - (id)initWithGlfwWindow:(_GLFWwindow *)initWindow { self = [super init]; @@ -522,11 +524,17 @@ static const NSRange kEmptyRange = { NSNotFound, 0 }; - (void)mouseExited:(NSEvent *)event { + if (window->cursorMode == GLFW_CURSOR_HIDDEN) + setCursorVisibility(window, YES); + _glfwInputCursorEnter(window, GLFW_FALSE); } - (void)mouseEntered:(NSEvent *)event { + if (window->cursorMode == GLFW_CURSOR_HIDDEN) + setCursorVisibility(window, NO); + _glfwInputCursorEnter(window, GLFW_TRUE); }