blob: 1908561664e074c35cf74710ce577230d016aabc [file] [log] [blame]
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/ui/toolbar/secondary_toolbar_view.h"
#import "base/check.h"
#import "ios/chrome/browser/ui/toolbar/buttons/toolbar_button.h"
#import "ios/chrome/browser/ui/toolbar/buttons/toolbar_button_factory.h"
#import "ios/chrome/browser/ui/toolbar/buttons/toolbar_configuration.h"
#import "ios/chrome/browser/ui/toolbar/buttons/toolbar_tab_grid_button.h"
#import "ios/chrome/browser/ui/toolbar/buttons/toolbar_tools_menu_button.h"
#import "ios/chrome/browser/ui/toolbar_container/toolbar_collapsing.h"
#import "ios/chrome/browser/ui/util/rtl_geometry.h"
#import "ios/chrome/common/ui/colors/semantic_color_names.h"
#import "ios/chrome/common/ui/util/constraints_ui_util.h"
#import "ui/gfx/ios/uikit_util.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
const CGFloat kToolsMenuOffset = -7;
} // namespace
@interface SecondaryToolbarView ()<ToolbarCollapsing>
// Factory used to create the buttons.
@property(nonatomic, strong) ToolbarButtonFactory* buttonFactory;
// Redefined as readwrite
@property(nonatomic, strong, readwrite) NSArray<ToolbarButton*>* allButtons;
// Separator above the toolbar, redefined as readwrite.
@property(nonatomic, strong, readwrite) UIView* separator;
// The stack view containing the buttons.
@property(nonatomic, strong) UIStackView* stackView;
// Button to navigate back, redefined as readwrite.
@property(nonatomic, strong, readwrite) ToolbarButton* backButton;
// Buttons to navigate forward, redefined as readwrite.
@property(nonatomic, strong, readwrite) ToolbarButton* forwardButton;
// Button to display the tools menu, redefined as readwrite.
@property(nonatomic, strong, readwrite) ToolbarButton* toolsMenuButton;
// Button to display the tab grid, redefined as readwrite.
@property(nonatomic, strong, readwrite) ToolbarTabGridButton* tabGridButton;
// Button to create a new tab, redefined as readwrite.
@property(nonatomic, strong, readwrite) ToolbarButton* openNewTabButton;
@end
@implementation SecondaryToolbarView
@synthesize allButtons = _allButtons;
@synthesize buttonFactory = _buttonFactory;
@synthesize stackView = _stackView;
@synthesize backButton = _backButton;
@synthesize forwardButton = _forwardButton;
@synthesize toolsMenuButton = _toolsMenuButton;
@synthesize openNewTabButton = _openNewTabButton;
@synthesize tabGridButton = _tabGridButton;
#pragma mark - Public
- (instancetype)initWithButtonFactory:(ToolbarButtonFactory*)factory {
self = [super initWithFrame:CGRectZero];
if (self) {
_buttonFactory = factory;
[self setUp];
}
return self;
}
#pragma mark - UIView
- (CGSize)intrinsicContentSize {
return CGSizeMake(UIViewNoIntrinsicMetric, kSecondaryToolbarHeight);
}
#pragma mark - Setup
// Sets all the subviews and constraints of the view.
- (void)setUp {
if (self.subviews.count > 0) {
// Make sure the view is instantiated only once.
return;
}
DCHECK(self.buttonFactory);
self.translatesAutoresizingMaskIntoConstraints = NO;
self.backgroundColor =
self.buttonFactory.toolbarConfiguration.backgroundColor;
UIView* contentView = self;
self.backButton = [self.buttonFactory backButton];
self.forwardButton = [self.buttonFactory forwardButton];
self.openNewTabButton = [self.buttonFactory openNewTabButton];
self.tabGridButton = [self.buttonFactory tabGridButton];
self.toolsMenuButton = [self.buttonFactory toolsMenuButton];
// Move the tools menu button such as it looks visually balanced with the
// button on the other side of the toolbar.
NSInteger textDirection = base::i18n::IsRTL() ? -1 : 1;
self.toolsMenuButton.transform =
CGAffineTransformMakeTranslation(textDirection * kToolsMenuOffset, 0);
self.allButtons = @[
self.backButton, self.forwardButton, self.openNewTabButton,
self.tabGridButton, self.toolsMenuButton
];
self.separator = [[UIView alloc] init];
self.separator.backgroundColor = [UIColor colorNamed:kToolbarShadowColor];
self.separator.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:self.separator];
self.stackView =
[[UIStackView alloc] initWithArrangedSubviews:self.allButtons];
self.stackView.distribution = UIStackViewDistributionEqualSpacing;
self.stackView.translatesAutoresizingMaskIntoConstraints = NO;
[contentView addSubview:self.stackView];
id<LayoutGuideProvider> safeArea = self.safeAreaLayoutGuide;
[NSLayoutConstraint activateConstraints:@[
[self.stackView.leadingAnchor
constraintEqualToAnchor:safeArea.leadingAnchor
constant:kAdaptiveToolbarMargin],
[self.stackView.trailingAnchor
constraintEqualToAnchor:safeArea.trailingAnchor
constant:-kAdaptiveToolbarMargin],
[self.stackView.topAnchor
constraintEqualToAnchor:self.topAnchor
constant:kBottomButtonsBottomMargin],
[self.separator.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
[self.separator.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
[self.separator.bottomAnchor constraintEqualToAnchor:self.topAnchor],
[self.separator.heightAnchor
constraintEqualToConstant:ui::AlignValueToUpperPixel(
kToolbarSeparatorHeight)],
]];
}
#pragma mark - AdaptiveToolbarView
- (ToolbarButton*)stopButton {
return nil;
}
- (ToolbarButton*)reloadButton {
return nil;
}
- (ToolbarButton*)shareButton {
return nil;
}
- (MDCProgressView*)progressBar {
return nil;
}
#pragma mark - ToolbarCollapsing
- (CGFloat)expandedToolbarHeight {
return self.intrinsicContentSize.height;
}
- (CGFloat)collapsedToolbarHeight {
return 0.0;
}
@end