Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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/chrome/browser/tabs/tab_model_order_controller.h" | 5 #import "ios/chrome/browser/tabs/tab_model_order_controller.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 |
| 9 @implementation TabModelOrderController | 9 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 10 #error "This file requires ARC support." | |
| 11 #endif | |
| 10 | 12 |
| 11 @synthesize insertionPolicy = insertionPolicy_; | 13 @implementation TabModelOrderController { |
| 14 __weak TabModel* model_; // weak, owns us | |
|
rohitrao (ping after 24h)
2017/02/13 19:54:12
Presumably this comment is unnecessary now?
sdefresne
2017/02/14 16:15:29
Done.
| |
| 15 } | |
| 12 | 16 |
| 13 - (id)initWithTabModel:(TabModel*)model { | 17 - (instancetype)initWithTabModel:(TabModel*)model { |
| 14 DCHECK(model); | 18 DCHECK(model); |
| 15 self = [super init]; | 19 if ((self = [super init])) |
| 16 if (self) { | |
| 17 model_ = model; | 20 model_ = model; |
| 18 insertionPolicy_ = TabModelOrderConstants::kInsertAfter; | |
| 19 } | |
| 20 return self; | 21 return self; |
| 21 } | 22 } |
| 22 | 23 |
| 23 - (NSUInteger)insertionIndexForTab:(Tab*)newTab | 24 - (NSUInteger)insertionIndexForTab:(Tab*)newTab |
| 24 transition:(ui::PageTransition)transition | 25 transition:(ui::PageTransition)transition |
| 25 opener:(Tab*)parentTab | 26 opener:(Tab*)parentTab |
| 26 adjacency:(TabModelOrderConstants::InsertionAdjacency) | 27 adjacency:(TabModelOrderConstants::InsertionAdjacency) |
| 27 adjacency { | 28 adjacency { |
| 28 if (model_.isEmpty) | 29 if (model_.isEmpty) |
| 29 return 0; | 30 return 0; |
| 30 | 31 |
| 31 if (PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_LINK) && | 32 if (!parentTab) |
| 32 parentTab) { | 33 return [self insertionIndexForAppending]; |
| 33 NSUInteger referenceIndex = [model_ indexOfTab:parentTab]; | |
| 34 Tab* openLocation = nil; | |
| 35 if (insertionPolicy_ == TabModelOrderConstants::kInsertAfter) { | |
| 36 openLocation = [model_ lastTabWithOpener:parentTab]; | |
| 37 } else { | |
| 38 openLocation = [model_ firstTabWithOpener:parentTab]; | |
| 39 } | |
| 40 if (openLocation) | |
| 41 referenceIndex = [model_ indexOfTab:openLocation]; | |
| 42 | 34 |
| 43 // If the reference tab is no longer in the model (e.g., it was closed | 35 if (!PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_LINK)) |
| 44 // before the open request was handled), fall through to default behavior. | 36 return [self insertionIndexForAppending]; |
| 45 if (referenceIndex != NSNotFound) { | 37 |
| 46 BOOL insertAfter = | 38 NSUInteger referenceIndex = [model_ indexOfTab:parentTab]; |
| 47 insertionPolicy_ == TabModelOrderConstants::kInsertAfter || | 39 Tab* openLocation = openLocation = [model_ lastTabWithOpener:parentTab]; |
|
rohitrao (ping after 24h)
2017/02/13 19:54:12
Remove "= openLocation"?
sdefresne
2017/02/14 16:15:29
Done.
| |
| 48 adjacency == TabModelOrderConstants::kAdjacentAfter; | 40 |
| 49 NSUInteger delta = insertAfter ? 1 : 0; | 41 // If the reference tab is no longer in the model (e.g., it was closed |
| 50 return referenceIndex + delta; | 42 // before the open request was handled), fall through to default behavior. |
| 51 } | 43 if (openLocation) |
|
rohitrao (ping after 24h)
2017/02/13 19:54:12
Should this be !openLocation?
sdefresne
2017/02/14 16:15:29
Good catch. My code was incorrect and I've fixed i
| |
| 52 } | 44 return [self insertionIndexForAppending]; |
| 53 // In other cases, such as a normal new tab, open at the end. | 45 |
| 54 return [self insertionIndexForAppending]; | 46 referenceIndex = [model_ indexOfTab:openLocation]; |
| 47 DCHECK_NE(referenceIndex, static_cast<NSUInteger>(NSNotFound)); | |
| 48 return referenceIndex + 1; | |
| 55 } | 49 } |
| 56 | 50 |
| 57 - (NSUInteger)insertionIndexForAppending { | 51 - (NSUInteger)insertionIndexForAppending { |
| 58 return insertionPolicy_ == TabModelOrderConstants::kInsertAfter ? model_.count | 52 return model_.count; |
| 59 : 0; | |
| 60 } | 53 } |
| 61 | 54 |
| 62 - (Tab*)determineNewSelectedTabFromRemovedTab:(Tab*)removedTab { | 55 - (Tab*)determineNewSelectedTabFromRemovedTab:(Tab*)removedTab { |
| 63 // While the desktop version of this code deals in indices, this deals in | 56 // While the desktop version of this code deals in indices, this deals in |
| 64 // actual tabs. As a result, the tab only needs to change iff the selection | 57 // actual tabs. As a result, the tab only needs to change iff the selection |
| 65 // is the tab that's removed. | 58 // is the tab that's removed. |
| 66 if (removedTab != model_.currentTab) | 59 if (removedTab != model_.currentTab) |
| 67 return model_.currentTab; | 60 return model_.currentTab; |
| 68 | 61 |
| 69 const NSUInteger numberOfTabs = model_.count; | 62 const NSUInteger numberOfTabs = model_.count; |
| 70 if (numberOfTabs < 2) | 63 if (numberOfTabs < 2) |
| 71 return nil; | 64 return nil; |
| 65 | |
| 72 DCHECK(numberOfTabs >= 2); | 66 DCHECK(numberOfTabs >= 2); |
| 73 DCHECK(removedTab == model_.currentTab); | 67 DCHECK(removedTab == model_.currentTab); |
| 74 | 68 |
| 75 // First see if the tab being removed has any "child" tabs. If it does, we | 69 // First see if the tab being removed has any "child" tabs. If it does, we |
| 76 // want to select the first in that child group, not the next tab in the same | 70 // want to select the first in that child group, not the next tab in the same |
| 77 // group of the removed tab. | 71 // group of the removed tab. |
| 78 Tab* firstChild = [model_ nextTabWithOpener:removedTab afterTab:nil]; | 72 Tab* firstChild = [model_ nextTabWithOpener:removedTab afterTab:nil]; |
| 79 if (firstChild) | 73 if (firstChild) |
| 80 return firstChild; | 74 return firstChild; |
| 81 Tab* parentTab = [model_ openerOfTab:removedTab]; | 75 Tab* parentTab = [model_ openerOfTab:removedTab]; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 95 NSUInteger selectedIndex = [model_ indexOfTab:removedTab]; | 89 NSUInteger selectedIndex = [model_ indexOfTab:removedTab]; |
| 96 DCHECK(selectedIndex <= numberOfTabs - 1); | 90 DCHECK(selectedIndex <= numberOfTabs - 1); |
| 97 // Is the closing tab the last one? If so, return the penultimate tab. | 91 // Is the closing tab the last one? If so, return the penultimate tab. |
| 98 if (selectedIndex == numberOfTabs - 1) | 92 if (selectedIndex == numberOfTabs - 1) |
| 99 return [model_ tabAtIndex:selectedIndex - 1]; | 93 return [model_ tabAtIndex:selectedIndex - 1]; |
| 100 // Otherwise return the next tab after the current tab. | 94 // Otherwise return the next tab after the current tab. |
| 101 return [model_ tabAtIndex:selectedIndex + 1]; | 95 return [model_ tabAtIndex:selectedIndex + 1]; |
| 102 } | 96 } |
| 103 | 97 |
| 104 @end | 98 @end |
| OLD | NEW |