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") {
"//ios/chrome/browser/web_state_list",
"//ios/chrome/common/ui/colors",
"//ios/chrome/common/ui/util",
"//ios/third_party/material_components_ios",
"//ios/web/public",
"//net",
"//services/network/public/cpp",
......
......@@ -325,7 +325,6 @@ class OpenInControllerBridge
OpenInToolbar* openInToolbar = [self openInToolbar];
if (!_isOpenInToolbarDisplayed) {
[openInToolbar updateBottomMarginHeight];
[UIView animateWithDuration:kOpenInToolbarAnimationDuration
animations:^{
[openInToolbar setAlpha:1.0];
......
......@@ -9,10 +9,13 @@
@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.
- (void)updateBottomMarginHeight;
- (instancetype)initWithFrame:(CGRect)aRect NS_UNAVAILABLE;
- (instancetype)initWithCoder:(NSCoder*)aDecoder NS_UNAVAILABLE;
@end
......
......@@ -6,8 +6,6 @@
#include <cmath>
#import <MaterialComponents/MaterialButtons.h>
#include "base/check.h"
#include "base/notreached.h"
#import "ios/chrome/browser/ui/util/named_guide.h"
......@@ -16,6 +14,7 @@
#import "ios/chrome/common/ui/colors/semantic_color_names.h"
#import "ios/chrome/common/ui/util/constraints_ui_util.h"
#include "ios/chrome/grit/ios_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/l10n/l10n_util_mac.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
......@@ -25,9 +24,8 @@
namespace {
// The toolbar's open button constants.
const CGFloat kOpenButtonVerticalPadding = 8.0f;
const CGFloat kOpenButtonTrailingPadding = 16.0f;
const CGFloat kOpenButtonHeight = 49.0f;
// The toolbar's border related constants.
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
// 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.
@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
@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 {
self = [super initWithFrame:CGRectZero];
if (self) {
DCHECK([target respondsToSelector:action]);
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;
self.openButton.translatesAutoresizingMaskIntoConstraints = NO;
_openButton = [self openButtonWithTarget:target action:action];
_topBorder = [self topBorderView];
[self addSubview:_openButton];
[self addSubview:_topBorder];
[NSLayoutConstraint activateConstraints:@[
[self.openButton.trailingAnchor
constraintEqualToAnchor:self.trailingAnchor
[_openButton.trailingAnchor
constraintEqualToAnchor:self.safeAreaLayoutGuide.trailingAnchor
constant:-kOpenButtonTrailingPadding],
[self.openButton.leadingAnchor
constraintGreaterThanOrEqualToAnchor:self.leadingAnchor
[_openButton.leadingAnchor
constraintGreaterThanOrEqualToAnchor:self.safeAreaLayoutGuide
.leadingAnchor
constant:kOpenButtonTrailingPadding],
[self.openButton.topAnchor
constraintEqualToAnchor:self.topAnchor
constant:kOpenButtonVerticalPadding],
[self.topBorder.heightAnchor constraintEqualToConstant:kTopBorderHeight],
[_openButton.heightAnchor
constraintGreaterThanOrEqualToConstant:kOpenButtonHeight],
[_openButton.topAnchor
constraintEqualToAnchor:self.safeAreaLayoutGuide.topAnchor],
[_topBorder.heightAnchor constraintEqualToConstant:kTopBorderHeight],
]];
AddSameConstraintsToSides(
self.topBorder, self,
_topBorder, self,
LayoutSides::kTop | LayoutSides::kLeading | LayoutSides::kTrailing);
OpenInBottomMargin* bottom = [[OpenInBottomMargin alloc] init];
bottom.owner = self;
bottom.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:bottom];
self.bottomMargin = bottom;
[self.bottomMargin.heightAnchor constraintEqualToConstant:0].active = YES;
self.translatesAutoresizingMaskIntoConstraints = NO;
}
return self;
}
#pragma mark Accessors
#pragma mark Helper
- (MDCButton*)openButton {
if (!_openButton) {
_openButton = [[MDCFlatButton alloc] init];
[_openButton setTitleColor:[UIColor colorNamed:kBlueColor]
forState:UIControlStateNormal];
[_openButton setTitle:l10n_util::GetNSStringWithFixup(IDS_IOS_OPEN_IN)
forState:UIControlStateNormal];
[_openButton sizeToFit];
}
return _openButton;
}
// Helper to create the OpenIn button.
- (UIButton*)openButtonWithTarget:(id)target action:(SEL)action {
UIButton* openButton = [[UIButton alloc] init];
[openButton setTitleColor:[UIColor colorNamed:kBlueColor]
forState:UIControlStateNormal];
[openButton setTitle:l10n_util::GetNSString(IDS_IOS_OPEN_IN)
forState:UIControlStateNormal];
[openButton sizeToFit];
openButton.translatesAutoresizingMaskIntoConstraints = NO;
- (UIView*)topBorder {
if (!_topBorder) {
_topBorder = [[UIView alloc] initWithFrame:CGRectZero];
_topBorder.backgroundColor = [UIColor colorNamed:kToolbarShadowColor];
}
return _topBorder;
[openButton addTarget:target
action:action
forControlEvents:UIControlEventTouchUpInside];
return openButton;
}
- (void)setBottomMarginTopConstraint:
(NSLayoutConstraint*)bottomMarginTopConstraint {
if (_bottomMarginTopConstraint == bottomMarginTopConstraint)
return;
// Helper to create the topBorder view.
- (UIView*)topBorderView {
UIView* topBorder = [[UIView alloc] initWithFrame:CGRectZero];
topBorder.backgroundColor = [UIColor colorNamed:kToolbarShadowColor];
topBorder.translatesAutoresizingMaskIntoConstraints = NO;
_bottomMarginTopConstraint.active = NO;
_bottomMarginTopConstraint = bottomMarginTopConstraint;
_bottomMarginTopConstraint.active = YES;
return topBorder;
}
#pragma mark Public
#pragma mark UIView
- (void)updateBottomMarginHeight {
if (!self.superview) {
self.bottomMarginTopConstraint = nil;
- (void)didMoveToSuperview {
if (!self.superview)
return;
}
NSLayoutAnchor* marginTopAnchor = self.bottomMargin.topAnchor;
NamedGuide* guide = [NamedGuide guideWithName:kSecondaryToolbarGuide
view:self];
self.bottomMarginTopConstraint =
guide ? [marginTopAnchor constraintEqualToAnchor:guide.topAnchor]
: [marginTopAnchor
constraintEqualToAnchor:self.superview.bottomAnchor];
[self relayout];
}
#pragma mark Layout
- (CGSize)sizeThatFits:(CGSize)size {
CGSize openButtonSize = [self.openButton sizeThatFits:size];
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;
[NSLayoutConstraint activateConstraints:@[
[self.leadingAnchor constraintEqualToAnchor:self.superview.leadingAnchor],
[self.trailingAnchor constraintEqualToAnchor:self.superview.trailingAnchor],
[self.bottomAnchor constraintEqualToAnchor:self.superview.bottomAnchor],
]];
if (guide) {
[self.openButton.bottomAnchor constraintEqualToAnchor:guide.topAnchor]
.active = YES;
} else {
[self.openButton.bottomAnchor
constraintEqualToAnchor:self.superview.safeAreaLayoutGuide.bottomAnchor]
.active = YES;
}
self.frame = frame;
}
@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