Commit 17f6ca5d authored by Nazerke's avatar Nazerke Committed by Commit Bot

[ios] Modifying TabStrip Mediator to add a new Tab.

This CL adds the functionality to the add-new-tab-button to add the
new tab via Mediator.

Bug: 1128249,1134132
Change-Id: I4a33befc0bf5c81c2726a7183120d489b7115d9c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2542904
Commit-Queue: Nazerke Kalidolda <nazerke@google.com>
Reviewed-by: default avatarGauthier Ambard <gambard@chromium.org>
Cr-Commit-Position: refs/heads/master@{#829618}
parent f83753c6
......@@ -26,6 +26,7 @@ source_set("tab_strip_ui") {
"tab_strip_cell.h",
"tab_strip_cell.mm",
"tab_strip_consumer.h",
"tab_strip_consumer_delegate.h",
"tab_strip_mediator.h",
"tab_strip_mediator.mm",
"tab_strip_view_controller.h",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_UI_TAB_SWITCHER_TAB_STRIP_TAB_STRIP_CONSUMER_DELEGATE_H_
#define IOS_CHROME_BROWSER_UI_TAB_SWITCHER_TAB_STRIP_TAB_STRIP_CONSUMER_DELEGATE_H_
#import <UIKit/UIKit.h>
// Protocol that the tabstrip UI uses to update the model.
@protocol TabStripConsumerDelegate
// Tells the receiver to insert a new item in the tabstrip.
- (void)addNewItem;
@end
#endif // IOS_CHROME_BROWSER_UI_TAB_SWITCHER_TAB_STRIP_TAB_STRIP_CONSUMER_DELEGATE_H_
......@@ -52,6 +52,7 @@
self.mediator.webStateList = self.browser->GetWebStateList();
self.tabStripViewController.faviconDataSource = self.mediator;
self.tabStripViewController.delegate = self.mediator;
}
- (void)stop {
......
......@@ -8,12 +8,14 @@
#import <Foundation/Foundation.h>
#import "ios/chrome/browser/ui/tab_switcher/tab_strip/tab_favicon_data_source.h"
#import "ios/chrome/browser/ui/tab_switcher/tab_strip/tab_strip_consumer_delegate.h"
@protocol TabStripConsumer;
class WebStateList;
// This mediator used to manage model interaction for its consumer.
@interface TabStripMediator : NSObject <TabFaviconDataSource>
@interface TabStripMediator
: NSObject <TabFaviconDataSource, TabStripConsumerDelegate>
// The WebStateList that this mediator listens for any changes on the total
// number of Webstates.
......
......@@ -6,6 +6,7 @@
#import "components/favicon/ios/web_favicon_driver.h"
#import "ios/chrome/browser/browser_state/chrome_browser_state.h"
#import "ios/chrome/browser/chrome_url_constants.h"
#import "ios/chrome/browser/chrome_url_util.h"
#import "ios/chrome/browser/tabs/tab_title_util.h"
#import "ios/chrome/browser/ui/tab_switcher/tab_strip/tab_strip_consumer.h"
......@@ -14,6 +15,8 @@
#import "ios/chrome/browser/web_state_list/all_web_state_observation_forwarder.h"
#import "ios/chrome/browser/web_state_list/web_state_list.h"
#import "ios/chrome/browser/web_state_list/web_state_list_observer_bridge.h"
#import "ios/chrome/browser/web_state_list/web_state_opener.h"
#import "ios/web/public/navigation/navigation_manager.h"
#import "ios/web/public/web_state.h"
#import "ios/web/public/web_state_observer_bridge.h"
#import "ui/gfx/image/image.h"
......@@ -173,6 +176,27 @@ web::WebState* GetWebStateWithId(WebStateList* web_state_list,
}
}
#pragma mark - TabStripConsumerDelegate
- (void)addNewItem {
if (!self.webStateList)
return;
web::WebState::CreateParams params(
self.webStateList->GetActiveWebState()->GetBrowserState());
std::unique_ptr<web::WebState> webState = web::WebState::Create(params);
GURL url(kChromeUINewTabURL);
web::NavigationManager::WebLoadParams loadParams(url);
loadParams.transition_type = ui::PAGE_TRANSITION_TYPED;
webState->GetNavigationManager()->LoadURLWithParams(loadParams);
self.webStateList->InsertWebState(
base::checked_cast<int>(self.webStateList->count()), std::move(webState),
(WebStateList::INSERT_FORCE_INDEX | WebStateList::INSERT_ACTIVATE),
WebStateOpener());
}
#pragma mark - Private
// Calls |-populateItems:selectedItemID:| on the consumer.
......
......@@ -11,6 +11,7 @@
@class TabStripMediator;
@protocol TabFaviconDataSource;
@protocol TabStripConsumerDelegate;
// ViewController for the TabStrip. This ViewController is contained by
// BrowserViewController. This TabStripViewController is responsible for
......@@ -19,8 +20,7 @@
: UICollectionViewController <TabStripConsumer>
@property(nonatomic, weak) id<TabFaviconDataSource> faviconDataSource;
@property(nonatomic, readonly, retain) UIButton* buttonNewTab;
@property(nonatomic, weak) id<TabStripConsumerDelegate> delegate;
- (instancetype)init NS_DESIGNATED_INITIALIZER;
......
......@@ -32,6 +32,7 @@ const CGFloat kNewTabButtonBottomImageInset = -2.0;
@interface TabStripViewController ()
@property(nonatomic, strong) UIButton* buttonNewTab;
// The local model backing the collection view.
@property(nonatomic, strong) NSMutableArray<TabSwitcherItem*>* items;
// Identifier of the selected item. This value is disregarded if |self.items| is
......@@ -58,24 +59,30 @@ const CGFloat kNewTabButtonBottomImageInset = -2.0;
[self.collectionView registerClass:[TabStripCell class]
forCellWithReuseIdentifier:kReuseIdentifier];
_buttonNewTab = [[UIButton alloc] init];
_buttonNewTab.translatesAutoresizingMaskIntoConstraints = NO;
_buttonNewTab.imageView.contentMode = UIViewContentModeCenter;
self.buttonNewTab = [[UIButton alloc] init];
self.buttonNewTab.translatesAutoresizingMaskIntoConstraints = NO;
self.buttonNewTab.imageView.contentMode = UIViewContentModeCenter;
UIImage* buttonNewTabImage = [[UIImage imageNamed:@"tabstrip_new_tab"]
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[_buttonNewTab setImage:buttonNewTabImage forState:UIControlStateNormal];
[_buttonNewTab.imageView setTintColor:[UIColor colorNamed:kGrey500Color]];
[self.buttonNewTab setImage:buttonNewTabImage forState:UIControlStateNormal];
[self.buttonNewTab.imageView setTintColor:[UIColor colorNamed:kGrey500Color]];
UIEdgeInsets imageInsets = UIEdgeInsetsMake(0, kNewTabButtonLeadingImageInset,
kNewTabButtonBottomImageInset, 0);
_buttonNewTab.imageEdgeInsets = imageInsets;
[self.view addSubview:_buttonNewTab];
self.buttonNewTab.imageEdgeInsets = imageInsets;
[self.view addSubview:self.buttonNewTab];
[NSLayoutConstraint activateConstraints:@[
[_buttonNewTab.trailingAnchor
[self.buttonNewTab.trailingAnchor
constraintEqualToAnchor:self.view.trailingAnchor],
[_buttonNewTab.topAnchor constraintEqualToAnchor:self.view.topAnchor],
[_buttonNewTab.heightAnchor constraintEqualToAnchor:self.view.heightAnchor],
[_buttonNewTab.widthAnchor constraintEqualToConstant:kNewTabButtonWidth],
[self.buttonNewTab.topAnchor constraintEqualToAnchor:self.view.topAnchor],
[self.buttonNewTab.heightAnchor
constraintEqualToAnchor:self.view.heightAnchor],
[self.buttonNewTab.widthAnchor
constraintEqualToConstant:kNewTabButtonWidth],
]];
[self.buttonNewTab addTarget:self
action:@selector(sendNewTabCommand)
forControlEvents:UIControlEventTouchUpInside];
}
- (NSInteger)numberOfSectionsInCollectionView:
......@@ -167,4 +174,8 @@ const CGFloat kNewTabButtonBottomImageInset = -2.0;
return [self.items indexOfObjectPassingTest:selectedTest];
}
- (void)sendNewTabCommand {
[self.delegate addNewItem];
}
@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