| 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 #if !defined(__has_feature) || !__has_feature(objc_arc) | 9 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 10 #error "This file requires ARC support." | 10 #error "This file requires ARC support." |
| 11 #endif | 11 #endif |
| 12 | 12 |
| 13 @implementation TabModelOrderController { | 13 @implementation TabModelOrderController { |
| 14 __weak TabModel* model_; | 14 __weak TabModel* model_; |
| 15 } | 15 } |
| 16 | 16 |
| 17 - (instancetype)initWithTabModel:(TabModel*)model { | 17 - (instancetype)initWithTabModel:(TabModel*)model { |
| 18 DCHECK(model); | 18 DCHECK(model); |
| 19 if ((self = [super init])) | 19 if ((self = [super init])) |
| 20 model_ = model; | 20 model_ = model; |
| 21 return self; | 21 return self; |
| 22 } | 22 } |
| 23 | 23 |
| 24 - (NSUInteger)insertionIndexForTab:(Tab*)newTab | |
| 25 transition:(ui::PageTransition)transition | |
| 26 opener:(Tab*)parentTab | |
| 27 adjacency:(TabModelOrderConstants::InsertionAdjacency) | |
| 28 adjacency { | |
| 29 if (model_.isEmpty) | |
| 30 return 0; | |
| 31 | |
| 32 if (!parentTab) | |
| 33 return [self insertionIndexForAppending]; | |
| 34 | |
| 35 if (!PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_LINK)) | |
| 36 return [self insertionIndexForAppending]; | |
| 37 | |
| 38 NSUInteger referenceIndex = [model_ indexOfTab:parentTab]; | |
| 39 Tab* openLocation = [model_ lastTabWithOpener:parentTab]; | |
| 40 if (openLocation) | |
| 41 referenceIndex = [model_ indexOfTab:openLocation]; | |
| 42 | |
| 43 DCHECK_NE(referenceIndex, static_cast<NSUInteger>(NSNotFound)); | |
| 44 return referenceIndex + 1; | |
| 45 } | |
| 46 | |
| 47 - (NSUInteger)insertionIndexForAppending { | |
| 48 return model_.count; | |
| 49 } | |
| 50 | |
| 51 - (Tab*)determineNewSelectedTabFromRemovedTab:(Tab*)removedTab { | 24 - (Tab*)determineNewSelectedTabFromRemovedTab:(Tab*)removedTab { |
| 52 // While the desktop version of this code deals in indices, this deals in | 25 // While the desktop version of this code deals in indices, this deals in |
| 53 // actual tabs. As a result, the tab only needs to change iff the selection | 26 // actual tabs. As a result, the tab only needs to change iff the selection |
| 54 // is the tab that's removed. | 27 // is the tab that's removed. |
| 55 if (removedTab != model_.currentTab) | 28 if (removedTab != model_.currentTab) |
| 56 return model_.currentTab; | 29 return model_.currentTab; |
| 57 | 30 |
| 58 const NSUInteger numberOfTabs = model_.count; | 31 const NSUInteger numberOfTabs = model_.count; |
| 59 if (numberOfTabs < 2) | 32 if (numberOfTabs < 2) |
| 60 return nil; | 33 return nil; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 85 NSUInteger selectedIndex = [model_ indexOfTab:removedTab]; | 58 NSUInteger selectedIndex = [model_ indexOfTab:removedTab]; |
| 86 DCHECK(selectedIndex <= numberOfTabs - 1); | 59 DCHECK(selectedIndex <= numberOfTabs - 1); |
| 87 // Is the closing tab the last one? If so, return the penultimate tab. | 60 // Is the closing tab the last one? If so, return the penultimate tab. |
| 88 if (selectedIndex == numberOfTabs - 1) | 61 if (selectedIndex == numberOfTabs - 1) |
| 89 return [model_ tabAtIndex:selectedIndex - 1]; | 62 return [model_ tabAtIndex:selectedIndex - 1]; |
| 90 // Otherwise return the next tab after the current tab. | 63 // Otherwise return the next tab after the current tab. |
| 91 return [model_ tabAtIndex:selectedIndex + 1]; | 64 return [model_ tabAtIndex:selectedIndex + 1]; |
| 92 } | 65 } |
| 93 | 66 |
| 94 @end | 67 @end |
| OLD | NEW |