From 0ace189e38446e9a7d0dce4d3d839becfd7fee97 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Wed, 6 Mar 2019 21:49:50 -0500 Subject: [PATCH] Takes a basic stab at mouse cursor hiding. --- .../Mac/Clock Signal/Views/CSOpenGLView.m | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/OSBindings/Mac/Clock Signal/Views/CSOpenGLView.m b/OSBindings/Mac/Clock Signal/Views/CSOpenGLView.m index b56ad364e..f169b2a30 100644 --- a/OSBindings/Mac/Clock Signal/Views/CSOpenGLView.m +++ b/OSBindings/Mac/Clock Signal/Views/CSOpenGLView.m @@ -16,6 +16,9 @@ @implementation CSOpenGLView { CVDisplayLinkRef _displayLink; CGSize _backingSize; + + NSTrackingArea *_mouseTrackingArea; + NSTimer *_mouseHideTimer; } - (void)prepareOpenGL @@ -184,4 +187,45 @@ static CVReturn DisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeSt return NSDragOperationLink; } +#pragma mark - Mouse hiding + +- (void)updateTrackingAreas { + [super updateTrackingAreas]; + + if(_mouseTrackingArea) { + [self removeTrackingArea:_mouseTrackingArea]; + } + _mouseTrackingArea = + [[NSTrackingArea alloc] + initWithRect:self.bounds + options:NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveWhenFirstResponder + owner:self + userInfo:nil]; + [self addTrackingArea:_mouseTrackingArea]; +} + +- (void)mouseMoved:(NSEvent *)event { + [super mouseMoved:event]; + [self scheduleMouseHide]; +} + +- (void)mouseEntered:(NSEvent *)event { + [super mouseEntered:event]; + [self scheduleMouseHide]; +} + +- (void)scheduleMouseHide { + [_mouseHideTimer invalidate]; + _mouseHideTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 repeats:NO block:^(NSTimer * _Nonnull timer) { + [NSCursor setHiddenUntilMouseMoves:YES]; + }]; +} + +- (void)mouseExited:(NSEvent *)event { + [super mouseExited:event]; + + [_mouseHideTimer invalidate]; + _mouseHideTimer = nil; +} + @end