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" |
(...skipping 22 matching lines...) Expand all Loading... |
33 opener, opener_index, true); | 33 opener, opener_index, true); |
34 | 34 |
35 int reference_index = list_child_index != WebStateList::kInvalidIndex | 35 int reference_index = list_child_index != WebStateList::kInvalidIndex |
36 ? list_child_index | 36 ? list_child_index |
37 : opener_index; | 37 : opener_index; |
38 | 38 |
39 // Check for overflows (just a DCHECK as INT_MAX open WebState is unlikely). | 39 // Check for overflows (just a DCHECK as INT_MAX open WebState is unlikely). |
40 DCHECK_LT(reference_index, INT_MAX); | 40 DCHECK_LT(reference_index, INT_MAX); |
41 return reference_index + 1; | 41 return reference_index + 1; |
42 } | 42 } |
| 43 |
| 44 int WebStateListOrderController::DetermineNewActiveIndex( |
| 45 int removing_index) const { |
| 46 DCHECK(web_state_list_->ContainsIndex(removing_index)); |
| 47 // 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 // index group. |
| 50 int index = web_state_list_->GetIndexOfNextWebStateOpenedBy( |
| 51 web_state_list_->GetWebStateAt(removing_index), removing_index, false); |
| 52 |
| 53 if (index != WebStateList::kInvalidIndex) |
| 54 return GetValidIndex(index, removing_index); |
| 55 |
| 56 web::WebState* opener = |
| 57 web_state_list_->GetOpenerOfWebStateAt(removing_index); |
| 58 if (opener) { |
| 59 // If the WebState was in a group, shift selection to the next WebState in |
| 60 // the group. |
| 61 int index = web_state_list_->GetIndexOfNextWebStateOpenedBy( |
| 62 opener, removing_index, false); |
| 63 |
| 64 if (index != WebStateList::kInvalidIndex) |
| 65 return GetValidIndex(index, removing_index); |
| 66 |
| 67 // If there is no subsequent group member, just fall back to opener itself. |
| 68 index = web_state_list_->GetIndexOfWebState(opener); |
| 69 return GetValidIndex(index, removing_index); |
| 70 } |
| 71 |
| 72 // If this is the last WebState in the WebStateList, clear the selection. |
| 73 if (web_state_list_->count() == 1) |
| 74 return WebStateList::kInvalidIndex; |
| 75 |
| 76 // 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 if (removing_index >= web_state_list_->count() - 1) |
| 79 return removing_index - 1; |
| 80 |
| 81 return removing_index; |
| 82 } |
| 83 |
| 84 int WebStateListOrderController::GetValidIndex(int index, |
| 85 int removing_index) const { |
| 86 if (removing_index < index) |
| 87 return std::min(0, index - 1); |
| 88 return index; |
| 89 } |
OLD | NEW |