forked from jessegrosjean/bpreferences
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBPreferencesController.m
More file actions
229 lines (173 loc) · 8.04 KB
/
BPreferencesController.m
File metadata and controls
229 lines (173 loc) · 8.04 KB
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
//
// BPreferencesController.m
// BPreferences
//
// Created by Jesse Grosjean on 8/23/07.
// Copyright 2007 Blocks. All rights reserved.
//
#import "BPreferencesController.h"
@interface BPreferencesController (BPreferencesControllerPrivate)
- (void)resizeWindowToSize:(NSSize)newSize display:(BOOL)display animate:(BOOL)animate;
- (BConfigurationElement *)paneConfigurationElementForIdentifier:(NSString *)paneIdentifier;
- (NSViewController *)paneControllerForIdentifier:(NSString *)paneIdentifier;
@end
@implementation BPreferencesController
#pragma mark Class Methods
+ (id)sharedInstance {
static id sharedInstance = nil;
if (sharedInstance == nil) {
sharedInstance = [[self alloc] init];
}
return sharedInstance;
}
#pragma mark Init
- (id)init {
if (self = [super initWithWindowNibName:@"BPreferencesController"]) {
}
return self;
}
#pragma mark AwakeFromNib-like Methods
- (void)windowDidLoad {
if ([[self paneIdentifiers] count] > 1) {
toolbar = [[NSToolbar alloc] initWithIdentifier:@"BPreferenceManagerToolbarIdentifier"];
[toolbar setDelegate:self];
[[self window] setToolbar:toolbar];
[[self window] setShowsToolbarButton:NO];
}
[[self window] center];
[[self window] makeKeyAndOrderFront:self];
NSString *firstPaneIdentifier = [[self paneIdentifiers] count] ? [paneIdentifiers objectAtIndex:0] : nil;
if (firstPaneIdentifier && [self selectedPaneIdentifier] == nil) {
[self setSelectedPaneIdentifier:firstPaneIdentifier];
}
[loadingTextField setHidden:YES];
}
#pragma Accessors
- (NSString *)selectedPaneIdentifier {
return selectedPaneIdentifier;
}
- (void)setSelectedPaneIdentifier:(NSString *)paneIdentifier {
[[self window] makeKeyAndOrderFront:self];
if ([[self selectedPaneIdentifier] isEqual:paneIdentifier]) {
return;
}
[toolbar setSelectedItemIdentifier:paneIdentifier];
[[self window] setContentView:loadingView];
BConfigurationElement *configurationElement = [self paneConfigurationElementForIdentifier:paneIdentifier];
NSViewController *preferencePaneController = [self paneControllerForIdentifier:paneIdentifier];
NSView *preferencePaneView = [preferencePaneController view];
BLogAssert(preferencePaneController != nil, @"invalid paneIdentifier");
BLogAssert(preferencePaneView != nil, @"failed to create preference pane view");
BLogAssert([configurationElement floatAttributeForKey:@"width"] == [preferencePaneView bounds].size.width, @"preference pane view width does not match declared width");
BLogInfo([NSString stringWithFormat:@"showing pane %@", paneIdentifier]);
selectedPaneIdentifier = paneIdentifier;
if ([[self window] isVisible]) {
[self resizeWindowToSize:[preferencePaneView bounds].size display:YES animate:YES];
} else {
[self resizeWindowToSize:[preferencePaneView bounds].size display:NO animate:NO];
}
[[self window] setContentView:preferencePaneView];
}
- (NSArray *)paneIdentifiers {
if (!paneIdentifiers) {
NSUInteger maxPreferencePaneViewWidth = 0;
paneIdentifiers = [[NSMutableArray alloc] init];
paneIdentifiersToConfigurationElements = [[NSMutableDictionary alloc] init];
paneIdentifiersToPreferencePaneControllers = [[NSMutableDictionary alloc] init];
NSArray *requiredKeys = [NSArray arrayWithObjects:@"id", @"label", @"title", @"image", @"width", @"controller", nil];
BExtensionPoint *extensionPoint = [[BExtensionRegistry sharedInstance] extensionPointFor:@"com.blocks.BPreferences.preferencePanes"];
for (BConfigurationElement *each in [extensionPoint configurationElementsNamed:@"preferencePane"]) {
if ([each assertKeysPresent:requiredKeys]) {
NSString *identifier = [[each attributes] objectForKey:@"id"];
if (![paneIdentifiers containsObject:identifier]) {
[paneIdentifiers addObject:identifier];
[paneIdentifiersToConfigurationElements setObject:each forKey:identifier];
NSString *eachWidth = [each attributeForKey:@"width"];
if (eachWidth && [eachWidth integerValue] > maxPreferencePaneViewWidth) {
maxPreferencePaneViewWidth = [eachWidth integerValue];
}
} else {
BLogWarning([NSString stringWithFormat:@"preference pane id %@ is already in use.", identifier]);
}
}
}
NSSize defaultWindowSize = [[[self window] contentView] bounds].size;
if (maxPreferencePaneViewWidth > defaultWindowSize.width) {
defaultWindowSize.width = maxPreferencePaneViewWidth;
[self resizeWindowToSize:defaultWindowSize display:NO animate:NO];
}
}
return paneIdentifiers;
}
#pragma mark Delegate/Datasource
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdent willBeInsertedIntoToolbar:(BOOL) willBeInserted {
NSToolbarItem *toolbarItem = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdent];
BConfigurationElement *paneConfigurationElement = [self paneConfigurationElementForIdentifier:itemIdent];
if (paneConfigurationElement) {
[toolbarItem setLabel:[paneConfigurationElement localizedAttributeForKey:@"label"]];
[toolbarItem setToolTip:[paneConfigurationElement localizedAttributeForKey:@"toolTip"]];
[toolbarItem setImage:[paneConfigurationElement imageAttributeForKey:@"image"]];
[toolbarItem setTarget:self];
[toolbarItem setAction:@selector(toolbarItemClicked:)];
} else {
toolbarItem = nil;
}
return toolbarItem;
}
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *) toolbar {
return [self paneIdentifiers];
}
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *) toolbar {
return [self paneIdentifiers];
}
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar {
return [self paneIdentifiers];
}
- (void)toolbarItemClicked:(id)sender {
[self setSelectedPaneIdentifier:[sender itemIdentifier]];
}
@end
@implementation BPreferencesController (BPreferencesControllerPrivate)
CGFloat ToolbarHeightForWindow(NSWindow *window) {
NSToolbar *toolbar;
CGFloat toolbarHeight = 0.0;
NSRect windowFrame;
toolbar = [window toolbar];
if(toolbar && [toolbar isVisible]) {
windowFrame = [NSWindow contentRectForFrameRect:[window frame] styleMask:[window styleMask]];
toolbarHeight = NSHeight(windowFrame) - NSHeight([[window contentView] frame]);
}
return toolbarHeight;
}
- (void)resizeWindowToSize:(NSSize)newSize display:(BOOL)display animate:(BOOL)animate {
NSRect aFrame;
if (newSize.width != 550) {
BLogWarning(@"preferences window resize width does not equal expected width of 550... tidy things up!");
}
CGFloat newHeight = newSize.height + ToolbarHeightForWindow([self window]);
CGFloat newWidth = newSize.width;
aFrame = [NSWindow contentRectForFrameRect:[[self window] frame] styleMask:[[self window] styleMask]];
aFrame.origin.y += aFrame.size.height;
aFrame.origin.y -= newHeight;
aFrame.size.height = newHeight;
aFrame.size.width = newWidth;
aFrame = [NSWindow frameRectForContentRect:aFrame styleMask:[[self window] styleMask]];
[[self window] setFrame:aFrame display:display animate:animate];
}
- (BConfigurationElement *)paneConfigurationElementForIdentifier:(NSString *)paneIdentifier {
if (!paneIdentifiers) [self paneIdentifiers];
return [paneIdentifiersToConfigurationElements objectForKey:paneIdentifier];
}
- (NSViewController *)paneControllerForIdentifier:(NSString *)paneIdentifier {
if (!paneIdentifiers) [self paneIdentifiers];
NSViewController * preferencePaneController = [paneIdentifiersToPreferencePaneControllers objectForKey:paneIdentifier];
if (!preferencePaneController) {
BConfigurationElement *configurationElement = [self paneConfigurationElementForIdentifier:paneIdentifier];
preferencePaneController = [configurationElement createExecutableExtensionFromAttribute:@"controller" conformingToClass:[NSViewController class] conformingToProtocol:nil respondingToSelectors:nil];
if (preferencePaneController) {
[paneIdentifiersToPreferencePaneControllers setObject:preferencePaneController forKey:paneIdentifier];
}
}
return preferencePaneController;
}
@end