OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/settings/autofill_edit_accessory_view.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/mac/scoped_nsobject.h" |
| 10 #import "ios/chrome/browser/ui/image_util.h" |
| 11 #include "ios/chrome/browser/ui/ui_util.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 const CGFloat kDefaultAccessoryHeight = 44; |
| 16 const CGRect kDefaultAccessoryFrame = {{0, 0}, {0, kDefaultAccessoryHeight}}; |
| 17 |
| 18 const CGFloat kDefaultAccessoryButtonWidth = kDefaultAccessoryHeight; |
| 19 const CGRect kDefaultAccessoryButtonRect = { |
| 20 {0, 0}, |
| 21 {kDefaultAccessoryButtonWidth, kDefaultAccessoryHeight}}; |
| 22 |
| 23 const CGFloat kDefaultAccessorySeparatorWidth = 1; |
| 24 const CGFloat kDefaultAccessorySeparatorHeight = kDefaultAccessoryHeight; |
| 25 const CGRect kDefaultAccessorySeparatorRect = { |
| 26 {0, 0}, |
| 27 {kDefaultAccessorySeparatorWidth, kDefaultAccessorySeparatorHeight}}; |
| 28 |
| 29 UIImage* ButtonImage(NSString* name) { |
| 30 UIImage* rawImage = [UIImage imageNamed:name]; |
| 31 return StretchableImageFromUIImage(rawImage, 1, 0); |
| 32 } |
| 33 |
| 34 UIImageView* ImageViewWithImageName(NSString* imageName) { |
| 35 UIImage* image = |
| 36 StretchableImageFromUIImage([UIImage imageNamed:imageName], 0, 0); |
| 37 |
| 38 base::scoped_nsobject<UIImageView> imageView( |
| 39 [[UIImageView alloc] initWithImage:image]); |
| 40 [imageView setTranslatesAutoresizingMaskIntoConstraints:NO]; |
| 41 [imageView setFrame:kDefaultAccessorySeparatorRect]; |
| 42 |
| 43 return imageView.autorelease(); |
| 44 } |
| 45 |
| 46 } // namespace |
| 47 |
| 48 @interface AutofillEditAccessoryView () { |
| 49 base::scoped_nsobject<UIButton> _previousButton; |
| 50 base::scoped_nsobject<UIButton> _nextButton; |
| 51 __unsafe_unretained id<AutofillEditAccessoryDelegate> _delegate; // weak |
| 52 } |
| 53 @end |
| 54 |
| 55 @implementation AutofillEditAccessoryView |
| 56 |
| 57 - (UIButton*)previousButton { |
| 58 return _previousButton; |
| 59 } |
| 60 |
| 61 - (UIButton*)nextButton { |
| 62 return _nextButton; |
| 63 } |
| 64 |
| 65 - (instancetype)initWithDelegate:(id<AutofillEditAccessoryDelegate>)delegate { |
| 66 self = [super initWithFrame:kDefaultAccessoryFrame]; |
| 67 if (!self) |
| 68 return nil; |
| 69 |
| 70 _delegate = delegate; |
| 71 [self addBackgroundImage]; |
| 72 [self setupSubviews]; |
| 73 |
| 74 return self; |
| 75 } |
| 76 |
| 77 - (void)setupSubviews { |
| 78 UIButton* previousButton = |
| 79 [self accessoryButtonWithNormalName:@"autofill_prev" |
| 80 pressedName:@"autofill_prev_pressed" |
| 81 disabledName:@"autofill_prev_inactive" |
| 82 action:@selector(previousPressed)]; |
| 83 [self addSubview:previousButton]; |
| 84 _previousButton.reset([previousButton retain]); |
| 85 |
| 86 UIImageView* firstSeparator = ImageViewWithImageName(@"autofill_middle_sep"); |
| 87 [self addSubview:firstSeparator]; |
| 88 |
| 89 UIButton* nextButton = |
| 90 [self accessoryButtonWithNormalName:@"autofill_next" |
| 91 pressedName:@"autofill_next_pressed" |
| 92 disabledName:@"autofill_next_inactive" |
| 93 action:@selector(nextPressed)]; |
| 94 [self addSubview:nextButton]; |
| 95 _nextButton.reset([nextButton retain]); |
| 96 |
| 97 UIImageView* secondSeparator = nil; |
| 98 UIButton* closeButton = nil; |
| 99 if (!IsIPadIdiom()) { |
| 100 closeButton = [self accessoryButtonWithNormalName:@"autofill_close" |
| 101 pressedName:@"autofill_close_pressed" |
| 102 disabledName:nil |
| 103 action:@selector(closePressed)]; |
| 104 |
| 105 [self addSubview:closeButton]; |
| 106 |
| 107 secondSeparator = ImageViewWithImageName(@"autofill_middle_sep"); |
| 108 [self addSubview:secondSeparator]; |
| 109 } |
| 110 |
| 111 NSDictionary* bindings = NSDictionaryOfVariableBindings( |
| 112 previousButton, firstSeparator, nextButton); |
| 113 NSString* horizontalLayout = |
| 114 @"H:[previousButton][firstSeparator][nextButton]|"; |
| 115 if (closeButton) { |
| 116 bindings = NSDictionaryOfVariableBindings(previousButton, firstSeparator, |
| 117 nextButton, secondSeparator, |
| 118 closeButton); |
| 119 horizontalLayout = @"H:[previousButton][firstSeparator][nextButton][" |
| 120 @"secondSeparator][closeButton]|"; |
| 121 } |
| 122 |
| 123 [self addConstraints:[NSLayoutConstraint |
| 124 constraintsWithVisualFormat:horizontalLayout |
| 125 options:0 |
| 126 metrics:0 |
| 127 views:bindings]]; |
| 128 [self addConstraints:[NSLayoutConstraint |
| 129 constraintsWithVisualFormat:@"V:[previousButton]|" |
| 130 options:0 |
| 131 metrics:0 |
| 132 views:bindings]]; |
| 133 [self addConstraints:[NSLayoutConstraint |
| 134 constraintsWithVisualFormat:@"V:[firstSeparator]|" |
| 135 options:0 |
| 136 metrics:0 |
| 137 views:bindings]]; |
| 138 [self addConstraints:[NSLayoutConstraint |
| 139 constraintsWithVisualFormat:@"V:[nextButton]|" |
| 140 options:0 |
| 141 metrics:nil |
| 142 views:bindings]]; |
| 143 |
| 144 if (closeButton) { |
| 145 [self addConstraints:[NSLayoutConstraint |
| 146 constraintsWithVisualFormat:@"V:[secondSeparator]|" |
| 147 options:0 |
| 148 metrics:nil |
| 149 views:bindings]]; |
| 150 [self addConstraints:[NSLayoutConstraint |
| 151 constraintsWithVisualFormat:@"V:[closeButton]|" |
| 152 options:0 |
| 153 metrics:nil |
| 154 views:bindings]]; |
| 155 } |
| 156 } |
| 157 |
| 158 - (UIButton*)accessoryButtonWithNormalName:(NSString*)normalName |
| 159 pressedName:(NSString*)pressedName |
| 160 disabledName:(NSString*)disabledName |
| 161 action:(SEL)action { |
| 162 UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 163 |
| 164 [button setTranslatesAutoresizingMaskIntoConstraints:NO]; |
| 165 [button setFrame:kDefaultAccessoryButtonRect]; |
| 166 |
| 167 [button setBackgroundImage:ButtonImage(normalName) |
| 168 forState:UIControlStateNormal]; |
| 169 [button setBackgroundImage:ButtonImage(pressedName) |
| 170 forState:UIControlStateSelected]; |
| 171 [button setBackgroundImage:ButtonImage(pressedName) |
| 172 forState:UIControlStateHighlighted]; |
| 173 |
| 174 if (disabledName) { |
| 175 [button setBackgroundImage:ButtonImage(disabledName) |
| 176 forState:UIControlStateDisabled]; |
| 177 } |
| 178 |
| 179 [button addTarget:_delegate |
| 180 action:action |
| 181 forControlEvents:UIControlEventTouchUpInside]; |
| 182 |
| 183 return button; |
| 184 } |
| 185 |
| 186 - (void)addBackgroundImage { |
| 187 UIImage* backgroundImage = |
| 188 StretchableImageNamed(@"autofill_keyboard_background"); |
| 189 |
| 190 UIImageView* backgroundImageView = |
| 191 [[UIImageView alloc] initWithFrame:[self bounds]]; |
| 192 [backgroundImageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; |
| 193 [backgroundImageView setImage:backgroundImage]; |
| 194 [self insertSubview:backgroundImageView atIndex:0]; |
| 195 [backgroundImageView release]; |
| 196 } |
| 197 |
| 198 @end |
OLD | NEW |