OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/shared/chrome/browser/tabs/web_state_list_order_controller.h" | 5 #import "ios/shared/chrome/browser/tabs/web_state_list_order_controller.h" |
6 | 6 |
7 #include <cstdint> | 7 #include <cstdint> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #import "ios/shared/chrome/browser/tabs/web_state_list.h" | 10 #import "ios/shared/chrome/browser/tabs/web_state_list.h" |
| 11 #import "ios/shared/chrome/browser/tabs/web_state_opener.h" |
11 | 12 |
12 WebStateListOrderController::WebStateListOrderController( | 13 WebStateListOrderController::WebStateListOrderController( |
13 WebStateList* web_state_list) | 14 WebStateList* web_state_list) |
14 : web_state_list_(web_state_list) { | 15 : web_state_list_(web_state_list) { |
15 DCHECK(web_state_list_); | 16 DCHECK(web_state_list_); |
16 } | 17 } |
17 | 18 |
18 WebStateListOrderController::~WebStateListOrderController() = default; | 19 WebStateListOrderController::~WebStateListOrderController() = default; |
19 | 20 |
20 int WebStateListOrderController::DetermineInsertionIndex( | 21 int WebStateListOrderController::DetermineInsertionIndex( |
(...skipping 25 matching lines...) Expand all Loading... |
46 DCHECK(web_state_list_->ContainsIndex(removing_index)); | 47 DCHECK(web_state_list_->ContainsIndex(removing_index)); |
47 // First see if the index being removed has any "child" WebState. If it does, | 48 // First see if the index being removed has any "child" WebState. If it does, |
48 // select the first WebState in that child group, not the next in the removed | 49 // select the first WebState in that child group, not the next in the removed |
49 // index group. | 50 // index group. |
50 int index = web_state_list_->GetIndexOfNextWebStateOpenedBy( | 51 int index = web_state_list_->GetIndexOfNextWebStateOpenedBy( |
51 web_state_list_->GetWebStateAt(removing_index), removing_index, false); | 52 web_state_list_->GetWebStateAt(removing_index), removing_index, false); |
52 | 53 |
53 if (index != WebStateList::kInvalidIndex) | 54 if (index != WebStateList::kInvalidIndex) |
54 return GetValidIndex(index, removing_index); | 55 return GetValidIndex(index, removing_index); |
55 | 56 |
56 web::WebState* opener = | 57 WebStateOpener opener = |
57 web_state_list_->GetOpenerOfWebStateAt(removing_index); | 58 web_state_list_->GetOpenerOfWebStateAt(removing_index); |
58 if (opener) { | 59 if (opener.opener) { |
59 // If the WebState was in a group, shift selection to the next WebState in | 60 // If the WebState was in a group, shift selection to the next WebState in |
60 // the group. | 61 // the group. |
61 int index = web_state_list_->GetIndexOfNextWebStateOpenedBy( | 62 int index = web_state_list_->GetIndexOfNextWebStateOpenedBy( |
62 opener, removing_index, false); | 63 opener.opener, removing_index, false); |
63 | 64 |
64 if (index != WebStateList::kInvalidIndex) | 65 if (index != WebStateList::kInvalidIndex) |
65 return GetValidIndex(index, removing_index); | 66 return GetValidIndex(index, removing_index); |
66 | 67 |
67 // If there is no subsequent group member, just fall back to opener itself. | 68 // If there is no subsequent group member, just fall back to opener itself. |
68 index = web_state_list_->GetIndexOfWebState(opener); | 69 index = web_state_list_->GetIndexOfWebState(opener.opener); |
69 return GetValidIndex(index, removing_index); | 70 return GetValidIndex(index, removing_index); |
70 } | 71 } |
71 | 72 |
72 // If this is the last WebState in the WebStateList, clear the selection. | 73 // If this is the last WebState in the WebStateList, clear the selection. |
73 if (web_state_list_->count() == 1) | 74 if (web_state_list_->count() == 1) |
74 return WebStateList::kInvalidIndex; | 75 return WebStateList::kInvalidIndex; |
75 | 76 |
76 // No opener, fall through to the default handler, i.e. returning the previous | 77 // No opener, fall through to the default handler, i.e. returning the previous |
77 // WebState if the removed one is the last, otherwise returning the next one. | 78 // WebState if the removed one is the last, otherwise returning the next one. |
78 if (removing_index >= web_state_list_->count() - 1) | 79 if (removing_index >= web_state_list_->count() - 1) |
79 return removing_index - 1; | 80 return removing_index - 1; |
80 | 81 |
81 return removing_index; | 82 return removing_index; |
82 } | 83 } |
83 | 84 |
84 int WebStateListOrderController::GetValidIndex(int index, | 85 int WebStateListOrderController::GetValidIndex(int index, |
85 int removing_index) const { | 86 int removing_index) const { |
86 if (removing_index < index) | 87 if (removing_index < index) |
87 return std::min(0, index - 1); | 88 return std::min(0, index - 1); |
88 return index; | 89 return index; |
89 } | 90 } |
OLD | NEW |