From c48b74d52d0920f258a20a40a10707f6df668ffe Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Thu, 6 Mar 2025 03:08:41 -0800 Subject: [PATCH] Bug Fix: Move default hotkeys to main app startup Instead of the Preferences plugin. Signed-off-by: Christopher Snowhill --- Application/AppController.m | 64 ++++++++++++++++++- .../Preferences/Base.lproj/Preferences.xib | 2 +- Preferences/Preferences/HotKeyPane.m | 59 +---------------- 3 files changed, 66 insertions(+), 59 deletions(-) diff --git a/Application/AppController.m b/Application/AppController.m index acd0e27d8..5a55582be 100644 --- a/Application/AppController.m +++ b/Application/AppController.m @@ -209,6 +209,8 @@ static BOOL consentLastEnabled = NO; [randomizeButton setToolTip:NSLocalizedString(@"RandomizeButtonTooltip", @"")]; [fileButton setToolTip:NSLocalizedString(@"FileButtonTooltip", @"")]; + [self registerDefaultHotKeys]; + [self registerHotKeys]; (void)[spotlightWindowController init]; @@ -715,7 +717,67 @@ static BOOL consentLastEnabled = NO; } } -/* Unassign previous handler first, so dealloc can unregister it from the global map before the new instances are assigned */ +MASShortcut *shortcutWithMigration(NSString *oldKeyCodePrefName, + NSString *oldKeyModifierPrefName, + NSString *newShortcutPrefName, + NSInteger newDefaultKeyCode) { + NSEventModifierFlags defaultModifiers = NSEventModifierFlagControl | NSEventModifierFlagCommand; + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + if([defaults objectForKey:oldKeyCodePrefName]) { + NSInteger oldKeyCode = [defaults integerForKey:oldKeyCodePrefName]; + NSEventModifierFlags oldKeyModifiers = [defaults integerForKey:oldKeyModifierPrefName]; + // Should we consider temporarily save these values for further migration? + [defaults removeObjectForKey:oldKeyCodePrefName]; + [defaults removeObjectForKey:oldKeyModifierPrefName]; + return [MASShortcut shortcutWithKeyCode:oldKeyCode modifierFlags:oldKeyModifiers]; + } else { + return [MASShortcut shortcutWithKeyCode:newDefaultKeyCode modifierFlags:defaultModifiers]; + } +} + +static NSUserDefaultsController *shortcutDefaultsController = nil; + +- (void)registerDefaultHotKeys { + MASShortcut *playShortcut = shortcutWithMigration(@"hotKeyPlayKeyCode", + @"hotKeyPlayModifiers", + CogPlayShortcutKey, + kVK_ANSI_P); + MASShortcut *nextShortcut = shortcutWithMigration(@"hotKeyNextKeyCode", + @"hotKeyNextModifiers", + CogNextShortcutKey, + kVK_ANSI_N); + MASShortcut *prevShortcut = shortcutWithMigration(@"hotKeyPreviousKeyCode", + @"hotKeyPreviousModifiers", + CogPrevShortcutKey, + kVK_ANSI_R); + MASShortcut *spamShortcut = [MASShortcut shortcutWithKeyCode:kVK_ANSI_C + modifierFlags:NSEventModifierFlagControl | NSEventModifierFlagCommand]; + MASShortcut *fadeShortcut = [MASShortcut shortcutWithKeyCode:kVK_ANSI_O + modifierFlags:NSEventModifierFlagControl | NSEventModifierFlagCommand]; + + NSData *playShortcutData = [NSKeyedArchiver archivedDataWithRootObject:playShortcut]; + NSData *nextShortcutData = [NSKeyedArchiver archivedDataWithRootObject:nextShortcut]; + NSData *prevShortcutData = [NSKeyedArchiver archivedDataWithRootObject:prevShortcut]; + NSData *spamShortcutData = [NSKeyedArchiver archivedDataWithRootObject:spamShortcut]; + NSData *fadeShortcutData = [NSKeyedArchiver archivedDataWithRootObject:fadeShortcut]; + + // Register default values to be used for the first app start + NSDictionary *defaultShortcuts = @{ + CogPlayShortcutKey: playShortcutData, + CogNextShortcutKey: nextShortcutData, + CogPrevShortcutKey: prevShortcutData, + CogSpamShortcutKey: spamShortcutData, + CogFadeShortcutKey: fadeShortcutData + }; + + [[NSUserDefaults standardUserDefaults] registerDefaults:defaultShortcuts]; + shortcutDefaultsController = [[NSUserDefaultsController sharedUserDefaultsController] initWithDefaults:nil initialValues:defaultShortcuts]; +} + +- (IBAction)resetHotkeys:(id)sender { + [shortcutDefaultsController revertToInitialValues:sender]; +} + - (void)registerHotKeys { MASShortcutBinder *binder = [MASShortcutBinder sharedBinder]; [binder bindShortcutWithDefaultsKey:CogPlayShortcutKey diff --git a/Preferences/Preferences/Base.lproj/Preferences.xib b/Preferences/Preferences/Base.lproj/Preferences.xib index 188686e9c..163bad74a 100644 --- a/Preferences/Preferences/Base.lproj/Preferences.xib +++ b/Preferences/Preferences/Base.lproj/Preferences.xib @@ -225,7 +225,7 @@ - + diff --git a/Preferences/Preferences/HotKeyPane.m b/Preferences/Preferences/HotKeyPane.m index 7102b77f7..e7fe2a513 100644 --- a/Preferences/Preferences/HotKeyPane.m +++ b/Preferences/Preferences/HotKeyPane.m @@ -9,68 +9,13 @@ #import "HotKeyPane.h" #import "Shortcuts.h" -MASShortcut *shortcutWithMigration(NSString *oldKeyCodePrefName, - NSString *oldKeyModifierPrefName, - NSString *newShortcutPrefName, - NSInteger newDefaultKeyCode) { - NSEventModifierFlags defaultModifiers = NSEventModifierFlagControl | NSEventModifierFlagCommand; - NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; - if([defaults objectForKey:oldKeyCodePrefName]) { - NSInteger oldKeyCode = [defaults integerForKey:oldKeyCodePrefName]; - NSEventModifierFlags oldKeyModifiers = [defaults integerForKey:oldKeyModifierPrefName]; - // Should we consider temporarily save these values for further migration? - [defaults removeObjectForKey:oldKeyCodePrefName]; - [defaults removeObjectForKey:oldKeyModifierPrefName]; - return [MASShortcut shortcutWithKeyCode:oldKeyCode modifierFlags:oldKeyModifiers]; - } else { - return [MASShortcut shortcutWithKeyCode:newDefaultKeyCode modifierFlags:defaultModifiers]; - } -} - @implementation HotKeyPane { NSUserDefaultsController *defaultsController; } +// Defaults have been moved to AppController.m - (void)awakeFromNib { - MASShortcut *playShortcut = shortcutWithMigration(@"hotKeyPlayKeyCode", - @"hotKeyPlayModifiers", - CogPlayShortcutKey, - kVK_ANSI_P); - MASShortcut *nextShortcut = shortcutWithMigration(@"hotKeyNextKeyCode", - @"hotKeyNextModifiers", - CogNextShortcutKey, - kVK_ANSI_N); - MASShortcut *prevShortcut = shortcutWithMigration(@"hotKeyPreviousKeyCode", - @"hotKeyPreviousModifiers", - CogPrevShortcutKey, - kVK_ANSI_R); - MASShortcut *spamShortcut = shortcutWithMigration(@"hotKeySpamKeyCode", - @"hotKeySpamModifiers", - CogSpamShortcutKey, - kVK_ANSI_C); - MASShortcut *fadeShortcut = shortcutWithMigration(@"hotKeyFadeKeyCode", - @"hotKeyFadeModifiers", - CogFadeShortcutKey, - kVK_ANSI_O); - - NSData *playShortcutData = [NSKeyedArchiver archivedDataWithRootObject:playShortcut]; - NSData *nextShortcutData = [NSKeyedArchiver archivedDataWithRootObject:nextShortcut]; - NSData *prevShortcutData = [NSKeyedArchiver archivedDataWithRootObject:prevShortcut]; - NSData *spamShortcutData = [NSKeyedArchiver archivedDataWithRootObject:spamShortcut]; - NSData *fadeShortcutData = [NSKeyedArchiver archivedDataWithRootObject:fadeShortcut]; - - // Register default values to be used for the first app start - NSDictionary *defaultShortcuts = @{ - CogPlayShortcutKey: playShortcutData, - CogNextShortcutKey: nextShortcutData, - CogPrevShortcutKey: prevShortcutData, - CogSpamShortcutKey: spamShortcutData, - CogFadeShortcutKey: fadeShortcutData - }; - - defaultsController = - [[NSUserDefaultsController sharedUserDefaultsController] initWithDefaults:nil - initialValues:defaultShortcuts]; + defaultsController = [NSUserDefaultsController sharedUserDefaultsController]; _playShortcutView.associatedUserDefaultsKey = CogPlayShortcutKey; _nextShortcutView.associatedUserDefaultsKey = CogNextShortcutKey;