OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/tabs/tab_model_order_controller.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 @implementation TabModelOrderController |
| 10 |
| 11 @synthesize insertionPolicy = insertionPolicy_; |
| 12 |
| 13 - (id)initWithTabModel:(TabModel*)model { |
| 14 DCHECK(model); |
| 15 self = [super init]; |
| 16 if (self) { |
| 17 model_ = model; |
| 18 insertionPolicy_ = TabModelOrderConstants::kInsertAfter; |
| 19 } |
| 20 return self; |
| 21 } |
| 22 |
| 23 - (NSUInteger)insertionIndexForTab:(Tab*)newTab |
| 24 transition:(ui::PageTransition)transition |
| 25 opener:(Tab*)parentTab |
| 26 adjacency:(TabModelOrderConstants::InsertionAdjacency) |
| 27 adjacency { |
| 28 if (model_.isEmpty) |
| 29 return 0; |
| 30 |
| 31 if (PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_LINK) && |
| 32 parentTab) { |
| 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 |
| 43 // If the reference tab is no longer in the model (e.g., it was closed |
| 44 // before the open request was handled), fall through to default behavior. |
| 45 if (referenceIndex != NSNotFound) { |
| 46 BOOL insertAfter = |
| 47 insertionPolicy_ == TabModelOrderConstants::kInsertAfter || |
| 48 adjacency == TabModelOrderConstants::kAdjacentAfter; |
| 49 NSUInteger delta = insertAfter ? 1 : 0; |
| 50 return referenceIndex + delta; |
| 51 } |
| 52 } |
| 53 // In other cases, such as a normal new tab, open at the end. |
| 54 return [self insertionIndexForAppending]; |
| 55 } |
| 56 |
| 57 - (NSUInteger)insertionIndexForAppending { |
| 58 return insertionPolicy_ == TabModelOrderConstants::kInsertAfter ? model_.count |
| 59 : 0; |
| 60 } |
| 61 |
| 62 - (Tab*)determineNewSelectedTabFromRemovedTab:(Tab*)removedTab { |
| 63 // 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 |
| 65 // is the tab that's removed. |
| 66 if (removedTab != model_.currentTab) |
| 67 return model_.currentTab; |
| 68 |
| 69 const NSUInteger numberOfTabs = model_.count; |
| 70 if (numberOfTabs < 2) |
| 71 return nil; |
| 72 DCHECK(numberOfTabs >= 2); |
| 73 DCHECK(removedTab == model_.currentTab); |
| 74 |
| 75 // 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 |
| 77 // group of the removed tab. |
| 78 Tab* firstChild = [model_ nextTabWithOpener:removedTab afterTab:nil]; |
| 79 if (firstChild) |
| 80 return firstChild; |
| 81 Tab* parentTab = [model_ openerOfTab:removedTab]; |
| 82 if (parentTab) { |
| 83 // If the tab was in a group, shift selection to the next tab in the group. |
| 84 Tab* nextTab = [model_ nextTabWithOpener:parentTab afterTab:removedTab]; |
| 85 if (nextTab) |
| 86 return nextTab; |
| 87 |
| 88 // If we can't find a subsequent group member, just fall back to the |
| 89 // parentTab itself. Note that we use "group" here since opener is |
| 90 // reset by select operations. |
| 91 return parentTab; |
| 92 } |
| 93 |
| 94 // No opener set, fall through to the default handler... |
| 95 NSUInteger selectedIndex = [model_ indexOfTab:removedTab]; |
| 96 DCHECK(selectedIndex <= numberOfTabs - 1); |
| 97 // Is the closing tab the last one? If so, return the penultimate tab. |
| 98 if (selectedIndex == numberOfTabs - 1) |
| 99 return [model_ tabAtIndex:selectedIndex - 1]; |
| 100 // Otherwise return the next tab after the current tab. |
| 101 return [model_ tabAtIndex:selectedIndex + 1]; |
| 102 } |
| 103 |
| 104 @end |
OLD | NEW |