AppKit: Insert original toolbar items rather than archived copies

Copying every item from delegates and cibs broke view outlets, left external references pointing to detached views, and stripped control targets.

- CPToolbar: Insert received items directly in -_reloadToolbarItems;
  copy an item only when -_isItemInUse: indicates prior placement in
  the current reload or active display in another toolbar.

- _CPToolbarView: Unsubscribe discarded item views from observing their toolbar items via KVO in -reloadToolbarItems.

- CPToolbar: Document direct insertion semantics for delegate-returned items.

- CPToolbarTest: Add tests for delegate and cib item insertion, preserved control targets, repeated and shared item copying, item reuse across reloads, and observer removal.
This commit is contained in:
David Richardson committed 2026-11-16 15:19:26 -06:00
1 parent 18ac703f4e
commit 6da20783fd
2 files changed
+287 -9

No files matched your search

+60 -9
View File
@@ -105,9 +105,10 @@ var CPToolbarsByIdentifier = nil,
Called to obtain a toolbar item. Required.
@param toolbar the toolbar the item belongs to
@param itemIdentifier the identifier of the toolbar item
@param flag \c YES means the item will be placed in the toolbar. \c NO means the item will be displayed for
some other purpose (non-functional)
@return the toolbar item or \c nil if no such item belongs in the toolbar
@param flag \c YES means the toolbar inserts the returned item itself, with its view. \c NO means the item will be
displayed for some other purpose (non-functional)
@return the toolbar item or \c nil if no such item belongs in the toolbar. Each toolbar expects an item of
its own.
*/
@implementation CPToolbar : CPObject
{
@@ -361,12 +362,21 @@ var CPToolbarsByIdentifier = nil,
if (!item && _delegate)
item = [self _sendDelegateItemForItemIdentifier:identifier willBeInsertedIntoToolbar:YES];
item = [item copy];
if (item == nil)
[CPException raise:CPInvalidArgumentException
reason:@"Toolbar delegate " + _delegate + " returned nil toolbar item for identifier \"" + identifier + "\""];
/*
The toolbar copied every item before it inserted it, also the item that the delegate
returned for insertion and the item decoded from a cib. A copy archives and unarchives
the item's view, so the toolbar showed a decoded copy: the application's references
reached an item and a view that were never shown, and the copy lost its target. AppKit
inserts the item that it receives. An item already placed in this reload or shown by
another toolbar is still copied, because a view has only one superview.
*/
if ([self _isItemInUse:item])
item = [item copy];
item._toolbar = self;
[_items addObject:item];
@@ -433,6 +443,24 @@ var CPToolbarsByIdentifier = nil,
}
}
/*
Returns YES when the receiver has already placed the item in the current reload, or when
another toolbar shows the item.
@ignore
*/
- (BOOL)_isItemInUse:(CPToolbarItem)anItem
{
if ([_items indexOfObjectIdenticalTo:anItem] !== CPNotFound)
return YES;
var owner = [anItem toolbar];
if (!owner || owner === self)
return NO;
return [[owner items] indexOfObjectIdenticalTo:anItem] !== CPNotFound;
}
/* @ignore */
- (id)_itemForItemIdentifier:(CPString)identifier willBeInsertedIntoToolbar:(BOOL)toolbar
{
@@ -945,6 +973,14 @@ var _CPToolbarItemInfoMake = function(anIndex, aView, aLabel, aMinWidth)
- (void)reloadToolbarItems
{
/*
The old item views kept observing their items after a reload. Each item was a new copy,
so nothing else used the items they observed. The toolbar now keeps an item across a
reload, and an old view would still update with it. The old views now stop observing.
*/
for (var UID in _viewsForToolbarItems)
[_viewsForToolbarItems[UID] _stopObservingToolbarItem];
// Get rid of all our current subviews.
var subviews = [self subviews],
count = subviews.length;
@@ -986,6 +1022,9 @@ var _CPToolbarItemInfoMake = function(anIndex, aView, aLabel, aMinWidth)
@end
/* @ignore */
var _CPToolbarItemViewObservedKeyPaths = [@"label", @"image", @"alternateImage", @"minSize", @"maxSize", @"target", @"action", @"enabled"];
/* @ignore */
var _CPToolbarItemVisibilityPriorityCompare = function(lhs, rhs)
{
@@ -1059,14 +1098,13 @@ var LABEL_MARGIN = 2.0;
_toolbar = aToolbar;
var keyPaths = [@"label", @"image", @"alternateImage", @"minSize", @"maxSize", @"target", @"action", @"enabled"],
index = 0,
count = [keyPaths count];
var index = 0,
count = [_CPToolbarItemViewObservedKeyPaths count];
for (; index < count; ++index)
[_toolbarItem
addObserver:self
forKeyPath:keyPaths[index]
forKeyPath:_CPToolbarItemViewObservedKeyPaths[index]
options:0
context:NULL];
}
@@ -1074,6 +1112,19 @@ var LABEL_MARGIN = 2.0;
return self;
}
/*
Ends the observation of the item when the toolbar view discards the receiver.
@ignore
*/
- (void)_stopObservingToolbarItem
{
var index = 0,
count = [_CPToolbarItemViewObservedKeyPaths count];
for (; index < count; ++index)
[_toolbarItem removeObserver:self forKeyPath:_CPToolbarItemViewObservedKeyPaths[index]];
}
- (void)FIXME_setIsHUD:(BOOL)shouldBeHUD
{
_FIXME_isHUD = shouldBeHUD;
+227
View File
@@ -0,0 +1,227 @@
/*
* CPToolbarTest.j
* AppKit
*
* Created by David Richardson on 2026-11-16.
* 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/AppKit.j>
var ToolbarTestItemIdentifier = @"ToolbarTestItem";
/*
Makes a toolbar item whose view is a button with the given target.
*/
var ToolbarTestMakeViewItem = function(aTarget)
{
var button = [[CPButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 60.0, 24.0)],
item = [[CPToolbarItem alloc] initWithItemIdentifier:ToolbarTestItemIdentifier];
[button setTitle:@"Test"];
[button setTarget:aTarget];
[button setAction:@selector(toolbarTestAction:)];
[item setLabel:@"Test"];
[item setView:button];
[item setMinSize:CGSizeMake(60.0, 24.0)];
[item setMaxSize:CGSizeMake(60.0, 24.0)];
return item;
};
/*
A toolbar delegate that gives view items. It gives a new item for each call, as AppKit expects,
or one cached item for every call when cachesItems is YES.
*/
@implementation _ToolbarTestDelegate : CPObject
{
CPArray itemIdentifiers @accessors;
BOOL cachesItems @accessors;
CPToolbarItem cachedItem @accessors(readonly);
CPArray givenItems @accessors(readonly);
}
- (id)init
{
if (self = [super init])
{
itemIdentifiers = [ToolbarTestItemIdentifier];
cachesItems = NO;
givenItems = [];
}
return self;
}
- (CPArray)toolbarDefaultItemIdentifiers:(CPToolbar)aToolbar
{
return itemIdentifiers;
}
- (CPArray)toolbarAllowedItemIdentifiers:(CPToolbar)aToolbar
{
return itemIdentifiers;
}
- (CPToolbarItem)toolbar:(CPToolbar)aToolbar itemForItemIdentifier:(CPString)anIdentifier willBeInsertedIntoToolbar:(BOOL)aFlag
{
if (cachesItems && cachedItem)
return cachedItem;
var item = ToolbarTestMakeViewItem(self);
[givenItems addObject:item];
if (cachesItems)
cachedItem = item;
return item;
}
- (void)toolbarTestAction:(id)aSender
{
}
@end
@implementation CPToolbarTest : OJTestCase
- (void)testTheItemTheDelegateGivesForInsertionIsInserted
{
var delegate = [_ToolbarTestDelegate new],
toolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestInsertion"];
[toolbar setDelegate:delegate];
var givenItem = [delegate givenItems][0],
insertedItem = [toolbar items][0];
[self assert:givenItem same:insertedItem message:@"the toolbar must insert the item that the delegate gives"];
[self assert:[givenItem view] same:[insertedItem view] message:@"the toolbar must show the view that the application made"];
[self assert:toolbar same:[insertedItem toolbar]];
}
- (void)testTheViewOfAnInsertedItemKeepsItsTarget
{
var delegate = [_ToolbarTestDelegate new],
toolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestTarget"];
[toolbar setDelegate:delegate];
[self assert:delegate same:[[[toolbar items][0] view] target] message:@"the control in a view item must keep its target"];
}
/*
nib2cib gives a decoded toolbar its items in these two instance variables. An outlet in the
same cib reaches the decoded item and its view, so the toolbar must show those.
*/
- (void)testAnItemDecodedWithTheToolbarIsInsertedAsDecoded
{
var sourceToolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestDecoded"],
sourceItem = ToolbarTestMakeViewItem(nil);
sourceToolbar._identifiedItems = @{ ToolbarTestItemIdentifier: sourceItem };
sourceToolbar._defaultItems = [sourceItem];
var toolbar = [CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:sourceToolbar]];
// The decoded toolbar loads its items at the end of the run loop.
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
var decodedItem = [toolbar._identifiedItems objectForKey:ToolbarTestItemIdentifier],
insertedItem = [toolbar items][0];
[self assert:decodedItem same:insertedItem message:@"the toolbar must insert the decoded item"];
[self assert:[decodedItem view] same:[insertedItem view] message:@"the toolbar must show the decoded view"];
}
- (void)testAnItemGivenForTwoPlacesIsCopiedForTheSecond
{
var delegate = [_ToolbarTestDelegate new],
toolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestTwoPlaces"];
[delegate setCachesItems:YES];
[delegate setItemIdentifiers:[ToolbarTestItemIdentifier, ToolbarTestItemIdentifier]];
[toolbar setDelegate:delegate];
var cachedItem = [delegate cachedItem],
items = [toolbar items];
[self assert:cachedItem same:items[0] message:@"the first place must hold the item that the delegate gives"];
[self assertFalse:cachedItem === items[1] message:@"the second place must hold a copy"];
[self assertFalse:[cachedItem view] === [items[1] view] message:@"the copy must have a view of its own"];
[self assert:toolbar same:[items[1] toolbar]];
}
- (void)testAnItemShownByAnotherToolbarIsCopied
{
var delegate = [_ToolbarTestDelegate new],
firstToolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestShared"],
secondToolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestShared"];
[delegate setCachesItems:YES];
[firstToolbar setDelegate:delegate];
[secondToolbar setDelegate:delegate];
var cachedItem = [delegate cachedItem];
[self assert:cachedItem same:[firstToolbar items][0] message:@"the first toolbar must keep the item"];
[self assert:firstToolbar same:[cachedItem toolbar]];
[self assertFalse:cachedItem === [secondToolbar items][0] message:@"the second toolbar must show a copy"];
[self assert:secondToolbar same:[[secondToolbar items][0] toolbar]];
}
- (void)testAReloadKeepsTheItemThatTheToolbarShows
{
var delegate = [_ToolbarTestDelegate new],
toolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestReload"];
[delegate setCachesItems:YES];
[toolbar setDelegate:delegate];
// A change of display mode reloads the items.
[toolbar setDisplayMode:CPToolbarDisplayModeIconOnly];
[self assert:[delegate cachedItem] same:[toolbar items][0] message:@"a reload must not copy an item that the toolbar shows"];
}
- (void)testAnItemViewThatAReloadDiscardsNoLongerFollowsItsItem
{
var delegate = [_ToolbarTestDelegate new],
toolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestItemViews"];
[delegate setCachesItems:YES];
[toolbar setDelegate:delegate];
var toolbarView = [toolbar _toolbarView],
item = [toolbar items][0],
discardedItemView = [toolbarView viewForItem:item];
[toolbar setDisplayMode:CPToolbarDisplayModeIconOnly];
var currentItemView = [toolbarView viewForItem:item];
[self assertFalse:discardedItemView === currentItemView message:@"a reload must make new item views"];
[item setEnabled:NO];
[self assertFalse:[currentItemView isEnabled] message:@"the current item view must follow its item"];
[self assertTrue:[discardedItemView isEnabled] message:@"a discarded item view must no longer follow its item"];
}
@end