CPToolbar: Implement configuration dictionary, item mutation, and autosave

CPToolbar previously archived allowsUserCustomization but lacked
accessors, item insertion and removal methods, and configuration
persistence, preventing runtime modification or saving of toolbar state.

CPToolbar now implements AppKit's customization and autosave accessors,
configurationDictionary (using standard AppKit keys), and item insertion
and removal methods that post will-add and did-remove notifications and
notify the delegate. When autosave is enabled, the toolbar persists its
configuration to user defaults on each change and restores any saved
configuration when autosave is enabled or when a toolbar with autosave
enabled is decoded, omitting identifiers that no longer resolve to an
item. Toolbars sharing an identifier synchronize their items, display
mode, and size mode.

This also fixes a bug where the delegate's toolbarWillAddItem: and
toolbarDidRemoveItem: methods were never invoked because the guard
performed a bitwise AND between _delegate and the method flag—always
evaluating to zero—rather than checking the _implementedDelegateMethods
bitmask recorded by -setDelegate:.

SmartFoldersDemo enables toolbar customization and autosave and expands
its allowed item identifiers to exercise the customization palette.
This commit is contained in:
David Richardson committed 2026-11-19 13:35:34 -06:00
1 parent d1c821b25d
commit f72cdb25dc
9 files changed
+600 -25

No files matched your search

