-
Notifications
You must be signed in to change notification settings - Fork 5
/
HotKeyPreferencesController.m
181 lines (143 loc) · 4.41 KB
/
HotKeyPreferencesController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* $Id$
*
* Copyright (C) 2005 - 2009 Stephen F. Booth <me@sbooth.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#import "HotKeyPreferencesController.h"
#import "PlayApplicationDelegate.h"
@implementation HotKeyPreferencesController {
SRShortcutValidator *_validator;
}
#pragma mark SRRecorderControlDelegate
- (BOOL)shortcutRecorder:(SRRecorderControl *)aRecorder
canRecordShortcut:(NSDictionary *)aShortcut
{
__autoreleasing NSError *error = nil;
BOOL isTaken =
[_validator isKeyCode:[aShortcut[SRShortcutKeyKeyCode] unsignedShortValue]
andFlagsTaken:[aShortcut[SRShortcutKeyModifierFlags] unsignedIntegerValue]
error:&error];
if (isTaken) {
NSBeep();
NSWindow *window = aRecorder.window;
[window presentError:error
modalForWindow:window
delegate:nil
didPresentSelector:NULL
contextInfo:NULL];
}
return !isTaken;
}
- (BOOL)shortcutRecorder:(SRRecorderControl *)aRecorder shouldUnconditionallyAllowModifierFlags:(NSEventModifierFlags)aModifierFlags forKeyCode:(unsigned short)aKeyCode
{
if ((aModifierFlags & aRecorder.requiredModifierFlags) != aRecorder.requiredModifierFlags) {
return NO;
}
if ((aModifierFlags & aRecorder.allowedModifierFlags) != aModifierFlags) {
return NO;
}
switch (aKeyCode) {
case kVK_F1:
case kVK_F2:
case kVK_F3:
case kVK_F4:
case kVK_F5:
case kVK_F6:
case kVK_F7:
case kVK_F8:
case kVK_F9:
case kVK_F10:
case kVK_F11:
case kVK_F12:
case kVK_F13:
case kVK_F14:
case kVK_F15:
case kVK_F16:
case kVK_F17:
case kVK_F18:
case kVK_F19:
case kVK_F20:
return YES;
default:
return NO;
}
}
#pragma mark SRShortcutValidatorDelegate
- (BOOL)shortcutValidator:(SRShortcutValidator *)aValidator
isKeyCode:(unsigned short)aKeyCode
andFlagsTaken:(NSEventModifierFlags)aFlags
reason:(NSString **)outReason
{
#define IS_TAKEN(aRecorder) (recorder != (aRecorder) && SRShortcutEqualToShortcut(shortcut, [(aRecorder) objectValue]))
SRRecorderControl *recorder = (SRRecorderControl *)self.window.firstResponder;
if (![recorder isKindOfClass:[SRRecorderControl class]]) {
return NO;
}
NSDictionary *shortcut = SRShortcutWithCocoaModifierFlagsAndKeyCode(aFlags, aKeyCode);
if (IS_TAKEN(_playPauseShortcutRecorder) ||
IS_TAKEN(_previousStreamShortcutRecorder) ||
IS_TAKEN(_nextStreamShortcutRecorder)
) {
*outReason = NSLocalizedString(@"The shortcut is already used. To use your shortcut, first remove or change the other shortcut.", @"shortcut already in use error message");
return YES;
}
else {
return NO;
}
#undef IS_TAKEN
}
- (BOOL)shortcutValidatorShouldCheckSystemShortcuts:(SRShortcutValidator *)aValidator
{
return YES;
}
- (BOOL)shortcutValidatorShouldCheckMenu:(SRShortcutValidator *)aValidator
{
return YES;
}
#pragma mark NSObject
- (id) init
{
if((self = [super initWithWindowNibName:@"HotKeyPreferences"])) {
}
return self;
}
- (void)awakeFromNib
{
[super awakeFromNib];
[self initKeyBindingsController];
}
- (void)initKeyBindingsController
{
[self prepareShortcutRecorder:_playPauseShortcutRecorder
forKey:@"playPauseHotKey"];
[self prepareShortcutRecorder:_nextStreamShortcutRecorder
forKey:@"playNextStreamHotKey"];
[self prepareShortcutRecorder:_previousStreamShortcutRecorder
forKey:@"playPreviousStreamHotKey"];
_validator = [[SRShortcutValidator alloc] initWithDelegate:self];
}
- (void)prepareShortcutRecorder:(SRRecorderControl *)shortcutRecorder
forKey:(NSString *)key;
{
NSUserDefaultsController *defaults = [NSUserDefaultsController sharedUserDefaultsController];
NSString *keyPath = [NSString stringWithFormat:@"values.%@", key];
[shortcutRecorder bind:NSValueBinding
toObject:defaults
withKeyPath:keyPath
options:nil];
}
@end