mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-11-29 09:52:03 +00:00
CPToolbar: Implement customization palette and fix related toolbar defects
CPToolbar adds -runCustomizationPalette: and -customizationPaletteIsRunning, and CPWindow adds -runToolbarCustomizationPalette:, which validates only when the toolbar allows customization and no palette is active. The palette runs as a sheet attached to the toolbar's window, displaying the allowed items, the default item set, a Show pop-up button for the display mode, and a Use small size checkbox for the size mode. Users can drag items into, within, and out of the toolbar, or drag the default set onto the toolbar to restore it. Dropping an item already present in the toolbar moves that item; only Space, Flexible Space, and Separator may appear multiple times. Custom view items render in the palette as archived copies of their views that ignore mouse events and refuse first responder status. Fixes several defects identified during manual verification: - -[CPWindow sendEvent:] previously discarded all mouse events targeting the toolbar while a sheet was attached, preventing items from being dragged out; events belonging to the toolbar view now pass through. - Attached sheets failed to adjust their vertical position when toolbar height changed; CPWindow now repositions the sheet accordingly. - CPToolbarDisplayModeLabelOnly previously hid only an item's image view, leaving custom item views visible; custom item views are now hidden as well. - The toolbar separator was nearly invisible in the light appearance and now uses the new toolbarSeparatorColor. - Space, Flexible Space, and Separator lacked palette labels. - Small size mode reduced toolbar height without scaling item images; image items now scale to 24 points (down from 32), and item views are rebuilt when the size mode changes. Adds unit tests covering insertion index calculation, palette contents, palette availability, window validation, default set drops, existing item drops, and small size scaling.
This commit is contained in:
1 parent
f72cdb25dc
commit
ee5db81642
11 files changed
+1049
-26
No files matched your search
+143
-21
@@ -27,6 +27,7 @@
|
||||
|
||||
@import "CPPopUpButton.j"
|
||||
@import "CPToolbarItem.j"
|
||||
@import "_CPToolbarCustomizationPalette.j"
|
||||
|
||||
@global CPApp
|
||||
|
||||
@@ -160,6 +161,8 @@ var CPToolbarConfigurationDisplayModeKey = @"TB Display Mode",
|
||||
|
||||
CPView _toolbarView;
|
||||
CPWindow _window;
|
||||
|
||||
_CPToolbarCustomizationPalette _customizationPalette;
|
||||
}
|
||||
|
||||
/* @ignore */
|
||||
@@ -261,6 +264,12 @@ var CPToolbarConfigurationDisplayModeKey = @"TB Display Mode",
|
||||
_sizeMode = aSize;
|
||||
|
||||
[[self _toolbarView] setFrame:[self _toolbarViewFrame]];
|
||||
|
||||
/*
|
||||
The item views size their images when they are made, so they are made again for the
|
||||
new size mode.
|
||||
*/
|
||||
[_toolbarView reloadToolbarItems];
|
||||
[_window _noteToolbarChanged];
|
||||
|
||||
[[self _toolbarsSharingConfiguration] makeObjectsPerformSelector:@selector(setSizeMode:) withObject:aSize];
|
||||
@@ -584,9 +593,6 @@ var CPToolbarConfigurationDisplayModeKey = @"TB Display Mode",
|
||||
// toolbar delegate is optional. Toolbar items can be combined from
|
||||
// both IB and a delegate (see Apple's NSToolbar guide for IB, for more details).
|
||||
|
||||
// _defaultItems may have been loaded from Cib
|
||||
_itemIdentifiers = [_defaultItems valueForKey:@"itemIdentifier"] || [];
|
||||
|
||||
/*
|
||||
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
|
||||
@@ -595,19 +601,7 @@ var CPToolbarConfigurationDisplayModeKey = @"TB Display Mode",
|
||||
*/
|
||||
var isConfigured = !!_configurationItemIdentifiers;
|
||||
|
||||
if (isConfigured)
|
||||
_itemIdentifiers = [_configurationItemIdentifiers copy];
|
||||
else if ([self _delegateRespondsToToolbarDefaultItemIdentifiers])
|
||||
{
|
||||
var itemIdentifiersFromDelegate = [self _sendDelegateToolbarDefaultItemIdentifiers];
|
||||
|
||||
// If we get items both from the Cib and from the delegate method, put the delegate items before the
|
||||
// Cib ones.
|
||||
if (itemIdentifiersFromDelegate)
|
||||
_itemIdentifiers = [itemIdentifiersFromDelegate arrayByAddingObjectsFromArray:_itemIdentifiers];
|
||||
}
|
||||
// If we didn't load from a cib and the delegate hasn't been set yet, we'll just work with an empty list
|
||||
// at this point.
|
||||
_itemIdentifiers = isConfigured ? [_configurationItemIdentifiers copy] : [self _defaultItemIdentifiers];
|
||||
|
||||
var index = 0,
|
||||
count = [_itemIdentifiers count];
|
||||
@@ -643,6 +637,90 @@ var CPToolbarConfigurationDisplayModeKey = @"TB Display Mode",
|
||||
[_toolbarView reloadToolbarItems];
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the identifiers of the default items: those that the delegate gives, then those of
|
||||
the default items decoded from a cib. Without either, the list is empty.
|
||||
@ignore
|
||||
*/
|
||||
- (CPArray)_defaultItemIdentifiers
|
||||
{
|
||||
// _defaultItems may have been loaded from Cib
|
||||
var identifiers = [_defaultItems valueForKey:@"itemIdentifier"] || [];
|
||||
|
||||
if ([self _delegateRespondsToToolbarDefaultItemIdentifiers])
|
||||
{
|
||||
var itemIdentifiersFromDelegate = [self _sendDelegateToolbarDefaultItemIdentifiers];
|
||||
|
||||
// If we get items both from the Cib and from the delegate method, put the delegate items before the
|
||||
// Cib ones.
|
||||
if (itemIdentifiersFromDelegate)
|
||||
identifiers = [itemIdentifiersFromDelegate arrayByAddingObjectsFromArray:identifiers];
|
||||
}
|
||||
|
||||
return identifiers;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the identifiers of the items the user may add: those that the delegate allows, then
|
||||
those of the allowed items decoded from a cib, each once.
|
||||
@ignore
|
||||
*/
|
||||
- (CPArray)_allowedItemIdentifiers
|
||||
{
|
||||
var identifiers = [[self _sendDelegateToolbarAllowedItemIdentifiers] || [] copy],
|
||||
decoded = [_allowedItems valueForKey:@"itemIdentifier"] || [];
|
||||
|
||||
for (var i = 0; i < decoded.length; i++)
|
||||
if ([identifiers indexOfObject:decoded[i]] === CPNotFound)
|
||||
[identifiers addObject:decoded[i]];
|
||||
|
||||
return identifiers;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns an item to show in the customization palette for the identifier, or nil. The
|
||||
toolbar does not insert it, so the delegate is asked for an item not to be inserted.
|
||||
@ignore
|
||||
*/
|
||||
- (CPToolbarItem)_paletteItemForItemIdentifier:(CPString)anIdentifier
|
||||
{
|
||||
return [CPToolbarItem _standardItemWithItemIdentifier:anIdentifier] ||
|
||||
[_identifiedItems objectForKey:anIdentifier] ||
|
||||
[self _sendDelegateItemForItemIdentifier:anIdentifier willBeInsertedIntoToolbar:NO];
|
||||
}
|
||||
|
||||
/*!
|
||||
Runs the customization palette: a sheet on the toolbar's window from which the user drags
|
||||
items into, within and out of the toolbar. It runs only when the toolbar allows
|
||||
customization, is in a window and has no palette running; it shows a hidden toolbar.
|
||||
@param aSender the object that sent the message
|
||||
*/
|
||||
- (void)runCustomizationPalette:(id)aSender
|
||||
{
|
||||
if (!_allowsUserCustomization || !_window || _customizationPalette)
|
||||
return;
|
||||
|
||||
[self setVisible:YES];
|
||||
|
||||
_customizationPalette = [[_CPToolbarCustomizationPalette alloc] initWithToolbar:self];
|
||||
[_customizationPalette run];
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns \c YES while the toolbar's customization palette runs.
|
||||
*/
|
||||
- (BOOL)customizationPaletteIsRunning
|
||||
{
|
||||
return !!_customizationPalette;
|
||||
}
|
||||
|
||||
/* @ignore */
|
||||
- (void)_customizationPaletteDidEnd
|
||||
{
|
||||
_customizationPalette = nil;
|
||||
[self validateVisibleItems];
|
||||
}
|
||||
|
||||
/*
|
||||
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.
|
||||
@@ -941,6 +1019,8 @@ var _CPToolbarItemInfoMake = function(anIndex, aView, aLabel, aMinWidth)
|
||||
CPDictionary _itemInfos;
|
||||
JSObject _viewsForToolbarItems;
|
||||
|
||||
CPView _customizationOverlay;
|
||||
|
||||
CPArray _visibleItems @accessors(readonly, property=visibleItems);
|
||||
CPArray _invisibleItems;
|
||||
|
||||
@@ -1287,6 +1367,27 @@ var _CPToolbarItemInfoMake = function(anIndex, aView, aLabel, aMinWidth)
|
||||
}
|
||||
|
||||
[self tile];
|
||||
|
||||
if (_customizationOverlay)
|
||||
[self addSubview:_customizationOverlay];
|
||||
}
|
||||
|
||||
/*
|
||||
Lays aView over the items while the customization palette runs, or removes the view laid
|
||||
earlier when aView is nil. A reload of the items keeps the view on top.
|
||||
@ignore
|
||||
*/
|
||||
- (void)_setCustomizationOverlay:(CPView)aView
|
||||
{
|
||||
[_customizationOverlay removeFromSuperview];
|
||||
|
||||
_customizationOverlay = aView;
|
||||
|
||||
if (_customizationOverlay)
|
||||
{
|
||||
[_customizationOverlay setFrame:[self bounds]];
|
||||
[self addSubview:_customizationOverlay];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)layoutSubviews
|
||||
@@ -1314,7 +1415,8 @@ var _CPToolbarItemVisibilityPriorityCompare = function(lhs, rhs)
|
||||
return CPOrderedDescending;
|
||||
};
|
||||
|
||||
var LABEL_MARGIN = 2.0;
|
||||
var LABEL_MARGIN = 2.0,
|
||||
_CPToolbarSmallSizeScale = 24.0 / 32.0;
|
||||
|
||||
@implementation _CPToolbarItemView : CPControl
|
||||
{
|
||||
@@ -1454,8 +1556,8 @@ var LABEL_MARGIN = 2.0;
|
||||
[_imageView setImage:[_toolbarItem image]];
|
||||
}
|
||||
|
||||
var minSize = [_toolbarItem minSize],
|
||||
maxSize = [_toolbarItem maxSize];
|
||||
var minSize = [self _sizeForSizeMode:[_toolbarItem minSize]],
|
||||
maxSize = [self _sizeForSizeMode:[_toolbarItem maxSize]];
|
||||
|
||||
if (_toolbar)
|
||||
[_labelField setFont:[_toolbar valueForThemeAttribute:@"label-font" inState:[self themeState]]];
|
||||
@@ -1470,7 +1572,12 @@ var LABEL_MARGIN = 2.0;
|
||||
var iconOnly = [[_toolbarItem toolbar] displayMode] === CPToolbarDisplayModeIconOnly,
|
||||
labelOnly = [[_toolbarItem toolbar] displayMode] === CPToolbarDisplayModeLabelOnly;
|
||||
[_labelField setHidden:iconOnly];
|
||||
[_view setHidden:labelOnly];
|
||||
|
||||
/*
|
||||
Text Only hid an item's own view but not its image, so an image item kept its image, and
|
||||
an item whose image had no room showed a dot above its label.
|
||||
*/
|
||||
[_view || _imageView setHidden:labelOnly];
|
||||
|
||||
_minSize = CGSizeMake(MAX(_labelSize.width, minSize.width), (labelOnly ? 0 : minSize.height) + (iconOnly ? 0 : _labelSize.height + LABEL_MARGIN));
|
||||
_maxSize = CGSizeMake(MAX(_labelSize.width, maxSize.width), 100000000.0);
|
||||
@@ -1502,7 +1609,7 @@ var LABEL_MARGIN = 2.0;
|
||||
|
||||
// The view is centred in the available space above the label.
|
||||
var view = _view || _imageView,
|
||||
itemMaxSize = [_toolbarItem maxSize],
|
||||
itemMaxSize = [self _sizeForSizeMode:[_toolbarItem maxSize]],
|
||||
iconOnly = [[_toolbarItem toolbar] displayMode] === CPToolbarDisplayModeIconOnly,
|
||||
height = CGRectGetHeight(bounds) - (iconOnly ? 0 : _labelSize.height),
|
||||
viewWidth = MIN(itemMaxSize.width, width),
|
||||
@@ -1519,6 +1626,21 @@ var LABEL_MARGIN = 2.0;
|
||||
[_labelField setFrameOrigin:CGPointMake(ROUND((width - _labelSize.width) / 2.0), CGRectGetHeight(bounds) - _labelSize.height)];
|
||||
}
|
||||
|
||||
/*
|
||||
Returns aSize for the toolbar's size mode. In the small size mode an image item takes AppKit's
|
||||
small toolbar image, 24 points against 32, so its size scales by three quarters. Small size
|
||||
lowered the toolbar but left every image at its full size, so the toolbar looked the same. An
|
||||
item with a view of its own keeps its size.
|
||||
@ignore
|
||||
*/
|
||||
- (CGSize)_sizeForSizeMode:(CGSize)aSize
|
||||
{
|
||||
if ([_toolbarItem view] || [[_toolbarItem toolbar] sizeMode] !== CPToolbarSizeModeSmall)
|
||||
return aSize;
|
||||
|
||||
return CGSizeMake(ROUND(aSize.width * _CPToolbarSmallSizeScale), ROUND(aSize.height * _CPToolbarSmallSizeScale));
|
||||
}
|
||||
|
||||
- (void)mouseDown:(CPEvent)anEvent
|
||||
{
|
||||
if ([_toolbarItem view])
|
||||
|
||||
@@ -2027,7 +2027,7 @@ CPTexturedBackgroundWindowMask
|
||||
if (sheet && (_sheetContext["isClosing"] || _sheetContext["isOpening"]))
|
||||
return;
|
||||
|
||||
if (sheet && _sheetContext["isAttached"])
|
||||
if (sheet && _sheetContext["isAttached"] && ![self _isToolbarCustomizationEvent:anEvent])
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
@@ -3031,6 +3031,26 @@ CPTexturedBackgroundWindowMask
|
||||
[toolbar setVisible:![toolbar isVisible]];
|
||||
}
|
||||
|
||||
/*!
|
||||
Runs the customization palette of the window's toolbar.
|
||||
@param aSender the object that sent the message
|
||||
*/
|
||||
- (void)runToolbarCustomizationPalette:(id)aSender
|
||||
{
|
||||
[[self toolbar] runCustomizationPalette:aSender];
|
||||
}
|
||||
|
||||
/*!
|
||||
Validates a toolbar item whose action the window handles, as -validateUserInterfaceItem:
|
||||
does for a menu item.
|
||||
@param anItem the toolbar item to validate
|
||||
@return \c YES if the item should be enabled
|
||||
*/
|
||||
- (BOOL)validateToolbarItem:(CPToolbarItem)anItem
|
||||
{
|
||||
return [self validateUserInterfaceItem:anItem];
|
||||
}
|
||||
|
||||
/*!
|
||||
Validates the user interface items whose actions the window handles.
|
||||
|
||||
@@ -3039,7 +3059,8 @@ CPTexturedBackgroundWindowMask
|
||||
a toolbar. \c -undo: and \c -redo: are enabled only when the window's undo manager has a
|
||||
change to undo or redo. \c -performMiniaturize: and \c -miniaturize: are enabled only
|
||||
when the window's minimize button is enabled, and \c -performZoom: and \c -zoom: only
|
||||
when its zoom button is enabled. Every other item is enabled.
|
||||
when its zoom button is enabled. \c -runToolbarCustomizationPalette: is enabled only when
|
||||
the window's toolbar allows customization and runs no palette. Every other item is enabled.
|
||||
@param anItem the item to validate
|
||||
@return \c YES if the item should be enabled
|
||||
*/
|
||||
@@ -3063,6 +3084,13 @@ CPTexturedBackgroundWindowMask
|
||||
if (action === @selector(performZoom:) || action === @selector(zoom:))
|
||||
return [self _canZoom];
|
||||
|
||||
if (action === @selector(runToolbarCustomizationPalette:))
|
||||
{
|
||||
var customizedToolbar = [self toolbar];
|
||||
|
||||
return [customizedToolbar allowsUserCustomization] && ![customizedToolbar customizationPaletteIsRunning];
|
||||
}
|
||||
|
||||
if (action !== @selector(toggleToolbarShown:))
|
||||
return YES;
|
||||
|
||||
@@ -3074,6 +3102,29 @@ CPTexturedBackgroundWindowMask
|
||||
return !!toolbar;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns YES for a mouse event over the toolbar while its customization palette runs. The
|
||||
palette is a sheet, and a sheet filtered every mouse event of its window but a mouse down,
|
||||
so the user could drop items on the toolbar but could not drag one out of it or within it.
|
||||
@ignore
|
||||
*/
|
||||
- (BOOL)_isToolbarCustomizationEvent:(CPEvent)anEvent
|
||||
{
|
||||
if (![_toolbar customizationPaletteIsRunning])
|
||||
return NO;
|
||||
|
||||
var type = [anEvent type],
|
||||
toolbarView = [_toolbar _toolbarView];
|
||||
|
||||
if (type === CPLeftMouseDown)
|
||||
return [[_windowView hitTest:[anEvent locationInWindow]] isDescendantOf:toolbarView];
|
||||
|
||||
if (type === CPLeftMouseDragged || type === CPLeftMouseUp)
|
||||
return [_leftMouseDownView isDescendantOf:toolbarView];
|
||||
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (void)_noteToolbarChanged
|
||||
{
|
||||
var frame = CGRectMakeCopy([self frame]),
|
||||
@@ -3091,6 +3142,15 @@ CPTexturedBackgroundWindowMask
|
||||
}
|
||||
|
||||
[self setFrame:newFrame];
|
||||
|
||||
/*
|
||||
A change of the toolbar's height moves the top of the content view, below which an
|
||||
attached sheet hangs. The sheet moved only with the window, so a sheet open while the
|
||||
toolbar changed size, such as the toolbar's customization palette, kept its old place.
|
||||
*/
|
||||
if ([self attachedSheet])
|
||||
[self _setAttachedSheetFrameOrigin];
|
||||
|
||||
/*
|
||||
[_windowView setAnimatingToolbar:YES];
|
||||
[self setFrame:frame];
|
||||
|
||||
@@ -206,6 +206,14 @@ A3ColorScrollerLegacy = A3DefineThemeColor(@"scrollerLegacyColor",
|
||||
A3ColorScrollerBackground = A3DefineThemeColor(@"scrollerBackgroundColor", [CPColor colorWithCSSString:@"rgb(250,250,250)"]);
|
||||
A3ColorScrollerBorder = A3DefineThemeColor(@"scrollerBorderColor", [CPColor colorWithCSSString:@"rgb(232,232,232)"]);
|
||||
|
||||
// Toolbars
|
||||
|
||||
/*
|
||||
The toolbar's separator item took borderLightColor, rgb(230,230,230), on a toolbar of about
|
||||
rgb(236,236,236), so it hardly showed in the light appearance.
|
||||
*/
|
||||
A3ColorToolbarSeparator = A3DefineThemeColor(@"toolbarSeparatorColor", [CPColor colorWithCSSString:@"rgba(0,0,0,0.25)"]);
|
||||
|
||||
// Sliders
|
||||
|
||||
A3ColorSliderTrack = A3DefineThemeColor(@"sliderTrackColor", [CPColor colorWithCSSString:@"rgba(0,0,0,0.20)"]);
|
||||
|
||||
@@ -120,6 +120,10 @@ A3DefineDarkThemeColor(@"scrollerLegacyColor", [CPColor colorWithCSSString:@"rgb
|
||||
A3DefineDarkThemeColor(@"scrollerBackgroundColor", [CPColor colorWithCSSString:@"rgb(35,35,35)"]);
|
||||
A3DefineDarkThemeColor(@"scrollerBorderColor", [CPColor colorWithCSSString:@"rgb(20,20,20)"]);
|
||||
|
||||
// Toolbars
|
||||
|
||||
A3DefineDarkThemeColor(@"toolbarSeparatorColor", [CPColor colorWithCSSString:@"rgba(255,255,255,0.25)"]);
|
||||
|
||||
// Sliders
|
||||
|
||||
/*
|
||||
|
||||
@@ -9453,7 +9453,7 @@ function A3MaskIconOfSize(aMaskImage, aColor, aSize)
|
||||
toolbarExtraItemsAlternateImage = toolbarExtraItemsImage,
|
||||
|
||||
toolbarSeparatorColor = [CPColor colorWithCSSDictionary:@{
|
||||
@"border-left": @[@"1px solid ", A3ColorBorderLight]
|
||||
@"border-left": @[@"1px solid ", A3ColorToolbarSeparator]
|
||||
}],
|
||||
|
||||
themeValues =
|
||||
|
||||
@@ -0,0 +1,673 @@
|
||||
/*
|
||||
* _CPToolbarCustomizationPalette.j
|
||||
* AppKit
|
||||
*
|
||||
* Created by David Richardson on 2026-11-19.
|
||||
* 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 <Foundation/CPKeyedArchiver.j>
|
||||
@import <Foundation/CPKeyedUnarchiver.j>
|
||||
@import <Foundation/CPObject.j>
|
||||
|
||||
@import "CPButton.j"
|
||||
@import "CPCheckBox.j"
|
||||
@import "CPDragServer_Constants.j"
|
||||
@import "CPImageView.j"
|
||||
@import "CPPasteboard.j"
|
||||
@import "CPPopUpButton.j"
|
||||
@import "CPTextField.j"
|
||||
@import "CPView.j"
|
||||
|
||||
@class CPToolbar
|
||||
@class CPToolbarItem
|
||||
|
||||
@global CPApp
|
||||
@global CPToolbarFlexibleSpaceItemIdentifier
|
||||
@global CPToolbarSeparatorItemIdentifier
|
||||
@global CPToolbarSpaceItemIdentifier
|
||||
@global CPToolbarDisplayModeIconAndLabel
|
||||
@global CPToolbarDisplayModeIconOnly
|
||||
@global CPToolbarDisplayModeLabelOnly
|
||||
@global CPToolbarSizeModeRegular
|
||||
@global CPToolbarSizeModeSmall
|
||||
|
||||
/*
|
||||
The pasteboard types of a drag in the customization palette: the identifier of one item, and
|
||||
the identifiers of the default set.
|
||||
*/
|
||||
_CPToolbarItemIdentifierPboardType = @"_CPToolbarItemIdentifierPboardType";
|
||||
_CPToolbarDefaultSetPboardType = @"_CPToolbarDefaultSetPboardType";
|
||||
|
||||
var _CPToolbarPaletteCellSize = CGSizeMake(76.0, 58.0),
|
||||
_CPToolbarPaletteImageSize = CGSizeMake(32.0, 32.0),
|
||||
_CPToolbarPaletteMargin = 16.0,
|
||||
_CPToolbarPaletteWidth = 560.0;
|
||||
|
||||
/*
|
||||
Returns the index at which a drop at x inserts an item, given the horizontal centres of the
|
||||
items in order. The item goes before the first item whose centre lies at or right of x.
|
||||
*/
|
||||
function _CPToolbarInsertionIndex(itemCentres, x)
|
||||
{
|
||||
var index = 0,
|
||||
count = itemCentres.length;
|
||||
|
||||
while (index < count && itemCentres[index] < x)
|
||||
index++;
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
/*
|
||||
The arrows of the flexible space: a line with a head at each end, as a mask that the palette
|
||||
paints in the secondary label colour.
|
||||
*/
|
||||
var _CPToolbarPaletteFlexibleSpaceArrows = "url(\"data:image/svg+xml," + encodeURIComponent(
|
||||
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 12"><path d="M2 6h20M2 6l4-4M2 6l4 4M22 6l-4-4M22 6l-4 4" fill="none" stroke="black" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>') + "\")";
|
||||
|
||||
/*
|
||||
Returns a view of aFrame drawn with a border of aStyle ("dashed" or "solid") in the
|
||||
secondary label colour, with rounded corners.
|
||||
*/
|
||||
function _CPToolbarPaletteOutline(aFrame, aStyle)
|
||||
{
|
||||
var view = [[CPView alloc] initWithFrame:aFrame];
|
||||
|
||||
[view setBackgroundColor:[CPColor colorWithCSSDictionary:@{
|
||||
@"border": @[@"1px " + aStyle + " ", [CPColor secondaryLabelColor]],
|
||||
@"border-radius": @"4px",
|
||||
@"box-sizing": @"border-box"
|
||||
}]];
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns a label for the palette in the label colour.
|
||||
*/
|
||||
function _CPToolbarPaletteLabel(aString, aFrame, anAlignment)
|
||||
{
|
||||
var label = [[CPTextField alloc] initWithFrame:aFrame];
|
||||
|
||||
[label setStringValue:aString];
|
||||
[label setTextColor:[CPColor labelColor]];
|
||||
[label setFont:[CPFont systemFontOfSize:11.0]];
|
||||
[label setAlignment:anAlignment];
|
||||
[label setLineBreakMode:CPLineBreakByTruncatingTail];
|
||||
|
||||
return label;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
The palette's view of one toolbar item, or of the default set: the item's image above its
|
||||
palette label. A mouse drag starts a drag of the item's identifier, or of the default set.
|
||||
@ignore
|
||||
*/
|
||||
/*
|
||||
Makes aView and its subviews refuse to edit or select text. The palette shows a copy of an
|
||||
item's view, and the sheet made the first text field in it the first responder. The copy then
|
||||
showed a text cursor, and a drag in it selected its text instead of dragging the item. The copy
|
||||
keeps its enabled look.
|
||||
*/
|
||||
function _CPToolbarPaletteMakeInert(aView)
|
||||
{
|
||||
if ([aView respondsToSelector:@selector(setEditable:)])
|
||||
[aView setEditable:NO];
|
||||
|
||||
if ([aView respondsToSelector:@selector(setSelectable:)])
|
||||
[aView setSelectable:NO];
|
||||
|
||||
[[aView subviews] enumerateObjectsUsingBlock:function(aSubview)
|
||||
{
|
||||
_CPToolbarPaletteMakeInert(aSubview);
|
||||
}];
|
||||
}
|
||||
|
||||
/*
|
||||
Returns YES when an item with anIdentifier can appear more than once in a toolbar: the
|
||||
space, flexible space and separator items.
|
||||
*/
|
||||
function _CPToolbarItemAllowsDuplicates(anIdentifier)
|
||||
{
|
||||
return anIdentifier === CPToolbarSpaceItemIdentifier ||
|
||||
anIdentifier === CPToolbarFlexibleSpaceItemIdentifier ||
|
||||
anIdentifier === CPToolbarSeparatorItemIdentifier;
|
||||
}
|
||||
|
||||
@implementation _CPToolbarPaletteItemView : CPView
|
||||
{
|
||||
CPString _itemIdentifier @accessors(readonly, property=itemIdentifier);
|
||||
CPArray _defaultSet @accessors(readonly, property=defaultSet);
|
||||
}
|
||||
|
||||
/*
|
||||
Initializes the view for anItem. The image is the item's image; an item without one, such
|
||||
as a space or an item with a view of its own, shows a frame of the item's width.
|
||||
*/
|
||||
- (id)initWithItem:(CPToolbarItem)anItem origin:(CGPoint)anOrigin
|
||||
{
|
||||
self = [super initWithFrame:CGRectMake(anOrigin.x, anOrigin.y, _CPToolbarPaletteCellSize.width, _CPToolbarPaletteCellSize.height)];
|
||||
|
||||
if (self)
|
||||
{
|
||||
_itemIdentifier = [anItem itemIdentifier];
|
||||
|
||||
var image = [anItem image],
|
||||
imageFrame = CGRectMake((_CPToolbarPaletteCellSize.width - _CPToolbarPaletteImageSize.width) / 2.0, 4.0, _CPToolbarPaletteImageSize.width, _CPToolbarPaletteImageSize.height);
|
||||
|
||||
/*
|
||||
An item without an image showed a grey box, so the spaces, the separator and an
|
||||
item with a view of its own, such as a search field, could not be told apart. Each
|
||||
now has a drawing in the manner of AppKit's palette: a dashed square for the space, a
|
||||
wide dashed frame with arrows for the flexible space and a line for the separator. An
|
||||
item with a view shows a copy of its view, as AppKit's palette shows the view, so a
|
||||
search field keeps its magnifying glass. The copy does not take mouse events, and the
|
||||
item's own view stays in the toolbar.
|
||||
*/
|
||||
var identifier = _itemIdentifier,
|
||||
midX = _CPToolbarPaletteCellSize.width / 2.0,
|
||||
midY = 4.0 + _CPToolbarPaletteImageSize.height / 2.0;
|
||||
|
||||
if (identifier === CPToolbarSpaceItemIdentifier)
|
||||
[self addSubview:_CPToolbarPaletteOutline(CGRectMake(midX - 12.0, midY - 12.0, 24.0, 24.0), @"dashed")];
|
||||
else if (identifier === CPToolbarFlexibleSpaceItemIdentifier)
|
||||
{
|
||||
var frame = CGRectMake(6.0, midY - 12.0, _CPToolbarPaletteCellSize.width - 12.0, 24.0),
|
||||
arrows = [[CPImageView alloc] initWithFrame:CGRectInset(frame, 8.0, 6.0)];
|
||||
|
||||
[self addSubview:_CPToolbarPaletteOutline(frame, @"dashed")];
|
||||
|
||||
[arrows setImage:[CPImage imageWithCSSDictionary:@{
|
||||
"-webkit-mask-image": _CPToolbarPaletteFlexibleSpaceArrows,
|
||||
"mask-image": _CPToolbarPaletteFlexibleSpaceArrows,
|
||||
"background-color": [CPColor secondaryLabelColor],
|
||||
"-webkit-mask-size": "contain",
|
||||
"mask-size": "contain",
|
||||
"-webkit-mask-repeat": "no-repeat",
|
||||
"mask-repeat": "no-repeat",
|
||||
"-webkit-mask-position": "center",
|
||||
"mask-position": "center"
|
||||
} size:CGSizeMake(CGRectGetWidth(frame) - 16.0, 12.0)]];
|
||||
[self addSubview:arrows];
|
||||
}
|
||||
else if (identifier === CPToolbarSeparatorItemIdentifier)
|
||||
{
|
||||
var line = [[CPView alloc] initWithFrame:CGRectMake(FLOOR(midX), midY - 14.0, 1.0, 28.0)];
|
||||
|
||||
[line setBackgroundColor:[CPColor secondaryLabelColor]];
|
||||
[self addSubview:line];
|
||||
}
|
||||
else if ([anItem view])
|
||||
{
|
||||
var view = [anItem view],
|
||||
copy = [CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:view]],
|
||||
size = [anItem minSize],
|
||||
width = MIN(MAX(size.width, 24.0), _CPToolbarPaletteCellSize.width - 8.0),
|
||||
height = MIN(MAX(size.height, 16.0), 30.0);
|
||||
|
||||
[copy setFrame:CGRectMake(FLOOR(midX - width / 2.0), FLOOR(midY - height / 2.0), width, height)];
|
||||
[copy setHitTests:NO];
|
||||
_CPToolbarPaletteMakeInert(copy);
|
||||
[self addSubview:copy];
|
||||
}
|
||||
else if (image)
|
||||
{
|
||||
var imageView = [[CPImageView alloc] initWithFrame:imageFrame];
|
||||
|
||||
[imageView setImage:image];
|
||||
[imageView setImageScaling:CPImageScaleProportionallyDown];
|
||||
[self addSubview:imageView];
|
||||
}
|
||||
else
|
||||
{
|
||||
var size = _CPToolbarPaletteImageSize;
|
||||
|
||||
[self addSubview:_CPToolbarPaletteOutline(CGRectMake(FLOOR(midX - size.width / 2.0), FLOOR(midY - size.height / 2.0), size.width, size.height), @"solid")];
|
||||
}
|
||||
|
||||
[self addSubview:_CPToolbarPaletteLabel([anItem paletteLabel] || [anItem label] || @"", CGRectMake(0.0, 38.0, _CPToolbarPaletteCellSize.width, 18.0), CPCenterTextAlignment)];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/*
|
||||
Initializes the view of the default set: the items of the set in a row, in aFrame.
|
||||
*/
|
||||
- (id)initWithDefaultSet:(CPArray)theItems frame:(CGRect)aFrame
|
||||
{
|
||||
self = [super initWithFrame:aFrame];
|
||||
|
||||
if (self)
|
||||
{
|
||||
_defaultSet = [theItems valueForKey:@"itemIdentifier"];
|
||||
|
||||
[self setBackgroundColor:[CPColor quaternaryLabelColor]];
|
||||
|
||||
var x = 4.0;
|
||||
|
||||
for (var i = 0; i < theItems.length && x + _CPToolbarPaletteCellSize.width <= CGRectGetWidth(aFrame); i++)
|
||||
{
|
||||
var view = [[_CPToolbarPaletteItemView alloc] initWithItem:theItems[i] origin:CGPointMake(x, 0.0)];
|
||||
|
||||
[view setHitTests:NO];
|
||||
[self addSubview:view];
|
||||
x += _CPToolbarPaletteCellSize.width;
|
||||
}
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)mouseDragged:(CPEvent)anEvent
|
||||
{
|
||||
var pasteboard = [CPPasteboard pasteboardWithName:CPDragPboard];
|
||||
|
||||
if (_defaultSet)
|
||||
{
|
||||
[pasteboard declareTypes:[_CPToolbarDefaultSetPboardType] owner:nil];
|
||||
[pasteboard setPropertyList:_defaultSet forType:_CPToolbarDefaultSetPboardType];
|
||||
}
|
||||
else
|
||||
{
|
||||
[pasteboard declareTypes:[_CPToolbarItemIdentifierPboardType] owner:nil];
|
||||
[pasteboard setString:_itemIdentifier forType:_CPToolbarItemIdentifierPboardType];
|
||||
}
|
||||
|
||||
var dragView = [[CPView alloc] initWithFrame:[self bounds]];
|
||||
|
||||
[dragView setBackgroundColor:[CPColor colorWithCalibratedWhite:0.5 alpha:0.3]];
|
||||
|
||||
[self dragView:dragView at:CGPointMakeZero() offset:CGSizeMakeZero() event:anEvent pasteboard:pasteboard source:self slideBack:YES];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
/*
|
||||
A view over the toolbar view while the palette runs. It takes every mouse event, so that a
|
||||
click on an item, even an item with a view of its own, starts a drag instead of the item's
|
||||
action. Dragging an item within the toolbar moves it; dropping it outside the toolbar removes
|
||||
it. It accepts the drags of the palette's items and of the default set.
|
||||
@ignore
|
||||
*/
|
||||
@implementation _CPToolbarCustomizationOverlay : CPView
|
||||
{
|
||||
CPToolbar _toolbar;
|
||||
CPView _insertionIndicator;
|
||||
CPInteger _draggedIndex;
|
||||
BOOL _droppedOnToolbar;
|
||||
}
|
||||
|
||||
- (id)initWithToolbar:(CPToolbar)aToolbar frame:(CGRect)aFrame
|
||||
{
|
||||
self = [super initWithFrame:aFrame];
|
||||
|
||||
if (self)
|
||||
{
|
||||
_toolbar = aToolbar;
|
||||
_draggedIndex = CPNotFound;
|
||||
|
||||
[self setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
|
||||
[self registerForDraggedTypes:[_CPToolbarItemIdentifierPboardType, _CPToolbarDefaultSetPboardType]];
|
||||
|
||||
_insertionIndicator = [[CPView alloc] initWithFrame:CGRectMake(0.0, 4.0, 2.0, CGRectGetHeight(aFrame) - 8.0)];
|
||||
[_insertionIndicator setBackgroundColor:[CPColor controlAccentColor]];
|
||||
[_insertionIndicator setHidden:YES];
|
||||
[self addSubview:_insertionIndicator];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the horizontal centres of the toolbar's items in the receiver. An item that the
|
||||
toolbar hides for lack of room counts as lying beyond the right edge.
|
||||
*/
|
||||
- (CPArray)_itemCentres
|
||||
{
|
||||
var toolbarView = [_toolbar _toolbarView],
|
||||
items = [_toolbar items];
|
||||
|
||||
return items.map(function(anItem)
|
||||
{
|
||||
var view = [toolbarView viewForItem:anItem];
|
||||
|
||||
if (!view || [view isHidden] || ![view superview])
|
||||
return Infinity;
|
||||
|
||||
return CGRectGetMidX([self convertRect:[view frame] fromView:[view superview]]);
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the index of the item under aPoint, or CPNotFound.
|
||||
*/
|
||||
- (CPInteger)_indexOfItemAtPoint:(CGPoint)aPoint
|
||||
{
|
||||
var toolbarView = [_toolbar _toolbarView],
|
||||
items = [_toolbar items];
|
||||
|
||||
for (var i = 0; i < items.length; i++)
|
||||
{
|
||||
var view = [toolbarView viewForItem:items[i]];
|
||||
|
||||
if (view && ![view isHidden] && [view superview] && CGRectContainsPoint([self convertRect:[view frame] fromView:[view superview]], aPoint))
|
||||
return i;
|
||||
}
|
||||
|
||||
return CPNotFound;
|
||||
}
|
||||
|
||||
- (void)mouseDown:(CPEvent)anEvent
|
||||
{
|
||||
}
|
||||
|
||||
- (void)mouseDragged:(CPEvent)anEvent
|
||||
{
|
||||
if (_draggedIndex !== CPNotFound)
|
||||
return;
|
||||
|
||||
var point = [self convertPoint:[anEvent locationInWindow] fromView:nil],
|
||||
index = [self _indexOfItemAtPoint:point];
|
||||
|
||||
if (index === CPNotFound)
|
||||
return;
|
||||
|
||||
var item = [_toolbar items][index],
|
||||
itemView = [[_toolbar _toolbarView] viewForItem:item],
|
||||
itemFrame = [self convertRect:[itemView frame] fromView:[itemView superview]],
|
||||
pasteboard = [CPPasteboard pasteboardWithName:CPDragPboard],
|
||||
dragView = [[CPView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(itemFrame), CGRectGetHeight(itemFrame))];
|
||||
|
||||
_draggedIndex = index;
|
||||
_droppedOnToolbar = NO;
|
||||
|
||||
[pasteboard declareTypes:[_CPToolbarItemIdentifierPboardType] owner:nil];
|
||||
[pasteboard setString:[item itemIdentifier] forType:_CPToolbarItemIdentifierPboardType];
|
||||
|
||||
[dragView setBackgroundColor:[CPColor colorWithCalibratedWhite:0.5 alpha:0.3]];
|
||||
|
||||
[self dragView:dragView at:itemFrame.origin offset:CGSizeMakeZero() event:anEvent pasteboard:pasteboard source:self slideBack:NO];
|
||||
}
|
||||
|
||||
/*
|
||||
An item dragged from the toolbar and dropped anywhere but on the toolbar is removed. The
|
||||
receiver notes a drop on the toolbar itself instead of reading the drag operation, because in
|
||||
the browser the operation comes from the browser's drop effect when the drag ends, which need
|
||||
not be none after a drop that no view accepted.
|
||||
*/
|
||||
- (void)draggedView:(CPView)aView endedAt:(CGPoint)aLocation operation:(CPDragOperation)anOperation
|
||||
{
|
||||
var index = _draggedIndex;
|
||||
|
||||
_draggedIndex = CPNotFound;
|
||||
[_insertionIndicator setHidden:YES];
|
||||
|
||||
if (!_droppedOnToolbar && index !== CPNotFound && index < [[_toolbar items] count])
|
||||
[_toolbar removeItemAtIndex:index];
|
||||
|
||||
_droppedOnToolbar = NO;
|
||||
}
|
||||
|
||||
- (CPDragOperation)draggingEntered:(id)aSender
|
||||
{
|
||||
return [self draggingUpdated:aSender];
|
||||
}
|
||||
|
||||
- (CPDragOperation)draggingUpdated:(id)aSender
|
||||
{
|
||||
var point = [self convertPoint:[aSender draggingLocation] fromView:nil],
|
||||
centres = [self _itemCentres],
|
||||
index = _CPToolbarInsertionIndex(centres, point.x),
|
||||
x = 8.0;
|
||||
|
||||
if ([[[aSender draggingPasteboard] types] containsObject:_CPToolbarDefaultSetPboardType])
|
||||
{
|
||||
[_insertionIndicator setHidden:YES];
|
||||
return CPDragOperationCopy;
|
||||
}
|
||||
|
||||
if (index > 0 && centres[index - 1] !== Infinity)
|
||||
{
|
||||
var previousView = [[_toolbar _toolbarView] viewForItem:[_toolbar items][index - 1]];
|
||||
|
||||
x = CGRectGetMaxX([self convertRect:[previousView frame] fromView:[previousView superview]]) + 2.0;
|
||||
}
|
||||
|
||||
[_insertionIndicator setFrameOrigin:CGPointMake(x, 4.0)];
|
||||
[_insertionIndicator setHidden:NO];
|
||||
|
||||
return [aSender draggingSource] === self ? CPDragOperationMove : CPDragOperationCopy;
|
||||
}
|
||||
|
||||
- (void)draggingExited:(id)aSender
|
||||
{
|
||||
[_insertionIndicator setHidden:YES];
|
||||
}
|
||||
|
||||
- (BOOL)prepareForDragOperation:(id)aSender
|
||||
{
|
||||
[_insertionIndicator setHidden:YES];
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (BOOL)performDragOperation:(id)aSender
|
||||
{
|
||||
var pasteboard = [aSender draggingPasteboard];
|
||||
|
||||
if ([[pasteboard types] containsObject:_CPToolbarDefaultSetPboardType])
|
||||
{
|
||||
[_toolbar setConfigurationFromDictionary:@{ @"TB Item Identifiers": [pasteboard propertyListForType:_CPToolbarDefaultSetPboardType] }];
|
||||
return YES;
|
||||
}
|
||||
|
||||
var identifier = [pasteboard stringForType:_CPToolbarItemIdentifierPboardType],
|
||||
point = [self convertPoint:[aSender draggingLocation] fromView:nil],
|
||||
index = _CPToolbarInsertionIndex([self _itemCentres], point.x);
|
||||
|
||||
if (!identifier)
|
||||
return NO;
|
||||
|
||||
/*
|
||||
A drop from the palette of an item already in the toolbar added a second item. The
|
||||
delegate can give the same view for each request, and a view has one superview, so the
|
||||
new item showed only its label. As in AppKit, only the space items can appear more than
|
||||
once; a drop of any other item already in the toolbar moves that item.
|
||||
*/
|
||||
var fromIndex = CPNotFound;
|
||||
|
||||
if ([aSender draggingSource] === self && _draggedIndex !== CPNotFound)
|
||||
{
|
||||
fromIndex = _draggedIndex;
|
||||
_droppedOnToolbar = YES;
|
||||
}
|
||||
else if (!_CPToolbarItemAllowsDuplicates(identifier))
|
||||
fromIndex = [[[_toolbar items] valueForKey:@"itemIdentifier"] indexOfObject:identifier];
|
||||
|
||||
if (fromIndex !== CPNotFound)
|
||||
{
|
||||
if (index === fromIndex || index === fromIndex + 1)
|
||||
return YES;
|
||||
|
||||
[_toolbar removeItemAtIndex:fromIndex];
|
||||
|
||||
if (index > fromIndex)
|
||||
index--;
|
||||
}
|
||||
|
||||
[_toolbar insertItemWithItemIdentifier:identifier atIndex:index];
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
/*
|
||||
The customization palette of a toolbar: a sheet on the toolbar's window that shows the
|
||||
allowed items and the default set. The user drags items into the toolbar, within it and out
|
||||
of it; the Show menu sets the display mode, Use small size the size mode, and Done ends the
|
||||
palette.
|
||||
@ignore
|
||||
*/
|
||||
@implementation _CPToolbarCustomizationPalette : CPObject
|
||||
{
|
||||
CPToolbar _toolbar;
|
||||
CPWindow _sheet @accessors(readonly, property=sheet);
|
||||
CPArray _itemViews @accessors(readonly, property=itemViews);
|
||||
_CPToolbarPaletteItemView _defaultSetView @accessors(readonly, property=defaultSetView);
|
||||
CPPopUpButton _displayModeButton;
|
||||
CPCheckBox _smallSizeCheckBox;
|
||||
_CPToolbarCustomizationOverlay _overlay;
|
||||
}
|
||||
|
||||
- (id)initWithToolbar:(CPToolbar)aToolbar
|
||||
{
|
||||
self = [super init];
|
||||
|
||||
if (self)
|
||||
{
|
||||
_toolbar = aToolbar;
|
||||
[self _buildSheet];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)_buildSheet
|
||||
{
|
||||
var identifiers = [_toolbar _allowedItemIdentifiers],
|
||||
columns = FLOOR((_CPToolbarPaletteWidth - 2.0 * _CPToolbarPaletteMargin) / _CPToolbarPaletteCellSize.width),
|
||||
rows = MAX(1, CEIL(identifiers.length / columns)),
|
||||
gridHeight = rows * _CPToolbarPaletteCellSize.height,
|
||||
height = _CPToolbarPaletteMargin + 20.0 + gridHeight + 12.0 + 20.0 + _CPToolbarPaletteCellSize.height + 16.0 + 24.0 + _CPToolbarPaletteMargin;
|
||||
|
||||
_sheet = [[CPWindow alloc] initWithContentRect:CGRectMake(0.0, 0.0, _CPToolbarPaletteWidth, height) styleMask:CPDocModalWindowMask];
|
||||
|
||||
var contentView = [_sheet contentView],
|
||||
y = _CPToolbarPaletteMargin;
|
||||
|
||||
[contentView addSubview:_CPToolbarPaletteLabel(@"Drag your favorite items into the toolbar…", CGRectMake(_CPToolbarPaletteMargin, y, _CPToolbarPaletteWidth - 2.0 * _CPToolbarPaletteMargin, 18.0), CPLeftTextAlignment)];
|
||||
y += 20.0;
|
||||
|
||||
_itemViews = [];
|
||||
|
||||
for (var i = 0; i < identifiers.length; i++)
|
||||
{
|
||||
var item = [_toolbar _paletteItemForItemIdentifier:identifiers[i]];
|
||||
|
||||
if (!item)
|
||||
continue;
|
||||
|
||||
var column = _itemViews.length % columns,
|
||||
row = FLOOR(_itemViews.length / columns),
|
||||
view = [[_CPToolbarPaletteItemView alloc] initWithItem:item origin:CGPointMake(_CPToolbarPaletteMargin + column * _CPToolbarPaletteCellSize.width, y + row * _CPToolbarPaletteCellSize.height)];
|
||||
|
||||
[_itemViews addObject:view];
|
||||
[contentView addSubview:view];
|
||||
}
|
||||
|
||||
y += gridHeight + 12.0;
|
||||
|
||||
[contentView addSubview:_CPToolbarPaletteLabel(@"…or drag the default set into the toolbar.", CGRectMake(_CPToolbarPaletteMargin, y, _CPToolbarPaletteWidth - 2.0 * _CPToolbarPaletteMargin, 18.0), CPLeftTextAlignment)];
|
||||
y += 20.0;
|
||||
|
||||
var defaultItems = [[_toolbar _defaultItemIdentifiers] arrayByApplyingBlock:function(anIdentifier)
|
||||
{
|
||||
return [_toolbar _paletteItemForItemIdentifier:anIdentifier];
|
||||
}].filter(function(anItem) { return !!anItem; });
|
||||
|
||||
_defaultSetView = [[_CPToolbarPaletteItemView alloc] initWithDefaultSet:defaultItems frame:CGRectMake(_CPToolbarPaletteMargin, y, _CPToolbarPaletteWidth - 2.0 * _CPToolbarPaletteMargin, _CPToolbarPaletteCellSize.height)];
|
||||
[contentView addSubview:_defaultSetView];
|
||||
y += _CPToolbarPaletteCellSize.height + 16.0;
|
||||
|
||||
[contentView addSubview:_CPToolbarPaletteLabel(@"Show", CGRectMake(_CPToolbarPaletteMargin, y + 4.0, 40.0, 18.0), CPLeftTextAlignment)];
|
||||
|
||||
_displayModeButton = [[CPPopUpButton alloc] initWithFrame:CGRectMake(_CPToolbarPaletteMargin + 44.0, y, 140.0, 24.0) pullsDown:NO];
|
||||
|
||||
[self _addDisplayMode:CPToolbarDisplayModeIconAndLabel title:@"Icon and Text"];
|
||||
[self _addDisplayMode:CPToolbarDisplayModeIconOnly title:@"Icon Only"];
|
||||
[self _addDisplayMode:CPToolbarDisplayModeLabelOnly title:@"Text Only"];
|
||||
[_displayModeButton selectItemWithTag:[_toolbar displayMode] || CPToolbarDisplayModeIconAndLabel];
|
||||
[_displayModeButton setTarget:self];
|
||||
[_displayModeButton setAction:@selector(_setDisplayMode:)];
|
||||
[contentView addSubview:_displayModeButton];
|
||||
|
||||
_smallSizeCheckBox = [[CPCheckBox alloc] initWithFrame:CGRectMake(_CPToolbarPaletteMargin + 200.0, y + 2.0, 130.0, 20.0)];
|
||||
[_smallSizeCheckBox setTitle:@"Use small size"];
|
||||
[_smallSizeCheckBox setState:[_toolbar sizeMode] === CPToolbarSizeModeSmall ? CPOnState : CPOffState];
|
||||
[_smallSizeCheckBox setTarget:self];
|
||||
[_smallSizeCheckBox setAction:@selector(_setSmallSize:)];
|
||||
[contentView addSubview:_smallSizeCheckBox];
|
||||
|
||||
var doneButton = [[CPButton alloc] initWithFrame:CGRectMake(_CPToolbarPaletteWidth - _CPToolbarPaletteMargin - 80.0, y, 80.0, 24.0)];
|
||||
|
||||
[doneButton setTitle:@"Done"];
|
||||
[doneButton setKeyEquivalent:CPCarriageReturnCharacter];
|
||||
[doneButton setTarget:self];
|
||||
[doneButton setAction:@selector(done:)];
|
||||
[contentView addSubview:doneButton];
|
||||
}
|
||||
|
||||
- (void)_addDisplayMode:(CPToolbarDisplayMode)aMode title:(CPString)aTitle
|
||||
{
|
||||
[_displayModeButton addItemWithTitle:aTitle];
|
||||
[[_displayModeButton lastItem] setTag:aMode];
|
||||
}
|
||||
|
||||
- (void)_setDisplayMode:(id)aSender
|
||||
{
|
||||
[_toolbar setDisplayMode:[[aSender selectedItem] tag]];
|
||||
}
|
||||
|
||||
- (void)_setSmallSize:(id)aSender
|
||||
{
|
||||
[_toolbar setSizeMode:[aSender state] === CPOnState ? CPToolbarSizeModeSmall : CPToolbarSizeModeRegular];
|
||||
}
|
||||
|
||||
/*
|
||||
Shows the palette as a sheet on the toolbar's window and lays the overlay over the toolbar.
|
||||
*/
|
||||
- (void)run
|
||||
{
|
||||
var toolbarView = [_toolbar _toolbarView];
|
||||
|
||||
_overlay = [[_CPToolbarCustomizationOverlay alloc] initWithToolbar:_toolbar frame:[toolbarView bounds]];
|
||||
[toolbarView _setCustomizationOverlay:_overlay];
|
||||
|
||||
[CPApp beginSheet:_sheet modalForWindow:[_toolbar _window] modalDelegate:self didEndSelector:@selector(_sheetDidEnd:returnCode:contextInfo:) contextInfo:nil];
|
||||
}
|
||||
|
||||
- (void)done:(id)aSender
|
||||
{
|
||||
[CPApp endSheet:_sheet];
|
||||
}
|
||||
|
||||
- (void)_sheetDidEnd:(CPWindow)aSheet returnCode:(int)aCode contextInfo:(id)aContext
|
||||
{
|
||||
[aSheet orderOut:self];
|
||||
[[_toolbar _toolbarView] _setCustomizationOverlay:nil];
|
||||
_overlay = nil;
|
||||
|
||||
[_toolbar _customizationPaletteDidEnd];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -33,6 +33,11 @@
|
||||
|
||||
if (self)
|
||||
{
|
||||
/*
|
||||
The item had no palette label, so the customization palette showed it without a
|
||||
name. AppKit's label is "Flexible Space"; the toolbar shows no label for it.
|
||||
*/
|
||||
[self setPaletteLabel:@"Flexible Space"];
|
||||
[self setMinSize:CGSizeMake(32.0, 32.0)];
|
||||
[self setMaxSize:CGSizeMake(10000.0, 32.0)];
|
||||
}
|
||||
|
||||
@@ -33,6 +33,11 @@
|
||||
|
||||
if (self)
|
||||
{
|
||||
/*
|
||||
The item had no palette label, so the customization palette showed it without a
|
||||
name. AppKit's label is "Separator"; the toolbar shows no label for it.
|
||||
*/
|
||||
[self setPaletteLabel:@"Separator"];
|
||||
[self setMinSize:CGSizeMake(2.0, 0.0)];
|
||||
[self setMaxSize:CGSizeMake(2.0, 100000.0)];
|
||||
}
|
||||
|
||||
@@ -33,6 +33,11 @@
|
||||
|
||||
if (self)
|
||||
{
|
||||
/*
|
||||
The item had no palette label, so the customization palette showed it without a
|
||||
name. AppKit's label is "Space"; the toolbar shows no label for it.
|
||||
*/
|
||||
[self setPaletteLabel:@"Space"];
|
||||
[self setMinSize:CGSizeMake(32.0, 32.0)];
|
||||
[self setMaxSize:CGSizeMake(32.0, 32.0)];
|
||||
}
|
||||
|
||||
@@ -154,6 +154,18 @@ var ToolbarTestIdentifiersOf = function(aToolbar)
|
||||
return [[aToolbar items] valueForKey:@"itemIdentifier"];
|
||||
};
|
||||
|
||||
/*
|
||||
Dragging information for a drop on the customization overlay.
|
||||
*/
|
||||
@implementation _ToolbarTestDraggingInfo : CPObject
|
||||
{
|
||||
CPPasteboard draggingPasteboard @accessors;
|
||||
CGPoint draggingLocation @accessors;
|
||||
id draggingSource @accessors;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation CPToolbarTest : OJTestCase
|
||||
|
||||
- (void)testTheItemTheDelegateGivesForInsertionIsInserted
|
||||
@@ -580,4 +592,129 @@ var ToolbarTestIdentifiersOf = function(aToolbar)
|
||||
[self assert:CPToolbarDisplayModeLabelOnly equals:[second displayMode] message:@"the display mode"];
|
||||
}
|
||||
|
||||
// MARK: Customization palette
|
||||
|
||||
- (void)testADropGoesBeforeTheFirstItemWhoseCentreIsAtOrRightOfIt
|
||||
{
|
||||
var centres = [20.0, 60.0, 100.0];
|
||||
|
||||
[self assert:0 equals:_CPToolbarInsertionIndex(centres, 5.0) message:@"left of every item"];
|
||||
[self assert:1 equals:_CPToolbarInsertionIndex(centres, 40.0) message:@"between the first two"];
|
||||
[self assert:3 equals:_CPToolbarInsertionIndex(centres, 120.0) message:@"right of every item"];
|
||||
[self assert:1 equals:_CPToolbarInsertionIndex([20.0, Infinity], 400.0) message:@"a hidden item counts as beyond the edge"];
|
||||
}
|
||||
|
||||
- (void)testThePaletteOffersTheAllowedItemsAndTheDefaultSet
|
||||
{
|
||||
var toolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestPalette"],
|
||||
delegate = [_ToolbarTestRecordingDelegate new];
|
||||
|
||||
[delegate setItemIdentifiers:[CPToolbarSpaceItemIdentifier, CPToolbarFlexibleSpaceItemIdentifier, CPToolbarSeparatorItemIdentifier]];
|
||||
[toolbar setDelegate:delegate];
|
||||
|
||||
var palette = [[_CPToolbarCustomizationPalette alloc] initWithToolbar:toolbar];
|
||||
|
||||
[self assert:[CPToolbarSpaceItemIdentifier, CPToolbarFlexibleSpaceItemIdentifier, CPToolbarSeparatorItemIdentifier] equals:[[palette itemViews] valueForKey:@"itemIdentifier"] message:@"the allowed items"];
|
||||
[self assert:[CPToolbarSpaceItemIdentifier, CPToolbarFlexibleSpaceItemIdentifier, CPToolbarSeparatorItemIdentifier] equals:[[palette defaultSetView] defaultSet] message:@"the default set"];
|
||||
}
|
||||
|
||||
- (void)testThePaletteRunsOnlyWhenTheToolbarAllowsCustomization
|
||||
{
|
||||
var toolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestNoCustomization"];
|
||||
|
||||
[toolbar setDelegate:[_ToolbarTestRecordingDelegate new]];
|
||||
[toolbar runCustomizationPalette:nil];
|
||||
|
||||
[self assertFalse:[toolbar customizationPaletteIsRunning]];
|
||||
}
|
||||
|
||||
- (void)testTheWindowEnablesCustomizationOnlyWhenTheToolbarAllowsIt
|
||||
{
|
||||
var window = [[CPWindow alloc] initWithContentRect:CGRectMake(0.0, 0.0, 400.0, 300.0) styleMask:CPTitledWindowMask],
|
||||
toolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestWindowValidation"],
|
||||
menuItem = [[CPMenuItem alloc] initWithTitle:@"Customize Toolbar…" action:@selector(runToolbarCustomizationPalette:) keyEquivalent:@""];
|
||||
|
||||
[toolbar setDelegate:[_ToolbarTestRecordingDelegate new]];
|
||||
[window setToolbar:toolbar];
|
||||
|
||||
[self assertFalse:[window validateUserInterfaceItem:menuItem] message:@"customization off"];
|
||||
|
||||
[toolbar setAllowsUserCustomization:YES];
|
||||
|
||||
[self assertTrue:[window validateUserInterfaceItem:menuItem] message:@"customization on"];
|
||||
}
|
||||
|
||||
- (void)testDroppingTheDefaultSetRestoresTheDefaultItems
|
||||
{
|
||||
var toolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestDefaultSetDrop"],
|
||||
pasteboard = [CPPasteboard pasteboardWithName:@"CPToolbarTestDefaultSetDrop"],
|
||||
info = [_ToolbarTestDraggingInfo new];
|
||||
|
||||
[toolbar setDelegate:[_ToolbarTestRecordingDelegate new]];
|
||||
[toolbar removeItemAtIndex:0];
|
||||
|
||||
var overlay = [[_CPToolbarCustomizationOverlay alloc] initWithToolbar:toolbar frame:CGRectMake(0.0, 0.0, 400.0, 50.0)];
|
||||
|
||||
[pasteboard declareTypes:[_CPToolbarDefaultSetPboardType] owner:nil];
|
||||
[pasteboard setPropertyList:[toolbar _defaultItemIdentifiers] forType:_CPToolbarDefaultSetPboardType];
|
||||
[info setDraggingPasteboard:pasteboard];
|
||||
[info setDraggingLocation:CGPointMake(10.0, 10.0)];
|
||||
|
||||
[self assertTrue:[overlay performDragOperation:info]];
|
||||
[self assert:[CPToolbarSpaceItemIdentifier, CPToolbarFlexibleSpaceItemIdentifier] equals:ToolbarTestIdentifiersOf(toolbar)];
|
||||
}
|
||||
|
||||
/*
|
||||
A drop from the palette of an item that is already in the toolbar moves that item; only the
|
||||
space items can appear more than once.
|
||||
*/
|
||||
- (void)testDroppingAPresentItemMovesItAndOnlySpacesRepeat
|
||||
{
|
||||
var toolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestPresentItemDrop"],
|
||||
delegate = [_ToolbarTestRecordingDelegate new],
|
||||
pasteboard = [CPPasteboard pasteboardWithName:@"CPToolbarTestPresentItemDrop"],
|
||||
info = [_ToolbarTestDraggingInfo new];
|
||||
|
||||
[delegate setItemIdentifiers:[CPToolbarSpaceItemIdentifier, CPToolbarPrintItemIdentifier]];
|
||||
[toolbar setDelegate:delegate];
|
||||
|
||||
var overlay = [[_CPToolbarCustomizationOverlay alloc] initWithToolbar:toolbar frame:CGRectMake(0.0, 0.0, 400.0, 50.0)];
|
||||
|
||||
[pasteboard declareTypes:[_CPToolbarItemIdentifierPboardType] owner:nil];
|
||||
[pasteboard setString:CPToolbarPrintItemIdentifier forType:_CPToolbarItemIdentifierPboardType];
|
||||
[info setDraggingPasteboard:pasteboard];
|
||||
[info setDraggingLocation:CGPointMake(0.0, 10.0)];
|
||||
|
||||
[self assertTrue:[overlay performDragOperation:info]];
|
||||
[self assert:[CPToolbarPrintItemIdentifier, CPToolbarSpaceItemIdentifier] equals:ToolbarTestIdentifiersOf(toolbar) message:@"a present item moves"];
|
||||
|
||||
[pasteboard setString:CPToolbarSpaceItemIdentifier forType:_CPToolbarItemIdentifierPboardType];
|
||||
|
||||
[self assertTrue:[overlay performDragOperation:info]];
|
||||
[self assert:[CPToolbarSpaceItemIdentifier, CPToolbarPrintItemIdentifier, CPToolbarSpaceItemIdentifier] equals:ToolbarTestIdentifiersOf(toolbar) message:@"a space repeats"];
|
||||
}
|
||||
|
||||
/*
|
||||
In the small size mode an image item takes AppKit's small toolbar image, 24 points against
|
||||
32; in the regular size mode it keeps its size.
|
||||
*/
|
||||
- (void)testSmallSizeScalesAnImageItemToTheSmallImageSize
|
||||
{
|
||||
var toolbar = [[CPToolbar alloc] initWithIdentifier:@"CPToolbarTestSmallSize"],
|
||||
delegate = [_ToolbarTestRecordingDelegate new];
|
||||
|
||||
[delegate setItemIdentifiers:[CPToolbarPrintItemIdentifier]];
|
||||
[toolbar setDelegate:delegate];
|
||||
[toolbar _toolbarView];
|
||||
|
||||
var itemView = [[toolbar _toolbarView] viewForItem:[toolbar items][0]];
|
||||
|
||||
[self assertTrue:CGSizeEqualToSize(CGSizeMake(32.0, 32.0), [itemView _sizeForSizeMode:CGSizeMake(32.0, 32.0)]) message:@"regular"];
|
||||
|
||||
[toolbar setSizeMode:CPToolbarSizeModeSmall];
|
||||
itemView = [[toolbar _toolbarView] viewForItem:[toolbar items][0]];
|
||||
|
||||
[self assertTrue:CGSizeEqualToSize(CGSizeMake(24.0, 24.0), [itemView _sizeForSizeMode:CGSizeMake(32.0, 32.0)]) message:@"small"];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -40,8 +40,12 @@ the other icons and the badges are drawn with `CPBezierPath`.
|
||||
12. View menu / toolbar: hide and show sidebar and detail pane; drag the sidebar resize control in the button bar.
|
||||
Toolbar items are enabled exactly when their menu items are: Edit only for a smart folder,
|
||||
New Smart Folder and New Group not while a sheet is open. In dark mode the glyphs are light.
|
||||
View ▸ Hide Toolbar hides it and the item then reads Show Toolbar. View ▸ Customize Toolbar…
|
||||
stays disabled until CPWindow implements the customisation palette.
|
||||
View ▸ Hide Toolbar hides it and the item then reads Show Toolbar.
|
||||
View ▸ Customize Toolbar… opens the customisation palette as a sheet: drag an item from the
|
||||
palette into the toolbar (a marker shows where it goes), drag a toolbar item to another place,
|
||||
drag one out of the toolbar to remove it, and drag the default set to restore it. Show sets
|
||||
Icon and Text, Icon Only or Text Only; Done closes the sheet. Reload the page: the changed
|
||||
toolbar stays (autosave). While the sheet is open, the toolbar items do not act.
|
||||
The standard Colors item opens the colour panel. In both appearances its labels, the boxes
|
||||
around the preview and the swatches, and the opacity slider's track and knob show.
|
||||
13. File ▸ Reset Library…: alert sheet, then sample data restored.
|
||||
Reference in new issue
Block a user