Commit 5fbf4234 authored by Gauthier Ambard's avatar Gauthier Ambard Committed by Commit Bot

Change the toolbar to use two stack views

Having two stack views allow the animation to be focused on the location
bar instead of having side effects on the buttons on the side.
It also makes sure the toolbar is respecting the Safe Area on iPhone X
landscape.

Bug: 791933, 791944
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: Ib6f0b3560c825cdfe67f5aca648ee0a2b96044a0
Reviewed-on: https://chromium-review.googlesource.com/807930
Commit-Queue: Sergio Collazos <sczs@chromium.org>
Reviewed-by: default avatarSergio Collazos <sczs@chromium.org>
Reviewed-by: default avatarStepan Khapugin <stkhapugin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#521742}
parent faacc850
...@@ -32,8 +32,16 @@ ...@@ -32,8 +32,16 @@
@interface ToolbarViewController () @interface ToolbarViewController ()
@property(nonatomic, strong) ToolbarButtonFactory* buttonFactory; @property(nonatomic, strong) ToolbarButtonFactory* buttonFactory;
@property(nonatomic, strong) ToolbarButtonUpdater* buttonUpdater; @property(nonatomic, strong) ToolbarButtonUpdater* buttonUpdater;
// The main objects in the view. Positionned as
// [leadingStackView][locationBarContainer][trailingStackView]. The stack views
// contain the buttons which should be shown on each sides of the location bar.
@property(nonatomic, strong) UIStackView* leadingStackView;
@property(nonatomic, strong) UIView* locationBarContainer; @property(nonatomic, strong) UIView* locationBarContainer;
@property(nonatomic, strong) UIStackView* stackView; @property(nonatomic, strong) UIStackView* trailingStackView;
// Array containing all the toolbar buttons, lazily instantiated.
@property(nonatomic, strong) NSArray<ToolbarButton*>* allButtons;
@property(nonatomic, strong) ToolbarButton* backButton; @property(nonatomic, strong) ToolbarButton* backButton;
@property(nonatomic, strong) ToolbarButton* forwardButton; @property(nonatomic, strong) ToolbarButton* forwardButton;
@property(nonatomic, strong) ToolbarButton* tabSwitchStripButton; @property(nonatomic, strong) ToolbarButton* tabSwitchStripButton;
...@@ -68,13 +76,15 @@ ...@@ -68,13 +76,15 @@
@end @end
@implementation ToolbarViewController @implementation ToolbarViewController
@synthesize allButtons = _allButtons;
@synthesize backgroundView = _backgroundView; @synthesize backgroundView = _backgroundView;
@synthesize buttonFactory = _buttonFactory; @synthesize buttonFactory = _buttonFactory;
@synthesize buttonUpdater = _buttonUpdater; @synthesize buttonUpdater = _buttonUpdater;
@synthesize dispatcher = _dispatcher; @synthesize dispatcher = _dispatcher;
@synthesize expanded = _expanded; @synthesize expanded = _expanded;
@synthesize locationBarView = _locationBarView; @synthesize locationBarView = _locationBarView;
@synthesize stackView = _stackView; @synthesize leadingStackView = _leadingStackView;
@synthesize trailingStackView = _trailingStackView;
@synthesize loading = _loading; @synthesize loading = _loading;
@synthesize locationBarContainer = _locationBarContainer; @synthesize locationBarContainer = _locationBarContainer;
@synthesize backButton = _backButton; @synthesize backButton = _backButton;
...@@ -180,7 +190,9 @@ ...@@ -180,7 +190,9 @@
[self setUpToolbarStackView]; [self setUpToolbarStackView];
[self setUpLocationBarContainerView]; [self setUpLocationBarContainerView];
[self.view addSubview:self.stackView]; [self.view addSubview:self.leadingStackView];
[self.view addSubview:self.locationBarContainer];
[self.view addSubview:self.trailingStackView];
[self.view addSubview:self.shadowView]; [self.view addSubview:self.shadowView];
[self.view addSubview:self.progressBar]; [self.view addSubview:self.progressBar];
[self setConstraints]; [self setConstraints];
...@@ -195,14 +207,19 @@ ...@@ -195,14 +207,19 @@
// Sets up the StackView that contains toolbar navigation items. // Sets up the StackView that contains toolbar navigation items.
- (void)setUpToolbarStackView { - (void)setUpToolbarStackView {
self.stackView = [[UIStackView alloc] initWithArrangedSubviews:@[ self.leadingStackView = [[UIStackView alloc] initWithArrangedSubviews:@[
self.backButton, self.forwardButton, self.reloadButton, self.stopButton, self.backButton, self.forwardButton, self.reloadButton, self.stopButton
self.locationBarContainer, self.shareButton, self.tabSwitchStripButton,
self.toolsMenuButton
]]; ]];
self.stackView.translatesAutoresizingMaskIntoConstraints = NO; self.leadingStackView.translatesAutoresizingMaskIntoConstraints = NO;
self.stackView.spacing = kStackViewSpacing; self.leadingStackView.spacing = kStackViewSpacing;
self.stackView.distribution = UIStackViewDistributionFill; self.leadingStackView.distribution = UIStackViewDistributionFill;
self.trailingStackView = [[UIStackView alloc] initWithArrangedSubviews:@[
self.shareButton, self.tabSwitchStripButton, self.toolsMenuButton
]];
self.trailingStackView.translatesAutoresizingMaskIntoConstraints = NO;
self.trailingStackView.spacing = kStackViewSpacing;
self.trailingStackView.distribution = UIStackViewDistributionFill;
} }
// Sets up the LocationContainerView. Which contains a StackView containing the // Sets up the LocationContainerView. Which contains a StackView containing the
...@@ -235,7 +252,7 @@ ...@@ -235,7 +252,7 @@
.active = YES; .active = YES;
// ProgressBar constraints. // ProgressBar constraints.
NSArray* progressBarConstraints = @[ [NSLayoutConstraint activateConstraints:@[
[self.progressBar.leadingAnchor [self.progressBar.leadingAnchor
constraintEqualToAnchor:self.view.leadingAnchor], constraintEqualToAnchor:self.view.leadingAnchor],
[self.progressBar.trailingAnchor [self.progressBar.trailingAnchor
...@@ -244,8 +261,7 @@ ...@@ -244,8 +261,7 @@
constraintEqualToAnchor:self.view.bottomAnchor], constraintEqualToAnchor:self.view.bottomAnchor],
[self.progressBar.heightAnchor [self.progressBar.heightAnchor
constraintEqualToConstant:kProgressBarHeight], constraintEqualToConstant:kProgressBarHeight],
]; ]];
[NSLayoutConstraint activateConstraints:progressBarConstraints];
// Shadow constraints. // Shadow constraints.
[NSLayoutConstraint activateConstraints:@[ [NSLayoutConstraint activateConstraints:@[
...@@ -258,39 +274,68 @@ ...@@ -258,39 +274,68 @@
constraintEqualToConstant:kToolbarShadowHeight], constraintEqualToConstant:kToolbarShadowHeight],
]]; ]];
// StackView constraints. The main Toolbar StackView. // Stack views directly in view constraints. Main StackViews.
NSArray* stackViewConstraints = @[ // Layout: |-[leadingStackView]-[locationBarContainer]-[trailingStackView]-|.
[self.stackView.heightAnchor UILayoutGuide* viewSafeAreaGuide = SafeAreaLayoutGuideForView(self.view);
constraintEqualToConstant:kToolbarHeight - 2 * kVerticalMargin], NSArray* stackViewRegularConstraints = @[
[self.stackView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor [self.leadingStackView.leadingAnchor
constant:-kVerticalMargin], constraintEqualToAnchor:viewSafeAreaGuide.leadingAnchor
[self.stackView.leadingAnchor
constraintEqualToAnchor:self.view.leadingAnchor
constant:kHorizontalMargin], constant:kHorizontalMargin],
[self.stackView.trailingAnchor [self.trailingStackView.trailingAnchor
constraintEqualToAnchor:self.view.trailingAnchor constraintEqualToAnchor:viewSafeAreaGuide.trailingAnchor
constant:-kHorizontalMargin], constant:-kHorizontalMargin]
];
[self.regularToolbarConstraints
addObjectsFromArray:stackViewRegularConstraints];
[NSLayoutConstraint activateConstraints:stackViewRegularConstraints];
ApplyVisualConstraintsWithMetrics(
@[
@"H:[leadingStack]-(spacing)-[locationBar]-(spacing)-[trailingStack]",
@"V:[leadingStack(height)]-(margin)-|",
@"V:[trailingStack(height)]-(margin)-|"
],
@{
@"leadingStack" : self.leadingStackView,
@"locationBar" : self.locationBarContainer,
@"trailingStack" : self.trailingStackView
},
@{
@"height" : @(kToolbarHeight - 2 * kVerticalMargin),
@"margin" : @(kVerticalMargin),
@"spacing" : @(kStackViewSpacing)
});
// LocationBarContainer constraints.
NSArray* locationBarRegularConstraints = @[
[self.locationBarContainer.bottomAnchor
constraintEqualToAnchor:self.leadingStackView.bottomAnchor],
[self.locationBarContainer.topAnchor
constraintEqualToAnchor:self.leadingStackView.topAnchor]
]; ];
[self.regularToolbarConstraints addObjectsFromArray:stackViewConstraints]; [self.regularToolbarConstraints
[NSLayoutConstraint activateConstraints:stackViewConstraints]; addObjectsFromArray:locationBarRegularConstraints];
[NSLayoutConstraint activateConstraints:locationBarRegularConstraints];
// LocationBarStackView constraints. The StackView inside the // LocationBarStackView constraints. The StackView inside the
// LocationBarContainer View. // LocationBarContainer View.
UILayoutGuide* locationBarContainerSafeAreaGuide =
SafeAreaLayoutGuideForView(self.locationBarContainer);
NSLayoutConstraint* locationBarContainerStackViewTopConstraint = NSLayoutConstraint* locationBarContainerStackViewTopConstraint =
[self.locationBarContainerStackView.topAnchor [self.locationBarContainerStackView.topAnchor
constraintEqualToAnchor:self.locationBarContainer.topAnchor]; constraintEqualToAnchor:self.locationBarContainer.topAnchor];
NSArray* locationBarViewStackViewConstraints = @[ [NSLayoutConstraint activateConstraints:@[
[self.locationBarContainerStackView.bottomAnchor [self.locationBarContainerStackView.bottomAnchor
constraintEqualToAnchor:self.locationBarContainer.bottomAnchor], constraintEqualToAnchor:self.locationBarContainer.bottomAnchor],
[self.locationBarContainerStackView.leadingAnchor [self.locationBarContainerStackView.leadingAnchor
constraintEqualToAnchor:self.locationBarContainer.leadingAnchor], constraintEqualToAnchor:locationBarContainerSafeAreaGuide
.leadingAnchor],
[self.locationBarContainerStackView.trailingAnchor [self.locationBarContainerStackView.trailingAnchor
constraintEqualToAnchor:self.locationBarContainer.trailingAnchor], constraintEqualToAnchor:locationBarContainerSafeAreaGuide
.trailingAnchor],
locationBarContainerStackViewTopConstraint, locationBarContainerStackViewTopConstraint,
]; ]];
[self.regularToolbarConstraints [self.regularToolbarConstraints
addObject:locationBarContainerStackViewTopConstraint]; addObject:locationBarContainerStackViewTopConstraint];
[NSLayoutConstraint activateConstraints:locationBarViewStackViewConstraints];
} }
#pragma mark - Components Setup #pragma mark - Components Setup
...@@ -602,18 +647,24 @@ ...@@ -602,18 +647,24 @@
// Updates all Buttons visibility to match any recent WebState change. // Updates all Buttons visibility to match any recent WebState change.
- (void)updateAllButtonsVisibility { - (void)updateAllButtonsVisibility {
for (UIView* view in self.stackView.arrangedSubviews) { for (ToolbarButton* button in self.allButtons) {
if ([view isKindOfClass:[ToolbarButton class]]) { [button updateHiddenInCurrentSizeClass];
ToolbarButton* button = base::mac::ObjCCastStrict<ToolbarButton>(view);
[button updateHiddenInCurrentSizeClass];
}
} }
[self.bookmarkButton updateHiddenInCurrentSizeClass];
[self.voiceSearchButton updateHiddenInCurrentSizeClass];
} }
#pragma mark - Setters & Getters. #pragma mark - Setters & Getters.
- (NSArray<ToolbarButton*>*)allButtons {
if (!_allButtons) {
_allButtons = [NSArray
arrayWithObjects:self.backButton, self.forwardButton, self.reloadButton,
self.stopButton, self.shareButton,
self.tabSwitchStripButton, self.toolsMenuButton,
self.bookmarkButton, self.voiceSearchButton, nil];
}
return _allButtons;
}
- (UIView*)backgroundView { - (UIView*)backgroundView {
if (!_backgroundView) { if (!_backgroundView) {
_backgroundView = [[UIView alloc] initWithFrame:CGRectZero]; _backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
......
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