Improved cursor hiding on OS X

This commit is contained in:
Marco Antognini 2014-09-14 15:27:20 +02:00 committed by Lukas Dürrenberger
parent 330ea0bbe3
commit 7159e4ba43
6 changed files with 65 additions and 44 deletions

View File

@ -133,4 +133,12 @@ namespace sf {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
-(NSPoint)cursorPositionFromEvent:(NSEvent*)eventOrNil; -(NSPoint)cursorPositionFromEvent:(NSEvent*)eventOrNil;
////////////////////////////////////////////////////////////
/// \brief Determine where the mouse is
///
/// \return true when the mouse is inside the OpenGL view, false otherwise
///
////////////////////////////////////////////////////////////
-(BOOL)isMouseInside;
@end @end

View File

@ -67,14 +67,6 @@ BOOL isValidTextUnicode(NSEvent* event);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
-(void)viewDidEndLiveResize; -(void)viewDidEndLiveResize;
////////////////////////////////////////////////////////////
/// \brief Determine where the mouse is
///
/// \return true when the mouse is inside the OpenGL view, false otherwise
///
////////////////////////////////////////////////////////////
-(BOOL)isMouseInside;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Update the mouse state (in or out) /// \brief Update the mouse state (in or out)
/// ///

View File

@ -115,16 +115,9 @@
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////
-(void)hideMouseCursor -(BOOL)isMouseInside
{ {
[NSCursor hide]; return [m_oglView isMouseInside];
}
////////////////////////////////////////////////////////
-(void)showMouseCursor
{
[NSCursor unhide];
} }

View File

@ -332,16 +332,9 @@
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////
-(void)hideMouseCursor -(BOOL)isMouseInside
{ {
[NSCursor hide]; return [m_oglView isMouseInside];
}
////////////////////////////////////////////////////////
-(void)showMouseCursor
{
[NSCursor unhide];
} }

View File

@ -112,6 +112,41 @@ void scaleOutXY(T& out, id<WindowImplDelegateProtocol> delegate)
scaleOut(out.y, delegate); scaleOut(out.y, delegate);
} }
////////////////////////////////////////////////////////////
/// According to Apple's documentation, each invocation of
/// unhide must be balanced by an invocation of hide in
/// order for the cursor display to be correct.
/// So we keep track of those calls ourself.
////////////////////////////////////////////////////////////
namespace
{
bool isCursorHidden = false; // initially, the cursor is visible
}
////////////////////////////////////////////////////////
void hideMouseCursor()
{
if (!isCursorHidden)
{
[NSCursor hide];
isCursorHidden = true;
}
}
////////////////////////////////////////////////////////
void showMouseCursor()
{
if (isCursorHidden)
{
[NSCursor unhide];
isCursorHidden = false;
}
}
#pragma mark #pragma mark
#pragma mark WindowImplCocoa's ctor/dtor #pragma mark WindowImplCocoa's ctor/dtor
@ -268,8 +303,8 @@ void WindowImplCocoa::windowResized(unsigned int width, unsigned int height)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void WindowImplCocoa::windowLostFocus(void) void WindowImplCocoa::windowLostFocus(void)
{ {
if (!m_showCursor) if (!m_showCursor && [m_delegate isMouseInside])
[m_delegate showMouseCursor]; // Make sure the cursor is visible showMouseCursor(); // Make sure the cursor is visible
Event event; Event event;
event.type = Event::LostFocus; event.type = Event::LostFocus;
@ -281,8 +316,8 @@ void WindowImplCocoa::windowLostFocus(void)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void WindowImplCocoa::windowGainedFocus(void) void WindowImplCocoa::windowGainedFocus(void)
{ {
if (!m_showCursor) if (!m_showCursor && [m_delegate isMouseInside])
[m_delegate hideMouseCursor]; // Restore user's setting hideMouseCursor(); // Restore user's setting
Event event; Event event;
event.type = Event::GainedFocus; event.type = Event::GainedFocus;
@ -351,7 +386,7 @@ void WindowImplCocoa::mouseWheelScrolledAt(float delta, int x, int y)
void WindowImplCocoa::mouseMovedIn(void) void WindowImplCocoa::mouseMovedIn(void)
{ {
if (!m_showCursor) if (!m_showCursor)
[m_delegate hideMouseCursor]; // Restore user's setting hideMouseCursor(); // Restore user's setting
Event event; Event event;
event.type = Event::MouseEntered; event.type = Event::MouseEntered;
@ -363,7 +398,7 @@ void WindowImplCocoa::mouseMovedIn(void)
void WindowImplCocoa::mouseMovedOut(void) void WindowImplCocoa::mouseMovedOut(void)
{ {
if (!m_showCursor) if (!m_showCursor)
[m_delegate showMouseCursor]; // Make sure the cursor is visible showMouseCursor(); // Make sure the cursor is visible
Event event; Event event;
event.type = Event::MouseLeft; event.type = Event::MouseLeft;
@ -496,10 +531,14 @@ void WindowImplCocoa::setMouseCursorVisible(bool visible)
{ {
m_showCursor = visible; m_showCursor = visible;
if (m_showCursor) // If the mouse is over the window, we apply the new setting
[m_delegate showMouseCursor]; if ([m_delegate isMouseInside])
else {
[m_delegate hideMouseCursor]; if (m_showCursor)
showMouseCursor();
else
hideMouseCursor();
}
} }

View File

@ -89,16 +89,12 @@ namespace sf {
-(sf::WindowHandle)getSystemHandle; -(sf::WindowHandle)getSystemHandle;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Hide the mouse cursor /// \brief Determine where the mouse is
///
/// \return true when the mouse is inside the OpenGL view, false otherwise
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
-(void)hideMouseCursor; -(BOOL)isMouseInside;
////////////////////////////////////////////////////////////
/// \brief Show the mouse cursor
///
////////////////////////////////////////////////////////////
-(void)showMouseCursor;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Get window position /// \brief Get window position