forked from glebd/bwtoolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BWSplitViewInspector.m
237 lines (196 loc) · 7.75 KB
/
BWSplitViewInspector.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
//
// BWSplitViewInspector.m
// BWToolkit
//
// Created by Brandon Walkin (www.brandonwalkin.com)
// All code is provided under the New BSD license.
//
#import "BWSplitViewInspector.h"
#import "NSView+BWAdditions.h"
#import "NSEvent+BWAdditions.h"
@interface BWSplitViewInspector (BWSVIPrivate)
- (void)updateSizeInputFields;
- (BOOL)toggleDividerCheckboxVisibilityWithAnimation:(BOOL)shouldAnimate;
- (void)updateSizeLabels;
- (void)updateUnitPopupSelections;
@end
@implementation BWSplitViewInspector
@synthesize subviewPopupSelection, subviewPopupContent, collapsiblePopupContent, minUnitPopupSelection, maxUnitPopupSelection, splitView, dividerCheckboxCollapsed;
- (NSString *)viewNibName
{
return @"BWSplitViewInspector";
}
- (void)awakeFromNib
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controlTextDidEndEditing:) name:NSControlTextDidEndEditingNotification object:minField];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controlTextDidEndEditing:) name:NSControlTextDidEndEditingNotification object:maxField];
}
- (void)updateSizeLabels
{
if ([splitView isVertical])
{
[maxLabel setStringValue:@"Max Width"];
[minLabel setStringValue:@"Min Width"];
}
else
{
[maxLabel setStringValue:@"Max Height"];
[minLabel setStringValue:@"Min Height"];
}
}
- (void)setSplitView:(BWSplitView *)aSplitView
{
if (splitView != aSplitView)
{
[splitView release];
splitView = [aSplitView retain];
[self toggleDividerCheckboxVisibilityWithAnimation:NO];
}
else
{
[self toggleDividerCheckboxVisibilityWithAnimation:YES];
}
}
- (BOOL)toggleDividerCheckboxVisibilityWithAnimation:(BOOL)shouldAnimate
{
// Conditions that must be met for a visibility switch to take place. If any of them fail, we return early.
if (dividerCheckboxCollapsed && [splitView dividerThickness] > 1.01 && [splitView collapsiblePopupSelection] != 0) {
}
else if (!dividerCheckboxCollapsed && ([splitView dividerThickness] < 1.01 || [splitView collapsiblePopupSelection] == 0)) {
}
else
return NO;
float alpha, duration = [NSEvent bwShiftKeyIsDown] ? 0.1 * 10 : 0.1;
NSRect targetFrame = NSZeroRect;
if (dividerCheckboxCollapsed)
{
targetFrame = NSMakeRect([[self view] frame].origin.x, [[self view] frame].origin.y, [[self view] frame].size.width, [[self view] frame].size.height + 20);
alpha = 1.0;
}
else
{
targetFrame = NSMakeRect([[self view] frame].origin.x, [[self view] frame].origin.y, [[self view] frame].size.width, [[self view] frame].size.height - 20);
alpha = 0.0;
}
if (shouldAnimate)
{
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:duration];
[[dividerCheckbox bwAnimator] setAlphaValue:alpha];
[[[self view] bwAnimator] setFrame:targetFrame];
[NSAnimationContext endGrouping];
}
else
{
[dividerCheckbox setAlphaValue:alpha];
[[self view] setFrame:targetFrame];
}
dividerCheckboxCollapsed = !dividerCheckboxCollapsed;
return YES;
}
- (void)refresh
{
[super refresh];
if ([[self inspectedObjects] count] > 0)
{
[autosizingView setSplitView:[[self inspectedObjects] objectAtIndex:0]];
[autosizingView layoutButtons];
[self setSplitView:[[self inspectedObjects] objectAtIndex:0]];
// Populate the subview popup button
NSMutableArray *content = [[NSMutableArray alloc] init];
for (NSView *subview in [splitView subviews])
{
int index = [[splitView subviews] indexOfObject:subview];
NSString *label = [NSString stringWithFormat:@"Subview %d",index];
if (![[subview className] isEqualToString:@"NSView"])
label = [label stringByAppendingString:[NSString stringWithFormat:@" - %@",[subview className]]];
[content addObject:label];
}
[self setSubviewPopupContent:content];
// Populate the collapsible popup button
if ([splitView isVertical])
[self setCollapsiblePopupContent:[NSMutableArray arrayWithObjects:@"None", @"Left Pane", @"Right Pane",nil]];
else
[self setCollapsiblePopupContent:[NSMutableArray arrayWithObjects:@"None", @"Top Pane", @"Bottom Pane",nil]];
[self updateSizeLabels];
[self updateSizeInputFields];
[self updateUnitPopupSelections];
}
}
+ (BOOL)supportsMultipleObjectInspection
{
return NO;
}
- (void)setMinUnitPopupSelection:(int)index
{
minUnitPopupSelection = index;
NSNumber *minUnit = [NSNumber numberWithInt:index];
NSMutableDictionary *tempMinUnits = [[[splitView minUnits] mutableCopy] autorelease];
[tempMinUnits setObject:minUnit forKey:[NSNumber numberWithInt:[self subviewPopupSelection]]];
[splitView setMinUnits:tempMinUnits];
}
- (void)setMaxUnitPopupSelection:(int)index
{
maxUnitPopupSelection = index;
NSNumber *maxUnit = [NSNumber numberWithInt:index];
NSMutableDictionary *tempMaxUnits = [[[splitView maxUnits] mutableCopy] autorelease];
[tempMaxUnits setObject:maxUnit forKey:[NSNumber numberWithInt:[self subviewPopupSelection]]];
[splitView setMaxUnits:tempMaxUnits];
}
- (void)updateUnitPopupSelections
{
minUnitPopupSelection = [[[splitView minUnits] objectForKey:[NSNumber numberWithInt:[self subviewPopupSelection]]] intValue];
maxUnitPopupSelection = [[[splitView maxUnits] objectForKey:[NSNumber numberWithInt:[self subviewPopupSelection]]] intValue];
}
- (void)controlTextDidEndEditing:(NSNotification *)aNotification
{
if ([aNotification object] == minField || aNotification == nil)
{
if ([minField stringValue] != nil && [[minField stringValue] isEqualToString:@""] == NO && [[minField stringValue] isEqualToString:@" "] == NO)
{
NSNumber *minValue = [NSNumber numberWithInt:[minField intValue]];
NSMutableDictionary *tempMinValues = [[[splitView minValues] mutableCopy] autorelease];
[tempMinValues setObject:minValue forKey:[NSNumber numberWithInt:[self subviewPopupSelection]]];
[splitView setMinValues:tempMinValues];
}
else
{
NSMutableDictionary *tempMinValues = [[[splitView minValues] mutableCopy] autorelease];
[tempMinValues removeObjectForKey:[NSNumber numberWithInt:[self subviewPopupSelection]]];
[splitView setMinValues:tempMinValues];
}
}
if ([aNotification object] == maxField || aNotification == nil)
{
if ([maxField stringValue] != nil && [[maxField stringValue] isEqualToString:@""] == NO && [[maxField stringValue] isEqualToString:@" "] == NO)
{
NSNumber *maxValue = [NSNumber numberWithInt:[maxField intValue]];
NSMutableDictionary *tempMaxValues = [[[splitView maxValues] mutableCopy] autorelease];
[tempMaxValues setObject:maxValue forKey:[NSNumber numberWithInt:[self subviewPopupSelection]]];
[splitView setMaxValues:tempMaxValues];
}
else
{
NSMutableDictionary *tempMaxValues = [[[splitView maxValues] mutableCopy] autorelease];
[tempMaxValues removeObjectForKey:[NSNumber numberWithInt:[self subviewPopupSelection]]];
[splitView setMaxValues:tempMaxValues];
}
}
}
- (void)setSubviewPopupSelection:(int)index
{
// If someone types into the text field and chooses a different subview without hitting return or clicking out of the field,
// the controlTextDidEndEditing notification won't fire and the value won't be saved. So we fire it manually here.
[self controlTextDidEndEditing:nil];
subviewPopupSelection = index;
// Update the input text fields with the values in the new subview
[self updateSizeInputFields];
}
- (void)updateSizeInputFields
{
[minField setObjectValue:[[splitView minValues] objectForKey:[NSNumber numberWithInt:[self subviewPopupSelection]]]];
[maxField setObjectValue:[[splitView maxValues] objectForKey:[NSNumber numberWithInt:[self subviewPopupSelection]]]];
[self setMinUnitPopupSelection:[[[splitView minUnits] objectForKey:[NSNumber numberWithInt:[self subviewPopupSelection]]] intValue]];
[self setMaxUnitPopupSelection:[[[splitView maxUnits] objectForKey:[NSNumber numberWithInt:[self subviewPopupSelection]]] intValue]];
}
@end