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;
////////////////////////////////////////////////////////////
/// \brief Determine where the mouse is
///
/// \return true when the mouse is inside the OpenGL view, false otherwise
///
////////////////////////////////////////////////////////////
-(BOOL)isMouseInside;
@end

View File

@ -67,14 +67,6 @@ BOOL isValidTextUnicode(NSEvent* event);
////////////////////////////////////////////////////////////
-(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)
///

View File

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

View File

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

View File

@ -112,6 +112,41 @@ void scaleOutXY(T& out, id<WindowImplDelegateProtocol> 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 WindowImplCocoa's ctor/dtor
@ -268,8 +303,8 @@ void WindowImplCocoa::windowResized(unsigned int width, unsigned int height)
////////////////////////////////////////////////////////////
void WindowImplCocoa::windowLostFocus(void)
{
if (!m_showCursor)
[m_delegate showMouseCursor]; // Make sure the cursor is visible
if (!m_showCursor && [m_delegate isMouseInside])
showMouseCursor(); // Make sure the cursor is visible
Event event;
event.type = Event::LostFocus;
@ -281,8 +316,8 @@ void WindowImplCocoa::windowLostFocus(void)
////////////////////////////////////////////////////////////
void WindowImplCocoa::windowGainedFocus(void)
{
if (!m_showCursor)
[m_delegate hideMouseCursor]; // Restore user's setting
if (!m_showCursor && [m_delegate isMouseInside])
hideMouseCursor(); // Restore user's setting
Event event;
event.type = Event::GainedFocus;
@ -351,7 +386,7 @@ void WindowImplCocoa::mouseWheelScrolledAt(float delta, int x, int y)
void WindowImplCocoa::mouseMovedIn(void)
{
if (!m_showCursor)
[m_delegate hideMouseCursor]; // Restore user's setting
hideMouseCursor(); // Restore user's setting
Event event;
event.type = Event::MouseEntered;
@ -363,7 +398,7 @@ void WindowImplCocoa::mouseMovedIn(void)
void WindowImplCocoa::mouseMovedOut(void)
{
if (!m_showCursor)
[m_delegate showMouseCursor]; // Make sure the cursor is visible
showMouseCursor(); // Make sure the cursor is visible
Event event;
event.type = Event::MouseLeft;
@ -496,10 +531,14 @@ void WindowImplCocoa::setMouseCursorVisible(bool visible)
{
m_showCursor = visible;
if (m_showCursor)
[m_delegate showMouseCursor];
else
[m_delegate hideMouseCursor];
// If the mouse is over the window, we apply the new setting
if ([m_delegate isMouseInside])
{
if (m_showCursor)
showMouseCursor();
else
hideMouseCursor();
}
}

View File

@ -89,16 +89,12 @@ namespace sf {
-(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;
////////////////////////////////////////////////////////////
/// \brief Show the mouse cursor
///
////////////////////////////////////////////////////////////
-(void)showMouseCursor;
-(BOOL)isMouseInside;
////////////////////////////////////////////////////////////
/// \brief Get window position