mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-11-29 09:52:03 +00:00
CPViewController: Add child view controller management
Previously, CPViewController lacked child view controller APIs, preventing container controllers from tracking children and causing the split view controller to raise on its first item. Following NSViewController, the receiver now maintains an ordered array of child view controllers and each child tracks its parent. Insertion and removal route through two primitives overridable by container subclasses, changes notify observers, and child view controllers are archived.
This commit is contained in:
1 parent
156335dc85
commit
709d8fa904
2 files changed
+370
-1
No files matched your search
+153
-1
@@ -78,6 +78,9 @@ var CPViewControllerCachedCibs;
|
||||
CPString _cibName @accessors(property=cibName, readonly);
|
||||
CPBundle _cibBundle @accessors(property=cibBundle, readonly);
|
||||
CPDictionary _cibExternalNameTable @accessors(property=cibExternalNameTable, readonly);
|
||||
|
||||
CPMutableArray _childViewControllers;
|
||||
CPViewController _parentViewController;
|
||||
}
|
||||
|
||||
+ (void)initialize
|
||||
@@ -430,6 +433,137 @@ var CPViewControllerCachedCibs;
|
||||
of the view controller's view. This is the second half of the insertion,
|
||||
completing the chain: view -> viewController -> superview.
|
||||
*/
|
||||
// MARK: - Child view controllers
|
||||
|
||||
/*!
|
||||
Returns the parent of the receiver: the view controller that has the receiver as a child,
|
||||
or nil.
|
||||
*/
|
||||
- (CPViewController)parentViewController
|
||||
{
|
||||
return _parentViewController;
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the child view controllers of the receiver, in order.
|
||||
*/
|
||||
- (CPArray)childViewControllers
|
||||
{
|
||||
return _childViewControllers ? [_childViewControllers copy] : @[];
|
||||
}
|
||||
|
||||
/*!
|
||||
Replaces the child view controllers of the receiver. Each child that is not in the new
|
||||
array is removed with -removeChildViewControllerAtIndex:, and each new child is inserted
|
||||
with -insertChildViewController:atIndex:, so a subclass that overrides those methods sees
|
||||
every change.
|
||||
|
||||
@param childViewControllers - the new children, in order.
|
||||
*/
|
||||
- (void)setChildViewControllers:(CPArray)childViewControllers
|
||||
{
|
||||
var newChildren = childViewControllers || @[];
|
||||
|
||||
for (var i = (_childViewControllers ? _childViewControllers.length : 0) - 1; i >= 0; i--)
|
||||
if (![newChildren containsObjectIdenticalTo:_childViewControllers[i]])
|
||||
[self removeChildViewControllerAtIndex:i];
|
||||
|
||||
for (var i = 0, count = [newChildren count]; i < count; i++)
|
||||
{
|
||||
var child = newChildren[i];
|
||||
|
||||
if (_childViewControllers && _childViewControllers[i] === child)
|
||||
continue;
|
||||
|
||||
if ([child parentViewController] === self)
|
||||
[self removeChildViewControllerAtIndex:[_childViewControllers indexOfObjectIdenticalTo:child]];
|
||||
|
||||
[self insertChildViewController:child atIndex:i];
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
Adds a child view controller at the end of the children. This calls
|
||||
-insertChildViewController:atIndex:.
|
||||
|
||||
@param childViewController - the view controller to add.
|
||||
*/
|
||||
- (void)addChildViewController:(CPViewController)childViewController
|
||||
{
|
||||
[self insertChildViewController:childViewController atIndex:_childViewControllers ? _childViewControllers.length : 0];
|
||||
}
|
||||
|
||||
/*!
|
||||
Inserts a child view controller at an index. A child that has a parent is first removed
|
||||
from it with -removeFromParentViewController. This is the method a container subclass
|
||||
overrides to act on insertions; the override must call super.
|
||||
|
||||
@param childViewController - the view controller to insert.
|
||||
@param index - the index for the child; it raises CPRangeException when it is past the end.
|
||||
*/
|
||||
- (void)insertChildViewController:(CPViewController)childViewController atIndex:(CPInteger)index
|
||||
{
|
||||
if (!childViewController)
|
||||
[CPException raise:CPInvalidArgumentException reason:@"-[CPViewController insertChildViewController:atIndex:] needs a view controller"];
|
||||
|
||||
/*
|
||||
AppKit states only that a child of another parent is removed first. A child of the
|
||||
receiver is removed too, so the insertion moves it; the index then refers to the
|
||||
children without it. Own judgement, not checked against AppKit.
|
||||
*/
|
||||
if ([childViewController parentViewController])
|
||||
[childViewController removeFromParentViewController];
|
||||
|
||||
if (index < 0 || index > (_childViewControllers ? _childViewControllers.length : 0))
|
||||
[CPException raise:CPRangeException reason:@"-[CPViewController insertChildViewController:atIndex:] index " + index + " is out of range"];
|
||||
|
||||
if (!_childViewControllers)
|
||||
_childViewControllers = @[];
|
||||
|
||||
[self willChangeValueForKey:@"childViewControllers"];
|
||||
[_childViewControllers insertObject:childViewController atIndex:index];
|
||||
childViewController._parentViewController = self;
|
||||
[self didChangeValueForKey:@"childViewControllers"];
|
||||
}
|
||||
|
||||
/*!
|
||||
Removes the child view controller at an index. This is the method a container subclass
|
||||
overrides to act on removals; the override must call super.
|
||||
|
||||
@param index - the index of the child; it raises CPRangeException when it is out of range.
|
||||
*/
|
||||
- (void)removeChildViewControllerAtIndex:(CPInteger)index
|
||||
{
|
||||
if (index < 0 || index >= (_childViewControllers ? _childViewControllers.length : 0))
|
||||
[CPException raise:CPRangeException reason:@"-[CPViewController removeChildViewControllerAtIndex:] index " + index + " is out of range"];
|
||||
|
||||
var child = _childViewControllers[index];
|
||||
|
||||
[self willChangeValueForKey:@"childViewControllers"];
|
||||
[_childViewControllers removeObjectAtIndex:index];
|
||||
child._parentViewController = nil;
|
||||
[self didChangeValueForKey:@"childViewControllers"];
|
||||
}
|
||||
|
||||
/*!
|
||||
Removes the receiver from its parent with the parent's -removeChildViewControllerAtIndex:.
|
||||
It does nothing when the receiver has no parent.
|
||||
*/
|
||||
- (void)removeFromParentViewController
|
||||
{
|
||||
var parent = _parentViewController;
|
||||
|
||||
if (!parent)
|
||||
return;
|
||||
|
||||
[parent removeChildViewControllerAtIndex:[parent._childViewControllers indexOfObjectIdenticalTo:self]];
|
||||
}
|
||||
|
||||
- (BOOL)automaticallyNotifiesObserversOfChildViewControllers
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (id)nextResponder
|
||||
{
|
||||
return [_view superview];
|
||||
@@ -470,7 +604,8 @@ var CPViewControllerCachedCibs;
|
||||
var CPViewControllerViewKey = @"CPViewControllerViewKey",
|
||||
CPViewControllerTitleKey = @"CPViewControllerTitleKey",
|
||||
CPViewControllerCibNameKey = @"CPViewControllerCibNameKey",
|
||||
CPViewControllerBundleKey = @"CPViewControllerBundleKey";
|
||||
CPViewControllerBundleKey = @"CPViewControllerBundleKey",
|
||||
CPViewControllerChildViewControllersKey = @"CPViewControllerChildViewControllersKey";
|
||||
|
||||
@implementation CPViewController (CPCoding)
|
||||
|
||||
@@ -498,6 +633,20 @@ var CPViewControllerViewKey = @"CPViewControllerViewKey",
|
||||
|
||||
_cibExternalNameTable = @{ CPCibOwner: self };
|
||||
_isLazy = YES;
|
||||
|
||||
/*
|
||||
The children are set directly, not through -insertChildViewController:atIndex:, so
|
||||
a container subclass decodes its own state before it acts on its children.
|
||||
*/
|
||||
var children = [aCoder decodeObjectForKey:CPViewControllerChildViewControllersKey];
|
||||
|
||||
if ([children count])
|
||||
{
|
||||
_childViewControllers = [children mutableCopy];
|
||||
|
||||
for (var i = 0, count = [children count]; i < count; i++)
|
||||
children[i]._parentViewController = self;
|
||||
}
|
||||
}
|
||||
|
||||
return self;
|
||||
@@ -515,6 +664,9 @@ var CPViewControllerViewKey = @"CPViewControllerViewKey",
|
||||
[aCoder encodeObject:_title forKey:CPViewControllerTitleKey];
|
||||
[aCoder encodeObject:_cibName forKey:CPViewControllerCibNameKey];
|
||||
[aCoder encodeObject:[_cibBundle bundlePath] forKey:CPViewControllerBundleKey];
|
||||
|
||||
if ([_childViewControllers count])
|
||||
[aCoder encodeObject:_childViewControllers forKey:CPViewControllerChildViewControllersKey];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,217 @@
|
||||
/*
|
||||
* CPViewControllerContainmentTest.j
|
||||
* AppKit
|
||||
*
|
||||
* Created by David Richardson on 2026-11-23.
|
||||
* Copyright David Richardson 2026.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
@import <AppKit/CPViewController.j>
|
||||
|
||||
@class _CPContainmentTestContainer
|
||||
@class _CPContainmentTestObserver
|
||||
|
||||
/*
|
||||
These tests state the child view controller API of CPViewController as AppKit's
|
||||
NSViewController has it: the children in order, the parent of each child, the insert and
|
||||
remove primitives that a container subclass overrides, and archiving.
|
||||
*/
|
||||
@implementation CPViewControllerContainmentTest : OJTestCase
|
||||
{
|
||||
CPViewController parent;
|
||||
CPViewController childA;
|
||||
CPViewController childB;
|
||||
}
|
||||
|
||||
- (void)setUp
|
||||
{
|
||||
parent = [[CPViewController alloc] init];
|
||||
childA = [[CPViewController alloc] init];
|
||||
childB = [[CPViewController alloc] init];
|
||||
}
|
||||
|
||||
- (void)testANewViewControllerHasNoChildrenAndNoParent
|
||||
{
|
||||
[self assert:0 equals:[[parent childViewControllers] count] message:@"no children"];
|
||||
[self assertNull:[parent parentViewController] message:@"no parent"];
|
||||
}
|
||||
|
||||
- (void)testAddedChildrenKeepTheirOrderAndKnowTheirParent
|
||||
{
|
||||
[parent addChildViewController:childA];
|
||||
[parent addChildViewController:childB];
|
||||
|
||||
[self assert:[childA, childB] equals:[parent childViewControllers] message:@"the children in order"];
|
||||
[self assert:parent equals:[childA parentViewController] message:@"the parent of the first child"];
|
||||
[self assert:parent equals:[childB parentViewController] message:@"the parent of the second child"];
|
||||
}
|
||||
|
||||
- (void)testAChildCanBeInsertedAtAnIndex
|
||||
{
|
||||
[parent addChildViewController:childA];
|
||||
[parent insertChildViewController:childB atIndex:0];
|
||||
|
||||
[self assert:[childB, childA] equals:[parent childViewControllers] message:@"the inserted child comes first"];
|
||||
}
|
||||
|
||||
- (void)testAnInsertionPastTheEndRaisesARangeException
|
||||
{
|
||||
var raised = nil;
|
||||
|
||||
try
|
||||
{
|
||||
[parent insertChildViewController:childA atIndex:1];
|
||||
}
|
||||
catch (anException)
|
||||
{
|
||||
raised = [anException name];
|
||||
}
|
||||
|
||||
[self assert:CPRangeException equals:raised message:@"an index past the end raises"];
|
||||
[self assertNull:[childA parentViewController] message:@"the child gets no parent"];
|
||||
}
|
||||
|
||||
- (void)testAChildOfAnotherParentMovesToTheNewParent
|
||||
{
|
||||
var otherParent = [[CPViewController alloc] init];
|
||||
|
||||
[otherParent addChildViewController:childA];
|
||||
[parent addChildViewController:childA];
|
||||
|
||||
[self assert:0 equals:[[otherParent childViewControllers] count] message:@"the old parent loses the child"];
|
||||
[self assert:[childA] equals:[parent childViewControllers] message:@"the new parent has the child"];
|
||||
[self assert:parent equals:[childA parentViewController] message:@"the child knows its new parent"];
|
||||
}
|
||||
|
||||
- (void)testAChildCanBeRemovedByIndexOrFromItsParent
|
||||
{
|
||||
[parent addChildViewController:childA];
|
||||
[parent addChildViewController:childB];
|
||||
|
||||
[parent removeChildViewControllerAtIndex:0];
|
||||
[self assert:[childB] equals:[parent childViewControllers] message:@"removal by index"];
|
||||
[self assertNull:[childA parentViewController] message:@"a removed child has no parent"];
|
||||
|
||||
[childB removeFromParentViewController];
|
||||
[self assert:0 equals:[[parent childViewControllers] count] message:@"removal from the parent"];
|
||||
[self assertNull:[childB parentViewController] message:@"the second removed child has no parent"];
|
||||
}
|
||||
|
||||
- (void)testARemovalByAnIndexOutOfRangeRaisesARangeException
|
||||
{
|
||||
var raised = nil;
|
||||
|
||||
try
|
||||
{
|
||||
[parent removeChildViewControllerAtIndex:0];
|
||||
}
|
||||
catch (anException)
|
||||
{
|
||||
raised = [anException name];
|
||||
}
|
||||
|
||||
[self assert:CPRangeException equals:raised message:@"an index out of range raises"];
|
||||
}
|
||||
|
||||
- (void)testSettingTheChildrenGoesThroughTheInsertAndRemovePrimitives
|
||||
{
|
||||
var container = [[_CPContainmentTestContainer alloc] init],
|
||||
childC = [[CPViewController alloc] init];
|
||||
|
||||
[container addChildViewController:childA];
|
||||
[container addChildViewController:childB];
|
||||
container.log = [];
|
||||
|
||||
[container setChildViewControllers:[childB, childC]];
|
||||
|
||||
[self assert:[childB, childC] equals:[container childViewControllers] message:@"the new children"];
|
||||
[self assert:["remove 0", "insert 1"] equals:container.log message:@"each change goes through a primitive"];
|
||||
[self assertNull:[childA parentViewController] message:@"the dropped child has no parent"];
|
||||
}
|
||||
|
||||
- (void)testTheChildrenChangeIsObservable
|
||||
{
|
||||
var observer = [[_CPContainmentTestObserver alloc] init];
|
||||
|
||||
[parent addObserver:observer forKeyPath:@"childViewControllers" options:0 context:nil];
|
||||
[parent addChildViewController:childA];
|
||||
[parent removeChildViewControllerAtIndex:0];
|
||||
[parent removeObserver:observer forKeyPath:@"childViewControllers"];
|
||||
|
||||
[self assert:2 equals:observer.count message:@"one notification for each change"];
|
||||
}
|
||||
|
||||
- (void)testTheChildrenAreArchived
|
||||
{
|
||||
[childA setTitle:@"First"];
|
||||
[childB setTitle:@"Second"];
|
||||
[parent addChildViewController:childA];
|
||||
[parent addChildViewController:childB];
|
||||
|
||||
var copy = [CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:parent]],
|
||||
children = [copy childViewControllers];
|
||||
|
||||
[self assert:2 equals:[children count] message:@"both children are decoded"];
|
||||
[self assert:@"First" equals:[children[0] title] message:@"the first child"];
|
||||
[self assert:@"Second" equals:[children[1] title] message:@"the second child"];
|
||||
[self assert:copy equals:[children[0] parentViewController] message:@"a decoded child knows its parent"];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation _CPContainmentTestContainer : CPViewController
|
||||
{
|
||||
CPArray log;
|
||||
}
|
||||
|
||||
- (void)insertChildViewController:(CPViewController)aChild atIndex:(CPInteger)anIndex
|
||||
{
|
||||
if (log)
|
||||
log.push("insert " + anIndex);
|
||||
|
||||
[super insertChildViewController:aChild atIndex:anIndex];
|
||||
}
|
||||
|
||||
- (void)removeChildViewControllerAtIndex:(CPInteger)anIndex
|
||||
{
|
||||
if (log)
|
||||
log.push("remove " + anIndex);
|
||||
|
||||
[super removeChildViewControllerAtIndex:anIndex];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation _CPContainmentTestObserver : CPObject
|
||||
{
|
||||
int count;
|
||||
}
|
||||
|
||||
- (id)init
|
||||
{
|
||||
if (self = [super init])
|
||||
count = 0;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)observeValueForKeyPath:(CPString)aKeyPath ofObject:(id)anObject change:(CPDictionary)aChange context:(id)aContext
|
||||
{
|
||||
count++;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user