Commit 5cc85545 authored by stkhapugin@chromium.org's avatar stkhapugin@chromium.org Committed by Commit Bot

Implement a custom clear button with custom graphics.

Bug: 852787
Cq-Include-Trybots: luci.chromium.try:ios-simulator-full-configs;master.tryserver.chromium.mac:ios-simulator-cronet
Change-Id: Ied16b9651281b7907e8628752938a2d6b42ae9c3
Reviewed-on: https://chromium-review.googlesource.com/1106337
Commit-Queue: Stepan Khapugin <stkhapugin@chromium.org>
Reviewed-by: default avatarJustin Cohen <justincohen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#568802}
parent 6e492278
...@@ -92,6 +92,7 @@ source_set("omnibox_internal") { ...@@ -92,6 +92,7 @@ source_set("omnibox_internal") {
":omnibox_popup_shared", ":omnibox_popup_shared",
":omnibox_util", ":omnibox_util",
"resources:omnibox_background", "resources:omnibox_background",
"resources:omnibox_clear_icon",
"resources:omnibox_transparent_background", "resources:omnibox_transparent_background",
"//base", "//base",
"//components/favicon/ios", "//components/favicon/ios",
......
...@@ -130,8 +130,7 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation"; ...@@ -130,8 +130,7 @@ NSString* const kOmniboxFadeAnimationKey = @"OmniboxFadeAnimation";
[self setKeyboardType:(UIKeyboardType)UIKeyboardTypeWebSearch]; [self setKeyboardType:(UIKeyboardType)UIKeyboardTypeWebSearch];
if (IsRefreshLocationBarEnabled()) { if (IsRefreshLocationBarEnabled()) {
[self setClearButtonMode:UITextFieldViewModeWhileEditing]; // The right view mode is managed by the view controller.
[self setRightViewMode:UITextFieldViewModeNever];
} else { } else {
[self setClearButtonMode:UITextFieldViewModeNever]; [self setClearButtonMode:UITextFieldViewModeNever];
[self setRightViewMode:UITextFieldViewModeAlways]; [self setRightViewMode:UITextFieldViewModeAlways];
......
...@@ -10,12 +10,19 @@ ...@@ -10,12 +10,19 @@
#include "ios/chrome/browser/ui/ui_util.h" #include "ios/chrome/browser/ui/ui_util.h"
#include "ios/chrome/browser/ui/uikit_ui_util.h" #include "ios/chrome/browser/ui/uikit_ui_util.h"
#import "ios/chrome/browser/ui/uikit_ui_util.h" #import "ios/chrome/browser/ui/uikit_ui_util.h"
#include "ios/chrome/grit/ios_strings.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
namespace {
const CGFloat kClearButtonSize = 28.0f;
} // namespace
@interface OmniboxViewController () @interface OmniboxViewController ()
// Override of UIViewController's view with a different type. // Override of UIViewController's view with a different type.
...@@ -63,12 +70,10 @@ ...@@ -63,12 +70,10 @@
- (void)viewDidLoad { - (void)viewDidLoad {
[super viewDidLoad]; [super viewDidLoad];
if (self.incognito) {
self.textField.placeholderTextColor = [UIColor colorWithWhite:1 alpha:0.5]; self.textField.placeholderTextColor = [self placeholderAndClearButtonColor];
} else {
self.textField.placeholderTextColor = [UIColor colorWithWhite:0 alpha:0.3];
}
self.textField.placeholder = l10n_util::GetNSString(IDS_OMNIBOX_EMPTY_HINT); self.textField.placeholder = l10n_util::GetNSString(IDS_OMNIBOX_EMPTY_HINT);
[self setupClearButton];
} }
- (void)traitCollectionDidChange:(UITraitCollection*)previousTraitCollection { - (void)traitCollectionDidChange:(UITraitCollection*)previousTraitCollection {
...@@ -90,8 +95,49 @@ ...@@ -90,8 +95,49 @@
#pragma mark - private #pragma mark - private
- (void)setupClearButton {
// Do not use the system clear button. Use a custom "right view" instead.
// Note that |rightView| is an incorrect name, it's really a trailing view.
[self.textField setClearButtonMode:UITextFieldViewModeNever];
[self.textField setRightViewMode:UITextFieldViewModeAlways];
UIButton* clearButton = [UIButton buttonWithType:UIButtonTypeSystem];
clearButton.frame = CGRectMake(0, 0, kClearButtonSize, kClearButtonSize);
[clearButton setImage:[self clearButtonIcon] forState:UIControlStateNormal];
[clearButton addTarget:self
action:@selector(clearButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
self.textField.rightView = clearButton;
clearButton.tintColor = [self placeholderAndClearButtonColor];
SetA11yLabelAndUiAutomationName(clearButton, IDS_IOS_ACCNAME_CLEAR_TEXT,
@"Clear Text");
}
- (UIImage*)clearButtonIcon {
UIImage* image = [[UIImage imageNamed:@"omnibox_clear_icon"]
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
return image;
}
- (void)clearButtonPressed {
// Emulate a system button clear callback.
BOOL shouldClear =
[self.textField.delegate textFieldShouldClear:self.textField];
if (shouldClear) {
[self.textField setText:@""];
}
}
- (void)updateLeadingImageVisibility { - (void)updateLeadingImageVisibility {
[self.view setLeadingImageHidden:!IsRegularXRegularSizeClass(self)]; [self.view setLeadingImageHidden:!IsRegularXRegularSizeClass(self)];
} }
// Tint color for the textfield placeholder and the clear button.
- (UIColor*)placeholderAndClearButtonColor {
return self.incognito ? [UIColor colorWithWhite:1 alpha:0.5]
: [UIColor colorWithWhite:0 alpha:0.3];
}
@end @end
...@@ -786,7 +786,7 @@ void OmniboxViewIOS::UpdateAppearance() { ...@@ -786,7 +786,7 @@ void OmniboxViewIOS::UpdateAppearance() {
void OmniboxViewIOS::CreateClearTextIcon(bool is_incognito) { void OmniboxViewIOS::CreateClearTextIcon(bool is_incognito) {
if (IsRefreshLocationBarEnabled()) { if (IsRefreshLocationBarEnabled()) {
// In UI Refresh, the system clear button is used. // In UI Refresh, the view controller sets up the clear button.
return; return;
} }
......
...@@ -21,3 +21,12 @@ imageset("omnibox_background") { ...@@ -21,3 +21,12 @@ imageset("omnibox_background") {
"omnibox_background.imageset/omnibox_background@3x.png", "omnibox_background.imageset/omnibox_background@3x.png",
] ]
} }
imageset("omnibox_clear_icon") {
sources = [
"omnibox_clear_icon.imageset/Contents.json",
"omnibox_clear_icon.imageset/omnibox_clear_icon.png",
"omnibox_clear_icon.imageset/omnibox_clear_icon@2x.png",
"omnibox_clear_icon.imageset/omnibox_clear_icon@3x.png",
]
}
{
"images": [
{
"idiom": "universal",
"scale": "1x",
"filename": "omnibox_clear_icon.png"
},
{
"idiom": "universal",
"scale": "2x",
"filename": "omnibox_clear_icon@2x.png"
},
{
"idiom": "universal",
"scale": "3x",
"filename": "omnibox_clear_icon@3x.png"
}
],
"info": {
"version": 1,
"author": "xcode"
}
}
...@@ -389,12 +389,7 @@ using chrome_test_util::SystemSelectionCalloutCopyButton; ...@@ -389,12 +389,7 @@ using chrome_test_util::SystemSelectionCalloutCopyButton;
[[EarlGrey selectElementWithMatcher:chrome_test_util::Omnibox()] [[EarlGrey selectElementWithMatcher:chrome_test_util::Omnibox()]
assertWithMatcher:chrome_test_util::OmniboxText("foo")]; assertWithMatcher:chrome_test_util::OmniboxText("foo")];
id<GREYMatcher> cancelButton = nil; id<GREYMatcher> cancelButton = grey_accessibilityLabel(@"Clear Text");
if (IsRefreshLocationBarEnabled()) {
cancelButton = grey_accessibilityLabel(@"Clear text");
} else {
cancelButton = grey_accessibilityLabel(@"Clear Text");
}
[[EarlGrey selectElementWithMatcher:cancelButton] performAction:grey_tap()]; [[EarlGrey selectElementWithMatcher:cancelButton] performAction:grey_tap()];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment