Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adds a new appearance and fixes bugs related to key equivalents.

  • Loading branch information...
commit da2e1af5741cff902ee4c06408316a92695450b8 1 parent dddb527
@shpakovski authored
View
45 MASShortcut.m
@@ -52,7 +52,8 @@ + (MASShortcut *)shortcutWithEvent:(NSEvent *)event
+ (MASShortcut *)shortcutWithData:(NSData *)data
{
- return (data ? (MASShortcut *)[NSKeyedUnarchiver unarchiveObjectWithData:data] : nil);
+ id shortcut = (data ? [NSKeyedUnarchiver unarchiveObjectWithData:data] : nil);
+ return shortcut;
}
#pragma mark - Shortcut accessors
@@ -82,6 +83,35 @@ - (NSString *)description
return [NSString stringWithFormat:@"%@%@", self.modifierFlagsString, self.keyCodeString];
}
+- (NSString *)keyCodeStringForKeyEquivalent
+{
+ NSString *keyCodeString = self.keyCodeString;
+ if (keyCodeString.length > 1) {
+ switch (self.keyCode) {
+ case kVK_F1: return MASShortcutChar(0xF704);
+ case kVK_F2: return MASShortcutChar(0xF705);
+ case kVK_F3: return MASShortcutChar(0xF706);
+ case kVK_F4: return MASShortcutChar(0xF707);
+ case kVK_F5: return MASShortcutChar(0xF708);
+ case kVK_F6: return MASShortcutChar(0xF709);
+ case kVK_F7: return MASShortcutChar(0xF70a);
+ case kVK_F8: return MASShortcutChar(0xF70b);
+ case kVK_F9: return MASShortcutChar(0xF70c);
+ case kVK_F10: return MASShortcutChar(0xF70d);
+ case kVK_F11: return MASShortcutChar(0xF70e);
+ case kVK_F12: return MASShortcutChar(0xF70f);
+ // From this point down I am guessing F13 etc come sequentially, I don't have a keyboard to test.
+ case kVK_F13: return MASShortcutChar(0xF710);
+ case kVK_F14: return MASShortcutChar(0xF711);
+ case kVK_F15: return MASShortcutChar(0xF712);
+ case kVK_F16: return MASShortcutChar(0xF713);
+ case kVK_Space: return MASShortcutChar(0x20);
+ default: return @"";
+ }
+ }
+ return keyCodeString.lowercaseString;
+}
+
- (NSString *)keyCodeString
{
// Some key codes don't have an equivalent
@@ -223,8 +253,15 @@ - (BOOL)isKeyEquivalent:(NSString *)keyEquivalent flags:(NSUInteger)flags takenI
if (menuItem.hasSubmenu && [self isKeyEquivalent:keyEquivalent flags:flags takenInMenu:menuItem.submenu error:outError]) return YES;
BOOL equalFlags = (MASShortcutClear(menuItem.keyEquivalentModifierMask) == flags);
- BOOL equalHotkey = [menuItem.keyEquivalent.uppercaseString isEqualToString:keyEquivalent];
- if (equalFlags && equalHotkey) {
+ BOOL equalHotkeyLowercase = [menuItem.keyEquivalent.lowercaseString isEqualToString:keyEquivalent];
+
+ // Check if the cases are different, we know ours is lower and that shift is included in our modifiers
+ // If theirs is capitol, we need to add shift to their modifiers
+ if (equalHotkeyLowercase && ![menuItem.keyEquivalent isEqualToString:keyEquivalent]) {
+ equalFlags = (MASShortcutClear(menuItem.keyEquivalentModifierMask | NSShiftKeyMask) == flags);
+ }
+
+ if (equalFlags && equalHotkeyLowercase) {
if (outError) {
NSString *format = NSLocalizedString(@"This shortcut cannot be used used because it is already used by the menu item ‘%@’.",
@"Message for alert when shortcut is already used");
@@ -265,7 +302,7 @@ - (BOOL)isTakenError:(NSError **)outError
}
CFRelease(globalHotKeys);
}
- return [self isKeyEquivalent:self.keyCodeString flags:self.modifierFlags takenInMenu:[NSApp mainMenu] error:outError];
+ return [self isKeyEquivalent:self.keyCodeStringForKeyEquivalent flags:self.modifierFlags takenInMenu:[NSApp mainMenu] error:outError];
}
@end
View
6 MASShortcutView+UserDefaults.m
@@ -113,7 +113,11 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
if (_internalShortcutChange) return;
MASShortcut *shortcut = [object valueForKey:keyPath];
_internalPreferenceChange = YES;
- [[NSUserDefaults standardUserDefaults] setObject:shortcut.data forKey:_userDefaultsKey];
+
+ NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
+ [defaults setObject:(shortcut.data ?: [NSKeyedArchiver archivedDataWithRootObject:nil]) forKey:_userDefaultsKey];
+ [defaults synchronize];
+
_internalPreferenceChange = NO;
}
else {
View
5 MASShortcutView.h
@@ -1,8 +1,9 @@
@class MASShortcut;
typedef enum {
- MASShortcutViewAppearanceDefault = 0,
- MASShortcutViewAppearanceTexturedRect
+ MASShortcutViewAppearanceDefault = 0, // Height = 19 px
+ MASShortcutViewAppearanceTexturedRect, // Height = 25 px
+ MASShortcutViewAppearanceRounded // Height = 43 px
} MASShortcutViewAppearance;
@interface MASShortcutView : NSView
View
16 MASShortcutView.m
@@ -83,6 +83,10 @@ - (void)resetShortcutCellStyle
_shortcutCell.bezelStyle = NSTexturedRoundedBezelStyle;
break;
}
+ case MASShortcutViewAppearanceRounded: {
+ _shortcutCell.bezelStyle = NSRoundedBezelStyle;
+ break;
+ }
}
}
@@ -148,6 +152,10 @@ - (void)drawInRect:(CGRect)frame withTitle:(NSString *)title alignment:(NSTextAl
[_shortcutCell drawWithFrame:CGRectOffset(frame, 0.0, 1.0) inView:self];
break;
}
+ case MASShortcutViewAppearanceRounded: {
+ [_shortcutCell drawWithFrame:CGRectOffset(frame, 0.0, 1.0) inView:self];
+ break;
+ }
}
}
@@ -195,7 +203,13 @@ - (void)drawRect:(CGRect)dirtyRect
- (void)getShortcutRect:(CGRect *)shortcutRectRef hintRect:(CGRect *)hintRectRef
{
CGRect shortcutRect, hintRect;
- CGRectDivide(self.bounds, &hintRect, &shortcutRect, HINT_BUTTON_WIDTH, CGRectMaxXEdge);
+ CGFloat hintButtonWidth = HINT_BUTTON_WIDTH;
+ switch (self.appearance) {
+ case MASShortcutViewAppearanceTexturedRect: hintButtonWidth += 2.0; break;
+ case MASShortcutViewAppearanceRounded: hintButtonWidth += 3.0; break;
+ default: break;
+ }
+ CGRectDivide(self.bounds, &hintRect, &shortcutRect, hintButtonWidth, CGRectMaxXEdge);
if (shortcutRectRef) *shortcutRectRef = shortcutRect;
if (hintRectRef) *hintRectRef = hintRect;
}
Please sign in to comment.
Something went wrong with that request. Please try again.