forked from pilotmoon/Scroll-Reverser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrefsWindowController.m
267 lines (211 loc) · 8.67 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
// 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 kKeyViewHeight=@"viewHeight";
static NSString *const kKeyTitle=@"title";
static NSString *const kKeyImageName=@"image";
static NSString *const kPrefsToolbarIdentifer=@"PrefsToolbar";
static NSString *const kPrefsLastUsedPanel=@"PrefsLastUsedPanel";
@interface PrefsWindowController ()
@property NSTabView *tabView;
@property NSToolbar *toolbar;
@property NSDictionary *panels;
@property CGFloat width;
@end
@implementation PrefsWindowController
- (void)windowDidLoad
{
[super windowDidLoad];
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
panelData[kKeyViewHeight] = @([view frame].size.height);
const NSSize size=[view fittingSize];
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];
}
// select the initial pane
[self.tabView selectTabViewItemWithIdentifier:startingIdentifier];
[self.toolbar setSelectedItemIdentifier:startingIdentifier];
[self updateHeightForIdentifier:startingIdentifier];
// other set-up
self.linkView.url=self.appDelegate.appLink;
}
- (void)showWindow:(id)sender
{
[[self window] setLevel:NSFloatingWindowLevel];
[[self window] center];
[NSApp activateIgnoringOtherApps:YES];
// small delay to prevent flash of window drawing (better way?)
dispatch_after(0.05, dispatch_get_main_queue(), ^{
[super showWindow:sender];
});
}
#pragma mark Private methods
- (void)setPane:(NSString *)identifier
{
// select the appropriate tab view item
[self.tabView selectTabViewItemWithIdentifier:identifier];
// save this as the last used panel
[[NSUserDefaults standardUserDefaults] setObject:identifier forKey:kPrefsLastUsedPanel];
}
// Futz about with the geometry to get the height right
- (void)setWindowContentHeight:(CGFloat)height
{
// get the current content rect
NSRect contentRect=[NSWindow contentRectForFrameRect:[self.window frame]
styleMask:[self.window styleMask]];
// calculate new content rect
const CGFloat toolbarHeight=NSHeight(contentRect)-NSHeight([(NSView *)[[self window] contentView] frame]);
const CGFloat diff=height+toolbarHeight-contentRect.size.height;
contentRect.size.height+=diff;
contentRect.origin.y-=diff;
contentRect.size.width=self.width;
// set window to new size
[self.window setFrame:[NSWindow frameRectForContentRect:contentRect
styleMask:[self.window styleMask]]
display:YES
animate:NO];
}
// Set the window to the previously stored height for the selected panel
- (void)updateHeightForIdentifier:(NSString *)identifier
{
[self setWindowContentHeight:[self.panels[identifier][kKeyViewHeight] floatValue]];
}
#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 updateHeightForIdentifier:[tabViewItem identifier]];
}
#pragma mark Bindings
- (AppDelegate *)appDelegate
{
return (AppDelegate *)[[NSApplication sharedApplication] delegate];
}
- (NSString *)menuStringReverseScrolling
{
return self.appDelegate.menuStringReverseScrolling;
}
- (NSString *)menuStringPreferencesTitle
{
return self.appDelegate.appName;
}
- (NSString *)menuStringAppSettings {
return NSLocalizedString(@"App", nil);
}
- (NSString *)menuStringScrollingSettings {
return NSLocalizedString(@"Scrolling", nil);
}
- (NSString *)menuStringScrollingAxes {
return NSLocalizedString(@"Scrolling Axes", nil);
}
- (NSString *)menuStringScrollingDevices {
return NSLocalizedString(@"Scrolling Devices", nil);
}
- (NSString *)menuStringHorizontal {
return NSLocalizedString(@"Reverse Horizontal", nil);
}
- (NSString *)menuStringVertical {
return NSLocalizedString(@"Reverse Vertical", nil);
}
- (NSString *)menuStringTrackpad {
return NSLocalizedString(@"Reverse Trackpad", nil);
}
- (NSString *)menuStringMouse {
return NSLocalizedString(@"Reverse Mouse", nil);
}
- (NSString *)menuStringTablet {
return NSLocalizedString(@"Reverse Tablet", nil);
}
- (NSString *)menuStringStartAtLogin {
return NSLocalizedString(@"Start at login", nil);
}
- (NSString *)menuStringShowInMenuBar {
return NSLocalizedString(@"Show in menu bar", nil);
}
- (NSString *)menuStringCheckNow {
return NSLocalizedString(@"Check for updates", nil);
}
- (NSString *)menuStringCheckForUpdates {
return NSLocalizedString(@"Automatically", @"check box next to the 'Check for updates' button");
}
@end