Commit aadf824e authored by Ewann's avatar Ewann Committed by Commit Bot

[iOS][OpenIn] Refactors OpenIn toolbar

This CL refactors the Open In toolbar and makes the following changes:
- Updates constraints using autoLayout.
- Replaces the MDCButton by an UIButton.
- Button text is no longer in uppercase.
- Cleans unused variables & methods.

based on: https://crrev.com/c/2517483

Bug: 1145477, 1136837, 1117398
Change-Id: I636adc502df159c077f3dc44dbf8d86dc6c0cf97
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2517574
Commit-Queue: Ewann Pellé <ewannpv@chromium.org>
Reviewed-by: default avatarGauthier Ambard <gambard@chromium.org>
Cr-Commit-Position: refs/heads/master@{#824368}
parent a3fed9ae
...@@ -26,7 +26,6 @@ source_set("open_in") { ...@@ -26,7 +26,6 @@ source_set("open_in") {
"//ios/chrome/browser/web_state_list", "//ios/chrome/browser/web_state_list",
"//ios/chrome/common/ui/colors", "//ios/chrome/common/ui/colors",
"//ios/chrome/common/ui/util", "//ios/chrome/common/ui/util",
"//ios/third_party/material_components_ios",
"//ios/web/public", "//ios/web/public",
"//net", "//net",
"//services/network/public/cpp", "//services/network/public/cpp",
......
...@@ -325,7 +325,6 @@ class OpenInControllerBridge ...@@ -325,7 +325,6 @@ class OpenInControllerBridge
OpenInToolbar* openInToolbar = [self openInToolbar]; OpenInToolbar* openInToolbar = [self openInToolbar];
if (!_isOpenInToolbarDisplayed) { if (!_isOpenInToolbarDisplayed) {
[openInToolbar updateBottomMarginHeight];
[UIView animateWithDuration:kOpenInToolbarAnimationDuration [UIView animateWithDuration:kOpenInToolbarAnimationDuration
animations:^{ animations:^{
[openInToolbar setAlpha:1.0]; [openInToolbar setAlpha:1.0];
......
...@@ -9,10 +9,13 @@ ...@@ -9,10 +9,13 @@
@interface OpenInToolbar : UIView @interface OpenInToolbar : UIView
- (instancetype)initWithTarget:(id)target action:(SEL)action; // Init with the given local target and action.
- (instancetype)initWithTarget:(id)target
action:(SEL)action NS_DESIGNATED_INITIALIZER;
// Updates the constraint managing the bottom margin height using NamedGuides. - (instancetype)initWithFrame:(CGRect)aRect NS_UNAVAILABLE;
- (void)updateBottomMarginHeight;
- (instancetype)initWithCoder:(NSCoder*)aDecoder NS_UNAVAILABLE;
@end @end
......
...@@ -6,8 +6,6 @@ ...@@ -6,8 +6,6 @@
#include <cmath> #include <cmath>
#import <MaterialComponents/MaterialButtons.h>
#include "base/check.h" #include "base/check.h"
#include "base/notreached.h" #include "base/notreached.h"
#import "ios/chrome/browser/ui/util/named_guide.h" #import "ios/chrome/browser/ui/util/named_guide.h"
...@@ -16,6 +14,7 @@ ...@@ -16,6 +14,7 @@
#import "ios/chrome/common/ui/colors/semantic_color_names.h" #import "ios/chrome/common/ui/colors/semantic_color_names.h"
#import "ios/chrome/common/ui/util/constraints_ui_util.h" #import "ios/chrome/common/ui/util/constraints_ui_util.h"
#include "ios/chrome/grit/ios_strings.h" #include "ios/chrome/grit/ios_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/l10n/l10n_util_mac.h" #include "ui/base/l10n/l10n_util_mac.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
...@@ -25,9 +24,8 @@ ...@@ -25,9 +24,8 @@
namespace { namespace {
// The toolbar's open button constants. // The toolbar's open button constants.
const CGFloat kOpenButtonVerticalPadding = 8.0f;
const CGFloat kOpenButtonTrailingPadding = 16.0f; const CGFloat kOpenButtonTrailingPadding = 16.0f;
const CGFloat kOpenButtonHeight = 49.0f;
// The toolbar's border related constants. // The toolbar's border related constants.
const CGFloat kTopBorderHeight = 0.5f; const CGFloat kTopBorderHeight = 0.5f;
...@@ -37,199 +35,98 @@ const CGFloat kTopBorderHeight = 0.5f; ...@@ -37,199 +35,98 @@ const CGFloat kTopBorderHeight = 0.5f;
// The "Open in..." button that's hooked up with the target and action passed // The "Open in..." button that's hooked up with the target and action passed
// on initialization. // on initialization.
@property(nonatomic, strong, readonly) MDCButton* openButton; @property(nonatomic, strong, readonly) UIButton* openButton;
// The line used as the border at the top of the toolbar. // The line used as the border at the top of the toolbar.
@property(nonatomic, strong, readonly) UIView* topBorder; @property(nonatomic, strong, readonly) UIView* topBorder;
// View used to have a bottom margin to prevent overlapping with the bottom
// toolbar. This view is used because the the CRWWebControllerContainerView is
// not using autolayout so the toolbar cannot use autolayout either. See
// https://crbug.com/857371.
@property(nonatomic, strong) UIView* bottomMargin;
// Constraint to have the open in toolbar be displayed above the bottom
// toolbar.
@property(nonatomic, strong) NSLayoutConstraint* bottomMarginTopConstraint;
// Relayout the frame of the Open In toolbar, based on its superview and the
// bottom margin.
- (void)relayout;
@end
// TODO(crbug.com/857371): Remove this once the OpenInToolbar is positioned with
// autolayout.
// This view is used to have a view the same height as the bottom toolbar as the
// OpenInToolbar cannot be positioned using autolayout. The height of this view
// should be constrained to the height of the bottom toolbar. When this view is
// re-layouted, its is asking the toolbar to relayout its frame.
@interface OpenInBottomMargin : UIView
// The OpenInToolbar that is notified when the bottom toolbar margin is changed.
@property(nonatomic, weak) OpenInToolbar* owner;
@end
@implementation OpenInBottomMargin
@synthesize owner = _owner;
- (void)layoutSubviews {
[super layoutSubviews];
// This call also happens when the height of this view is changed. And its
// height is the same as the height of the bottom toolbar. So this allows to
// monitor the height changes in the bottom toolbar. Calling relayout on the
// OpenInToolbar ensures that it is correctly positioned.
[self.owner relayout];
}
@end @end
@implementation OpenInToolbar @implementation OpenInToolbar
@synthesize bottomMargin = _bottomMargin;
@synthesize bottomMarginTopConstraint = _bottomMarginTopConstraint;
@synthesize openButton = _openButton;
@synthesize topBorder = _topBorder;
- (instancetype)initWithFrame:(CGRect)aRect {
NOTREACHED();
return nil;
}
- (instancetype)initWithTarget:(id)target action:(SEL)action { - (instancetype)initWithTarget:(id)target action:(SEL)action {
self = [super initWithFrame:CGRectZero]; self = [super initWithFrame:CGRectZero];
if (self) { if (self) {
DCHECK([target respondsToSelector:action]); DCHECK([target respondsToSelector:action]);
self.backgroundColor = [UIColor colorNamed:kBackgroundColor]; self.backgroundColor = [UIColor colorNamed:kBackgroundColor];
[self addSubview:self.openButton];
[self.openButton addTarget:target
action:action
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.topBorder];
self.topBorder.translatesAutoresizingMaskIntoConstraints = NO; _openButton = [self openButtonWithTarget:target action:action];
self.openButton.translatesAutoresizingMaskIntoConstraints = NO; _topBorder = [self topBorderView];
[self addSubview:_openButton];
[self addSubview:_topBorder];
[NSLayoutConstraint activateConstraints:@[ [NSLayoutConstraint activateConstraints:@[
[self.openButton.trailingAnchor [_openButton.trailingAnchor
constraintEqualToAnchor:self.trailingAnchor constraintEqualToAnchor:self.safeAreaLayoutGuide.trailingAnchor
constant:-kOpenButtonTrailingPadding], constant:-kOpenButtonTrailingPadding],
[self.openButton.leadingAnchor [_openButton.leadingAnchor
constraintGreaterThanOrEqualToAnchor:self.leadingAnchor constraintGreaterThanOrEqualToAnchor:self.safeAreaLayoutGuide
.leadingAnchor
constant:kOpenButtonTrailingPadding], constant:kOpenButtonTrailingPadding],
[self.openButton.topAnchor [_openButton.heightAnchor
constraintEqualToAnchor:self.topAnchor constraintGreaterThanOrEqualToConstant:kOpenButtonHeight],
constant:kOpenButtonVerticalPadding], [_openButton.topAnchor
[self.topBorder.heightAnchor constraintEqualToConstant:kTopBorderHeight], constraintEqualToAnchor:self.safeAreaLayoutGuide.topAnchor],
[_topBorder.heightAnchor constraintEqualToConstant:kTopBorderHeight],
]]; ]];
AddSameConstraintsToSides( AddSameConstraintsToSides(
self.topBorder, self, _topBorder, self,
LayoutSides::kTop | LayoutSides::kLeading | LayoutSides::kTrailing); LayoutSides::kTop | LayoutSides::kLeading | LayoutSides::kTrailing);
self.translatesAutoresizingMaskIntoConstraints = NO;
OpenInBottomMargin* bottom = [[OpenInBottomMargin alloc] init];
bottom.owner = self;
bottom.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:bottom];
self.bottomMargin = bottom;
[self.bottomMargin.heightAnchor constraintEqualToConstant:0].active = YES;
} }
return self; return self;
} }
#pragma mark Accessors #pragma mark Helper
- (MDCButton*)openButton { // Helper to create the OpenIn button.
if (!_openButton) { - (UIButton*)openButtonWithTarget:(id)target action:(SEL)action {
_openButton = [[MDCFlatButton alloc] init]; UIButton* openButton = [[UIButton alloc] init];
[_openButton setTitleColor:[UIColor colorNamed:kBlueColor] [openButton setTitleColor:[UIColor colorNamed:kBlueColor]
forState:UIControlStateNormal]; forState:UIControlStateNormal];
[_openButton setTitle:l10n_util::GetNSStringWithFixup(IDS_IOS_OPEN_IN) [openButton setTitle:l10n_util::GetNSString(IDS_IOS_OPEN_IN)
forState:UIControlStateNormal]; forState:UIControlStateNormal];
[_openButton sizeToFit]; [openButton sizeToFit];
} openButton.translatesAutoresizingMaskIntoConstraints = NO;
return _openButton;
}
- (UIView*)topBorder { [openButton addTarget:target
if (!_topBorder) { action:action
_topBorder = [[UIView alloc] initWithFrame:CGRectZero]; forControlEvents:UIControlEventTouchUpInside];
_topBorder.backgroundColor = [UIColor colorNamed:kToolbarShadowColor];
} return openButton;
return _topBorder;
} }
- (void)setBottomMarginTopConstraint: // Helper to create the topBorder view.
(NSLayoutConstraint*)bottomMarginTopConstraint { - (UIView*)topBorderView {
if (_bottomMarginTopConstraint == bottomMarginTopConstraint) UIView* topBorder = [[UIView alloc] initWithFrame:CGRectZero];
return; topBorder.backgroundColor = [UIColor colorNamed:kToolbarShadowColor];
topBorder.translatesAutoresizingMaskIntoConstraints = NO;
_bottomMarginTopConstraint.active = NO; return topBorder;
_bottomMarginTopConstraint = bottomMarginTopConstraint;
_bottomMarginTopConstraint.active = YES;
} }
#pragma mark Public #pragma mark UIView
- (void)updateBottomMarginHeight { - (void)didMoveToSuperview {
if (!self.superview) { if (!self.superview)
self.bottomMarginTopConstraint = nil;
return; return;
}
NSLayoutAnchor* marginTopAnchor = self.bottomMargin.topAnchor;
NamedGuide* guide = [NamedGuide guideWithName:kSecondaryToolbarGuide NamedGuide* guide = [NamedGuide guideWithName:kSecondaryToolbarGuide
view:self]; view:self];
self.bottomMarginTopConstraint = [NSLayoutConstraint activateConstraints:@[
guide ? [marginTopAnchor constraintEqualToAnchor:guide.topAnchor] [self.leadingAnchor constraintEqualToAnchor:self.superview.leadingAnchor],
: [marginTopAnchor [self.trailingAnchor constraintEqualToAnchor:self.superview.trailingAnchor],
constraintEqualToAnchor:self.superview.bottomAnchor]; [self.bottomAnchor constraintEqualToAnchor:self.superview.bottomAnchor],
]];
[self relayout]; if (guide) {
} [self.openButton.bottomAnchor constraintEqualToAnchor:guide.topAnchor]
.active = YES;
#pragma mark Layout } else {
[self.openButton.bottomAnchor
- (CGSize)sizeThatFits:(CGSize)size { constraintEqualToAnchor:self.superview.safeAreaLayoutGuide.bottomAnchor]
CGSize openButtonSize = [self.openButton sizeThatFits:size]; .active = YES;
CGFloat requiredHeight =
openButtonSize.height + 2.0 * kOpenButtonVerticalPadding;
return CGSizeMake(size.width, requiredHeight);
}
- (void)didMoveToWindow {
[self updateBottomMarginHeight];
}
- (void)relayout {
[self layoutIfNeeded];
CGRect frame = self.superview.bounds;
CGRect frameFromSuperview =
[self.bottomMargin convertRect:self.bottomMargin.bounds
toView:self.superview];
CGSize sizeThatFits = [self sizeThatFits:frame.size];
CGFloat toolbarHeight = sizeThatFits.height + frame.size.height -
CGRectGetMinY(frameFromSuperview);
frame.origin.y = frame.size.height - toolbarHeight;
frame.size.height = toolbarHeight;
CGFloat xDiff = std::fabs(frame.origin.x - self.frame.origin.x);
CGFloat yDiff = std::fabs(frame.origin.y - self.frame.origin.y);
CGFloat widthDiff = std::fabs(frame.size.width - self.frame.size.width);
CGFloat heightDiff = std::fabs(frame.size.height - self.frame.size.height);
if (xDiff <= std::numeric_limits<CGFloat>::epsilon() &&
yDiff <= std::numeric_limits<CGFloat>::epsilon() &&
widthDiff <= std::numeric_limits<CGFloat>::epsilon() &&
heightDiff <= std::numeric_limits<CGFloat>::epsilon()) {
// Don't update the frame if it is close enough to prevent potential
// infinite layout cycle.
return;
} }
self.frame = frame;
} }
@end @end
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