forked from cocoabits/MASPreferences
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MASPreferencesWindowController.m
353 lines (281 loc) · 12.4 KB
/
MASPreferencesWindowController.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
#import "MASPreferencesWindowController.h"
NSString *const kMASPreferencesWindowControllerDidChangeViewNotification = @"MASPreferencesWindowControllerDidChangeViewNotification";
static NSString *const kMASPreferencesFrameTopLeftKey = @"MASPreferences Frame Top Left";
static NSString *const kMASPreferencesSelectedViewKey = @"MASPreferences Selected Identifier View";
static NSString *const PreferencesKeyForViewBounds (NSString *identifier)
{
return [NSString stringWithFormat:@"MASPreferences %@ Frame", identifier];
}
@interface MASPreferencesWindowController () // Private
- (NSViewController <MASPreferencesViewController> *)viewControllerForIdentifier:(NSString *)identifier;
@property (readonly) NSArray *toolbarItemIdentifiers;
@property (nonatomic, retain) NSViewController <MASPreferencesViewController> *selectedViewController;
@end
#pragma mark -
@implementation MASPreferencesWindowController
@synthesize viewControllers = _viewControllers;
@synthesize selectedViewController = _selectedViewController;
@synthesize title = _title;
#pragma mark -
- (id)initWithViewControllers:(NSArray *)viewControllers
{
return [self initWithViewControllers:viewControllers title:nil];
}
- (id)initWithViewControllers:(NSArray *)viewControllers title:(NSString *)title
{
if ((self = [super initWithWindowNibName:@"MASPreferencesWindow"]))
{
#if __has_feature(objc_arc)
_viewControllers = viewControllers;
#else
_viewControllers = [viewControllers retain];
#endif
_minimumViewRects = [[NSMutableDictionary alloc] init];
_title = [title copy];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[self window] setDelegate:nil];
#if !__has_feature(objc_arc)
[_viewControllers release];
[_selectedViewController release];
[_minimumViewRects release];
[_title release];
[super dealloc];
#endif
}
#pragma mark -
- (void)windowDidLoad
{
if ([self.title length] > 0)
[[self window] setTitle:self.title];
if ([self.viewControllers count])
self.selectedViewController = [self viewControllerForIdentifier:[[NSUserDefaults standardUserDefaults] stringForKey:kMASPreferencesSelectedViewKey]] ?: [self firstViewController];
NSString *origin = [[NSUserDefaults standardUserDefaults] stringForKey:kMASPreferencesFrameTopLeftKey];
if (origin)
[self.window setFrameTopLeftPoint:NSPointFromString(origin)];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification object:self.window];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:self.window];
}
- (NSViewController <MASPreferencesViewController> *)firstViewController {
for (id viewController in self.viewControllers)
if ([viewController isKindOfClass:[NSViewController class]])
return viewController;
return nil;
}
#pragma mark -
#pragma mark NSWindowDelegate
- (BOOL)windowShouldClose:(id)sender
{
return !self.selectedViewController || [self.selectedViewController commitEditing];
}
- (void)windowDidMove:(NSNotification*)aNotification
{
[[NSUserDefaults standardUserDefaults] setObject:NSStringFromPoint(NSMakePoint(NSMinX([self.window frame]), NSMaxY([self.window frame]))) forKey:kMASPreferencesFrameTopLeftKey];
}
- (void)windowDidResize:(NSNotification*)aNotification
{
NSViewController <MASPreferencesViewController> *viewController = self.selectedViewController;
if (viewController)
[[NSUserDefaults standardUserDefaults] setObject:NSStringFromRect([viewController.view bounds]) forKey:PreferencesKeyForViewBounds(viewController.identifier)];
}
#pragma mark -
#pragma mark Accessors
- (NSArray *)toolbarItemIdentifiers
{
NSMutableArray *identifiers = [NSMutableArray arrayWithCapacity:_viewControllers.count];
for (id viewController in _viewControllers)
if (viewController == [NSNull null])
[identifiers addObject:NSToolbarFlexibleSpaceItemIdentifier];
else
[identifiers addObject:[viewController identifier]];
return identifiers;
}
#pragma mark -
- (NSUInteger)indexOfSelectedController
{
NSUInteger index = [self.toolbarItemIdentifiers indexOfObject:self.selectedViewController.identifier];
return index;
}
#pragma mark -
#pragma mark NSToolbarDelegate
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
{
NSArray *identifiers = self.toolbarItemIdentifiers;
return identifiers;
}
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
{
NSArray *identifiers = self.toolbarItemIdentifiers;
return identifiers;
}
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
{
NSArray *identifiers = self.toolbarItemIdentifiers;
return identifiers;
}
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
{
NSToolbarItem *toolbarItem = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
NSArray *identifiers = self.toolbarItemIdentifiers;
NSUInteger controllerIndex = [identifiers indexOfObject:itemIdentifier];
if (controllerIndex != NSNotFound)
{
id <MASPreferencesViewController> controller = [_viewControllers objectAtIndex:controllerIndex];
toolbarItem.image = controller.toolbarItemImage;
toolbarItem.label = controller.toolbarItemLabel;
toolbarItem.target = self;
toolbarItem.action = @selector(toolbarItemDidClick:);
}
#if !__has_feature(objc_arc)
[toolbarItem autorelease];
#endif
return toolbarItem;
}
#pragma mark -
#pragma mark Private methods
- (void)clearResponderChain
{
// Remove view controller from the responder chain
NSResponder *chainedController = self.window.nextResponder;
if ([self.viewControllers indexOfObject:chainedController] == NSNotFound)
return;
self.window.nextResponder = chainedController.nextResponder;
chainedController.nextResponder = nil;
}
- (void)patchResponderChain
{
[self clearResponderChain];
NSViewController *selectedController = self.selectedViewController;
if (!selectedController)
return;
// Add current controller to the responder chain
NSResponder *nextResponder = self.window.nextResponder;
self.window.nextResponder = selectedController;
selectedController.nextResponder = nextResponder;
}
- (NSViewController <MASPreferencesViewController> *)viewControllerForIdentifier:(NSString *)identifier
{
for (id viewController in self.viewControllers) {
if (viewController == [NSNull null]) continue;
if ([[viewController identifier] isEqualToString:identifier])
return viewController;
}
return nil;
}
#pragma mark -
- (void)setSelectedViewController:(NSViewController <MASPreferencesViewController> *)controller
{
if (_selectedViewController == controller)
return;
if (_selectedViewController)
{
// Check if we can commit changes for old controller
if (![_selectedViewController commitEditing])
{
[[self.window toolbar] setSelectedItemIdentifier:_selectedViewController.identifier];
return;
}
#if __has_feature(objc_arc)
[self.window setContentView:[[NSView alloc] init]];
#else
[self.window setContentView:[[[NSView alloc] init] autorelease]];
#endif
if ([_selectedViewController respondsToSelector:@selector(viewDidDisappear)])
[_selectedViewController viewDidDisappear];
#if !__has_feature(objc_arc)
[_selectedViewController release];
#endif
_selectedViewController = nil;
}
if (!controller)
return;
// Retrieve the new window tile from the controller view
if ([self.title length] == 0)
{
NSString *label = controller.toolbarItemLabel;
self.window.title = label;
}
[[self.window toolbar] setSelectedItemIdentifier:controller.identifier];
// Record new selected controller in user defaults
[[NSUserDefaults standardUserDefaults] setObject:controller.identifier forKey:kMASPreferencesSelectedViewKey];
NSView *controllerView = controller.view;
// Retrieve current and minimum frame size for the view
NSString *oldViewRectString = [[NSUserDefaults standardUserDefaults] stringForKey:PreferencesKeyForViewBounds(controller.identifier)];
NSString *minViewRectString = [_minimumViewRects objectForKey:controller.identifier];
if (!minViewRectString)
[_minimumViewRects setObject:NSStringFromRect(controllerView.bounds) forKey:controller.identifier];
BOOL sizableWidth = ([controller respondsToSelector:@selector(hasResizableWidth)]
? controller.hasResizableWidth
: controllerView.autoresizingMask & NSViewWidthSizable);
BOOL sizableHeight = ([controller respondsToSelector:@selector(hasResizableHeight)]
? controller.hasResizableHeight
: controllerView.autoresizingMask & NSViewHeightSizable);
NSRect oldViewRect = oldViewRectString ? NSRectFromString(oldViewRectString) : controllerView.bounds;
NSRect minViewRect = minViewRectString ? NSRectFromString(minViewRectString) : controllerView.bounds;
oldViewRect.size.width = NSWidth(oldViewRect) < NSWidth(minViewRect) || !sizableWidth ? NSWidth(minViewRect) : NSWidth(oldViewRect);
oldViewRect.size.height = NSHeight(oldViewRect) < NSHeight(minViewRect) || !sizableHeight ? NSHeight(minViewRect) : NSHeight(oldViewRect);
[controllerView setFrame:oldViewRect];
// Calculate new window size and position
NSRect oldFrame = [self.window frame];
NSRect newFrame = [self.window frameRectForContentRect:oldViewRect];
newFrame = NSOffsetRect(newFrame, NSMinX(oldFrame), NSMaxY(oldFrame) - NSMaxY(newFrame));
// Setup min/max sizes and show/hide resize indicator
[self.window setContentMinSize:minViewRect.size];
[self.window setContentMaxSize:NSMakeSize(sizableWidth ? CGFLOAT_MAX : NSWidth(oldViewRect), sizableHeight ? CGFLOAT_MAX : NSHeight(oldViewRect))];
[self.window setShowsResizeIndicator:sizableWidth || sizableHeight];
[[self.window standardWindowButton:NSWindowZoomButton] setEnabled:sizableWidth || sizableHeight];
[self.window setFrame:newFrame display:YES animate:[self.window isVisible]];
#if __has_feature(objc_arc)
_selectedViewController = controller;
#else
_selectedViewController = [controller retain];
#endif
if ([controller respondsToSelector:@selector(viewWillAppear)])
[controller viewWillAppear];
[self.window setContentView:controllerView];
[self.window recalculateKeyViewLoop];
if ([self.window firstResponder] == self.window) {
if ([controller respondsToSelector:@selector(initialKeyView)])
[self.window makeFirstResponder:[controller initialKeyView]];
else
[self.window selectKeyViewFollowingView:controllerView];
}
// Insert view controller into responder chain
[self patchResponderChain];
[[NSNotificationCenter defaultCenter] postNotificationName:kMASPreferencesWindowControllerDidChangeViewNotification object:self];
}
- (void)toolbarItemDidClick:(id)sender
{
if ([sender respondsToSelector:@selector(itemIdentifier)])
self.selectedViewController = [self viewControllerForIdentifier:[sender itemIdentifier]];
}
#pragma mark -
#pragma mark Public methods
- (void)selectControllerAtIndex:(NSUInteger)controllerIndex
{
if (NSLocationInRange(controllerIndex, NSMakeRange(0, _viewControllers.count)))
self.selectedViewController = [self.viewControllers objectAtIndex:controllerIndex];
}
#pragma mark -
#pragma mark Actions
- (IBAction)goNextTab:(id)sender
{
NSUInteger selectedIndex = self.indexOfSelectedController;
NSUInteger numberOfControllers = [_viewControllers count];
do { selectedIndex = (selectedIndex + 1) % numberOfControllers; }
while ([_viewControllers objectAtIndex:selectedIndex] == [NSNull null]);
[self selectControllerAtIndex:selectedIndex];
}
- (IBAction)goPreviousTab:(id)sender
{
NSUInteger selectedIndex = self.indexOfSelectedController;
NSUInteger numberOfControllers = [_viewControllers count];
do { selectedIndex = (selectedIndex + numberOfControllers - 1) % numberOfControllers; }
while ([_viewControllers objectAtIndex:selectedIndex] == [NSNull null]);
[self selectControllerAtIndex:selectedIndex];
}
@end