Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "chrome/browser/tabs/tab_strip_model.h" | 5 #include "chrome/browser/tabs/tab_strip_model.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 551 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 562 return count(); | 562 return count(); |
| 563 } | 563 } |
| 564 | 564 |
| 565 int TabStripModel::ConstrainInsertionIndex(int index, bool mini_tab) { | 565 int TabStripModel::ConstrainInsertionIndex(int index, bool mini_tab) { |
| 566 return mini_tab ? std::min(std::max(0, index), IndexOfFirstNonMiniTab()) : | 566 return mini_tab ? std::min(std::max(0, index), IndexOfFirstNonMiniTab()) : |
| 567 std::min(count(), std::max(index, IndexOfFirstNonMiniTab())); | 567 std::min(count(), std::max(index, IndexOfFirstNonMiniTab())); |
| 568 } | 568 } |
| 569 | 569 |
| 570 void TabStripModel::ExtendSelectionTo(int index) { | 570 void TabStripModel::ExtendSelectionTo(int index) { |
| 571 DCHECK(ContainsIndex(index)); | 571 DCHECK(ContainsIndex(index)); |
| 572 int old_active = active_index(); | 572 TabStripSelectionModel old_selection_model; |
| 573 old_selection_model.Copy(selection_model()); | |
| 573 selection_model_.SetSelectionFromAnchorTo(index); | 574 selection_model_.SetSelectionFromAnchorTo(index); |
| 575 if (old_selection_model.active() != active_index()) | |
|
dpapad
2011/06/02 18:40:33
Instead of repeatedly checking this I would prefer
sky
2011/06/02 20:27:58
Yes. In fact how about a single SelectionChanged m
dpapad
2011/06/02 23:29:50
Done.
| |
| 576 NotifyActiveTabChanged(old_selection_model.active()); | |
| 577 | |
| 574 // This may not have resulted in a change, but we assume it did. | 578 // This may not have resulted in a change, but we assume it did. |
| 575 NotifyActiveTabChanged(old_active); | 579 NotifySelectionChanged(old_selection_model); |
| 576 } | 580 } |
| 577 | 581 |
| 578 void TabStripModel::ToggleSelectionAt(int index) { | 582 void TabStripModel::ToggleSelectionAt(int index) { |
| 579 DCHECK(ContainsIndex(index)); | 583 DCHECK(ContainsIndex(index)); |
| 580 int old_active = active_index(); | 584 TabStripSelectionModel old_selection_model; |
| 585 old_selection_model.Copy(selection_model()); | |
| 581 if (selection_model_.IsSelected(index)) { | 586 if (selection_model_.IsSelected(index)) { |
| 582 if (selection_model_.size() == 1) { | 587 if (selection_model_.size() == 1) { |
| 583 // One tab must be selected and this tab is currently selected so we can't | 588 // One tab must be selected and this tab is currently selected so we can't |
| 584 // unselect it. | 589 // unselect it. |
| 585 return; | 590 return; |
| 586 } | 591 } |
| 587 selection_model_.RemoveIndexFromSelection(index); | 592 selection_model_.RemoveIndexFromSelection(index); |
| 588 selection_model_.set_anchor(index); | 593 selection_model_.set_anchor(index); |
| 589 if (selection_model_.active() == TabStripSelectionModel::kUnselectedIndex) | 594 if (selection_model_.active() == TabStripSelectionModel::kUnselectedIndex) |
| 590 selection_model_.set_active(selection_model_.selected_indices()[0]); | 595 selection_model_.set_active(selection_model_.selected_indices()[0]); |
| 591 } else { | 596 } else { |
| 592 selection_model_.AddIndexToSelection(index); | 597 selection_model_.AddIndexToSelection(index); |
| 593 selection_model_.set_anchor(index); | 598 selection_model_.set_anchor(index); |
| 594 selection_model_.set_active(index); | 599 selection_model_.set_active(index); |
| 595 } | 600 } |
| 596 NotifyActiveTabChanged(old_active); | 601 if (old_selection_model.active() != active_index()) |
| 602 NotifyActiveTabChanged(old_selection_model.active()); | |
| 603 NotifySelectionChanged(old_selection_model); | |
| 597 } | 604 } |
| 598 | 605 |
| 599 void TabStripModel::AddSelectionFromAnchorTo(int index) { | 606 void TabStripModel::AddSelectionFromAnchorTo(int index) { |
| 600 int old_active = active_index(); | 607 TabStripSelectionModel old_selection_model; |
| 608 old_selection_model.Copy(selection_model()); | |
| 601 selection_model_.AddSelectionFromAnchorTo(index); | 609 selection_model_.AddSelectionFromAnchorTo(index); |
| 602 NotifyActiveTabChanged(old_active); | 610 if (old_selection_model.active() != active_index()) |
| 611 NotifyActiveTabChanged(old_selection_model.active()); | |
| 612 NotifySelectionChanged(old_selection_model); | |
| 603 } | 613 } |
| 604 | 614 |
| 605 bool TabStripModel::IsTabSelected(int index) const { | 615 bool TabStripModel::IsTabSelected(int index) const { |
| 606 DCHECK(ContainsIndex(index)); | 616 DCHECK(ContainsIndex(index)); |
| 607 return selection_model_.IsSelected(index); | 617 return selection_model_.IsSelected(index); |
| 608 } | 618 } |
| 609 | 619 |
| 610 void TabStripModel::SetSelectionFromModel( | 620 void TabStripModel::SetSelectionFromModel( |
| 611 const TabStripSelectionModel& source) { | 621 const TabStripSelectionModel& source) { |
| 612 DCHECK_NE(TabStripSelectionModel::kUnselectedIndex, source.active()); | 622 DCHECK_NE(TabStripSelectionModel::kUnselectedIndex, source.active()); |
| 613 int old_active_index = active_index(); | 623 TabStripSelectionModel old_selection_model; |
| 624 old_selection_model.Copy(selection_model()); | |
| 614 selection_model_.Copy(source); | 625 selection_model_.Copy(source); |
| 615 // This may not have resulted in a change, but we assume it did. | 626 // This may not have resulted in a change, but we assume it did. |
| 616 NotifyActiveTabChanged(old_active_index); | 627 if (old_selection_model.active() != active_index()) |
| 628 NotifyActiveTabChanged(old_selection_model.active()); | |
| 629 NotifySelectionChanged(old_selection_model); | |
| 617 } | 630 } |
| 618 | 631 |
| 619 void TabStripModel::AddTabContents(TabContentsWrapper* contents, | 632 void TabStripModel::AddTabContents(TabContentsWrapper* contents, |
| 620 int index, | 633 int index, |
| 621 PageTransition::Type transition, | 634 PageTransition::Type transition, |
| 622 int add_types) { | 635 int add_types) { |
| 623 // If the newly-opened tab is part of the same task as the parent tab, we want | 636 // If the newly-opened tab is part of the same task as the parent tab, we want |
| 624 // to inherit the parent's "group" attribute, so that if this tab is then | 637 // to inherit the parent's "group" attribute, so that if this tab is then |
| 625 // closed we'll jump back to the parent tab. | 638 // closed we'll jump back to the parent tab. |
| 626 bool inherit_group = (add_types & ADD_INHERIT_GROUP) == ADD_INHERIT_GROUP; | 639 bool inherit_group = (add_types & ADD_INHERIT_GROUP) == ADD_INHERIT_GROUP; |
| (...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1229 TabContentsWrapper* old_tab = | 1242 TabContentsWrapper* old_tab = |
| 1230 old_active_index == TabStripSelectionModel::kUnselectedIndex ? | 1243 old_active_index == TabStripSelectionModel::kUnselectedIndex ? |
| 1231 NULL : GetTabContentsAt(old_active_index); | 1244 NULL : GetTabContentsAt(old_active_index); |
| 1232 TabContentsWrapper* new_tab = | 1245 TabContentsWrapper* new_tab = |
| 1233 active_index() == TabStripSelectionModel::kUnselectedIndex ? | 1246 active_index() == TabStripSelectionModel::kUnselectedIndex ? |
| 1234 NULL : GetTabContentsAt(active_index()); | 1247 NULL : GetTabContentsAt(active_index()); |
| 1235 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | 1248 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 1236 ActiveTabChanged(old_tab, new_tab, active_index(), true)); | 1249 ActiveTabChanged(old_tab, new_tab, active_index(), true)); |
| 1237 } | 1250 } |
| 1238 | 1251 |
| 1252 void TabStripModel::NotifySelectionChanged( | |
| 1253 const TabStripSelectionModel& model) { | |
|
dpapad
2011/06/02 18:40:33
Also how about adding a check here to determine if
sky
2011/06/02 20:27:58
Sure, but make sure you include the anchor/active
dpapad
2011/06/02 23:29:50
Done.
| |
| 1254 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | |
| 1255 TabSelectionChanged(model)); | |
| 1256 } | |
| 1257 | |
| 1239 void TabStripModel::SelectRelativeTab(bool next) { | 1258 void TabStripModel::SelectRelativeTab(bool next) { |
| 1240 // This may happen during automated testing or if a user somehow buffers | 1259 // This may happen during automated testing or if a user somehow buffers |
| 1241 // many key accelerators. | 1260 // many key accelerators. |
| 1242 if (contents_data_.empty()) | 1261 if (contents_data_.empty()) |
| 1243 return; | 1262 return; |
| 1244 | 1263 |
| 1245 int index = active_index(); | 1264 int index = active_index(); |
| 1246 int delta = next ? 1 : -1; | 1265 int delta = next ? 1 : -1; |
| 1247 index = (index + count() + delta) % count(); | 1266 index = (index + count() + delta) % count(); |
| 1248 ActivateTabAt(index, true); | 1267 ActivateTabAt(index, true); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1311 void TabStripModel::ForgetOpenersAndGroupsReferencing( | 1330 void TabStripModel::ForgetOpenersAndGroupsReferencing( |
| 1312 const NavigationController* tab) { | 1331 const NavigationController* tab) { |
| 1313 for (TabContentsDataVector::const_iterator i = contents_data_.begin(); | 1332 for (TabContentsDataVector::const_iterator i = contents_data_.begin(); |
| 1314 i != contents_data_.end(); ++i) { | 1333 i != contents_data_.end(); ++i) { |
| 1315 if ((*i)->group == tab) | 1334 if ((*i)->group == tab) |
| 1316 (*i)->group = NULL; | 1335 (*i)->group = NULL; |
| 1317 if ((*i)->opener == tab) | 1336 if ((*i)->opener == tab) |
| 1318 (*i)->opener = NULL; | 1337 (*i)->opener = NULL; |
| 1319 } | 1338 } |
| 1320 } | 1339 } |
| OLD | NEW |