-
Notifications
You must be signed in to change notification settings - Fork 139
/
PrefsWindowController.m
456 lines (364 loc) · 15.4 KB
/
PrefsWindowController.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// This file is part of Scroll Reverser <https://pilotmoon.com/scrollreverser/>
// Licensed under Apache License v2.0 <http://www.apache.org/licenses/LICENSE-2.0>
#import "PrefsWindowController.h"
#import "AppDelegate.h"
#import "LinkView.h"
static NSString *const kPanelScrolling=@"scrolling";
static NSString *const kPanelApp=@"app";
static NSString *const kKeyView=@"view";
static NSString *const kKeyTitle=@"title";
static NSString *const kKeyImageName=@"image";
static NSString *const kPrefsToolbarIdentifer=@"PrefsToolbar";
static NSString *const kPrefsLastUsedPanel=@"PrefsLastUsedPanel";
static void *_contextRefresh=&_contextRefresh;
static void *_contextPrefsStepSize=&_contextPrefsStepSize;
@interface PrefsWindowController ()
@property NSTabView *tabView;
@property NSToolbar *toolbar;
@property NSDictionary *panels;
@property CGFloat width;
@end
@implementation PrefsWindowController
#pragma mark Step size slider
static const double _offset=0.1;
static const double _exponent=1.8;
static const double _multiplier=25.0;
- (NSNumber *)stepSizeSliderValue
{
const NSInteger stepSize=[[NSUserDefaults standardUserDefaults] integerForKey:PrefsDiscreteScrollStepSize];
const double result=pow(stepSize/_multiplier,1.0/_exponent);
NSLog(@"read pref %@ from %@", @(result), @(stepSize));
return @(result-_offset);
}
- (void)setStepSizeSliderValue:(NSNumber *)stepSizeSliderValue
{
const double sliderValue=[stepSizeSliderValue doubleValue]+_offset;
const NSInteger result=lround(pow(sliderValue,_exponent)*_multiplier);
NSLog(@"set pref %@ from %@", @(result), @(sliderValue));
[[NSUserDefaults standardUserDefaults] setInteger:result forKey:PrefsDiscreteScrollStepSize];
}
#pragma mark Updater
- (SPUUpdater *)updater
{
return ((AppDelegate *)[NSApp delegate]).updater;
}
- (IBAction)buttonCheckForUpdatesClicked:(id)sender
{
[self.updater checkForUpdates];
}
#pragma mark Window showing
// animate window frame to draw user's attention
- (void)callAttention
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC*0.05), dispatch_get_main_queue(), ^{
const NSRect frame = [self.window frame];
const float offset = 0.04 * frame.size.height;
[NSAnimationContext currentContext].duration = 0.08;
[NSAnimationContext runAnimationGroup:^(NSAnimationContext * _Nonnull context) {
[[self.window animator] setFrame:NSMakeRect(frame.origin.x, frame.origin.y+offset, frame.size.width, frame.size.height)
display:NO];
} completionHandler:^{
[NSAnimationContext runAnimationGroup:^(NSAnimationContext * _Nonnull context) {
[[self.window animator] setFrame:frame display:NO];
}];
}];
});
}
- (void)windowDidLoad
{
[super windowDidLoad];
self.width=400; // minimum width to avoid toolbar collapse
NSArray *const toolbarDefinition=@[kPanelScrolling, kPanelApp];
NSDictionary *const panelsDefinition=@{kPanelScrolling: @{kKeyView: self.scrollingSettings,
kKeyTitle: self.menuStringScrollingSettings,
kKeyImageName: NSImageNamePreferencesGeneral},
kPanelApp: @{kKeyView: self.appSettings,
kKeyTitle: self.menuStringAppSettings,
kKeyImageName: NSImageNameApplicationIcon}};
// set up tab view
self.tabView=[[NSTabView alloc] initWithFrame:[(NSView *)[self.window contentView] frame]];
[self.tabView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
[self.tabView setTabViewType:NSNoTabsNoBorder];
[self.tabView setDelegate:self];
[[self.window contentView] addSubview:self.tabView];
// set up panels
self.panels=[NSMutableDictionary dictionary];
for(NSString *key in [panelsDefinition allKeys]) {
// make mutable copy of definition
NSMutableDictionary *panelData=[panelsDefinition[key] mutableCopy];
// get the view
NSView *view=panelData[kKeyView];
// create tab view item
NSTabViewItem *tabViewItem=[[NSTabViewItem alloc] initWithIdentifier:key];
[tabViewItem setLabel:panelData[kKeyTitle]];
[tabViewItem setView:view];
// save to panels dict
const NSSize size=[view fittingSize];
// set window width to largest view fitting width
self.width=MAX(self.width, size.width);
((NSMutableDictionary *)self.panels)[key] = panelData;
// add to tab bar
[self.tabView addTabViewItem:tabViewItem];
}
// set up toolbar
self.toolbar = [[NSToolbar alloc] initWithIdentifier:kPrefsToolbarIdentifer];
[self.toolbar setAllowsUserCustomization:NO];
[self.toolbar setAutosavesConfiguration:NO];
[self.toolbar setDisplayMode:NSToolbarDisplayModeIconAndLabel];
[self.toolbar setDelegate:self];
[self.window setToolbar:self.toolbar];
[toolbarDefinition enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[self.toolbar insertItemWithItemIdentifier:obj atIndex:idx];
}];
// identify the starting pane
NSString *startingIdentifier=[[NSUserDefaults standardUserDefaults] stringForKey:kPrefsLastUsedPanel];
if(!startingIdentifier||
![[self.panels allKeys] containsObject:startingIdentifier]||
![toolbarDefinition containsObject:startingIdentifier])
{
startingIdentifier=[toolbarDefinition firstObject];
}
// other set-up
self.linkView.url=self.appDelegate.appLink;
[self.appDelegate.permissionsManager addObserver:self forKeyPath:@"accessibilityEnabled" options:0 context:_contextRefresh];
[self.appDelegate.permissionsManager addObserver:self forKeyPath:@"inputMonitoringEnabled" options:0 context:_contextRefresh];
[[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:[@"values." stringByAppendingString:PrefsDiscreteScrollStepSize] options:NSKeyValueObservingOptionNew context:_contextPrefsStepSize];
// select the initial pane
[self.tabView selectTabViewItemWithIdentifier:startingIdentifier];
[self.toolbar setSelectedItemIdentifier:startingIdentifier];
[self updateWindowForIdentifier:startingIdentifier];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
if (context==_contextRefresh) {
NSLog(@"refresh observe");
}
else if (context==_contextPrefsStepSize) {
NSLog(@"stepsize observe");
}
}
- (void)showWindow:(id)sender
{
self.window.delegate=self;
self.window.level=NSNormalWindowLevel;
if (![NSApp isActive]) {
[NSApp activateIgnoringOtherApps:YES];
}
if (self.window.visible) {
[self callAttention];
}
else {
[self.window center];
}
[super showWindow:sender];
}
- (void)setPane:(NSString *)identifier
{
// select the appropriate tab view item
[self.tabView selectTabViewItemWithIdentifier:identifier];
[self.toolbar setSelectedItemIdentifier:identifier];
// save this as the last used panel
[[NSUserDefaults standardUserDefaults] setObject:identifier forKey:kPrefsLastUsedPanel];
}
// Futz about with the geometry
- (void)updateWindowForIdentifier:(NSString *)identifier
{
// we simply set the width to out pre-stored width. autolayout will deal with the height.
NSRect contentRect=[NSWindow contentRectForFrameRect:[self.window frame]
styleMask:[self.window styleMask]];
contentRect.size.width=self.width;
[self.window setFrame:[NSWindow frameRectForContentRect:contentRect
styleMask:[self.window styleMask]]
display:YES
animate:NO];
}
#pragma mark Permissions
- (void)showPermissionsPane {
[self setPane:kPanelScrolling];
}
- (IBAction)buttonAXClicked:(id)sender {
if (self.appDelegate.permissionsManager.accessibilityEnabled) {
[self.appDelegate.permissionsManager openAccessibilityPrefs];
}
else {
[self.appDelegate.permissionsManager requestAccessibilityPermission];
}
}
- (IBAction)buttonIMClicked:(id)sender {
if (self.appDelegate.permissionsManager.inputMonitoringRequested) {
[self.appDelegate.permissionsManager openInputMonitoringPrefs];
}
else {
[self.appDelegate.permissionsManager requestInputMonitoringPermission];
}
}
- (IBAction)buttonPermissionsHelpClicked:(id)sender {
NSLog(@"Permissions help clicked %@", sender);
[[NSWorkspace sharedWorkspace] openURL:self.appDelegate.appPermissionsHelpLink];
}
#pragma mark Toolbar Delegate methods
// Called when a toolbar button is clicked, to effect the pane switch
- (void)toolbarItemClicked:(id)sender
{
[self setPane:[[self.tabView tabViewItemAtIndex:[sender tag]] identifier]];
}
// This is where we actually create the toolbar item.
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)identifier willBeInsertedIntoToolbar:(BOOL)flag
{
NSDictionary *const panelInfo=(self.panels)[identifier];
// Set up the toolbar item
NSToolbarItem *const toolbarItem=[[NSToolbarItem alloc] initWithItemIdentifier:identifier];
[toolbarItem setTarget:self];
[toolbarItem setAction:@selector(toolbarItemClicked:)];
[toolbarItem setImage:[NSImage imageNamed:panelInfo[kKeyImageName]]];
[toolbarItem setLabel:panelInfo[kKeyTitle]];
// We use the tag to record the index of the corresponding tab view
[toolbarItem setTag:[self.tabView indexOfTabViewItemWithIdentifier:identifier]];
return toolbarItem;
}
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
{
return @[];
}
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
{
return [self.panels allKeys];
}
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
{
return [self.panels allKeys];
}
#pragma mark Tab view delegate methods
- (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem
{
[self updateWindowForIdentifier:[tabViewItem identifier]];
}
#pragma mark Dynamic labels
- (NSString *)buttonLabel:(BOOL)open label:(NSString *)label
{
if (open) {
return [NSString stringWithFormat: NSLocalizedString(@"Open %1$@ preferences", @"1=`Input Monitoring` or `Accessibility`"), label];
}
else {
return [NSString stringWithFormat: NSLocalizedString(@"Request %1$@ permission", @"1=`Input Monitoring` or `Accessibility`"), label];
}
}
+ (NSSet *)keyPathsForValuesAffectingMenuStringAXButtonLabel
{
return [NSSet setWithObject:@"appDelegate.permissionsManager.accessibilityEnabled"];
}
- (NSString *)menuStringAXButtonLabel
{
return [self buttonLabel:self.appDelegate.permissionsManager.accessibilityEnabled label:self.menuStringPermissionsAX];
}
+ (NSSet *)keyPathsForValuesAffectingMenuStringIMButtonLabel
{
return [NSSet setWithObject:@"appDelegate.permissionsManager.inputMonitoringRequested"];
}
- (NSString *)menuStringIMButtonLabel
{
return [self buttonLabel:self.appDelegate.permissionsManager.inputMonitoringRequested label:self.menuStringPermissionsIM];
}
+ (NSSet *)keyPathsForValuesAffectingMenuStringAXStatus
{
return [NSSet setWithObject:@"appDelegate.permissionsManager.accessibilityEnabled"];
}
- (NSString *)menuStringAXStatus
{
return [self statusString:self.appDelegate.permissionsManager.accessibilityEnabled label:self.menuStringPermissionsAX];
}
+ (NSSet *)keyPathsForValuesAffectingMenuStringIMStatus
{
return [NSSet setWithObject:@"appDelegate.permissionsManager.inputMonitoringEnabled"];
}
- (NSString *)menuStringIMStatus
{
return [self statusString:self.appDelegate.permissionsManager.inputMonitoringEnabled label:self.menuStringPermissionsIM];
}
- (NSString *)statusString:(BOOL)state label:(NSString *)label
{
return [NSString stringWithFormat:NSLocalizedString(@"%1$@ permission: %2$@", "for example: `Accessibility permission: ⛔️ required`"), label, state ?
NSLocalizedString(@"✅ granted", nil) :
NSLocalizedString(@"⛔️ required", nil)];
}
#pragma mark Bindings
// I'm sure there's a better way of doing this 😂
- (AppDelegate *)appDelegate
{
return (AppDelegate *)[[NSApplication sharedApplication] delegate];
}
- (NSString *)menuStringReverseScrolling
{
return self.appDelegate.menuStringReverseScrolling;
}
- (NSString *)menuStringPreferencesTitle
{
return self.appDelegate.appName;
}
- (NSString *)menuStringAppSettings {
return NSLocalizedString(@"App", @"Preferences pane for `App` settings");
}
- (NSString *)menuStringScrollingSettings {
return NSLocalizedString(@"Scrolling", @"Preferences pane for `Scrolling` serttings");
}
- (NSString *)menuStringScrollingAxes {
return NSLocalizedString(@"Scrolling Axes", @"Prefs section title");
}
- (NSString *)menuStringScrollingDevices {
return NSLocalizedString(@"Scrolling Devices", @"Prefs section title");
}
- (NSString *)menuStringHorizontal {
return NSLocalizedString(@"Reverse Horizontal", @"Prefs check box");
}
- (NSString *)menuStringVertical {
return NSLocalizedString(@"Reverse Vertical", @"Prefs check box");
}
- (NSString *)menuStringTrackpad {
return NSLocalizedString(@"Reverse Trackpad", @"Prefs check box");
}
- (NSString *)menuStringMouse {
return NSLocalizedString(@"Reverse Mouse", @"Prefs check box");
}
- (NSString *)menuStringStartAtLogin {
return NSLocalizedString(@"Start at login", @"Prefs check box");
}
- (NSString *)menuStringShowInMenuBar {
return NSLocalizedString(@"Show in menu bar", @"Prefs check box");
}
- (NSString *)menuStringCheckNow {
return NSLocalizedString(@"Check for updates", @"Button, when pressed, checks for updates now");
}
- (NSString *)menuStringCheckForUpdates {
return NSLocalizedString(@"Automatically", @"Check box next to the 'Check for updates' button");
}
- (NSString *)menuStringBetaUpdates {
return NSLocalizedString(@"Include beta versions", @"Check box: Include beta versionss of the app when checking for updates");
}
- (NSString *)menuStringPermissionsHeader {
return NSLocalizedString(@"Permissions", @"Section title");
}
- (NSString *)menuStringPermissionsAXDescription {
return NSLocalizedString(@"Scroll Reverser needs Accessibility permission to modify your scrolling.", nil);
}
- (NSString *)menuStringPermissionsIMDescription {
return NSLocalizedString(@"Scroll Reverser needs Input Monitoring permission to detect whether your fingers are touching the trackpad.", nil);
}
- (NSString *)menuStringPermissionsAX {
return NSLocalizedString(@"Accessibility", @"corresponds to Accessibility in system Privacy settings");
}
- (NSString *)menuStringPermissionsIM {
return NSLocalizedString(@"Input Monitoring", @"corresponds to Input Monitoring in system Privacy settings");
}
- (NSString *)menuStringMouseWheelHeader {
return NSLocalizedString(@"Scroll Wheel", @"Prefs section header");
}
- (NSString *)menuStringMouseWheelStepSize {
return NSLocalizedString(@"Step size", @"Size of one step of the mouse scroll wheel");
}
- (NSString *)menuStringMouseWheelStepMin {
return NSLocalizedString(@"Small", @"Small step size");
}
- (NSString *)menuStringMouseWheelStepMax {
return NSLocalizedString(@"Large", @"Large step size");
}
@end