+320 -24
View File
@@ -65,9 +65,32 @@ CPToolbarSizeModeDefault = 0;
CPToolbarSizeModeRegular = 1;
CPToolbarSizeModeSmall = 2;
/*!
Posted before an item is added to a toolbar. The notification's object is the toolbar, and
its user info holds the item under the key \c "item".
*/
CPToolbarWillAddItemNotification = @"CPToolbarWillAddItemNotification";
/*!
Posted after an item is removed from a toolbar. The notification's object is the toolbar,
and its user info holds the item under the key \c "item".
*/
CPToolbarDidRemoveItemNotification = @"CPToolbarDidRemoveItemNotification";
var CPToolbarsByIdentifier = nil,
CPToolbarConfigurationsByIdentifier = nil;
/*
The keys of a configuration dictionary (see -configurationDictionary), as AppKit names them,
and the prefix of the user defaults key under which an autosaving toolbar keeps its
configuration.
*/
var CPToolbarConfigurationDisplayModeKey = @"TB Display Mode",
CPToolbarConfigurationSizeModeKey = @"TB Size Mode",
CPToolbarConfigurationIsShownKey = @"TB Is Shown",
CPToolbarConfigurationItemIdentifiersKey = @"TB Item Identifiers",
CPToolbarConfigurationDefaultsKeyPrefix = @"CPToolbar Configuration ";
@protocol CPToolbarDelegate <CPObject>
@@ -116,6 +139,7 @@ var CPToolbarsByIdentifier = nil,
CPToolbarDisplayMode _displayMode @accessors(property=displayMode);
BOOL _showsBaselineSeparator;
BOOL _allowsUserCustomization;
BOOL _autosavesConfiguration;
BOOL _isVisible;
CPToolbarSizeMode _sizeMode @accessors(property=sizeMode);
int _desiredHeight;
@@ -124,6 +148,7 @@ var CPToolbarsByIdentifier = nil,
unsigned _implementedDelegateMethods;
CPArray _itemIdentifiers;
CPArray _configurationItemIdentifiers;
CPDictionary _identifiedItems;
CPArray _defaultItems;
@@ -226,6 +251,7 @@ var CPToolbarsByIdentifier = nil,
_isVisible = aFlag;
[_window _noteToolbarChanged];
[self _autosaveConfiguration];
}
- (void)setSizeMode:(CPToolbarSizeMode)aSize
@@ -236,6 +262,9 @@ var CPToolbarsByIdentifier = nil,
[[self _toolbarView] setFrame:[self _toolbarViewFrame]];
[_window _noteToolbarChanged];
[[self _toolbarsSharingConfiguration] makeObjectsPerformSelector:@selector(setSizeMode:) withObject:aSize];
[self _autosaveConfiguration];
}
- (CPWindow)_window
@@ -294,12 +323,237 @@ var CPToolbarsByIdentifier = nil,
_displayMode = aDisplayMode;
[self _reloadToolbarItems];
[[self _toolbarsSharingConfiguration] makeObjectsPerformSelector:@selector(setDisplayMode:) withObject:aDisplayMode];
[self _autosaveConfiguration];
}
// MARK: Customization and configuration
/*!
Returns \c YES if the user may customize the toolbar.
*/
- (BOOL)allowsUserCustomization
{
return _allowsUserCustomization;
}
/*!
Sets whether the user may customize the toolbar: add, remove and rearrange its items.
@param aFlag \c YES allows customization
*/
- (void)setAllowsUserCustomization:(BOOL)aFlag
{
_allowsUserCustomization = !!aFlag;
}
/*!
Returns \c YES if the toolbar saves its configuration in the user defaults.
*/
- (BOOL)autosavesConfiguration
{
return _autosavesConfiguration;
}
/*!
Sets whether the toolbar saves its configuration in the user defaults whenever the
configuration changes, under a key made from its identifier. When the flag becomes \c YES,
the toolbar takes a configuration saved earlier, if there is one. A toolbar without an
identifier saves nothing.
@param aFlag \c YES saves the configuration
*/
- (void)setAutosavesConfiguration:(BOOL)aFlag
{
aFlag = !!aFlag;
if (_autosavesConfiguration === aFlag)
return;
_autosavesConfiguration = aFlag;
if (_autosavesConfiguration)
[self _loadConfiguration];
}
/*!
Returns the toolbar's configuration: its display mode, size mode, visibility and item
identifiers, under the keys that AppKit uses. The identifiers are those of the configuration
the toolbar took, also one for which no item could be made yet, such as an item that the
delegate gives once it is set.
*/
- (CPDictionary)configurationDictionary
{
return @{
CPToolbarConfigurationDisplayModeKey: _displayMode || CPToolbarDisplayModeDefault,
CPToolbarConfigurationSizeModeKey: _sizeMode || CPToolbarSizeModeDefault,
CPToolbarConfigurationIsShownKey: _isVisible ? 1 : 0,
CPToolbarConfigurationItemIdentifiersKey: [_configurationItemIdentifiers || _itemIdentifiers || [] copy]
};
}
/*!
Sets the toolbar's configuration from a dictionary that -configurationDictionary returned.
A key that the dictionary does not hold leaves that part of the configuration as it is.
@param aDictionary the configuration
*/
- (void)setConfigurationFromDictionary:(CPDictionary)aDictionary
{
var displayMode = [aDictionary objectForKey:CPToolbarConfigurationDisplayModeKey],
sizeMode = [aDictionary objectForKey:CPToolbarConfigurationSizeModeKey],
isShown = [aDictionary objectForKey:CPToolbarConfigurationIsShownKey],
itemIdentifiers = [aDictionary objectForKey:CPToolbarConfigurationItemIdentifiersKey];
if (displayMode != nil)
_displayMode = displayMode;
if (sizeMode != nil)
{
_sizeMode = sizeMode;
[_toolbarView setFrame:[self _toolbarViewFrame]];
}
if (isShown != nil)
_isVisible = !!isShown;
if (itemIdentifiers)
_configurationItemIdentifiers = [itemIdentifiers copy];
[self _reloadToolbarItems];
[_window _noteToolbarChanged];
[self _autosaveConfiguration];
}
/*!
Inserts the item with the identifier at the index. The toolbar makes the item as it makes
its default items, and posts \c CPToolbarWillAddItemNotification before it adds the item.
Every other toolbar with the same identifier makes the same change.
@param anIdentifier the identifier of the item
@param anIndex the index at which to insert the item
*/
- (void)insertItemWithItemIdentifier:(CPString)anIdentifier atIndex:(CPInteger)anIndex
{
[self _insertItemWithItemIdentifier:anIdentifier atIndex:anIndex];
[[self _toolbarsSharingConfiguration] enumerateObjectsUsingBlock:function(aToolbar)
{
[aToolbar _insertItemWithItemIdentifier:anIdentifier atIndex:anIndex];
}];
[self _autosaveConfiguration];
}
/*!
Removes the item at the index, and posts \c CPToolbarDidRemoveItemNotification after it
removes the item. Every other toolbar with the same identifier makes the same change.
@param anIndex the index of the item
*/
- (void)removeItemAtIndex:(CPInteger)anIndex
{
[self _removeItemAtIndex:anIndex];
[[self _toolbarsSharingConfiguration] makeObjectsPerformSelector:@selector(_removeItemAtIndex:) withObject:anIndex];
[self _autosaveConfiguration];
}
/* @ignore */
- (void)_insertItemWithItemIdentifier:(CPString)anIdentifier atIndex:(CPInteger)anIndex
{
var item = [self _itemForInsertionWithItemIdentifier:anIdentifier];
if (!item)
[CPException raise:CPInvalidArgumentException
reason:@"Toolbar delegate " + _delegate + " returned nil toolbar item for identifier \"" + anIdentifier + "\""];
anIndex = MAX(0, MIN(anIndex, [_items count]));
var notification = [CPNotification notificationWithName:CPToolbarWillAddItemNotification object:self userInfo:@{ @"item": item }];
[self _sendDelegateToolbarWillAddItem:notification];
[[CPNotificationCenter defaultCenter] postNotification:notification];
[_items insertObject:item atIndex:anIndex];
_itemIdentifiers = [_items valueForKey:@"itemIdentifier"];
_configurationItemIdentifiers = [_itemIdentifiers copy];
[self _noteItemsChanged];
}
/* @ignore */
- (void)_removeItemAtIndex:(CPInteger)anIndex
{
if (anIndex < 0 || anIndex >= [_items count])
[CPException raise:CPRangeException reason:@"Index " + anIndex + " is beyond the toolbar's " + [_items count] + " items"];
var item = _items[anIndex];
[_items removeObjectAtIndex:anIndex];
_itemIdentifiers = [_items valueForKey:@"itemIdentifier"];
_configurationItemIdentifiers = [_itemIdentifiers copy];
[self _noteItemsChanged];
var notification = [CPNotification notificationWithName:CPToolbarDidRemoveItemNotification object:self userInfo:@{ @"item": item }];
[self _sendDelegateToolbarDidRemoveItem:notification];
[[CPNotificationCenter defaultCenter] postNotification:notification];
}
/* @ignore */
- (void)_noteItemsChanged
{
_itemsSortedByVisibilityPriority = [_items sortedArrayUsingFunction:_CPToolbarItemVisibilityPriorityCompare context:NULL];
[_toolbarView reloadToolbarItems];
}
/*
Returns the other toolbars that share the receiver's configuration: those with the same
identifier. Toolbars without an identifier share nothing.
@ignore
*/
- (CPArray)_toolbarsSharingConfiguration
{
if (![_identifier length])
return [];
return ([CPToolbarsByIdentifier objectForKey:_identifier] || []).filter(function(aToolbar)
{
return aToolbar !== self;
});
}
/* @ignore */
- (CPString)_configurationDefaultsKey
{
return CPToolbarConfigurationDefaultsKeyPrefix + _identifier;
}
/*
Takes the configuration saved in the user defaults, if there is one.
@ignore
*/
- (void)_loadConfiguration
{
if (![_identifier length])
return;
var configuration = [[CPUserDefaults standardUserDefaults] objectForKey:[self _configurationDefaultsKey]];
if (configuration)
[self setConfigurationFromDictionary:configuration];
}
/*
Saves the configuration in the user defaults when the toolbar autosaves.
@ignore
*/
- (void)_autosaveConfiguration
{
if (!_autosavesConfiguration || ![_identifier length])
return;
[[CPUserDefaults standardUserDefaults] setObject:[self configurationDictionary] forKey:[self _configurationDefaultsKey]];
}
- (CGRect)_toolbarViewFrame
@@ -333,7 +587,17 @@ var CPToolbarsByIdentifier = nil,
// _defaultItems may have been loaded from Cib
_itemIdentifiers = [_defaultItems valueForKey:@"itemIdentifier"] || [];
if ([self _delegateRespondsToToolbarDefaultItemIdentifiers])
/*
A configuration that the toolbar took, or that the user made by adding and removing
items, gives the items in place of the default ones. An identifier for which no item can
be made, such as one that a saved configuration still holds after the application
dropped it, is left out.
*/
var isConfigured = !!_configurationItemIdentifiers;
if (isConfigured)
_itemIdentifiers = [_configurationItemIdentifiers copy];
else if ([self _delegateRespondsToToolbarDefaultItemIdentifiers])
{
var itemIdentifiersFromDelegate = [self _sendDelegateToolbarDefaultItemIdentifiers];
@@ -353,35 +617,22 @@ var CPToolbarsByIdentifier = nil,
for (; index < count; ++index)
{
var identifier = _itemIdentifiers[index],
item = [CPToolbarItem _standardItemWithItemIdentifier:identifier];
// May come from a Cib.
if (!item)
item = [_identifiedItems objectForKey:identifier];
if (!item && _delegate)
item = [self _sendDelegateItemForItemIdentifier:identifier willBeInsertedIntoToolbar:YES];
item = [self _itemForInsertionWithItemIdentifier:identifier];
if (item == nil)
{
if (isConfigured)
continue;
[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];
}
_itemIdentifiers = [_items valueForKey:@"itemIdentifier"];
// _items = [[self _defaultToolbarItems] mutableCopy];
// Store items sorted by priority. We want items to be removed first at the end of the array,
@@ -392,6 +643,41 @@ var CPToolbarsByIdentifier = nil,
[_toolbarView reloadToolbarItems];
}
/*
Returns the item to insert for the identifier: a standard item, an item decoded from a cib,
or the item the delegate gives for insertion; nil when there is none.
@ignore
*/
- (CPToolbarItem)_itemForInsertionWithItemIdentifier:(CPString)identifier
{
var item = [CPToolbarItem _standardItemWithItemIdentifier:identifier];
// May come from a Cib.
if (!item)
item = [_identifiedItems objectForKey:identifier];
if (!item && _delegate)
item = [self _sendDelegateItemForItemIdentifier:identifier willBeInsertedIntoToolbar:YES];
if (!item)
return nil;
/*
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;
return item;
}
/*!
Returns all the items in this toolbar.
*/
@@ -544,6 +830,7 @@ var CPToolbarIdentifierKey = @"CPToolbarIdentifierKey",
CPToolbarDisplayModeKey = @"CPToolbarDisplayModeKey",
CPToolbarShowsBaselineSeparatorKey = @"CPToolbarShowsBaselineSeparatorKey",
CPToolbarAllowsUserCustomizationKey = @"CPToolbarAllowsUserCustomizationKey",
CPToolbarAutosavesConfigurationKey = @"CPToolbarAutosavesConfigurationKey",
CPToolbarIsVisibleKey = @"CPToolbarIsVisibleKey",
CPToolbarDelegateKey = @"CPToolbarDelegateKey",
CPToolbarIdentifiedItemsKey = @"CPToolbarIdentifiedItemsKey",
@@ -570,6 +857,7 @@ var CPToolbarIdentifierKey = @"CPToolbarIdentifierKey",
_allowsUserCustomization = [aCoder decodeBoolForKey:CPToolbarAllowsUserCustomizationKey];
_isVisible = [aCoder decodeBoolForKey:CPToolbarIsVisibleKey];
_sizeMode = [aCoder decodeIntForKey:CPToolbarSizeModeKey];
_autosavesConfiguration = [aCoder decodeBoolForKey:CPToolbarAutosavesConfigurationKey];
_identifiedItems = [aCoder decodeObjectForKey:CPToolbarIdentifiedItemsKey];
_defaultItems = [aCoder decodeObjectForKey:CPToolbarDefaultItemsKey];
@@ -582,6 +870,9 @@ var CPToolbarIdentifierKey = @"CPToolbarIdentifierKey",
[CPToolbar _addToolbar:self forIdentifier:_identifier];
if (_autosavesConfiguration)
[self _loadConfiguration];
// This won't come from a Cib, but can come from manual encoding.
[self setDelegate:[aCoder decodeObjectForKey:CPToolbarDelegateKey]];
@@ -615,6 +906,7 @@ var CPToolbarIdentifierKey = @"CPToolbarIdentifierKey",
[aCoder encodeInt:_displayMode forKey:CPToolbarDisplayModeKey];
[aCoder encodeBool:_showsBaselineSeparator forKey:CPToolbarShowsBaselineSeparatorKey];
[aCoder encodeBool:_allowsUserCustomization forKey:CPToolbarAllowsUserCustomizationKey];
[aCoder encodeBool:_autosavesConfiguration forKey:CPToolbarAutosavesConfigurationKey];
[aCoder encodeBool:_isVisible forKey:CPToolbarIsVisibleKey];
[aCoder encodeInt:_sizeMode forKey:CPToolbarSizeModeKey];
@@ -1389,7 +1681,11 @@ var LABEL_MARGIN = 2.0;
*/
- (void)_sendDelegateToolbarDidRemoveItem:(CPNotification)notification
{
if (!(_delegate & CPToolbarDelegate_toolbarDidRemoveItem_))
/*
The test read the delegate object instead of the flags of the methods it implements, so
the delegate never received the notification.
*/
if (!(_implementedDelegateMethods & CPToolbarDelegate_toolbarDidRemoveItem_))
return;
[_delegate toolbarDidRemoveItem:notification];
@@ -1413,7 +1709,7 @@ var LABEL_MARGIN = 2.0;
*/
- (void)_sendDelegateToolbarWillAddItem:(CPNotification)notification
{
if (!(_delegate & CPToolbarDelegate_toolbarWillAddItem_))
if (!(_implementedDelegateMethods & CPToolbarDelegate_toolbarWillAddItem_))
return;
[_delegate toolbarWillAddItem:notification];
+213
View File
@@ -101,6 +101,59 @@ var ToolbarTestMakeViewItem = function(aTarget)
@end
/*
A toolbar delegate that gives standard items only and records the notifications of added and
removed items.
*/
@implementation _ToolbarTestRecordingDelegate : CPObject
{
CPArray itemIdentifiers @accessors;
CPArray addedItems @accessors(readonly);
CPArray removedItems @accessors(readonly);
}
- (id)init
{
if (self = [super init])
{
itemIdentifiers = [CPToolbarSpaceItemIdentifier, CPToolbarFlexibleSpaceItemIdentifier];
addedItems = [];
removedItems = [];
}
return self;
}
- (CPArray)toolbarDefaultItemIdentifiers:(CPToolbar)aToolbar
{
return itemIdentifiers;
}
- (CPArray)toolbarAllowedItemIdentifiers:(CPToolbar)aToolbar
{
return itemIdentifiers;
}
- (void)toolbarWillAddItem:(CPNotification)aNotification
{
[addedItems addObject:[[aNotification userInfo] objectForKey:@"item"]];
}
- (void)toolbarDidRemoveItem:(CPNotification)aNotification
{
[removedItems addObject:[[aNotification userInfo] objectForKey:@"item"]];
}
@end
/*
Returns the item identifiers of a toolbar's items.
*/
var ToolbarTestIdentifiersOf = function(aToolbar)
{
return [[aToolbar items] valueForKey:@"itemIdentifier"];
};
@implementation CPToolbarTest : OJTestCase
- (void)testTheItemTheDelegateGivesForInsertionIsInserted
@@ -367,4 +420,164 @@ var ToolbarTestMakeViewItem = function(aTarget)
[self assertNotNull:[[theme attributeWithName:names[i] forClass:[CPImage defaultThemeClass]] values] message:names[i]];
}
// MARK: Customization and configuration
- (void)testCustomizationAndAutosaveAreOffByDefault
{
var toolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestDefaults"];
[self assertFalse:[toolbar allowsUserCustomization] message:@"customization"];
[self assertFalse:[toolbar autosavesConfiguration] message:@"autosave"];
}
- (void)testCustomizationAndAutosaveSurviveArchiving
{
var toolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestArchiving"];
[toolbar setAllowsUserCustomization:YES];
[toolbar setAutosavesConfiguration:YES];
var decoded = [CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:toolbar]];
[self assertTrue:[decoded allowsUserCustomization] message:@"customization"];
[self assertTrue:[decoded autosavesConfiguration] message:@"autosave"];
[[CPUserDefaults standardUserDefaults] removeObjectForKey:@"CPToolbar Configuration CPToolbarTestArchiving"];
}
- (void)testInsertingAnItemPlacesItAtTheIndex
{
var toolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestInsert"];
[toolbar setDelegate:[_ToolbarTestRecordingDelegate new]];
[toolbar insertItemWithItemIdentifier:CPToolbarSeparatorItemIdentifier atIndex:1];
[self assert:[CPToolbarSpaceItemIdentifier, CPToolbarSeparatorItemIdentifier, CPToolbarFlexibleSpaceItemIdentifier] equals:ToolbarTestIdentifiersOf(toolbar)];
[self assert:toolbar same:[[toolbar items][1] toolbar]];
}
- (void)testInsertingAnItemTellsTheDelegateAndPostsANotification
{
var toolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestWillAdd"],
delegate = [_ToolbarTestRecordingDelegate new],
posted = [],
observer = [[CPNotificationCenter defaultCenter] addObserverForName:CPToolbarWillAddItemNotification object:toolbar queue:nil usingBlock:function(aNotification)
{
posted.push(aNotification);
}];
[toolbar setDelegate:delegate];
[toolbar insertItemWithItemIdentifier:CPToolbarSeparatorItemIdentifier atIndex:0];
[[CPNotificationCenter defaultCenter] removeObserver:observer];
[self assert:[[toolbar items][0]] equals:[delegate addedItems] message:@"the delegate"];
[self assert:1 equals:posted.length message:@"the notification"];
[self assert:[toolbar items][0] same:[[posted[0] userInfo] objectForKey:@"item"]];
}
- (void)testRemovingAnItemTellsTheDelegateAndPostsANotification
{
var toolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestDidRemove"],
delegate = [_ToolbarTestRecordingDelegate new],
posted = [],
observer = [[CPNotificationCenter defaultCenter] addObserverForName:CPToolbarDidRemoveItemNotification object:toolbar queue:nil usingBlock:function(aNotification)
{
posted.push(aNotification);
}];
[toolbar setDelegate:delegate];
var removedItem = [toolbar items][0];
[toolbar removeItemAtIndex:0];
[[CPNotificationCenter defaultCenter] removeObserver:observer];
[self assert:[CPToolbarFlexibleSpaceItemIdentifier] equals:ToolbarTestIdentifiersOf(toolbar)];
[self assert:[removedItem] equals:[delegate removedItems] message:@"the delegate"];
[self assert:1 equals:posted.length message:@"the notification"];
}
- (void)testTheConfigurationDictionaryHoldsTheItemsAndTheModes
{
var toolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestConfiguration"];
[toolbar setDelegate:[_ToolbarTestRecordingDelegate new]];
[toolbar setDisplayMode:CPToolbarDisplayModeIconOnly];
var configuration = [toolbar configurationDictionary];
[self assert:[CPToolbarSpaceItemIdentifier, CPToolbarFlexibleSpaceItemIdentifier] equals:[configuration objectForKey:@"TB Item Identifiers"]];
[self assert:CPToolbarDisplayModeIconOnly equals:[configuration objectForKey:@"TB Display Mode"]];
[self assert:1 equals:[configuration objectForKey:@"TB Is Shown"]];
}
- (void)testAConfigurationTakesThePlaceOfTheDefaultItems
{
var toolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestSetConfiguration"];
[toolbar setDelegate:[_ToolbarTestRecordingDelegate new]];
[toolbar setConfigurationFromDictionary:@{ @"TB Item Identifiers": [CPToolbarSeparatorItemIdentifier, CPToolbarSpaceItemIdentifier] }];
[self assert:[CPToolbarSeparatorItemIdentifier, CPToolbarSpaceItemIdentifier] equals:ToolbarTestIdentifiersOf(toolbar)];
}
/*
A saved configuration can hold an identifier that the application no longer gives. The
toolbar leaves it out instead of raising.
*/
- (void)testAnIdentifierWithoutAnItemIsLeftOutOfAConfiguration
{
var toolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestUnknownIdentifier"];
[toolbar setDelegate:[_ToolbarTestRecordingDelegate new]];
[toolbar setConfigurationFromDictionary:@{ @"TB Item Identifiers": [@"CPToolbarTestNoSuchItem", CPToolbarSpaceItemIdentifier] }];
[self assert:[CPToolbarSpaceItemIdentifier] equals:ToolbarTestIdentifiersOf(toolbar)];
}
- (void)testAnAutosavingToolbarSavesAChangeAndANewToolbarTakesIt
{
var identifier = @"CPToolbarTestAutosave",
defaultsKey = @"CPToolbar Configuration " + identifier,
toolbar = [[CPToolbar alloc] initWithIdentifier:identifier];
[[CPUserDefaults standardUserDefaults] removeObjectForKey:defaultsKey];
[toolbar setDelegate:[_ToolbarTestRecordingDelegate new]];
[toolbar setAutosavesConfiguration:YES];
[toolbar removeItemAtIndex:0];
var saved = [[CPUserDefaults standardUserDefaults] objectForKey:defaultsKey];
[self assert:[CPToolbarFlexibleSpaceItemIdentifier] equals:[saved objectForKey:@"TB Item Identifiers"] message:@"the saved configuration"];
var laterToolbar = [[CPToolbar alloc] initWithIdentifier:identifier + @"Later"];
[[CPUserDefaults standardUserDefaults] setObject:saved forKey:defaultsKey + @"Later"];
[laterToolbar setDelegate:[_ToolbarTestRecordingDelegate new]];
[laterToolbar setAutosavesConfiguration:YES];
[self assert:[CPToolbarFlexibleSpaceItemIdentifier] equals:ToolbarTestIdentifiersOf(laterToolbar) message:@"the toolbar that takes it"];
[[CPUserDefaults standardUserDefaults] removeObjectForKey:defaultsKey];
[[CPUserDefaults standardUserDefaults] removeObjectForKey:defaultsKey + @"Later"];
}
- (void)testAToolbarWithTheSameIdentifierFollowsAChange
{
var first = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestShared"],
second = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestShared"];
[first setDelegate:[_ToolbarTestRecordingDelegate new]];
[second setDelegate:[_ToolbarTestRecordingDelegate new]];
[first insertItemWithItemIdentifier:CPToolbarSeparatorItemIdentifier atIndex:2];
[first setDisplayMode:CPToolbarDisplayModeLabelOnly];
[self assert:ToolbarTestIdentifiersOf(first) equals:ToolbarTestIdentifiersOf(second) message:@"the items"];
[self assert:CPToolbarDisplayModeLabelOnly equals:[second displayMode] message:@"the display mode"];
}
@end
@@ -29,6 +29,8 @@ var ToolbarSidebarIdentifier = @"SidebarItem",
ToolbarEditIdentifier = @"EditFolderItem",
ToolbarDetailIdentifier = @"DetailItem",
ToolbarSearchIdentifier = @"SearchItem",
ToolbarNewPersonIdentifier = @"NewPersonItem",
ToolbarAllPeopleIdentifier = @"ShowAllPeopleItem",
ToolbarItemImageSize = CGSizeMake(32.0, 32.0),
SidebarDefaultWidth = 230.0,
SidebarMinWidth = 160.0,
@@ -120,6 +122,8 @@ var ToolbarSidebarIdentifier = @"SidebarItem",
[_searchField setSearchMenuTemplate:[_searchField defaultSearchMenuTemplate]];
var toolbar = [[CPToolbar alloc] initWithIdentifier:@"SmartFoldersDemoToolbar"];
[toolbar setAllowsUserCustomization:YES];
[toolbar setAutosavesConfiguration:YES];
[toolbar setDelegate:self];
[toolbar setVisible:YES];
[[self window] setToolbar:toolbar];
@@ -128,7 +132,9 @@ var ToolbarSidebarIdentifier = @"SidebarItem",
- (CPArray)toolbarAllowedItemIdentifiers:(CPToolbar)aToolbar
{
return [ToolbarSidebarIdentifier, ToolbarNewFolderIdentifier, ToolbarNewGroupIdentifier, ToolbarEditIdentifier,
ToolbarDetailIdentifier, ToolbarSearchIdentifier, CPToolbarShowColorsItemIdentifier,
ToolbarNewPersonIdentifier, ToolbarAllPeopleIdentifier, ToolbarDetailIdentifier, ToolbarSearchIdentifier,
CPToolbarShowColorsItemIdentifier, CPToolbarShowFontsItemIdentifier, CPToolbarPrintItemIdentifier,
CPToolbarCustomizeToolbarItemIdentifier,
CPToolbarFlexibleSpaceItemIdentifier, CPToolbarSpaceItemIdentifier, CPToolbarSeparatorItemIdentifier];
}
@@ -162,6 +168,14 @@ var ToolbarSidebarIdentifier = @"SidebarItem",
[self _configureItem:item label:@"Edit" imageName:@"ToolbarEdit" action:@selector(editSelectedSmartFolder:) toolTip:@"Edit the selected smart folder"];
break;
case ToolbarNewPersonIdentifier:
[self _configureItem:item label:@"New Person" imageName:@"ToolbarNewPerson" action:@selector(newPerson:) toolTip:@"Add a person to the library"];
break;
case ToolbarAllPeopleIdentifier:
[self _configureItem:item label:@"All People" imageName:@"ToolbarShowAllPeople" action:@selector(showAllPeople:) toolTip:@"Show all people"];
break;
case ToolbarDetailIdentifier:
[self _configureItem:item label:@"Details" imageName:@"ToolbarDetails" action:@selector(toggleDetail:) toolTip:@"Show or hide the detail pane"];
break;
@@ -0,0 +1,24 @@
{
"images": [
{
"idiom": "universal",
"filename": "toolbarNewPerson.svg",
"scale": "1x"
},
{
"idiom": "universal",
"filename": "toolbarNewPerson-dark.svg",
"scale": "1x",
"appearances": [
{
"appearance": "luminosity",
"value": "dark"
}
]
}
],
"info": {
"author": "xcode",
"version": 1
}
}
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32"><g fill="none" stroke="#ffffff" stroke-opacity="0.85" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><circle cx="13" cy="11" r="4"/><path d="M5 26c0-4.4 3.6-8 8-8s8 3.6 8 8"/><path d="M24 9v8M20 13h8"/></g></svg>

After

Width:  |  Height:  |  Size: 315 B

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32"><g fill="none" stroke="#000000" stroke-opacity="0.85" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><circle cx="13" cy="11" r="4"/><path d="M5 26c0-4.4 3.6-8 8-8s8 3.6 8 8"/><path d="M24 9v8M20 13h8"/></g></svg>

After

Width:  |  Height:  |  Size: 315 B

@@ -0,0 +1,24 @@
{
"images": [
{
"idiom": "universal",
"filename": "toolbarShowAllPeople.svg",
"scale": "1x"
},
{
"idiom": "universal",
"filename": "toolbarShowAllPeople-dark.svg",
"scale": "1x",
"appearances": [
{
"appearance": "luminosity",
"value": "dark"
}
]
}
],
"info": {
"author": "xcode",
"version": 1
}
}
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32"><g fill="none" stroke="#ffffff" stroke-opacity="0.85" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><rect x="5" y="6" width="22" height="20" rx="2.5"/><circle cx="16" cy="13" r="3"/><path d="M10 22c0-3.3 2.7-5.5 6-5.5s6 2.2 6 5.5"/></g></svg>

After

Width:  |  Height:  |  Size: 346 B

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32"><g fill="none" stroke="#000000" stroke-opacity="0.85" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><rect x="5" y="6" width="22" height="20" rx="2.5"/><circle cx="16" cy="13" r="3"/><path d="M10 22c0-3.3 2.7-5.5 6-5.5s6 2.2 6 5.5"/></g></svg>

After

Width:  |  Height:  |  Size: 346 B