Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Side by Side Diff: ios/clean/chrome/browser/ui/tools/menu_view_controller.mm

Issue 2901663002: [ios clean] Fixes Menu Overflow Controls UI (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ios/clean/chrome/browser/ui/tools/menu_overflow_controls_stackview.mm ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "ios/clean/chrome/browser/ui/tools/menu_view_controller.h" 5 #import "ios/clean/chrome/browser/ui/tools/menu_view_controller.h"
6 6
7 #include "base/i18n/rtl.h" 7 #include "base/i18n/rtl.h"
8 #import "base/logging.h" 8 #import "base/logging.h"
9 #import "base/mac/foundation_util.h" 9 #import "base/mac/foundation_util.h"
10 #import "base/macros.h" 10 #import "base/macros.h"
11 #import "ios/chrome/browser/ui/rtl_geometry.h" 11 #import "ios/chrome/browser/ui/rtl_geometry.h"
12 #import "ios/chrome/browser/ui/uikit_ui_util.h"
13 #include "ios/chrome/grit/ios_theme_resources.h"
12 #import "ios/clean/chrome/browser/ui/commands/find_in_page_visibility_commands.h " 14 #import "ios/clean/chrome/browser/ui/commands/find_in_page_visibility_commands.h "
13 #import "ios/clean/chrome/browser/ui/commands/navigation_commands.h" 15 #import "ios/clean/chrome/browser/ui/commands/navigation_commands.h"
14 #import "ios/clean/chrome/browser/ui/commands/tools_menu_commands.h" 16 #import "ios/clean/chrome/browser/ui/commands/tools_menu_commands.h"
15 #import "ios/clean/chrome/browser/ui/toolbar/toolbar_button+factory.h" 17 #import "ios/clean/chrome/browser/ui/toolbar/toolbar_button+factory.h"
16 #import "ios/clean/chrome/browser/ui/toolbar/toolbar_button.h" 18 #import "ios/clean/chrome/browser/ui/toolbar/toolbar_button.h"
19 #import "ios/clean/chrome/browser/ui/toolbar/toolbar_constants.h"
17 #import "ios/clean/chrome/browser/ui/tools/menu_overflow_controls_stackview.h" 20 #import "ios/clean/chrome/browser/ui/tools/menu_overflow_controls_stackview.h"
18 #import "ios/clean/chrome/browser/ui/tools/tools_actions.h" 21 #import "ios/clean/chrome/browser/ui/tools/tools_actions.h"
19 #import "ios/clean/chrome/browser/ui/tools/tools_menu_item.h" 22 #import "ios/clean/chrome/browser/ui/tools/tools_menu_item.h"
20 #import "ios/third_party/material_components_ios/src/components/Typography/src/M aterialTypography.h" 23 #import "ios/third_party/material_components_ios/src/components/Typography/src/M aterialTypography.h"
21 24
22 #if !defined(__has_feature) || !__has_feature(objc_arc) 25 #if !defined(__has_feature) || !__has_feature(objc_arc)
23 #error "This file requires ARC support." 26 #error "This file requires ARC support."
24 #endif 27 #endif
25 28
26 namespace { 29 namespace {
27 const CGFloat kMenuWidth = 250.0; 30 const CGFloat kMenuWidth = 250.0;
28 const CGFloat kMenuItemHeight = 48.0; 31 const CGFloat kMenuItemHeight = 48.0;
29 const CGFloat kMenuItemLeadingEdgeInset = 10.0; 32 const CGFloat kMenuItemLeadingEdgeInset = 12.0;
30 const CGFloat kOverflowControlsMargin = 50.0; 33 const CGFloat kOverflowControlsMargin = 58.0;
31 const CGFloat kCloseButtonHeight = 44.0; 34 const CGFloat kCloseButtonHeight = 44.0;
32 } 35 }
33 36
34 @interface MenuViewController ()<ToolsActions> 37 @interface MenuViewController ()<ToolsActions>
35 @property(nonatomic, strong) UIScrollView* menuScrollView; 38 @property(nonatomic, strong) UIScrollView* menuScrollView;
36 @property(nonatomic, strong) UIStackView* menuStackView; 39 @property(nonatomic, strong) UIStackView* menuStackView;
37 @property(nonatomic, strong) NSArray<ToolsMenuItem*>* menuItems; 40 @property(nonatomic, strong) NSArray<ToolsMenuItem*>* menuItems;
38 @property(nonatomic, strong) 41 @property(nonatomic, strong)
39 MenuOverflowControlsStackView* toolbarOverflowStackView; 42 MenuOverflowControlsStackView* toolbarOverflowStackView;
40 @property(nonatomic, assign) BOOL displayOverflowControls; 43 @property(nonatomic, assign) BOOL displayOverflowControls;
41 @property(nonatomic, strong) ToolbarButton* closeMenuButton; 44 @property(nonatomic, strong) ToolbarButton* closeMenuButton;
45 @property(nonatomic, assign) BOOL currentPageLoading;
42 46
43 // Sets up the main StackView and creates a button for each Menu item. 47 // Sets up the main StackView and creates a button for each Menu item.
44 - (void)setupMenuStackView; 48 - (void)setupMenuStackView;
45 // Sets up the Overflow navigation controls stack view. 49 // Sets up the Overflow navigation controls stack view.
46 - (void)setUpOverFlowControlsStackView; 50 - (void)setUpOverFlowControlsStackView;
47 // Sets up and activates all the View constraints. 51 // Sets up and activates all the View constraints.
48 - (void)setupConstraints; 52 - (void)setupConstraints;
49 @end 53 @end
50 54
51 @implementation MenuViewController 55 @implementation MenuViewController
52 @synthesize dispatcher = _dispatcher; 56 @synthesize dispatcher = _dispatcher;
53 @synthesize menuItems = _menuItems; 57 @synthesize menuItems = _menuItems;
54 @synthesize menuStackView = _menuStackView; 58 @synthesize menuStackView = _menuStackView;
55 @synthesize toolbarOverflowStackView = _toolbarOverflowStackView; 59 @synthesize toolbarOverflowStackView = _toolbarOverflowStackView;
56 @synthesize displayOverflowControls = _displayOverflowControls; 60 @synthesize displayOverflowControls = _displayOverflowControls;
57 @synthesize menuScrollView = _menuScrollView; 61 @synthesize menuScrollView = _menuScrollView;
58 @synthesize closeMenuButton = _closeMenuButton; 62 @synthesize closeMenuButton = _closeMenuButton;
63 @synthesize currentPageLoading = _currentPageLoading;
59 64
60 #pragma mark - View Lifecycle 65 #pragma mark - View Lifecycle
61 66
62 - (void)loadView { 67 - (void)loadView {
63 CGRect frame; 68 CGRect frame;
64 // Set the MenuVC view height depending on the current screen height. 69 // Set the MenuVC view height depending on the current screen height.
65 CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height; 70 CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
66 CGFloat itemsHeight = kMenuItemHeight * _menuItems.count; 71 CGFloat itemsHeight = kMenuItemHeight * _menuItems.count;
67 CGFloat menuHeight = 72 CGFloat menuHeight =
68 itemsHeight > screenHeight ? screenHeight - kMenuItemHeight : itemsHeight; 73 itemsHeight > screenHeight ? screenHeight - kMenuItemHeight : itemsHeight;
69 frame.size = CGSizeMake(kMenuWidth, menuHeight); 74 frame.size = CGSizeMake(kMenuWidth, menuHeight);
70 frame.origin = CGPointZero; 75 frame.origin = CGPointZero;
71 self.view = [[UIView alloc] initWithFrame:frame]; 76 self.view = [[UIView alloc] initWithFrame:frame];
72 self.view.backgroundColor = [UIColor whiteColor]; 77 self.view.backgroundColor = [UIColor whiteColor];
73 self.view.autoresizingMask = UIViewAutoresizingNone; 78 self.view.autoresizingMask = UIViewAutoresizingNone;
74 self.view.layer.borderColor = [UIColor clearColor].CGColor; 79 self.view.layer.borderColor = [UIColor clearColor].CGColor;
75 } 80 }
76 81
77 - (void)viewDidLoad { 82 - (void)viewDidLoad {
78 self.menuScrollView = [[UIScrollView alloc] init]; 83 self.menuScrollView = [[UIScrollView alloc] init];
79 self.menuScrollView.translatesAutoresizingMaskIntoConstraints = NO; 84 self.menuScrollView.translatesAutoresizingMaskIntoConstraints = NO;
80 [self.view addSubview:self.menuScrollView]; 85 [self.view addSubview:self.menuScrollView];
81 86
82 // Add close tools menu button. This button is fixed on the top right corner 87 // PLACEHOLDER: Hardcoded value until the mediator observes the Webstate.
83 // and will always be visible. 88 self.currentPageLoading = NO;
84 self.closeMenuButton = [ToolbarButton toolsMenuToolbarButton];
85 [self.closeMenuButton addTarget:self.dispatcher
86 action:@selector(closeToolsMenu)
87 forControlEvents:UIControlEventTouchUpInside];
88 [self.view addSubview:self.closeMenuButton];
89 89
90 [self setupCloseMenuButton];
90 [self setupMenuStackView]; 91 [self setupMenuStackView];
91 [self setupConstraints]; 92 [self setupConstraints];
92 } 93 }
93 94
94 #pragma mark - UI Setup 95 #pragma mark - UI Setup
95 96
97 - (void)setupCloseMenuButton {
98 // Add close tools menu button. This button is fixed on the top right corner
99 // and will always be visible.
100 self.closeMenuButton = [ToolbarButton
101 toolbarButtonWithImageForNormalState:
102 NativeImage(IDR_IOS_TOOLBAR_LIGHT_TOOLS_PRESSED)
103 imageForHighlightedState:NativeImage(
104 IDR_IOS_TOOLBAR_LIGHT_TOOLS)
105 imageForDisabledState:nil];
106 [self.closeMenuButton
107 setImageEdgeInsets:UIEdgeInsetsMakeDirected(0, -3, 0, 0)];
108 [self.closeMenuButton addTarget:self.dispatcher
109 action:@selector(closeToolsMenu)
110 forControlEvents:UIControlEventTouchUpInside];
111
112 NSLayoutConstraint* widthConstraint = [self.closeMenuButton.widthAnchor
113 constraintEqualToConstant:kToolbarButtonWidth];
114 widthConstraint.priority = UILayoutPriorityDefaultHigh;
115 NSLayoutConstraint* heightConstraint = [self.closeMenuButton.heightAnchor
116 constraintEqualToConstant:kCloseButtonHeight];
117 heightConstraint.priority = UILayoutPriorityDefaultHigh;
118 [NSLayoutConstraint
119 activateConstraints:@[ widthConstraint, heightConstraint ]];
120 [self.view addSubview:self.closeMenuButton];
121 }
122
96 - (void)setupMenuStackView { 123 - (void)setupMenuStackView {
97 NSMutableArray<UIButton*>* buttons = 124 NSMutableArray<UIButton*>* buttons =
98 [[NSMutableArray alloc] initWithCapacity:_menuItems.count]; 125 [[NSMutableArray alloc] initWithCapacity:_menuItems.count];
99 // Load menu items. 126 // Load menu items.
100 for (ToolsMenuItem* item in _menuItems) { 127 for (ToolsMenuItem* item in _menuItems) {
101 UIButton* menuButton = [UIButton buttonWithType:UIButtonTypeSystem]; 128 UIButton* menuButton = [UIButton buttonWithType:UIButtonTypeSystem];
102 menuButton.translatesAutoresizingMaskIntoConstraints = NO; 129 menuButton.translatesAutoresizingMaskIntoConstraints = NO;
103 menuButton.tintColor = [UIColor blackColor]; 130 menuButton.tintColor = [UIColor blackColor];
131 // Button constraints will be changed in order to match the menu width, for
132 // this reason the content will be centered if no content alignment is set.
133 menuButton.contentHorizontalAlignment =
134 UseRTLLayout() ? UIControlContentHorizontalAlignmentRight
135 : UIControlContentHorizontalAlignmentLeft;
104 [menuButton setTitle:item.title forState:UIControlStateNormal]; 136 [menuButton setTitle:item.title forState:UIControlStateNormal];
105 [menuButton setContentEdgeInsets:UIEdgeInsetsMakeDirected( 137 [menuButton setContentEdgeInsets:UIEdgeInsetsMakeDirected(
106 0, kMenuItemLeadingEdgeInset, 0, 0)]; 138 0, kMenuItemLeadingEdgeInset, 0, 0)];
107 [menuButton.titleLabel setFont:[MDCTypography subheadFont]]; 139 [menuButton.titleLabel setFont:[MDCTypography subheadFont]];
108 [menuButton.titleLabel setTextAlignment:NSTextAlignmentNatural]; 140 [menuButton.titleLabel setTextAlignment:NSTextAlignmentNatural];
109 [menuButton addTarget:self.dispatcher 141 [menuButton addTarget:self.dispatcher
110 action:@selector(closeToolsMenu) 142 action:@selector(closeToolsMenu)
111 forControlEvents:UIControlEventTouchUpInside]; 143 forControlEvents:UIControlEventTouchUpInside];
112 if (item.action) { 144 if (item.action) {
113 [menuButton addTarget:self.dispatcher 145 [menuButton addTarget:self.dispatcher
114 action:item.action 146 action:item.action
115 forControlEvents:UIControlEventTouchUpInside]; 147 forControlEvents:UIControlEventTouchUpInside];
116 } 148 }
117 [buttons addObject:menuButton]; 149 [buttons addObject:menuButton];
118 } 150 }
119 151
120 self.menuStackView = [[UIStackView alloc] initWithArrangedSubviews:buttons]; 152 self.menuStackView = [[UIStackView alloc] initWithArrangedSubviews:buttons];
121 self.menuStackView.translatesAutoresizingMaskIntoConstraints = NO; 153 self.menuStackView.translatesAutoresizingMaskIntoConstraints = NO;
122 self.menuStackView.axis = UILayoutConstraintAxisVertical; 154 self.menuStackView.axis = UILayoutConstraintAxisVertical;
123 self.menuStackView.distribution = UIStackViewDistributionFillEqually; 155 self.menuStackView.distribution = UIStackViewDistributionFillEqually;
124 self.menuStackView.alignment = UIStackViewAlignmentLeading; 156 self.menuStackView.alignment = UIStackViewAlignmentLeading;
125 157
158 // Set button constraints so they have the same width as the StackView that
159 // contains them.
160 NSMutableArray* buttonConstraints = [[NSMutableArray alloc] init];
161 for (UIView* view in self.menuStackView.arrangedSubviews) {
162 [buttonConstraints
163 addObject:[view.leadingAnchor
164 constraintEqualToAnchor:self.menuStackView
165 .leadingAnchor]];
166 [buttonConstraints
167 addObject:[view.trailingAnchor
168 constraintEqualToAnchor:self.menuStackView
169 .trailingAnchor]];
170 }
171 [NSLayoutConstraint activateConstraints:buttonConstraints];
172
126 // Stack view to hold overflow ToolbarButtons. 173 // Stack view to hold overflow ToolbarButtons.
127 if (self.traitCollection.horizontalSizeClass == 174 if (self.traitCollection.horizontalSizeClass ==
128 UIUserInterfaceSizeClassCompact && 175 UIUserInterfaceSizeClassCompact &&
129 self.displayOverflowControls) { 176 self.displayOverflowControls) {
130 [self setUpOverFlowControlsStackView]; 177 [self setUpOverFlowControlsStackView];
131 } 178 }
132 } 179 }
133 180
134 - (void)setUpOverFlowControlsStackView { 181 - (void)setUpOverFlowControlsStackView {
135 self.toolbarOverflowStackView = [[MenuOverflowControlsStackView alloc] init]; 182 self.toolbarOverflowStackView = [[MenuOverflowControlsStackView alloc] init];
136 // PLACEHOLDER: ToolsMenuButton might end up being part of the MenuVC's view
137 // instead of the StackView. We are waiting confirmation on this.
138 for (UIView* view in self.toolbarOverflowStackView.arrangedSubviews) { 183 for (UIView* view in self.toolbarOverflowStackView.arrangedSubviews) {
139 if ([view isKindOfClass:[ToolbarButton class]]) { 184 if ([view isKindOfClass:[ToolbarButton class]]) {
140 ToolbarButton* button = base::mac::ObjCCastStrict<ToolbarButton>(view); 185 ToolbarButton* button = base::mac::ObjCCastStrict<ToolbarButton>(view);
141 [button addTarget:self.dispatcher 186 [button addTarget:self.dispatcher
142 action:@selector(closeToolsMenu) 187 action:@selector(closeToolsMenu)
143 forControlEvents:UIControlEventTouchUpInside]; 188 forControlEvents:UIControlEventTouchUpInside];
144 } 189 }
145 } 190 }
191 self.toolbarOverflowStackView.reloadButton.hidden = self.currentPageLoading;
192 self.toolbarOverflowStackView.stopButton.hidden = !self.currentPageLoading;
146 [self.toolbarOverflowStackView.reloadButton 193 [self.toolbarOverflowStackView.reloadButton
147 addTarget:self.dispatcher 194 addTarget:self.dispatcher
148 action:@selector(reloadPage) 195 action:@selector(reloadPage)
149 forControlEvents:UIControlEventTouchUpInside]; 196 forControlEvents:UIControlEventTouchUpInside];
150 [self.toolbarOverflowStackView.stopButton 197 [self.toolbarOverflowStackView.stopButton
151 addTarget:self.dispatcher 198 addTarget:self.dispatcher
152 action:@selector(stopLoadingPage) 199 action:@selector(stopLoadingPage)
153 forControlEvents:UIControlEventTouchUpInside]; 200 forControlEvents:UIControlEventTouchUpInside];
154 201
155 [self.menuStackView insertArrangedSubview:self.toolbarOverflowStackView 202 [self.menuStackView insertArrangedSubview:self.toolbarOverflowStackView
156 atIndex:0]; 203 atIndex:0];
157 [NSLayoutConstraint activateConstraints:@[ 204 NSLayoutConstraint* leadingConstraint =
158 [self.toolbarOverflowStackView.leadingAnchor 205 [self.toolbarOverflowStackView.leadingAnchor
159 constraintEqualToAnchor:self.menuStackView.leadingAnchor], 206 constraintEqualToAnchor:self.menuStackView.leadingAnchor];
160 [self.toolbarOverflowStackView.trailingAnchor 207 leadingConstraint.priority = UILayoutPriorityDefaultHigh;
161 constraintEqualToAnchor:self.menuStackView.trailingAnchor 208 NSLayoutConstraint* trailingConstraint =
162 constant:-kOverflowControlsMargin], 209 [self.toolbarOverflowStackView.trailingAnchor
163 ]]; 210 constraintEqualToAnchor:self.menuStackView.trailingAnchor
211 constant:-kOverflowControlsMargin];
212 trailingConstraint.priority = UILayoutPriorityDefaultHigh;
213 [NSLayoutConstraint
214 activateConstraints:@[ leadingConstraint, trailingConstraint ]];
164 } 215 }
165 216
166 - (void)setupConstraints { 217 - (void)setupConstraints {
167 [self.menuScrollView addSubview:self.menuStackView]; 218 [self.menuScrollView addSubview:self.menuStackView];
168 [NSLayoutConstraint activateConstraints:@[ 219 [NSLayoutConstraint activateConstraints:@[
169 // ScrollView Constraints. 220 // ScrollView Constraints.
170 [self.menuScrollView.leadingAnchor 221 [self.menuScrollView.leadingAnchor
171 constraintEqualToAnchor:self.view.leadingAnchor], 222 constraintEqualToAnchor:self.view.leadingAnchor],
172 [self.menuScrollView.trailingAnchor 223 [self.menuScrollView.trailingAnchor
173 constraintEqualToAnchor:self.view.trailingAnchor], 224 constraintEqualToAnchor:self.view.trailingAnchor],
174 [self.menuScrollView.topAnchor constraintEqualToAnchor:self.view.topAnchor], 225 [self.menuScrollView.topAnchor constraintEqualToAnchor:self.view.topAnchor],
175 [self.menuScrollView.bottomAnchor 226 [self.menuScrollView.bottomAnchor
176 constraintEqualToAnchor:self.view.bottomAnchor], 227 constraintEqualToAnchor:self.view.bottomAnchor],
177 // StackView Constraints. 228 // StackView Constraints.
178 [self.menuStackView.leadingAnchor 229 [self.menuStackView.leadingAnchor
179 constraintEqualToAnchor:self.menuScrollView.leadingAnchor], 230 constraintEqualToAnchor:self.menuScrollView.leadingAnchor],
180 [self.menuStackView.trailingAnchor 231 [self.menuStackView.trailingAnchor
181 constraintEqualToAnchor:self.menuScrollView.trailingAnchor], 232 constraintEqualToAnchor:self.menuScrollView.trailingAnchor],
182 [self.menuStackView.bottomAnchor 233 [self.menuStackView.bottomAnchor
183 constraintEqualToAnchor:self.menuScrollView.bottomAnchor], 234 constraintEqualToAnchor:self.menuScrollView.bottomAnchor],
184 [self.menuStackView.topAnchor 235 [self.menuStackView.topAnchor
185 constraintEqualToAnchor:self.menuScrollView.topAnchor], 236 constraintEqualToAnchor:self.menuScrollView.topAnchor],
186 [self.menuStackView.widthAnchor 237 [self.menuStackView.widthAnchor
187 constraintEqualToAnchor:self.menuScrollView.widthAnchor], 238 constraintEqualToAnchor:self.menuScrollView.widthAnchor],
188 [self.menuStackView.heightAnchor 239 [self.menuStackView.heightAnchor
189 constraintEqualToConstant:kMenuItemHeight * self.menuItems.count], 240 constraintEqualToConstant:kMenuItemHeight * self.menuItems.count],
190 // CloseMenu Button Constraints. 241 // CloseMenu Button Constraint.
191 [self.closeMenuButton.trailingAnchor 242 [self.closeMenuButton.trailingAnchor
192 constraintEqualToAnchor:self.view.trailingAnchor], 243 constraintEqualToAnchor:self.view.trailingAnchor],
193 [self.closeMenuButton.heightAnchor
194 constraintEqualToConstant:kCloseButtonHeight],
195 ]]; 244 ]];
196 } 245 }
197 246
198 #pragma mark - Tools Consumer 247 #pragma mark - Tools Consumer
199 248
200 - (void)setToolsMenuItems:(NSArray*)menuItems { 249 - (void)setToolsMenuItems:(NSArray*)menuItems {
201 _menuItems = menuItems; 250 _menuItems = menuItems;
202 } 251 }
203 252
204 - (void)displayOverflowControls:(BOOL)displayOverflowControls { 253 - (void)displayOverflowControls:(BOOL)displayOverflowControls {
205 self.displayOverflowControls = displayOverflowControls; 254 self.displayOverflowControls = displayOverflowControls;
206 } 255 }
207 256
208 @end 257 @end
OLDNEW
« no previous file with comments | « ios/clean/chrome/browser/ui/tools/menu_overflow_controls_stackview.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698