From 4a5d68ef73f44536feb38bb5033fe190b9b7a813 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 18 Nov 2016 23:14:04 -0500 Subject: AppDelegate: Only allocate one `Mouse` instance in app lifecycle Don't keep allocating and releasing `Mouse` objects when the hotkey is pressed. Instead, allocate a single instance to use for the entire run time of the application. --- Low Battery Yup d/AppDelegate.h | 5 ++++- Low Battery Yup d/AppDelegate.m | 12 ++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Low Battery Yup d/AppDelegate.h b/Low Battery Yup d/AppDelegate.h index 10f6e4b..700c26b 100644 --- a/Low Battery Yup d/AppDelegate.h +++ b/Low Battery Yup d/AppDelegate.h @@ -7,8 +7,11 @@ // #import +#import "Mouse.h" -@interface AppDelegate : NSObject +@interface AppDelegate : NSObject { + Mouse *_mouse; +} - (void)dismissLowBatteryWarning:(NSEvent *)hotKeyEvent; diff --git a/Low Battery Yup d/AppDelegate.m b/Low Battery Yup d/AppDelegate.m index 51c1626..7aa29fe 100644 --- a/Low Battery Yup d/AppDelegate.m +++ b/Low Battery Yup d/AppDelegate.m @@ -8,18 +8,20 @@ #import "AppDelegate.h" #import "DDHotKeyCenter.h" -#import "Mouse.h" #import @implementation AppDelegate - (void)dealloc { - [super dealloc]; + [_mouse release]; + [super dealloc]; } - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { + _mouse = [[Mouse alloc] init]; + DDHotKeyCenter *hotkey_center = [DDHotKeyCenter sharedHotKeyCenter]; [hotkey_center registerHotKeyWithKeyCode:kVK_ANSI_0 modifierFlags:(NSCommandKeyMask | NSAlternateKeyMask | NSShiftKeyMask | NSControlKeyMask) @@ -30,10 +32,8 @@ - (void)dismissLowBatteryWarning:(NSEvent *)hotKeyEvent { - Mouse *m = [[Mouse alloc] init]; - [m moveToLowBatteryOK]; - [m click]; - [m release]; + [_mouse moveToLowBatteryOK]; + [_mouse click]; } @end -- cgit v1.2.3