-
Notifications
You must be signed in to change notification settings - Fork 2
/
KPViewController.j
309 lines (267 loc) · 9.93 KB
/
KPViewController.j
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
// KPViewController.j
// View Controllers
//
// Created by Klaas Pieter Annema on 19/02/2010.
// Inspired by KTUIKit created by Cathy Shive and Jonathan Dann
//
// Copyright (c) 2010 Klaas Pieter Annema
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
@import <AppKit/CPViewController.j>
@import "KPWindowController.j"
@import "KPViewPositioningManager.j"
/*!
The KPViewController class is a subclass of <a href=http://cappuccino.org/learn/documentation/interface_c_p_view_controller.html>CPViewController</a>. KPViewController provides a unified method (KPViewController::placeViewController:inView:) for creating a view controller hiearchy and adding it to the responder chain.
*/
@implementation KPViewController : CPViewController
{
KPWindowController _windowController @accessors(property=windowController);
CPArray _subcontrollers @accessors(property=subcontrollers);
BOOL _isVisible @accessors(property=isVisible, readonly);
}
#pragma mark -
#pragma mark Initialization
/*!
Convenience method for initWithWindowController:
@param theWindowController The window controller this receiver belongs to
@return An initialized instance of the receiver
@see initWithWindowController:
*/
+ (id)viewControllerWithWindowController:(KPWindowController)theWindowController
{
return [[self alloc] initWithWindowController:theWindowController];
}
/*!
Initialize the receiver associating it with the window controller. The receiver
will automatically be made part of the window controller's responder chain.
This is the designated initializer.
@param theWindowController The window controller this receiver belongs to
@return An initialized instance of the receiver
@see KPWindowController::patchResponderChain
*/
- (id)initWithWindowController:(KPWindowController)theWindowController
{
return [self initWithCibName:[self cibName] bundle:[self cibBundle] windowController:theWindowController];
}
/*!
@ignore
*/
- (id)initWithCibName:(CPString)theCibName bundle:(CPBundle)theBundle windowController:(KPWindowController)theWindowController
{
if (self = [super initWithCibName:theCibName bundle:theBundle])
{
_windowController = theWindowController;
_subcontrollers = [];
_isVisible = NO;
}
return self;
}
/*!
@ignore
*/
- (id)initWithCibName:(CPString)theCibName bundle:(CPBundle)theBundle
{
[CPException raise:@"KPViewControllerException"
reason:@"An instance of an KPViewController concrete subclass was initialized using the CPViewController method -initWithNibName:bundle: all view controllers in the enusing tree will have no reference to an KPWindowController object and cannot be automatically added to the responder chain"];
return nil;
}
#pragma mark -
#pragma mark View lifetime
/*!
@ignore
*/
- (void)_makeSubcontrollersPerformVisibilitySelector:(SEL)theSelector isVisible:(BOOL)shouldBeVisible
{
var subcontrollers = [self subcontrollers];
for (var i = 0; i < [[self subcontrollers] count]; i++)
{
var subcontroller = [subcontrollers objectAtIndex:i];
if ([subcontroller isVisible] === shouldBeVisible)
[subcontroller performSelector:theSelector];
}
}
/*!
Called after the receiver's view is loaded into memory.
*/
- (void)viewDidLoad
{
// Overriden by subclasses
}
/*!
Notifies the receiver that its view is about to become visible.
Subclasses must always call super or the message might not propogate to subcontrollers.
*/
- (void)viewWillAppear
{
[self _makeSubcontrollersPerformVisibilitySelector:_cmd isVisible:NO];
}
/*!
Notifies the receiver that its view became visible.
Subclasses must always call super or the message might not propogate to subcontrollers.
*/
- (void)viewDidAppear
{
[self _makeSubcontrollersPerformVisibilitySelector:_cmd isVisible:NO];
[self _setVisible:YES];
}
/*!
Notifies the receiver that its view is about to become invisible.
Subclasses must always call super or the message might not propogate to subcontrollers.
*/
- (void)viewWillDisappear
{
[self _makeSubcontrollersPerformVisibilitySelector:_cmd isVisible:YES];
}
/*!
Notifies the receiver that its view became invisible.
Subclasses must always call super or the message might not propogate to subcontrollers.
*/
- (void)viewDidDisappear
{
[self _makeSubcontrollersPerformVisibilitySelector:_cmd isVisible:YES];
[self _setVisible:NO];
}
#pragma mark -
#pragma mark View placement
/*!
Can be overriden in subclasses to return a different view positioning manager class to initialize
@return The view positioning manager class the receiver should use
*/
- (Class)viewPositioningManagerClass
{
return [KPViewPositioningManager class];
}
/*!
Add the viewcontroller as a subcontroller of the receiver and place the viewcontroller's view
in the container view. If the view already contains a subcontroller of the receiver the subcontroller is removed.
Internally uses an instance of KPViewPositioningManager to place the subcontroller's view into the container view.
Override +viewPositioningManagerClass to override the class the receiver should use.
@param theViewController The view controller to add as a subcontroller of the receiver
@param theView The view the subcontroller's view is added to
*/
- (void)placeViewController:(KPViewController)theViewController inView:(CPView)theView;
{
if (!theView)
[CPException raise:CPInternalInconsistencyException reason:@"KPViewController placeViewController:inView: view must not be nil"];
var previousViewController = [self _controllerInView:theView];
[previousViewController viewWillDisappear];
[[previousViewController view] removeFromSuperview];
[previousViewController viewDidDisappear];
[self removeSubcontroller:previousViewController];
if (!theViewController)
return;
if (![[self subcontrollers] containsObject:theViewController])
[self addSubcontroller:theViewController];
var subview = [theViewController view];
[theViewController viewWillAppear];
[[[self viewPositioningManagerClass] defaultManager] placeView:subview inView:theView];
[theViewController viewDidAppear];
}
/*!
@ignore
*/
- (KPViewController)_controllerInView:(CPView)theView
{
var subcontrollerIndex = [[self subcontrollers] count];
while (subcontrollerIndex--)
{
var subcontroller = [[self subcontrollers] objectAtIndex:subcontrollerIndex];
if ([[subcontroller view] superview] === theView)
return subcontroller;
}
return nil;
}
/*!
@ignore
*/
- (void)_setVisible:(BOOL)isVisible
{
_isVisible = !!isVisible;
}
// =============
// = Accessors =
// =============
/*!
Simply returns a boolean value indicating wheter the receiver's view is loaded into memory.
@returns YES if the receiver's view is loaded, otherwise NO
*/
- (BOOL)isViewLoaded
{
// Use the instance variable here because [self view] will load the view if it isn't loaded
return !!_view;
}
- (void)setWindowController:(KPWindowController)theWindowController
{
if (_windowController === theWindowController)
return;
_windowController = theWindowController
[[self subcontrollers] makeObjectsPerformSelector:@selector(setWindowController:) withObject:theWindowController];
[[self windowController] patchResponderChain];
}
#pragma mark -
#pragma mark Subcontrollers
/*!
Sets the receiver's subcontrollers to the specified subcontrollers.
@param theSubcontrollers An array of subcontrollers
*/
- (void)setSubcontrollers:(CPArray)theSubcontrollers
{
if (_subcontrollers === theSubcontrollers)
return;
_subcontrollers = theSubcontrollers;
[[self windowController] patchResponderChain];
}
/*!
Adds a viewcontroller to the receiver's subcontrollers.
@param theViewController The viewcontroller to add as the receiver's subcontroller
*/
- (void)addSubcontroller:(KPViewController)theViewController
{
[_subcontrollers addObject:theViewController];
[[self windowController] patchResponderChain];
}
/*!
Removes a viewcontroller from the receiver's subcontrollers.
@param theViewController The viewcontroller to remove as the receiver's subcontroller
*/
- (void)removeSubcontroller:(KPViewController)theViewController
{
[_subcontrollers removeObject:theViewController];
[[self windowController] patchResponderChain];
}
/*!
Returns an array of the descendant viewcontrollers of the receiver.
@return an array of descdendants
*/
- (CPArray)descendants
{
var descendantsList = [];
for (var i = 0; i < [[self subcontrollers] count]; i++)
{
var subViewController = [[self subcontrollers] objectAtIndex:i];
[descendantsList addObject:subViewController];
if ([[subViewController subcontrollers] count] > 0)
[descendantsList addObjectsFromArray:[subViewController descendants]];
}
return descendantsList;
}
@end