-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYSidebarController.m
257 lines (218 loc) · 7.96 KB
/
SSYSidebarController.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
#import "SSYSidebarController.h"
enum SSYSidebarControllerExpansionDirection_enum {
SSYSidebarControllerExpandsHorizontally,
SSYSidebarControllerExpandsVertically
};
typedef enum SSYSidebarControllerExpansionDirection_enum SSYSidebarControllerExpansionDirection;
@interface SSYSidebarController()
@property (assign) BOOL isSidebarShowing;
@property (assign) CGFloat sidebarLength;
@property (assign) CGFloat lengthBorrowedFromMainView;
@end
@implementation SSYSidebarController
- (instancetype)initWithMainView:(NSView*)mainView
sidebarView:(NSView*)sidebarView
whichEdge:(SSYSidebarControllerWhichEdge)whichEdge {
self = [super init];
if (self) {
self.mainView = mainView;
self.sidebarView = sidebarView;
self.whichEdge = whichEdge;
}
return self ;
}
- (void)awakeFromNib {
/* Grab the original length of the sidebar for future reference */
self.sidebarLength = [self lengthOfFrame:self.sidebarView.frame];
/* Initially, we want the sidebar to be collapsed, and the main view to
be its orignal (current) length. */
[self regardlesslyExpandSidebar:NO
mainViewMinimum:[self lengthOfFrame:self.mainView.frame]
animate:NO];
}
- (BOOL)mainIsAtZeroPosition {
BOOL answer = NO;
switch (self.whichEdge) {
case SSYSidebarControllerWhichEdgeRight:
case SSYSidebarControllerWhichEdgeTop:
answer = YES;
break;
case SSYSidebarControllerWhichEdgeLeft:
case SSYSidebarControllerWhichEdgeBottom:
answer = NO;
break;
}
return answer;
}
- (SSYSidebarControllerExpansionDirection)direction {
if (
(self.whichEdge == SSYSidebarControllerWhichEdgeLeft)
|| (self.whichEdge == SSYSidebarControllerWhichEdgeRight)) {
return SSYSidebarControllerExpandsHorizontally;
}
else {
return SSYSidebarControllerExpandsVertically;
}
}
- (CGFloat)lengthOfFrame:(NSRect)frame {
if ([self direction] == SSYSidebarControllerExpandsHorizontally) {
return frame.size.width;
} else {
return frame.size.height;
}
}
- (CGFloat)positionOfFrame:(NSRect)frame {
if ([self direction] == SSYSidebarControllerExpandsHorizontally) {
return frame.origin.x;
} else {
return frame.origin.y;
}
}
- (void)addLength:(CGFloat)length
toFrame:(NSRect*)frame_p {
if ([self direction] == SSYSidebarControllerExpandsHorizontally) {
(*frame_p).size.width += length;
} else {
(*frame_p).size.height += length;
}
}
- (void)addPosition:(CGFloat)position
toFrame:(NSRect*)frame_p {
if ([self direction] == SSYSidebarControllerExpandsHorizontally) {
(*frame_p).origin.x += position;
} else {
(*frame_p).origin.y += position;
}
}
- (void)setLength:(CGFloat)length
ofFrame:(NSRect*)frame_p {
if ([self direction] == SSYSidebarControllerExpandsHorizontally) {
(*frame_p).size.width = length;
} else {
(*frame_p).size.height = length;
}
}
- (void)setPosition:(CGFloat)position
ofFrame:(NSRect*)frame_p {
if ([self direction] == SSYSidebarControllerExpandsHorizontally) {
(*frame_p).origin.x = position;
} else {
(*frame_p).origin.y = position;
}
}
- (void)expandSidebar:(BOOL)expand
mainViewMinimum:(CGFloat)mainViewMinimum
animate:(BOOL)animate {
if (self.isSidebarShowing == expand) {
/* Sidebar is already expanded/collapsed as desired. */
return;
}
self.isSidebarShowing = expand;
[self regardlesslyExpandSidebar:expand
mainViewMinimum:mainViewMinimum
animate:animate];
}
- (void)regardlesslyExpandSidebar:(BOOL)expand
mainViewMinimum:(CGFloat)mainViewMinimum
animate:(BOOL)animate {
/* This method contains dual-purpose generalized code:
• We use 'length' to mean either 'height' or 'width'
• We use 'position' to mean either 'x' or 'y'
where the "either" choice depends on whether we are expanding and
collapsing horizontally or vertically, which in turn depends on the
property `whichEdge`. */
NSRect windowFrame = self.mainView.window.frame;
NSRect windowContentFrame = self.mainView.window.contentView.frame;
CGFloat delta;
if (expand) {
if (animate) {
self.sidebarView.animator.hidden = NO;
}
else {
self.sidebarView.hidden = NO;
}
CGFloat availableLengthFromMainView ;
availableLengthFromMainView = [self lengthOfFrame:windowContentFrame] - mainViewMinimum;
delta = self.sidebarLength - availableLengthFromMainView;
if (delta > 0.0) {
[self addLength:delta
toFrame:&windowFrame];
[self addLength:delta
toFrame:&windowContentFrame];
NSRect useableFrame = self.mainView.window.screen.visibleFrame ;
// That useableFrame does not include main menu bar or Dock.
CGFloat screenLength = [self lengthOfFrame:useableFrame] ;
CGFloat overflowLength;
if ([self mainIsAtZeroPosition]) {
overflowLength = [self positionOfFrame:windowFrame] + [self lengthOfFrame:windowFrame] - screenLength;
if (overflowLength > 0.0) {
[self addPosition:-overflowLength
toFrame:&windowFrame];
}
} else {
overflowLength = -([self positionOfFrame:windowFrame] - delta);
if (overflowLength > 0.0) {
[self addPosition:overflowLength
toFrame:&windowFrame];
}
}
}
self.lengthBorrowedFromMainView = availableLengthFromMainView;
if (![self mainIsAtZeroPosition]) {
[self addPosition:-delta
toFrame:&windowFrame];
}
} else {
if (animate) {
self.sidebarView.animator.hidden = YES;
}
else {
self.sidebarView.hidden = YES;
}
delta = self.sidebarLength - self.lengthBorrowedFromMainView;
[self addLength:-delta
toFrame:&windowFrame];
[self addLength:-delta
toFrame:&windowContentFrame];
if (![self mainIsAtZeroPosition]) {
[self addPosition:delta
toFrame:&windowFrame];
}
}
[self.mainView.window setFrame:windowFrame
display:YES
animate:YES];
NSRect mainFrame = self.mainView.frame;
NSRect sidebarFrame = self.sidebarView.frame;
[self setLength:(expand ? self.sidebarLength : 0.0)
ofFrame:&sidebarFrame];
CGFloat newLengthOfMainFrame = [self lengthOfFrame:windowContentFrame] - [self lengthOfFrame:sidebarFrame];
CGFloat newLengthOfSidebarFrame = (expand ? self.sidebarLength : 0.0);
[self setLength:newLengthOfMainFrame
ofFrame:&mainFrame];
[self setLength:newLengthOfSidebarFrame
ofFrame:&sidebarFrame];
if ([self mainIsAtZeroPosition]) {
[self setPosition:0.0
ofFrame:&mainFrame];
[self setPosition:newLengthOfMainFrame
ofFrame:&sidebarFrame];
} else {
[self setPosition:0.0
ofFrame:&sidebarFrame];
[self setPosition:newLengthOfSidebarFrame
ofFrame:&mainFrame];
}
if (animate) {
NSView* mainAnimator = [self.mainView animator];
NSView* sidebarAnimator = [self.sidebarView animator];
[mainAnimator setFrame:mainFrame];
[sidebarAnimator setFrame:sidebarFrame];
} else {
[self.mainView setFrame:mainFrame];
[self.sidebarView setFrame:sidebarFrame];
}
self.mainView.needsDisplay = YES;
self.sidebarView.needsDisplay = YES;
}
@end