mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-11-29 09:52:03 +00:00
CPWindow: Add content view controllers
Previously, CPWindow did not support content view controllers, requiring applications to attach controller views manually and keep them synchronized. Following AppKit, a window's content view controller now supplies its content view and resizes the window to fit that view, while setting a content view directly replaces the controller. The new +windowWithContentViewController: method creates a standard window whose title tracks the controller's title. CPWindowController's content view controller tracks its window, and cib window templates archive the controller.
This commit is contained in:
1 parent
974314fb00
commit
7358e939bd
4 files changed
+379
-1
No files matched your search
@@ -51,6 +51,7 @@
|
||||
@class CPMenu
|
||||
@class CPProgressIndicator
|
||||
@class CPToolbar
|
||||
@class CPViewController
|
||||
@class CPWindowController
|
||||
@class _CPWindowFrameAnimation
|
||||
@class _CPWindowFrameAnimationDelegate
|
||||
@@ -211,6 +212,7 @@ var CPWindowActionMessageKeys = [
|
||||
|
||||
CPView _windowView;
|
||||
CPView _contentView;
|
||||
CPViewController _contentViewController;
|
||||
CPView _toolbarView;
|
||||
|
||||
BOOL _handlingTrackingAreaEvent;
|
||||
@@ -284,6 +286,7 @@ var CPWindowActionMessageKeys = [
|
||||
}
|
||||
|
||||
@global document
|
||||
@global CPTitleBinding
|
||||
|
||||
+ (Class)_binderClassForBinding:(CPString)aBinding
|
||||
{
|
||||
@@ -1279,6 +1282,17 @@ CPTexturedBackgroundWindowMask
|
||||
*/
|
||||
- (void)setContentView:(CPView)aView
|
||||
{
|
||||
/*
|
||||
A content view set directly replaces the view of the content view controller, so the
|
||||
window no longer has one, as in AppKit.
|
||||
*/
|
||||
if (_contentViewController)
|
||||
{
|
||||
[self willChangeValueForKey:@"contentViewController"];
|
||||
_contentViewController = nil;
|
||||
[self didChangeValueForKey:@"contentViewController"];
|
||||
}
|
||||
|
||||
if (_contentView && _contentView !== aView)
|
||||
[_contentView removeFromSuperview];
|
||||
|
||||
@@ -1310,6 +1324,84 @@ CPTexturedBackgroundWindowMask
|
||||
return _contentView;
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the view controller whose view is the window's content view, or nil.
|
||||
*/
|
||||
- (CPViewController)contentViewController
|
||||
{
|
||||
return _contentViewController;
|
||||
}
|
||||
|
||||
/*!
|
||||
Makes a view controller's view the window's content view, in place of the current content
|
||||
view, and resizes the window to fit the size of that view. A window that fills its platform
|
||||
window does not resize, and neither does a window whose new content view has no size.
|
||||
The controller manages the content view only, not the window's title; bind the title to the
|
||||
controller's title to follow it. Setting nil keeps the content view and drops the controller.
|
||||
Setting a content view directly drops the controller too.
|
||||
|
||||
@param aViewController the view controller whose view becomes the content view
|
||||
*/
|
||||
- (void)setContentViewController:(CPViewController)aViewController
|
||||
{
|
||||
if (_contentViewController === aViewController)
|
||||
return;
|
||||
|
||||
if (!aViewController)
|
||||
{
|
||||
[self willChangeValueForKey:@"contentViewController"];
|
||||
_contentViewController = nil;
|
||||
[self didChangeValueForKey:@"contentViewController"];
|
||||
return;
|
||||
}
|
||||
|
||||
var view = [aViewController view],
|
||||
size = CGSizeMakeCopy([view frameSize]);
|
||||
|
||||
[self setContentView:view];
|
||||
|
||||
[self willChangeValueForKey:@"contentViewController"];
|
||||
_contentViewController = aViewController;
|
||||
[self didChangeValueForKey:@"contentViewController"];
|
||||
|
||||
/*
|
||||
AppKit sizes the window from the controller's view through Auto Layout. Without it, the
|
||||
frame size of the view is the size it asks for; a view without a size asks for nothing.
|
||||
Own judgement, not checked against AppKit.
|
||||
*/
|
||||
if (!_isFullPlatformWindow && size.width > 0 && size.height > 0)
|
||||
[self setFrameSize:[self frameRectForContentRect:CGRectMake(0.0, 0.0, size.width, size.height)].size];
|
||||
}
|
||||
|
||||
- (void)_setContentViewControllerWithoutResize:(CPViewController)aViewController
|
||||
{
|
||||
[self willChangeValueForKey:@"contentViewController"];
|
||||
_contentViewController = aViewController;
|
||||
[self didChangeValueForKey:@"contentViewController"];
|
||||
}
|
||||
|
||||
+ (BOOL)automaticallyNotifiesObserversOfContentViewController
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns a titled, closable, miniaturizable and resizable window whose content view
|
||||
controller is the given one. The window's title is bound to the controller's title.
|
||||
|
||||
@param aViewController the view controller whose view becomes the content view
|
||||
*/
|
||||
+ (id)windowWithContentViewController:(CPViewController)aViewController
|
||||
{
|
||||
var window = [[self alloc] initWithContentRect:CGRectMakeZero()
|
||||
styleMask:CPTitledWindowMask | CPClosableWindowMask | CPMiniaturizableWindowMask | CPResizableWindowMask];
|
||||
|
||||
[window setContentViewController:aViewController];
|
||||
[window bind:CPTitleBinding toObject:aViewController withKeyPath:@"title" options:nil];
|
||||
|
||||
return window;
|
||||
}
|
||||
|
||||
/*!
|
||||
Applies an alpha value to the window.
|
||||
@param aValue the alpha value to apply
|
||||
|
||||
@@ -65,6 +65,8 @@
|
||||
|
||||
CPViewController _viewController;
|
||||
CPView _viewControllerContainerView;
|
||||
|
||||
CPViewController _pendingContentViewController;
|
||||
}
|
||||
|
||||
- (id)init
|
||||
@@ -215,6 +217,43 @@
|
||||
|
||||
[_window setWindowController:self];
|
||||
[_window setNextResponder:self];
|
||||
|
||||
if (_window && _pendingContentViewController)
|
||||
{
|
||||
var viewController = _pendingContentViewController;
|
||||
|
||||
_pendingContentViewController = nil;
|
||||
[_window setContentViewController:viewController];
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the view controller whose view is the content view of the window, as the window's
|
||||
property of the same name. Before the window loads, it returns the view controller that
|
||||
will become the window's.
|
||||
*/
|
||||
- (CPViewController)contentViewController
|
||||
{
|
||||
return _window ? [_window contentViewController] : _pendingContentViewController;
|
||||
}
|
||||
|
||||
/*!
|
||||
Sets the content view controller of the window. A view controller set before the window
|
||||
loads becomes the window's when the window loads.
|
||||
|
||||
@param aViewController the view controller whose view becomes the content view
|
||||
*/
|
||||
- (void)setContentViewController:(CPViewController)aViewController
|
||||
{
|
||||
if (_window)
|
||||
[_window setContentViewController:aViewController];
|
||||
else
|
||||
_pendingContentViewController = aViewController;
|
||||
}
|
||||
|
||||
+ (CPSet)keyPathsForValuesAffectingContentViewController
|
||||
{
|
||||
return [CPSet setWithObject:@"window.contentViewController"];
|
||||
}
|
||||
|
||||
/*!
|
||||
|
||||
@@ -29,6 +29,8 @@
|
||||
@import "CPWindow_Constants.j"
|
||||
@import "_CPToolTip.j"
|
||||
|
||||
@class CPViewController
|
||||
|
||||
|
||||
@implementation _CPCibWindowTemplate : CPObject
|
||||
{
|
||||
@@ -49,6 +51,8 @@
|
||||
BOOL _windowIsFullPlatformWindow;
|
||||
|
||||
CPAppearance _windowAppearance;
|
||||
|
||||
CPViewController _windowContentViewController;
|
||||
}
|
||||
|
||||
- (id)init
|
||||
@@ -121,6 +125,18 @@
|
||||
[theWindow setContentView:_windowView];
|
||||
[_windowView setAutoresizesSubviews:contentViewAutoresizesSubviews];
|
||||
|
||||
/*
|
||||
The content view of the template is the view of the content view controller when both
|
||||
come from the same archive; the window then keeps the template's frame.
|
||||
*/
|
||||
if (_windowContentViewController)
|
||||
{
|
||||
if ([_windowContentViewController view] === _windowView)
|
||||
[theWindow _setContentViewControllerWithoutResize:_windowContentViewController];
|
||||
else
|
||||
[theWindow setContentViewController:_windowContentViewController];
|
||||
}
|
||||
|
||||
if ([_viewClass isKindOfClass:[CPToolbar class]])
|
||||
[theWindow setToolbar:_viewClass];
|
||||
|
||||
@@ -150,7 +166,8 @@ var _CPCibWindowTemplateMinSizeKey = @"_CPCibWindowTemp
|
||||
|
||||
_CPCibWindowTemplateWindowAutorecalculatesKeyViewLoop = @"_CPCibWindowTemplateWindowAutorecalculatesKeyViewLoop",
|
||||
_CPCibWindowTemplateWindowIsFullPlatformWindowKey = @"_CPCibWindowTemplateWindowIsFullPlatformWindowKey",
|
||||
_CPCibWindowTemplateWindowAppearanceKey = @"_CPCibWindowTemplateWindowAppearanceKey";
|
||||
_CPCibWindowTemplateWindowAppearanceKey = @"_CPCibWindowTemplateWindowAppearanceKey",
|
||||
_CPCibWindowTemplateWindowContentViewControllerKey = @"_CPCibWindowTemplateWindowContentViewControllerKey";
|
||||
|
||||
@implementation _CPCibWindowTemplate (Coding)
|
||||
|
||||
@@ -180,6 +197,7 @@ var _CPCibWindowTemplateMinSizeKey = @"_CPCibWindowTemp
|
||||
_windowAutorecalculatesKeyViewLoop = [aCoder decodeBoolForKey:_CPCibWindowTemplateWindowAutorecalculatesKeyViewLoop];
|
||||
_windowIsFullPlatformWindow = [aCoder decodeBoolForKey:_CPCibWindowTemplateWindowIsFullPlatformWindowKey];
|
||||
_windowAppearance = [aCoder decodeObjectForKey:_CPCibWindowTemplateWindowAppearanceKey];
|
||||
_windowContentViewController = [aCoder decodeObjectForKey:_CPCibWindowTemplateWindowContentViewControllerKey];
|
||||
}
|
||||
|
||||
return self;
|
||||
@@ -216,6 +234,9 @@ var _CPCibWindowTemplateMinSizeKey = @"_CPCibWindowTemp
|
||||
|
||||
if (_windowAppearance)
|
||||
[aCoder encodeObject:_windowAppearance forKey:_CPCibWindowTemplateWindowAppearanceKey];
|
||||
|
||||
if (_windowContentViewController)
|
||||
[aCoder encodeObject:_windowContentViewController forKey:_CPCibWindowTemplateWindowContentViewControllerKey];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -0,0 +1,226 @@
|
||||
/*
|
||||
* CPWindowContentViewControllerTest.j
|
||||
* AppKit
|
||||
*
|
||||
* Created by David Richardson on 2026-11-24.
|
||||
* 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/CPApplication.j>
|
||||
@import <AppKit/CPWindow.j>
|
||||
@import <AppKit/CPWindowController.j>
|
||||
@import <AppKit/CPViewController.j>
|
||||
@import <AppKit/_CPCibWindowTemplate.j>
|
||||
|
||||
@class _CPContentTestViewController
|
||||
@class _CPContentTestObserver
|
||||
|
||||
/*
|
||||
These tests state the content view controller of a window and of a window controller as
|
||||
AppKit's NSWindow and NSWindowController have it: the controller's view becomes the content
|
||||
view, the window resizes to fit it, a content view set directly drops the controller, the
|
||||
responder chain runs from the view through the controller, and the window controller
|
||||
follows the window. They also state the archiving of the controller in a cib window
|
||||
template.
|
||||
|
||||
The windows are borderless, so a window's frame is its content rect.
|
||||
*/
|
||||
@implementation CPWindowContentViewControllerTest : OJTestCase
|
||||
{
|
||||
CPWindow window;
|
||||
_CPContentTestViewController viewController;
|
||||
}
|
||||
|
||||
- (void)setUp
|
||||
{
|
||||
[[CPApplication alloc] init];
|
||||
|
||||
window = [[CPWindow alloc] initWithContentRect:CGRectMake(0, 0, 400, 300) styleMask:CPBorderlessWindowMask];
|
||||
viewController = [[_CPContentTestViewController alloc] initWithSize:CGSizeMake(250, 150)];
|
||||
}
|
||||
|
||||
- (void)testAWindowHasNoContentViewControllerByDefault
|
||||
{
|
||||
[self assertNull:[window contentViewController] message:@"no content view controller"];
|
||||
}
|
||||
|
||||
- (void)testTheControllerViewBecomesTheContentView
|
||||
{
|
||||
var oldContentView = [window contentView];
|
||||
|
||||
[window setContentViewController:viewController];
|
||||
|
||||
[self assert:viewController equals:[window contentViewController] message:@"the content view controller"];
|
||||
[self assert:[viewController view] equals:[window contentView] message:@"the controller's view is the content view"];
|
||||
[self assertNull:[oldContentView superview] message:@"the old content view is removed"];
|
||||
}
|
||||
|
||||
- (void)testTheWindowResizesToFitTheControllerView
|
||||
{
|
||||
[window setContentViewController:viewController];
|
||||
|
||||
[self assert:250 equals:CGRectGetWidth([window frame]) message:@"the window width"];
|
||||
[self assert:150 equals:CGRectGetHeight([window frame]) message:@"the window height"];
|
||||
[self assert:250 equals:CGRectGetWidth([[window contentView] frame]) message:@"the content view keeps its width"];
|
||||
}
|
||||
|
||||
- (void)testAControllerViewWithoutASizeDoesNotResizeTheWindow
|
||||
{
|
||||
[window setContentViewController:[[_CPContentTestViewController alloc] initWithSize:CGSizeMakeZero()]];
|
||||
|
||||
[self assert:400 equals:CGRectGetWidth([window frame]) message:@"the window keeps its width"];
|
||||
[self assert:300 equals:CGRectGetHeight([window frame]) message:@"the window keeps its height"];
|
||||
}
|
||||
|
||||
/*
|
||||
ojtest has no browser window for a window to fill, so this test marks the window as one
|
||||
that fills its platform window without going through -setFullPlatformWindow:.
|
||||
*/
|
||||
- (void)testAWindowThatFillsItsPlatformWindowDoesNotResize
|
||||
{
|
||||
window._isFullPlatformWindow = YES;
|
||||
[window setContentViewController:viewController];
|
||||
|
||||
[self assert:400 equals:CGRectGetWidth([window frame]) message:@"the window keeps its width"];
|
||||
}
|
||||
|
||||
- (void)testAContentViewSetDirectlyDropsTheController
|
||||
{
|
||||
var observer = [[_CPContentTestObserver alloc] init];
|
||||
|
||||
[window setContentViewController:viewController];
|
||||
[window addObserver:observer forKeyPath:@"contentViewController" options:0 context:nil];
|
||||
[window setContentView:[[CPView alloc] initWithFrame:CGRectMakeZero()]];
|
||||
[window removeObserver:observer forKeyPath:@"contentViewController"];
|
||||
|
||||
[self assertNull:[window contentViewController] message:@"the controller is dropped"];
|
||||
[self assert:1 equals:observer.count message:@"observers learn of the change"];
|
||||
}
|
||||
|
||||
- (void)testSettingNilKeepsTheContentView
|
||||
{
|
||||
[window setContentViewController:viewController];
|
||||
[window setContentViewController:nil];
|
||||
|
||||
[self assertNull:[window contentViewController] message:@"no controller"];
|
||||
[self assert:[viewController view] equals:[window contentView] message:@"the content view stays"];
|
||||
}
|
||||
|
||||
- (void)testTheResponderChainRunsThroughTheController
|
||||
{
|
||||
[window setContentViewController:viewController];
|
||||
|
||||
[self assert:viewController equals:[[window contentView] nextResponder] message:@"the content view's next responder is the controller"];
|
||||
}
|
||||
|
||||
- (void)testAWindowCanBeMadeWithAContentViewController
|
||||
{
|
||||
[viewController setTitle:@"People"];
|
||||
|
||||
var newWindow = [CPWindow windowWithContentViewController:viewController];
|
||||
|
||||
[self assert:viewController equals:[newWindow contentViewController] message:@"the content view controller"];
|
||||
[self assert:CPTitledWindowMask | CPClosableWindowMask | CPMiniaturizableWindowMask | CPResizableWindowMask equals:[newWindow styleMask] message:@"the style mask"];
|
||||
[self assert:@"People" equals:[newWindow title] message:@"the title comes from the controller"];
|
||||
|
||||
[viewController setTitle:@"Groups"];
|
||||
[self assert:@"Groups" equals:[newWindow title] message:@"the title follows the controller"];
|
||||
}
|
||||
|
||||
- (void)testAWindowControllerFollowsItsWindow
|
||||
{
|
||||
var windowController = [[CPWindowController alloc] initWithWindow:window];
|
||||
|
||||
[windowController setContentViewController:viewController];
|
||||
|
||||
[self assert:viewController equals:[window contentViewController] message:@"the window has the controller"];
|
||||
[self assert:viewController equals:[windowController contentViewController] message:@"the window controller reports it"];
|
||||
|
||||
[window setContentView:[[CPView alloc] initWithFrame:CGRectMakeZero()]];
|
||||
[self assertNull:[windowController contentViewController] message:@"the window controller follows the window"];
|
||||
}
|
||||
|
||||
- (void)testAControllerSetBeforeTheWindowLoadsIsAppliedWhenItLoads
|
||||
{
|
||||
var windowController = [[CPWindowController alloc] init];
|
||||
|
||||
[windowController setContentViewController:viewController];
|
||||
[self assert:viewController equals:[windowController contentViewController] message:@"the pending controller"];
|
||||
|
||||
[windowController setWindow:window];
|
||||
[self assert:viewController equals:[window contentViewController] message:@"the window gets the controller"];
|
||||
}
|
||||
|
||||
- (void)testAWindowTemplateArchivesTheContentViewController
|
||||
{
|
||||
var template = [[_CPCibWindowTemplate alloc] init];
|
||||
|
||||
template._windowContentViewController = viewController;
|
||||
template._windowView = [viewController view];
|
||||
template._windowStyleMask = CPBorderlessWindowMask;
|
||||
template._windowRect = CGRectMake(0, 0, 250, 150);
|
||||
|
||||
var copy = [CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:template]],
|
||||
newWindow = [copy _cibInstantiate],
|
||||
newController = [newWindow contentViewController];
|
||||
|
||||
[self assertNotNull:newController message:@"the window has a content view controller"];
|
||||
[self assert:[newController view] equals:[newWindow contentView] message:@"the controller's view is the content view"];
|
||||
[self assert:250 equals:CGRectGetWidth([newWindow frame]) message:@"the window keeps the template's frame"];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation _CPContentTestViewController : CPViewController
|
||||
{
|
||||
CGSize _size;
|
||||
}
|
||||
|
||||
- (id)initWithSize:(CGSize)aSize
|
||||
{
|
||||
if (self = [super initWithCibName:nil bundle:nil])
|
||||
_size = aSize;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)loadView
|
||||
{
|
||||
[self setView:[[CPView alloc] initWithFrame:CGRectMake(0, 0, _size.width, _size.height)]];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation _CPContentTestObserver : 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