OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/history/history_search_view.h" |
| 6 |
| 7 #include "base/mac/objc_property_releaser.h" |
| 8 #include "components/strings/grit/components_strings.h" |
| 9 #include "ios/chrome/browser/ui/uikit_ui_util.h" |
| 10 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" |
| 11 #include "ui/base/l10n/l10n_util_mac.h" |
| 12 |
| 13 namespace { |
| 14 // Shadow opacity for the search view. |
| 15 CGFloat kShadowOpacity = 0.2f; |
| 16 // Margin for the search view. |
| 17 CGFloat kHorizontalMargin = 16.0f; |
| 18 } // namespace |
| 19 |
| 20 @interface HistorySearchView () { |
| 21 base::mac::ObjCPropertyReleaser propertyReleaser_HistorySearchView_; |
| 22 } |
| 23 |
| 24 // Stack view for laying out the text field and cancel button. |
| 25 @property(nonatomic, strong) UIStackView* stackView; |
| 26 // Text field for the search view. |
| 27 @property(nonatomic, strong) UITextField* textField; |
| 28 // Cancel button for dismissing the search view. |
| 29 @property(nonatomic, strong) UIButton* cancelButton; |
| 30 // Constraint for the top anchor. |
| 31 @property(nonatomic, strong) NSLayoutConstraint* topAnchorConstraint; |
| 32 |
| 33 @end |
| 34 |
| 35 @implementation HistorySearchView |
| 36 |
| 37 @synthesize enabled = _enabled; |
| 38 @synthesize stackView = _stackView; |
| 39 @synthesize textField = _textField; |
| 40 @synthesize cancelButton = _cancelButton; |
| 41 @synthesize topAnchorConstraint = _topAnchorConstraint; |
| 42 |
| 43 - (instancetype)initWithFrame:(CGRect)frame { |
| 44 self = [super initWithFrame:frame]; |
| 45 if (self) { |
| 46 [self setBackgroundColor:[UIColor whiteColor]]; |
| 47 [[self layer] setShadowOpacity:kShadowOpacity]; |
| 48 |
| 49 _textField = [[UITextField alloc] init]; |
| 50 _textField.contentVerticalAlignment = |
| 51 UIControlContentVerticalAlignmentCenter; |
| 52 _textField.backgroundColor = [UIColor whiteColor]; |
| 53 _textField.textColor = |
| 54 [UIColor colorWithWhite:0 alpha:[MDCTypography body1FontOpacity]]; |
| 55 _textField.font = [MDCTypography subheadFont]; |
| 56 _textField.borderStyle = UITextBorderStyleNone; |
| 57 [_textField setLeftViewMode:UITextFieldViewModeNever]; |
| 58 _textField.clearButtonMode = UITextFieldViewModeAlways; |
| 59 _textField.placeholder = l10n_util::GetNSString(IDS_HISTORY_SEARCH_BUTTON); |
| 60 |
| 61 _cancelButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; |
| 62 [_cancelButton setImage:[UIImage imageNamed:@"collapse"] |
| 63 forState:UIControlStateNormal]; |
| 64 [_cancelButton setImage:[UIImage imageNamed:@"collapse_pressed"] |
| 65 forState:UIControlStateHighlighted]; |
| 66 [_cancelButton |
| 67 setContentCompressionResistancePriority:UILayoutPriorityRequired |
| 68 forAxis: |
| 69 UILayoutConstraintAxisHorizontal]; |
| 70 [_cancelButton setContentHuggingPriority:UILayoutPriorityRequired |
| 71 forAxis:UILayoutConstraintAxisHorizontal]; |
| 72 [_cancelButton setAccessibilityLabel:l10n_util::GetNSString(IDS_CANCEL)]; |
| 73 |
| 74 _stackView = [[UIStackView alloc] |
| 75 initWithArrangedSubviews:@[ _textField, _cancelButton ]]; |
| 76 _stackView.alignment = UIStackViewAlignmentFill; |
| 77 _stackView.axis = UILayoutConstraintAxisHorizontal; |
| 78 _stackView.distribution = UIStackViewDistributionFill; |
| 79 [self addSubview:_stackView]; |
| 80 _stackView.translatesAutoresizingMaskIntoConstraints = NO; |
| 81 _stackView.layoutMarginsRelativeArrangement = YES; |
| 82 |
| 83 CGFloat topAnchorConstant = IsCompact() ? StatusBarHeight() : 0; |
| 84 _topAnchorConstraint = [[_stackView.topAnchor |
| 85 constraintEqualToAnchor:self.topAnchor |
| 86 constant:topAnchorConstant] retain]; |
| 87 [NSLayoutConstraint activateConstraints:@[ |
| 88 _topAnchorConstraint, |
| 89 [_stackView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor], |
| 90 [_stackView.layoutMarginsGuide.leadingAnchor |
| 91 constraintEqualToAnchor:self.leadingAnchor |
| 92 constant:kHorizontalMargin], |
| 93 [_stackView.layoutMarginsGuide.trailingAnchor |
| 94 constraintEqualToAnchor:self.trailingAnchor |
| 95 constant:-kHorizontalMargin], |
| 96 ]]; |
| 97 |
| 98 propertyReleaser_HistorySearchView_.Init(self, [HistorySearchView class]); |
| 99 } |
| 100 return self; |
| 101 } |
| 102 |
| 103 - (BOOL)becomeFirstResponder { |
| 104 return [self.textField becomeFirstResponder]; |
| 105 } |
| 106 |
| 107 - (void)setEnabled:(BOOL)enabled { |
| 108 _enabled = enabled; |
| 109 self.cancelButton.enabled = enabled; |
| 110 self.textField.enabled = enabled; |
| 111 self.textField.clearButtonMode = |
| 112 enabled ? UITextFieldViewModeAlways : UITextFieldViewModeNever; |
| 113 } |
| 114 |
| 115 - (void)setCancelTarget:(id)target action:(SEL)action { |
| 116 [_cancelButton addTarget:target |
| 117 action:action |
| 118 forControlEvents:UIControlEventTouchUpInside]; |
| 119 } |
| 120 |
| 121 - (void)setSearchBarDelegate:(id<UITextFieldDelegate>)delegate { |
| 122 [self.textField setDelegate:delegate]; |
| 123 } |
| 124 |
| 125 - (void)clearText { |
| 126 self.textField.text = nil; |
| 127 } |
| 128 |
| 129 #pragma mark - UITraitEnvironment |
| 130 |
| 131 - (void)traitCollectionDidChange:(UITraitCollection*)previousTraitCollection { |
| 132 self.topAnchorConstraint.constant = IsCompact() ? StatusBarHeight() : 0; |
| 133 } |
| 134 |
| 135 @end |
OLD | NEW |