| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/clean/chrome/browser/ui/tab_grid/tab_grid_coordinator.h" | 5 #import "ios/clean/chrome/browser/ui/tab_grid/tab_grid_coordinator.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | 10 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 11 #import "ios/clean/chrome/browser/ui/commands/settings_commands.h" | 11 #import "ios/clean/chrome/browser/ui/commands/settings_commands.h" |
| 12 #import "ios/clean/chrome/browser/ui/commands/tab_commands.h" | |
| 13 #import "ios/clean/chrome/browser/ui/commands/tab_grid_commands.h" | 12 #import "ios/clean/chrome/browser/ui/commands/tab_grid_commands.h" |
| 14 #import "ios/clean/chrome/browser/ui/settings/settings_coordinator.h" | 13 #import "ios/clean/chrome/browser/ui/settings/settings_coordinator.h" |
| 15 #import "ios/clean/chrome/browser/ui/tab/tab_coordinator.h" | 14 #import "ios/clean/chrome/browser/ui/tab/tab_coordinator.h" |
| 16 #import "ios/clean/chrome/browser/ui/tab_grid/tab_grid_mediator.h" | 15 #import "ios/clean/chrome/browser/ui/tab_grid/tab_grid_mediator.h" |
| 17 #import "ios/clean/chrome/browser/ui/tab_grid/tab_grid_view_controller.h" | 16 #import "ios/clean/chrome/browser/ui/tab_grid/tab_grid_view_controller.h" |
| 18 #import "ios/shared/chrome/browser/coordinator_context/coordinator_context.h" | 17 #import "ios/shared/chrome/browser/coordinator_context/coordinator_context.h" |
| 19 #import "ios/shared/chrome/browser/tabs/web_state_list.h" | 18 #import "ios/shared/chrome/browser/tabs/web_state_list.h" |
| 20 #import "ios/shared/chrome/browser/ui/browser_list/browser.h" | 19 #import "ios/shared/chrome/browser/ui/browser_list/browser.h" |
| 21 #import "ios/shared/chrome/browser/ui/commands/command_dispatcher.h" | 20 #import "ios/shared/chrome/browser/ui/commands/command_dispatcher.h" |
| 22 #import "ios/shared/chrome/browser/ui/coordinators/browser_coordinator+internal.
h" | 21 #import "ios/shared/chrome/browser/ui/coordinators/browser_coordinator+internal.
h" |
| 23 #import "ios/web/public/navigation_manager.h" | 22 #import "ios/web/public/navigation_manager.h" |
| 24 #include "ios/web/public/web_state/web_state.h" | 23 #include "ios/web/public/web_state/web_state.h" |
| 25 #import "net/base/mac/url_conversions.h" | 24 #import "net/base/mac/url_conversions.h" |
| 26 #include "ui/base/page_transition_types.h" | 25 #include "ui/base/page_transition_types.h" |
| 27 | 26 |
| 28 #if !defined(__has_feature) || !__has_feature(objc_arc) | 27 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 29 #error "This file requires ARC support." | 28 #error "This file requires ARC support." |
| 30 #endif | 29 #endif |
| 31 | 30 |
| 32 @interface TabGridCoordinator ()<SettingsCommands, TabCommands, TabGridCommands> | 31 @interface TabGridCoordinator ()<SettingsCommands, TabGridCommands> |
| 33 @property(nonatomic, strong) TabGridViewController* viewController; | 32 @property(nonatomic, strong) TabGridViewController* viewController; |
| 34 @property(nonatomic, weak) SettingsCoordinator* settingsCoordinator; | 33 @property(nonatomic, weak) SettingsCoordinator* settingsCoordinator; |
| 35 @property(nonatomic, readonly) WebStateList& webStateList; | 34 @property(nonatomic, readonly) WebStateList& webStateList; |
| 36 @property(nonatomic, strong) TabGridMediator* mediator; | 35 @property(nonatomic, strong) TabGridMediator* mediator; |
| 37 @end | 36 @end |
| 38 | 37 |
| 39 @implementation TabGridCoordinator | 38 @implementation TabGridCoordinator |
| 40 @synthesize viewController = _viewController; | 39 @synthesize viewController = _viewController; |
| 41 @synthesize settingsCoordinator = _settingsCoordinator; | 40 @synthesize settingsCoordinator = _settingsCoordinator; |
| 42 @synthesize mediator = _mediator; | 41 @synthesize mediator = _mediator; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 58 - (WebStateList&)webStateList { | 57 - (WebStateList&)webStateList { |
| 59 return self.browser->web_state_list(); | 58 return self.browser->web_state_list(); |
| 60 } | 59 } |
| 61 | 60 |
| 62 #pragma mark - BrowserCoordinator | 61 #pragma mark - BrowserCoordinator |
| 63 | 62 |
| 64 - (void)start { | 63 - (void)start { |
| 65 self.mediator = [[TabGridMediator alloc] init]; | 64 self.mediator = [[TabGridMediator alloc] init]; |
| 66 self.mediator.webStateList = &self.webStateList; | 65 self.mediator.webStateList = &self.webStateList; |
| 67 | 66 |
| 67 CommandDispatcher* dispatcher = self.browser->dispatcher(); |
| 68 // SettingsCommands |
| 69 [dispatcher startDispatchingToTarget:self |
| 70 forSelector:@selector(showSettings)]; |
| 71 [dispatcher startDispatchingToTarget:self |
| 72 forSelector:@selector(closeSettings)]; |
| 73 // TabGridCommands |
| 74 [dispatcher startDispatchingToTarget:self |
| 75 forSelector:@selector(showTabGridTabAtIndex:)]; |
| 76 [dispatcher startDispatchingToTarget:self |
| 77 forSelector:@selector(closeTabGridTabAtIndex:)]; |
| 78 [dispatcher startDispatchingToTarget:self |
| 79 forSelector:@selector(createAndShowNewTabInTabGrid)]; |
| 80 [dispatcher startDispatchingToTarget:self forSelector:@selector(showTabGrid)]; |
| 81 |
| 68 self.viewController = [[TabGridViewController alloc] init]; | 82 self.viewController = [[TabGridViewController alloc] init]; |
| 69 self.viewController.dataSource = self.mediator; | 83 self.viewController.dataSource = self.mediator; |
| 70 self.viewController.settingsCommandHandler = self; | 84 self.viewController.dispatcher = static_cast<id>(self.browser->dispatcher()); |
| 71 self.viewController.tabCommandHandler = self; | |
| 72 self.viewController.tabGridCommandHandler = self; | |
| 73 | 85 |
| 74 self.mediator.consumer = self.viewController; | 86 self.mediator.consumer = self.viewController; |
| 75 | 87 |
| 76 // |baseViewController| is nullable, so this is by design a no-op if it hasn't | 88 // |baseViewController| is nullable, so this is by design a no-op if it hasn't |
| 77 // been set. This may be true in a unit test, or if this coordinator is being | 89 // been set. This may be true in a unit test, or if this coordinator is being |
| 78 // used as a root coordinator. | 90 // used as a root coordinator. |
| 79 [self.context.baseViewController presentViewController:self.viewController | 91 [self.context.baseViewController presentViewController:self.viewController |
| 80 animated:self.context.animated | 92 animated:self.context.animated |
| 81 completion:nil]; | 93 completion:nil]; |
| 82 [super start]; | 94 [super start]; |
| 83 } | 95 } |
| 84 | 96 |
| 85 #pragma mark - TabCommands | 97 - (void)stop { |
| 98 [super stop]; |
| 99 [self.browser->dispatcher() stopDispatchingToTarget:self]; |
| 100 } |
| 86 | 101 |
| 87 - (void)showTabAtIndex:(int)index { | 102 #pragma mark - TabGridCommands |
| 103 |
| 104 - (void)showTabGridTabAtIndex:(int)index { |
| 88 self.webStateList.ActivateWebStateAt(index); | 105 self.webStateList.ActivateWebStateAt(index); |
| 89 // PLACEHOLDER: The tab coordinator should be able to get the active webState | 106 // PLACEHOLDER: The tab coordinator should be able to get the active webState |
| 90 // on its own. | 107 // on its own. |
| 91 TabCoordinator* tabCoordinator = [[TabCoordinator alloc] init]; | 108 TabCoordinator* tabCoordinator = [[TabCoordinator alloc] init]; |
| 92 tabCoordinator.webState = self.webStateList.GetWebStateAt(index); | 109 tabCoordinator.webState = self.webStateList.GetWebStateAt(index); |
| 93 tabCoordinator.presentationKey = | 110 tabCoordinator.presentationKey = |
| 94 [NSIndexPath indexPathForItem:index inSection:0]; | 111 [NSIndexPath indexPathForItem:index inSection:0]; |
| 95 [self addChildCoordinator:tabCoordinator]; | 112 [self addChildCoordinator:tabCoordinator]; |
| 96 [tabCoordinator start]; | 113 [tabCoordinator start]; |
| 97 } | 114 } |
| 98 | 115 |
| 99 - (void)closeTabAtIndex:(int)index { | 116 - (void)closeTabGridTabAtIndex:(int)index { |
| 100 std::unique_ptr<web::WebState> closedWebState( | 117 std::unique_ptr<web::WebState> closedWebState( |
| 101 self.webStateList.DetachWebStateAt(index)); | 118 self.webStateList.DetachWebStateAt(index)); |
| 102 } | 119 } |
| 103 | 120 |
| 104 - (void)createAndShowNewTab { | 121 - (void)createAndShowNewTabInTabGrid { |
| 105 web::WebState::CreateParams webStateCreateParams( | 122 web::WebState::CreateParams webStateCreateParams( |
| 106 self.browser->browser_state()); | 123 self.browser->browser_state()); |
| 107 std::unique_ptr<web::WebState> webState = | 124 std::unique_ptr<web::WebState> webState = |
| 108 web::WebState::Create(webStateCreateParams); | 125 web::WebState::Create(webStateCreateParams); |
| 109 self.webStateList.InsertWebState(self.webStateList.count(), | 126 self.webStateList.InsertWebState(self.webStateList.count(), |
| 110 webState.release()); | 127 webState.release()); |
| 111 [self showTabAtIndex:self.webStateList.count() - 1]; | 128 [self showTabGridTabAtIndex:self.webStateList.count() - 1]; |
| 112 } | 129 } |
| 113 | 130 |
| 114 #pragma mark - TabGridCommands | |
| 115 | |
| 116 - (void)showTabGrid { | 131 - (void)showTabGrid { |
| 117 // This object should only ever have at most one child. | 132 // This object should only ever have at most one child. |
| 118 DCHECK_LE(self.children.count, 1UL); | 133 DCHECK_LE(self.children.count, 1UL); |
| 119 BrowserCoordinator* child = [self.children anyObject]; | 134 BrowserCoordinator* child = [self.children anyObject]; |
| 120 [child stop]; | 135 [child stop]; |
| 121 [self removeChildCoordinator:child]; | 136 [self removeChildCoordinator:child]; |
| 122 } | 137 } |
| 123 | 138 |
| 124 #pragma mark - SettingsCommands | 139 #pragma mark - SettingsCommands |
| 125 | 140 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 148 if (self.webStateList.active_index() == WebStateList::kInvalidIndex) { | 163 if (self.webStateList.active_index() == WebStateList::kInvalidIndex) { |
| 149 return; | 164 return; |
| 150 } | 165 } |
| 151 [self.overlayCoordinator stop]; | 166 [self.overlayCoordinator stop]; |
| 152 [self removeOverlayCoordinator]; | 167 [self removeOverlayCoordinator]; |
| 153 web::WebState* activeWebState = self.webStateList.GetActiveWebState(); | 168 web::WebState* activeWebState = self.webStateList.GetActiveWebState(); |
| 154 web::NavigationManager::WebLoadParams params(net::GURLWithNSURL(URL)); | 169 web::NavigationManager::WebLoadParams params(net::GURLWithNSURL(URL)); |
| 155 params.transition_type = ui::PAGE_TRANSITION_LINK; | 170 params.transition_type = ui::PAGE_TRANSITION_LINK; |
| 156 activeWebState->GetNavigationManager()->LoadURLWithParams(params); | 171 activeWebState->GetNavigationManager()->LoadURLWithParams(params); |
| 157 if (!self.children.count) { | 172 if (!self.children.count) { |
| 158 [self showTabAtIndex:self.webStateList.active_index()]; | 173 [self showTabGridTabAtIndex:self.webStateList.active_index()]; |
| 159 } | 174 } |
| 160 } | 175 } |
| 161 | 176 |
| 162 @end | 177 @end |
| OLD | NEW |