mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-11-29 09:52:03 +00:00
CPSearchField: Move search button placement from theme data into control
Aristo3 previously stored search button placement and layout animation as JavaScript source strings in theme data, which CPSearchField evaluated with objj_eval. Button placement and layout animation belong in control logic rather than theme data. CPSearchField now implements AppKit's centersPlaceholder (default YES, archived). When not editing and displaying a placeholder, the search button and placeholder center together as a group; if the placeholder is wider than the field, the button remains at its leading position. Layout transitions are animated directly by the control. _CPButtonBarSearchField disables centersPlaceholder rather than copying a theme function across states. The search-button-rect-function and layout-function attributes and their Aristo3 script strings are removed. Tests: - CPSearchFieldTest: Verify centersPlaceholder default, centered and leading placement, narrow fields, _CPButtonBarSearchField behavior, and archiving.
This commit is contained in:
1 parent
5ba275a7ce
commit
b5d5889166
4 files changed
+192
-68
No files matched your search
@@ -1508,6 +1508,9 @@ var CPButtonBarHasLeftResizeControlKey = @"CPButtonBarHasLeftResizeControl
|
||||
_minWidth = minWidth;
|
||||
_maxWidth = maxWidth;
|
||||
|
||||
/* A button bar search field keeps its search button and text at the leading edge. */
|
||||
[self setCentersPlaceholder:NO];
|
||||
|
||||
[self _initThemedValues];
|
||||
|
||||
[self setPlaceholderString:placeholder];
|
||||
@@ -1523,9 +1526,6 @@ var CPButtonBarHasLeftResizeControlKey = @"CPButtonBarHasLeftResizeControl
|
||||
// Set height to the default height from theme
|
||||
[self setFrameSize:CGSizeMake(_minWidth, [self currentValueForThemeAttribute:@"min-size"].height)];
|
||||
|
||||
// Disable the not editing / editing difference in layouts
|
||||
[self setValue:[self valueForThemeAttribute:@"search-button-rect-function" inStates:[CPTextFieldStateRounded, CPThemeStateBezeled, CPThemeStateEditing]] forThemeAttribute:@"search-button-rect-function" inStates:[CPTextFieldStateRounded, CPThemeStateBezeled]];
|
||||
|
||||
// Get extra-spacing
|
||||
_extraSpacing = [self currentValueForThemeAttribute:@"extra-spacing"];
|
||||
}
|
||||
|
||||
+91
-17
@@ -65,6 +65,7 @@ var CPAutosavedRecentsChangedNotification = @"CPAutosavedRecentsChangedNotificat
|
||||
|
||||
CPView _contentView;
|
||||
BOOL _isBecomingFirstResponder;
|
||||
BOOL _centersPlaceholder;
|
||||
}
|
||||
|
||||
+ (CPString)defaultThemeClass
|
||||
@@ -81,8 +82,6 @@ var CPAutosavedRecentsChangedNotification = @"CPAutosavedRecentsChangedNotificat
|
||||
@"image-cancel-pressed": [CPNull null],
|
||||
@"image-search-inset" : CGInsetMake(0, 0, 0, 5),
|
||||
@"image-cancel-inset" : CGInsetMake(0, 5, 0, 0),
|
||||
@"search-button-rect-function": [CPNull null],
|
||||
@"layout-function": [CPNull null],
|
||||
@"search-right-margin": 2,
|
||||
@"search-menu-offset": CGPointMake(10, -4)
|
||||
};
|
||||
@@ -104,7 +103,8 @@ var CPAutosavedRecentsChangedNotification = @"CPAutosavedRecentsChangedNotificat
|
||||
_sendsWholeSearchString = NO;
|
||||
_sendsSearchStringImmediately = NO;
|
||||
_recentsAutosaveName = nil;
|
||||
|
||||
_centersPlaceholder = YES;
|
||||
|
||||
[self setPlaceholderString:@"Search"];
|
||||
|
||||
[self _init];
|
||||
@@ -256,6 +256,44 @@ var CPAutosavedRecentsChangedNotification = @"CPAutosavedRecentsChangedNotificat
|
||||
}
|
||||
|
||||
// Custom Layout
|
||||
/*!
|
||||
Returns a Boolean value that indicates whether the receiver centres its placeholder.
|
||||
@return \c YES if the search button and the placeholder are centred as one group while the receiver is not editing and has no value; otherwise \c NO. The default value is \c YES.
|
||||
*/
|
||||
- (BOOL)centersPlaceholder
|
||||
{
|
||||
return _centersPlaceholder;
|
||||
}
|
||||
|
||||
/*!
|
||||
Sets whether the receiver centres its placeholder.
|
||||
@param shouldCenter \c YES to centre the search button and the placeholder as one group while the receiver is not editing and has no value; \c NO to keep them at the leading edge.
|
||||
*/
|
||||
- (void)setCentersPlaceholder:(BOOL)shouldCenter
|
||||
{
|
||||
shouldCenter = !!shouldCenter;
|
||||
|
||||
if (_centersPlaceholder === shouldCenter)
|
||||
return;
|
||||
|
||||
_centersPlaceholder = shouldCenter;
|
||||
[self _layoutContent];
|
||||
}
|
||||
|
||||
/*
|
||||
The search button and the placeholder are centred as one group only while the receiver is not
|
||||
editing, is not about to edit, and shows a placeholder.
|
||||
*/
|
||||
- (BOOL)_isPlaceholderCentered
|
||||
{
|
||||
if (!_centersPlaceholder || _isBecomingFirstResponder || [self hasThemeState:CPThemeStateEditing])
|
||||
return NO;
|
||||
|
||||
var placeholder = [self placeholderString];
|
||||
|
||||
return [[self stringValue] length] === 0 && [placeholder length] > 0 && placeholder !== @" ";
|
||||
}
|
||||
|
||||
/*!
|
||||
Modifies the bounding rectangle for the search-text field.
|
||||
@param rect The current bounding rectangle for the search text field.
|
||||
@@ -291,18 +329,30 @@ var CPAutosavedRecentsChangedNotification = @"CPAutosavedRecentsChangedNotificat
|
||||
*/
|
||||
- (CGRect)searchButtonRectForBounds:(CGRect)rect
|
||||
{
|
||||
var themedRectFunction = [self _potentialCurrentValueForThemeAttribute:@"search-button-rect-function"];
|
||||
var size = [[self _potentialCurrentValueForThemeAttribute:@"image-search"] size] || CGSizeMakeZero(),
|
||||
inset = [self _potentialCurrentValueForThemeAttribute:@"image-search-inset"],
|
||||
x = inset.left - inset.right,
|
||||
y = inset.top - inset.bottom + (CGRectGetHeight(rect) - size.height) / 2;
|
||||
|
||||
if (themedRectFunction)
|
||||
// There's a theme defined positioning function, just use it
|
||||
return objj_eval("("+themedRectFunction+")")(self, rect);
|
||||
else
|
||||
/*
|
||||
Aristo3 stored this placement as JavaScript source in its theme data, and the receiver ran
|
||||
it with objj_eval. The placement is control behaviour, not theme data, so it is here now;
|
||||
the theme gives only the image, its inset and the margin.
|
||||
|
||||
The search button and the placeholder are centred as one group. A placeholder wider than
|
||||
the field put the button at a negative x, outside the field, so a narrow search field
|
||||
showed no button. The button now stays at its leading position when the group does not
|
||||
fit, and the placeholder after it is truncated.
|
||||
*/
|
||||
if ([self _isPlaceholderCentered])
|
||||
{
|
||||
var size = [[self _potentialCurrentValueForThemeAttribute:@"image-search"] size] || CGSizeMakeZero(),
|
||||
inset = [self _potentialCurrentValueForThemeAttribute:@"image-search-inset"];
|
||||
var margin = [self _potentialCurrentValueForThemeAttribute:@"search-right-margin"],
|
||||
placeholderSize = [[self placeholderString] sizeWithFont:[self font]];
|
||||
|
||||
return CGRectMake(inset.left - inset.right, inset.top - inset.bottom + (CGRectGetHeight(rect) - size.height) / 2, size.width, size.height);
|
||||
x = MAX(x, (CGRectGetWidth(rect) - placeholderSize.width) / 2 - size.width - margin);
|
||||
}
|
||||
|
||||
return CGRectMake(x, y, size.width, size.height);
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -900,13 +950,29 @@ var CPAutosavedRecentsChangedNotification = @"CPAutosavedRecentsChangedNotificat
|
||||
|
||||
try
|
||||
{
|
||||
var themedLayoutFunction = [self currentValueForThemeAttribute:@"layout-function"];
|
||||
/*
|
||||
A receiver that centres its placeholder moves the search button and the text between
|
||||
the centred and the leading positions, so it animates the move.
|
||||
*/
|
||||
if (_centersPlaceholder)
|
||||
{
|
||||
[CPAnimationContext beginGrouping];
|
||||
|
||||
if (themedLayoutFunction)
|
||||
// There's a theme defined layout function, just use it
|
||||
objj_eval("("+themedLayoutFunction+")")(self);
|
||||
var context = [CPAnimationContext currentContext];
|
||||
|
||||
[context setDuration:0.2];
|
||||
[context setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
|
||||
[context setCompletionHandler:function()
|
||||
{
|
||||
[self themedLayoutFunctionCompletionHandler];
|
||||
}];
|
||||
|
||||
[[_searchButton animator] setFrame:[self searchButtonRectForBounds:[self bounds]]];
|
||||
[[_contentView animator] setFrame:[self contentRectForBounds:[self bounds]]];
|
||||
|
||||
[CPAnimationContext endGrouping];
|
||||
}
|
||||
else
|
||||
// Call directly the completion handler
|
||||
[self themedLayoutFunctionCompletionHandler];
|
||||
}
|
||||
finally
|
||||
@@ -1081,7 +1147,8 @@ var CPRecentsAutosaveNameKey = @"CPRecentsAutosaveNameKey",
|
||||
CPSendsWholeSearchStringKey = @"CPSendsWholeSearchStringKey",
|
||||
CPSendsSearchStringImmediatelyKey = @"CPSendsSearchStringImmediatelyKey",
|
||||
CPMaximumRecentsKey = @"CPMaximumRecentsKey",
|
||||
CPSearchMenuTemplateKey = @"CPSearchMenuTemplateKey";
|
||||
CPSearchMenuTemplateKey = @"CPSearchMenuTemplateKey",
|
||||
CPCentersPlaceholderKey = @"CPCentersPlaceholderKey";
|
||||
|
||||
@implementation CPSearchField (CPCoding)
|
||||
|
||||
@@ -1100,6 +1167,7 @@ var CPRecentsAutosaveNameKey = @"CPRecentsAutosaveNameKey",
|
||||
[coder encodeBool:_sendsWholeSearchString forKey:CPSendsWholeSearchStringKey];
|
||||
[coder encodeBool:_sendsSearchStringImmediately forKey:CPSendsSearchStringImmediatelyKey];
|
||||
[coder encodeInt:_maximumRecents forKey:CPMaximumRecentsKey];
|
||||
[coder encodeBool:_centersPlaceholder forKey:CPCentersPlaceholderKey];
|
||||
|
||||
if (_recentsAutosaveName)
|
||||
[coder encodeObject:_recentsAutosaveName forKey:CPRecentsAutosaveNameKey];
|
||||
@@ -1117,6 +1185,12 @@ var CPRecentsAutosaveNameKey = @"CPRecentsAutosaveNameKey",
|
||||
_sendsSearchStringImmediately = [coder decodeBoolForKey:CPSendsSearchStringImmediatelyKey];
|
||||
_maximumRecents = [coder decodeIntForKey:CPMaximumRecentsKey];
|
||||
|
||||
/*
|
||||
Archives made before the property existed have no value for it; AppKit's default
|
||||
applies.
|
||||
*/
|
||||
_centersPlaceholder = [coder containsValueForKey:CPCentersPlaceholderKey] ? [coder decodeBoolForKey:CPCentersPlaceholderKey] : YES;
|
||||
|
||||
var template = [coder decodeObjectForKey:CPSearchMenuTemplateKey];
|
||||
|
||||
if (template)
|
||||
|
||||
@@ -3123,50 +3123,6 @@ function A3MaskIconOfSize(aMaskImage, aColor, aSize)
|
||||
@"box-shadow": @"0 0 3px rgba(255,255,255,0.3)"
|
||||
}],
|
||||
|
||||
calcRectFunctionNotEditing = "" + function(s, rect) {
|
||||
var size = [[s _potentialCurrentValueForThemeAttribute:@"image-search"] size],
|
||||
inset = [s _potentialCurrentValueForThemeAttribute:@"image-search-inset"],
|
||||
margin = [s _potentialCurrentValueForThemeAttribute:@"search-right-margin"],
|
||||
value = [s objectValue],
|
||||
placeholder = [s placeholderString],
|
||||
hasValue = ([value length] > 0),
|
||||
text = hasValue ? value : placeholder,
|
||||
labelSize = [text sizeWithFont:[s _potentialCurrentValueForThemeAttribute:@"font"]];
|
||||
|
||||
/*
|
||||
The glass and the placeholder are centred as one group. A placeholder wider than the
|
||||
field put the glass at a negative x, outside the field, so a narrow search field showed
|
||||
no glass. The glass now stays at its leading position when the group does not fit,
|
||||
and the placeholder after it is truncated.
|
||||
*/
|
||||
var leadingX = inset.left - inset.right;
|
||||
|
||||
if (hasValue || (placeholder === @" "))
|
||||
return CGRectMake(leadingX, inset.top - inset.bottom + (CGRectGetHeight(rect) - size.height) / 2, size.width, size.height);
|
||||
else
|
||||
return CGRectMake(MAX(leadingX, (rect.size.width - labelSize.width) / 2 - size.width - margin), inset.top - inset.bottom + (rect.size.height - size.height) / 2, size.width, size.height);
|
||||
},
|
||||
|
||||
calcRectFunctionEditing = "" + function(s, rect) {
|
||||
var size = [[s _potentialCurrentValueForThemeAttribute:@"image-search"] size] || CGSizeMakeZero(),
|
||||
inset = [s _potentialCurrentValueForThemeAttribute:@"image-search-inset"];
|
||||
return CGRectMake(inset.left - inset.right, inset.top - inset.bottom + (CGRectGetHeight(rect) - size.height) / 2, size.width, size.height);
|
||||
},
|
||||
|
||||
animateLayoutFunction = "" + function(s) {
|
||||
for (var i = 0, subviews = [s subviews], nb = [subviews count], textView = nil; (!textView && (i < nb)); i++)
|
||||
if ([subviews[i] isKindOfClass:_CPImageAndTextView]) textView = subviews[i];
|
||||
|
||||
[CPAnimationContext beginGrouping];
|
||||
var context = [CPAnimationContext currentContext];
|
||||
[context setDuration:0.2];
|
||||
[context setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
|
||||
[context setCompletionHandler:function() { [s themedLayoutFunctionCompletionHandler]; }];
|
||||
[[[s searchButton] animator] setFrame:[s searchButtonRectForBounds:[s bounds]]];
|
||||
[[textView animator] setFrame:[s contentRectForBounds:[s bounds]]];
|
||||
[CPAnimationContext endGrouping];
|
||||
},
|
||||
|
||||
// ==========================================================
|
||||
// 6. REGISTRATION
|
||||
// ==========================================================
|
||||
@@ -3254,10 +3210,7 @@ function A3MaskIconOfSize(aMaskImage, aColor, aSize)
|
||||
[@"bezel-color", hudBezelCssColor, [CPThemeStateHUD, CPThemeStateControlSizeMini, CPTextFieldStateRounded, CPThemeStateBezeled]],
|
||||
[@"bezel-color", hudBezelFocusedCssColor, [CPThemeStateHUD, CPThemeStateControlSizeMini, CPTextFieldStateRounded, CPThemeStateBezeled, CPThemeStateEditing]],
|
||||
|
||||
// --- Animation & Menu ---
|
||||
[@"search-button-rect-function", calcRectFunctionNotEditing, [CPTextFieldStateRounded, CPThemeStateBezeled]],
|
||||
[@"search-button-rect-function", calcRectFunctionEditing, [CPTextFieldStateRounded, CPThemeStateBezeled, CPThemeStateEditing]],
|
||||
[@"layout-function", animateLayoutFunction],
|
||||
// --- Menu ---
|
||||
[@"search-menu-offset", CGPointMake(1, 0)]
|
||||
];
|
||||
|
||||
|
||||
@@ -158,4 +158,101 @@
|
||||
[self assert:menu same:[_searchField menuForEvent:nil]];
|
||||
}
|
||||
|
||||
// MARK: Search button placement
|
||||
|
||||
/*
|
||||
The theme gives the search button its image, its inset and the margin before the text; the
|
||||
field places the button. Under ojtest no theme is loaded, so each test field gets these
|
||||
measurements as its own theme values. -sizeWithFont: uses the DOM and gives a zero size in
|
||||
the terminal, so the tests compute the expected centred position with it too.
|
||||
*/
|
||||
- (CPSearchField)placementTestFieldOfWidth:(float)aWidth class:(Class)aClass
|
||||
{
|
||||
var searchField = [[aClass alloc] initWithFrame:CGRectMake(0.0, 0.0, aWidth, 22.0)];
|
||||
|
||||
[searchField setValue:[[CPImage alloc] initWithContentsOfFile:@"search.png" size:CGSizeMake(16.0, 16.0)] forThemeAttribute:@"image-search"];
|
||||
[searchField setValue:CGInsetMake(0.0, 0.0, 0.0, 5.0) forThemeAttribute:@"image-search-inset"];
|
||||
[searchField setValue:4 forThemeAttribute:@"search-right-margin"];
|
||||
|
||||
return searchField;
|
||||
}
|
||||
|
||||
- (float)centredSearchButtonXIn:(CPSearchField)aSearchField
|
||||
{
|
||||
var placeholderWidth = [[aSearchField placeholderString] sizeWithFont:[aSearchField font]].width;
|
||||
|
||||
return MAX(5.0, (CGRectGetWidth([aSearchField bounds]) - placeholderWidth) / 2 - 16.0 - 4.0);
|
||||
}
|
||||
|
||||
- (float)searchButtonXIn:(CPSearchField)aSearchField
|
||||
{
|
||||
return CGRectGetMinX([aSearchField searchButtonRectForBounds:[aSearchField bounds]]);
|
||||
}
|
||||
|
||||
- (void)testASearchFieldCentersItsPlaceholderByDefault
|
||||
{
|
||||
[self assertTrue:[_searchField centersPlaceholder]];
|
||||
}
|
||||
|
||||
- (void)testAnEmptyFieldCentresTheSearchButtonWithThePlaceholder
|
||||
{
|
||||
var searchField = [self placementTestFieldOfWidth:200.0 class:[CPSearchField class]];
|
||||
|
||||
[self assert:[self centredSearchButtonXIn:searchField] equals:[self searchButtonXIn:searchField]];
|
||||
[self assertTrue:[self searchButtonXIn:searchField] > 5.0 message:@"the search button is not at its leading position"];
|
||||
}
|
||||
|
||||
/*
|
||||
A placeholder wider than the field must not push the search button outside it.
|
||||
*/
|
||||
- (void)testANarrowFieldKeepsTheSearchButtonAtItsLeadingPosition
|
||||
{
|
||||
var searchField = [self placementTestFieldOfWidth:30.0 class:[CPSearchField class]];
|
||||
|
||||
[self assert:5.0 equals:[self searchButtonXIn:searchField]];
|
||||
}
|
||||
|
||||
- (void)testAFieldWithAValueKeepsTheSearchButtonAtItsLeadingPosition
|
||||
{
|
||||
var searchField = [self placementTestFieldOfWidth:200.0 class:[CPSearchField class]];
|
||||
|
||||
[searchField setStringValue:@"text"];
|
||||
|
||||
[self assert:5.0 equals:[self searchButtonXIn:searchField]];
|
||||
}
|
||||
|
||||
- (void)testAnEditingFieldKeepsTheSearchButtonAtItsLeadingPosition
|
||||
{
|
||||
var searchField = [self placementTestFieldOfWidth:200.0 class:[CPSearchField class]];
|
||||
|
||||
[searchField setThemeState:CPThemeStateEditing];
|
||||
|
||||
[self assert:5.0 equals:[self searchButtonXIn:searchField]];
|
||||
}
|
||||
|
||||
- (void)testAFieldThatDoesNotCentreItsPlaceholderKeepsTheSearchButtonAtItsLeadingPosition
|
||||
{
|
||||
var searchField = [self placementTestFieldOfWidth:200.0 class:[CPSearchField class]];
|
||||
|
||||
[searchField setCentersPlaceholder:NO];
|
||||
|
||||
[self assert:5.0 equals:[self searchButtonXIn:searchField]];
|
||||
}
|
||||
|
||||
- (void)testAButtonBarSearchFieldDoesNotCentreItsPlaceholder
|
||||
{
|
||||
var searchField = [[_CPButtonBarSearchField alloc] initWithPlaceholder:@"Search" minWidth:200.0 maxWidth:200.0];
|
||||
|
||||
[self assertFalse:[searchField centersPlaceholder]];
|
||||
}
|
||||
|
||||
- (void)testCentersPlaceholderIsArchived
|
||||
{
|
||||
[_searchField setCentersPlaceholder:NO];
|
||||
|
||||
var copy = [CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:_searchField]];
|
||||
|
||||
[self assertFalse:[copy centersPlaceholder]];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user