From dce0e5fdebb4ab7add5eaf785a82d763c79048e6 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 5 Jul 2016 23:34:33 +0300 Subject: [PATCH] Hide mouse cursor when running (Cocoa) --- Cocoa/Document.m | 7 ++++++ Cocoa/GBView.h | 1 + Cocoa/GBView.m | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 24482c31..bc276cf5 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -94,6 +94,7 @@ static uint32_t rgbEncode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b) - (void) vblank { + self.view.mouseHidingEnabled = YES; [self.view flip]; GB_set_pixels_output(&gb, self.view.pixels); } @@ -107,11 +108,13 @@ static uint32_t rgbEncode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b) self.audioClient = [[GBAudioClient alloc] initWithRendererBlock:^(UInt32 sampleRate, UInt32 nFrames, GB_sample_t *buffer) { GB_apu_copy_buffer(&gb, buffer, nFrames); } andSampleRate:96000]; + self.view.mouseHidingEnabled = YES; [self.audioClient start]; while (running) { GB_run(&gb); } [self.audioClient stop]; + self.view.mouseHidingEnabled = NO; GB_save_battery(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sav"] UTF8String]); stopping = false; } @@ -309,6 +312,10 @@ static uint32_t rgbEncode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b) return; } pendingLogLines++; + + /* Make sure mouse is not hidden while debugging */ + self.view.mouseHidingEnabled = NO; + NSString *nsstring = @(string); // For ref-counting dispatch_async(dispatch_get_main_queue(), ^{ NSFont *font = [NSFont userFixedPitchFontOfSize:12]; diff --git a/Cocoa/GBView.h b/Cocoa/GBView.h index a2baada2..f8763ac5 100644 --- a/Cocoa/GBView.h +++ b/Cocoa/GBView.h @@ -8,4 +8,5 @@ @property GB_gameboy_t *gb; @property (nonatomic) BOOL shouldBlendFrameWithPrevious; @property GBShader *shader; +@property (getter=isMouseHidingEnabled) BOOL mouseHidingEnabled; @end diff --git a/Cocoa/GBView.m b/Cocoa/GBView.m index f6581de0..0053c667 100644 --- a/Cocoa/GBView.m +++ b/Cocoa/GBView.m @@ -8,6 +8,9 @@ { uint32_t *image_buffers[3]; unsigned char current_buffer; + BOOL mouse_hidden; + NSTrackingArea *tracking_area; + BOOL _mouseHidingEnabled; } - (void) awakeFromNib @@ -42,6 +45,11 @@ _shouldBlendFrameWithPrevious = 1; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(filterChanged) name:@"GBFilterChanged" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ratioKeepingChanged) name:@"GBAspectChanged" object:nil]; + tracking_area = [ [NSTrackingArea alloc] initWithRect:(NSRect){} + options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect + owner:self + userInfo:nil]; + [self addTrackingArea:tracking_area]; } - (void) filterChanged @@ -71,6 +79,10 @@ free(image_buffers[0]); free(image_buffers[1]); free(image_buffers[2]); + if (mouse_hidden) { + mouse_hidden = false; + [NSCursor unhide]; + } [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (instancetype)initWithCoder:(NSCoder *)coder @@ -111,6 +123,7 @@ frame.origin.x = 0; } } + [super setFrame:frame]; } @@ -223,4 +236,46 @@ { return YES; } + +- (void)mouseEntered:(NSEvent *)theEvent +{ + if (!mouse_hidden) { + mouse_hidden = true; + if (_mouseHidingEnabled) { + [NSCursor hide]; + } + } + [super mouseEntered:theEvent]; +} + +- (void)mouseExited:(NSEvent *)theEvent +{ + if (mouse_hidden) { + mouse_hidden = false; + if (_mouseHidingEnabled) { + [NSCursor unhide]; + } + } + [super mouseExited:theEvent]; +} + +- (void)setMouseHidingEnabled:(BOOL)mouseHidingEnabled +{ + if (mouseHidingEnabled == _mouseHidingEnabled) return; + + _mouseHidingEnabled = mouseHidingEnabled; + + if (mouse_hidden && _mouseHidingEnabled) { + [NSCursor hide]; + } + + if (mouse_hidden && !_mouseHidingEnabled) { + [NSCursor unhide]; + } +} + +- (BOOL)isMouseHidingEnabled +{ + return _mouseHidingEnabled; +} @end