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 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
10 #error "This file requires ARC support." | |
11 #endif | |
12 | |
13 @implementation TabModelOrderController { | |
14 __weak TabModel* model_; | |
15 } | |
16 | |
17 - (instancetype)initWithTabModel:(TabModel*)model { | |
18 DCHECK(model); | |
19 if ((self = [super init])) | |
20 model_ = model; | |
21 return self; | |
22 } | |
23 | |
24 - (Tab*)determineNewSelectedTabFromRemovedTab:(Tab*)removedTab { | |
25 // While the desktop version of this code deals in indices, this deals in | |
26 // actual tabs. As a result, the tab only needs to change iff the selection | |
27 // is the tab that's removed. | |
28 if (removedTab != model_.currentTab) | |
29 return model_.currentTab; | |
30 | |
31 const NSUInteger numberOfTabs = model_.count; | |
32 if (numberOfTabs < 2) | |
33 return nil; | |
34 | |
35 DCHECK(numberOfTabs >= 2); | |
36 DCHECK(removedTab == model_.currentTab); | |
37 | |
38 // First see if the tab being removed has any "child" tabs. If it does, we | |
39 // want to select the first in that child group, not the next tab in the same | |
40 // group of the removed tab. | |
41 Tab* firstChild = [model_ nextTabWithOpener:removedTab afterTab:nil]; | |
42 if (firstChild) | |
43 return firstChild; | |
44 Tab* parentTab = [model_ openerOfTab:removedTab]; | |
45 if (parentTab) { | |
46 // If the tab was in a group, shift selection to the next tab in the group. | |
47 Tab* nextTab = [model_ nextTabWithOpener:parentTab afterTab:removedTab]; | |
48 if (nextTab) | |
49 return nextTab; | |
50 | |
51 // If we can't find a subsequent group member, just fall back to the | |
52 // parentTab itself. Note that we use "group" here since opener is | |
53 // reset by select operations. | |
54 return parentTab; | |
55 } | |
56 | |
57 // No opener set, fall through to the default handler... | |
58 NSUInteger selectedIndex = [model_ indexOfTab:removedTab]; | |
59 DCHECK(selectedIndex <= numberOfTabs - 1); | |
60 // Is the closing tab the last one? If so, return the penultimate tab. | |
61 if (selectedIndex == numberOfTabs - 1) | |
62 return [model_ tabAtIndex:selectedIndex - 1]; | |
63 // Otherwise return the next tab after the current tab. | |
64 return [model_ tabAtIndex:selectedIndex + 1]; | |
65 } | |
66 | |
67 @end | |
OLD | NEW |