-
Notifications
You must be signed in to change notification settings - Fork 2
/
XSViewController.m
executable file
·423 lines (338 loc) · 12.9 KB
/
XSViewController.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//
// XSViewController.m
// View Controllers
//
// Created by Jonathan Dann and Cathy Shive on 14/04/2008.
//
// Copyright (c) 2008 Jonathan Dann and Cathy Shive
// Copyright (c) 2013 Jonathan Mitchell
//
// 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.
//
// If you use it, acknowledgement in an About Page or other appropriate place would be nice.
// For example, "Contains "View Controllers" by Jonathan Dann and Cathy Shive" will do.
#import "XSViewController.h"
#import "XSWindowController.h"
static BOOL _raiseExceptionForDesignatedInitialiser = NO;
#pragma mark Private API
@interface XSViewController ()
/*!
This method is made private so users of the class can't (easily) set the children
array whenever they want, they must use the indexed accessors provided. If this was
public then our methods that maintain the responder chain and pass the represented
object and window controller to the children would be subverted. Alternatively the
setter could set all the required variables on the objects in the newChildren array,
but the API then becomes a little clunkier.
*/
- (void)setRespondingChildren:(NSMutableArray *)newChildren;
- (void)configureViewController:(XSViewController *)viewController;
- (void)configureViewControllers:(NSArray *)viewControllers;
@property (strong, readwrite, nonatomic) NSViewController *proxyViewController;
@end
#pragma mark -
#pragma mark Public API
@implementation XSViewController
@synthesize windowController = _windowController;
+ (BOOL)raiseExceptionForDesignatedInitialiser
{
return _raiseExceptionForDesignatedInitialiser;
}
+ (void)setRaiseExceptionForDesignatedInitialiser:(BOOL)value
{
_raiseExceptionForDesignatedInitialiser = value;
}
#pragma mark -
#pragma mark Life cycle
- (id)initWithNibName:(NSString *)name bundle:(NSBundle *)bundle windowController:(XSWindowController *)windowController
{
self = [super initWithNibName:name bundle:bundle];
if (self) {
[self setupInstance];
_windowController = windowController; // non-retained to avoid retain cycles
}
return self;
}
- (id)initWithNibName:(NSString *)name bundle:(NSBundle *)bundle
{
if ([[self class] raiseExceptionForDesignatedInitialiser]) {
// Legacy support only!
@throw [NSException exceptionWithName:@"XSViewControllerException"
reason:@"An instance of an XSViewController concrete subclass was initialized using the NSViewController desigated initialiser method -initWithNibName:bundle: all view controllers in the ensuing tree will have no initial reference to an XSWindowController object however they may easily obtain one subsequently!. To allow calling of the designated initialiser set +raiseExceptionForDesignatedInitialiser = NO."
userInfo:nil];
}
self = [super initWithNibName:name bundle:bundle];
if (self) {
[self setupInstance];
}
return self;
}
- (void)setupInstance
{
_respondingChildren = [NSMutableArray array]; // set up a blank mutable array
_alwaysQueryRootControllerForWindowController = NO;
}
- (void)dealloc
{
// this seems prudent given that the superclass on 10.10+
// may operate on -nextResponder on dealloc to fixup the responder chain.
/*
Hmm - looks like we need to be careful here as the 10.10+ support
wants to patch the views next responder to self.nextResponder which could cut chunks out of the chain.
void -[NSViewController _removeFromResponderChain](void * self, void * _cmd) {
r14 = self;
if ([self _viewControllerSupports10_10Features] == 0x0) goto .l1;
loc_307c3c:
r12 = r14->view;
rax = [r12 nextResponder];
if (rax == 0x0) goto .l1;
loc_307c63:
rbx = rax;
if (rbx == r14) goto loc_307c88;
loc_307c6b:
rax = [rbx nextResponder];
r12 = rbx;
if (rax != 0x0) goto loc_307c63;
.l1:
return;
loc_307c88:
[r12 setNextResponder:[r14 nextResponder]];
return;
}
*/
// TODO : it is possible that it would be better to set self.nextResponder = self.proxyViewController.nextResponder here to ensure that the view next responder gets updated correctly.
self.nextResponder = nil;
self.proxyViewController.nextResponder = nil;
}
#pragma mark -
#pragma mark Accessors
- (void)detach
{
self.parent = nil; // detach from NSViewController tree
self.nextResponder = nil; // detach from responder chain
[self willChangeValueForKey:@"windowController"];
_windowController = nil;
[self didChangeValueForKey:@"windowController"];
}
- (void)setWindowController:(XSWindowController *)controller
{
_windowController = controller;
if (!self.alwaysQueryRootControllerForWindowController) {
[self patchResponderChain];
}
}
- (XSWindowController *)windowController
{
XSWindowController *controller = nil;
if (_windowController && !self.alwaysQueryRootControllerForWindowController) {
controller = _windowController;
} else if (self.parent) {
// If no local controller instance available then query the root
controller = [self.rootController windowController];
// Cache the window controller if dynamic querying not required.
if (controller && !self.alwaysQueryRootControllerForWindowController) {
_windowController = controller;
}
}
return controller;
}
- (void)setView:(NSView *)view
{
[super setView:view];
// On 10.10 and above AppKit inserts NSViewController into the responder chain above the controller view.
// This clearly interfers with the XSViewController design philosophy of placing controllers
// between the NSWindow and NSWindowController.
// Just manipulating the view's nextResponder directly doesn't seem to work - the view wants to retain its controller as its nextReponder.
// Hence we allocate a controller proxy to serve as the view's next responder.
// This leaves the proxy parent free to be inserted elsewhere in the chain.
if (!self.proxyViewController) {
Class proxyViewControllerClass = self.proxyViewControllerClass;
// contract
NSAssert([proxyViewControllerClass isSubclassOfClass:[NSViewController class]], @"invalid proxy view controller class");
// create proxy view controller
self.proxyViewController = [[proxyViewControllerClass alloc] init];
}
self.proxyViewController.view = view;
}
- (NSView *)view
{
if (self.proxyViewController) {
return self.proxyViewController.view;
} else {
return [super view];
}
}
- (Class)proxyViewControllerClass
{
// subclasses can override to provide proxy class customisation
return [NSViewController class];
}
#pragma mark -
#pragma mark Indexed Accessors
- (NSUInteger)countOfRespondingChildren
{
return [self.respondingChildren count];
}
- (XSViewController *)objectInRespondingChildrenAtIndex:(NSUInteger)index
{
return [self.respondingChildren objectAtIndex:index];
}
- (void)addRespondingChild:(XSViewController *)viewController
{
if ([self.respondingChildren containsObject:viewController]) {
return;
}
[self insertObject:viewController inRespondingChildrenAtIndex:[self.respondingChildren count]];
}
- (void)removeRespondingChild:(XSViewController *)viewController
{
if ([self.respondingChildren containsObject:viewController]) {
[self.respondingChildren removeObject:viewController];
[viewController detach];
[self patchResponderChain];
}
}
- (void)removeAllRespondingChildren
{
if (self.respondingChildren.count == 0) {
return;
}
// remove all the children
for (XSViewController * child in [self.respondingChildren copy]) {
[self.respondingChildren removeObject:child];
[child detach];
}
// patch the chain
[self patchResponderChain];
}
- (void)removeObjectFromRespondingChildrenAtIndex:(NSUInteger)index
{
XSViewController * child = self.respondingChildren[index];
[self.respondingChildren removeObjectAtIndex:index];
[child detach];
[self patchResponderChain];
}
- (void)insertObject:(XSViewController *)viewController inRespondingChildrenAtIndex:(NSUInteger)index
{
[self configureViewController:viewController];
// Note: the viewController is strongly retained.
// it might be preferable to retain a zeroing weak reference instead.
[self.respondingChildren insertObject:viewController atIndex:index];
[viewController setParent:self]; // weak
[self patchResponderChain];
}
- (void)insertObjects:(NSArray *)viewControllers inRespondingChildrenAtIndexes:(NSIndexSet *)indexes
{
[self configureViewControllers:viewControllers];
[self.respondingChildren insertObjects:viewControllers atIndexes:indexes];
[viewControllers makeObjectsPerformSelector:@selector(setParent:) withObject:self];
[self patchResponderChain];
}
- (void)insertObjects:(NSArray *)viewControllers inRespondingChildrenAtIndex:(NSUInteger)index
{
[self insertObjects:viewControllers inRespondingChildrenAtIndexes:[NSIndexSet indexSetWithIndex:index]];
}
#pragma mark -
#pragma mark View controller configuration
- (void)configureViewController:(XSViewController *)viewController
{
// Assign the window controller if available and dynamic window controller resolution is off
if (viewController.windowController != self.windowController && !self.alwaysQueryRootControllerForWindowController) {
viewController.windowController = self.windowController;
}
}
- (void)configureViewControllers:(NSArray *)viewControllers
{
for (XSViewController *viewController in viewControllers) {
[self configureViewController:viewController];
}
}
#pragma mark -
#pragma mark Responder chain management
- (void)patchResponderChain
{
// if the root controller has a window controller then this view is part
// of an active view hierachy that wants to be included in the responder chain
XSWindowController *windowController = [self.rootController windowController];
if (windowController) {
/*
The window controller will build a chain including all of its responding view controllers.
*/
[windowController patchResponderChain];
}
else {
/*
No Window controller available so just patch the chain locally.
*/
[XSWindowController patchResponderChain:self];
}
}
#pragma mark -
#pragma mark Utilities
- (void)setRespondingChildren:(NSMutableArray *)newChildren
{
if (_respondingChildren == newChildren) {
return;
}
NSMutableArray *newChildrenCopy = [newChildren mutableCopy];
_respondingChildren = newChildrenCopy;
}
- (XSViewController *)rootController
{
XSViewController *root = self.parent;
// we are the top of the tree
if (!root) {
return self;
}
// if this is nil then there is no parent, the whole system is based on the idea
// that the top of the tree has nil parent, not the windowController as its parent.
while (root.parent) {
root = root.parent;
}
return root;
}
- (NSArray *)respondingDescendants
{
NSMutableArray *array = [NSMutableArray array];
for (XSViewController *child in self.respondingChildren) {
[array addObject:child];
if ([child countOfRespondingChildren] > 0) {
[array addObjectsFromArray:[child respondingDescendants]];
}
}
return [array copy]; // return an immutable array
}
- (void)removeObservations
{
[self.respondingChildren makeObjectsPerformSelector:@selector(removeObservations)];
}
@end
@implementation NSArray(XSViewController)
- (NSArray *)xsv_reverse
{
NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self count]];
NSEnumerator *enumerator = [self reverseObjectEnumerator];
for (id element in enumerator) {
[array addObject:element];
}
return array;
}
@end