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 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 } | 153 } |
| 154 | 154 |
| 155 contents_data_.insert(contents_data_.begin() + index, data); | 155 contents_data_.insert(contents_data_.begin() + index, data); |
| 156 | 156 |
| 157 selection_model_.IncrementFrom(index); | 157 selection_model_.IncrementFrom(index); |
| 158 | 158 |
| 159 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | 159 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 160 TabInsertedAt(contents, index, active)); | 160 TabInsertedAt(contents, index, active)); |
| 161 | 161 |
| 162 if (active) { | 162 if (active) { |
| 163 selection_model_.SetSelectedIndex(index); | 163 selection_model_.SetSelectedIndex(index); |
|
sky
2011/06/03 15:35:56
As mentioned in other email, make this send out bo
dpapad
2011/06/03 17:49:01
Done.
| |
| 164 NotifyTabSelectedIfChanged(selected_contents, index, false); | 164 NotifyTabSelectedIfChanged(selected_contents, index, false); |
| 165 } | 165 } |
| 166 } | 166 } |
| 167 | 167 |
| 168 TabContentsWrapper* TabStripModel::ReplaceTabContentsAt( | 168 TabContentsWrapper* TabStripModel::ReplaceTabContentsAt( |
| 169 int index, | 169 int index, |
| 170 TabContentsWrapper* new_contents) { | 170 TabContentsWrapper* new_contents) { |
| 171 DCHECK(ContainsIndex(index)); | 171 DCHECK(ContainsIndex(index)); |
| 172 TabContentsWrapper* old_contents = GetContentsAt(index); | 172 TabContentsWrapper* old_contents = GetContentsAt(index); |
| 173 | 173 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | 216 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 217 TabDetachedAt(removed_contents, index)); | 217 TabDetachedAt(removed_contents, index)); |
| 218 if (empty()) { | 218 if (empty()) { |
| 219 // TabDetachedAt() might unregister observers, so send |TabStripEmtpy()| in | 219 // TabDetachedAt() might unregister observers, so send |TabStripEmtpy()| in |
| 220 // a second pass. | 220 // a second pass. |
| 221 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, TabStripEmpty()); | 221 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, TabStripEmpty()); |
| 222 } else { | 222 } else { |
| 223 int old_active = active_index(); | 223 int old_active = active_index(); |
| 224 selection_model_.DecrementFrom(index); | 224 selection_model_.DecrementFrom(index); |
| 225 if (index == old_active) { | 225 if (index == old_active) { |
| 226 if (!selection_model_.empty()) { | 226 if (!selection_model_.empty()) { |
|
sky
2011/06/03 15:35:56
Make these send out new notification too.
dpapad
2011/06/03 17:49:01
Done.
| |
| 227 // A selected tab was removed, but there is still something selected. | 227 // A selected tab was removed, but there is still something selected. |
| 228 // Move the active and anchor to the first selected index. | 228 // Move the active and anchor to the first selected index. |
| 229 selection_model_.set_active(selection_model_.selected_indices()[0]); | 229 selection_model_.set_active(selection_model_.selected_indices()[0]); |
| 230 selection_model_.set_anchor(selection_model_.active()); | 230 selection_model_.set_anchor(selection_model_.active()); |
| 231 NotifyTabSelectedIfChanged(removed_contents, active_index(), false); | 231 NotifyTabSelectedIfChanged(removed_contents, active_index(), false); |
| 232 } else { | 232 } else { |
| 233 // The active tab was removed and nothing is selected. Reset the | 233 // The active tab was removed and nothing is selected. Reset the |
| 234 // selection and send out notification. | 234 // selection and send out notification. |
| 235 selection_model_.SetSelectedIndex(next_selected_index); | 235 selection_model_.SetSelectedIndex(next_selected_index); |
| 236 NotifyTabSelectedIfChanged(removed_contents, next_selected_index, | 236 NotifyTabSelectedIfChanged(removed_contents, next_selected_index, |
| (...skipping 325 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); |
| 574 // This may not have resulted in a change, but we assume it did. | 575 NotifyIfActiveOrSelectionChanged(old_selection_model); |
| 575 NotifyActiveTabChanged(old_active); | |
| 576 } | 576 } |
| 577 | 577 |
| 578 void TabStripModel::ToggleSelectionAt(int index) { | 578 void TabStripModel::ToggleSelectionAt(int index) { |
| 579 DCHECK(ContainsIndex(index)); | 579 DCHECK(ContainsIndex(index)); |
| 580 int old_active = active_index(); | 580 TabStripSelectionModel old_selection_model; |
| 581 old_selection_model.Copy(selection_model()); | |
| 581 if (selection_model_.IsSelected(index)) { | 582 if (selection_model_.IsSelected(index)) { |
| 582 if (selection_model_.size() == 1) { | 583 if (selection_model_.size() == 1) { |
| 583 // One tab must be selected and this tab is currently selected so we can't | 584 // One tab must be selected and this tab is currently selected so we can't |
| 584 // unselect it. | 585 // unselect it. |
| 585 return; | 586 return; |
| 586 } | 587 } |
| 587 selection_model_.RemoveIndexFromSelection(index); | 588 selection_model_.RemoveIndexFromSelection(index); |
| 588 selection_model_.set_anchor(index); | 589 selection_model_.set_anchor(index); |
| 589 if (selection_model_.active() == TabStripSelectionModel::kUnselectedIndex) | 590 if (selection_model_.active() == TabStripSelectionModel::kUnselectedIndex) |
| 590 selection_model_.set_active(selection_model_.selected_indices()[0]); | 591 selection_model_.set_active(selection_model_.selected_indices()[0]); |
| 591 } else { | 592 } else { |
| 592 selection_model_.AddIndexToSelection(index); | 593 selection_model_.AddIndexToSelection(index); |
| 593 selection_model_.set_anchor(index); | 594 selection_model_.set_anchor(index); |
| 594 selection_model_.set_active(index); | 595 selection_model_.set_active(index); |
| 595 } | 596 } |
| 596 NotifyActiveTabChanged(old_active); | 597 NotifyIfActiveOrSelectionChanged(old_selection_model); |
| 597 } | 598 } |
| 598 | 599 |
| 599 void TabStripModel::AddSelectionFromAnchorTo(int index) { | 600 void TabStripModel::AddSelectionFromAnchorTo(int index) { |
| 600 int old_active = active_index(); | 601 TabStripSelectionModel old_selection_model; |
| 602 old_selection_model.Copy(selection_model()); | |
| 601 selection_model_.AddSelectionFromAnchorTo(index); | 603 selection_model_.AddSelectionFromAnchorTo(index); |
| 602 NotifyActiveTabChanged(old_active); | 604 NotifyIfActiveOrSelectionChanged(old_selection_model); |
| 603 } | 605 } |
| 604 | 606 |
| 605 bool TabStripModel::IsTabSelected(int index) const { | 607 bool TabStripModel::IsTabSelected(int index) const { |
| 606 DCHECK(ContainsIndex(index)); | 608 DCHECK(ContainsIndex(index)); |
| 607 return selection_model_.IsSelected(index); | 609 return selection_model_.IsSelected(index); |
| 608 } | 610 } |
| 609 | 611 |
| 610 void TabStripModel::SetSelectionFromModel( | 612 void TabStripModel::SetSelectionFromModel( |
| 611 const TabStripSelectionModel& source) { | 613 const TabStripSelectionModel& source) { |
| 612 DCHECK_NE(TabStripSelectionModel::kUnselectedIndex, source.active()); | 614 DCHECK_NE(TabStripSelectionModel::kUnselectedIndex, source.active()); |
| 613 int old_active_index = active_index(); | 615 TabStripSelectionModel old_selection_model; |
| 616 old_selection_model.Copy(selection_model()); | |
| 614 selection_model_.Copy(source); | 617 selection_model_.Copy(source); |
| 615 // This may not have resulted in a change, but we assume it did. | 618 NotifyIfActiveOrSelectionChanged(old_selection_model); |
| 616 NotifyActiveTabChanged(old_active_index); | |
| 617 } | 619 } |
| 618 | 620 |
| 619 void TabStripModel::AddTabContents(TabContentsWrapper* contents, | 621 void TabStripModel::AddTabContents(TabContentsWrapper* contents, |
| 620 int index, | 622 int index, |
| 621 PageTransition::Type transition, | 623 PageTransition::Type transition, |
| 622 int add_types) { | 624 int add_types) { |
| 623 // If the newly-opened tab is part of the same task as the parent tab, we want | 625 // 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 | 626 // to inherit the parent's "group" attribute, so that if this tab is then |
| 625 // closed we'll jump back to the parent tab. | 627 // closed we'll jump back to the parent tab. |
| 626 bool inherit_group = (add_types & ADD_INHERIT_GROUP) == ADD_INHERIT_GROUP; | 628 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 = | 1231 TabContentsWrapper* old_tab = |
| 1230 old_active_index == TabStripSelectionModel::kUnselectedIndex ? | 1232 old_active_index == TabStripSelectionModel::kUnselectedIndex ? |
| 1231 NULL : GetTabContentsAt(old_active_index); | 1233 NULL : GetTabContentsAt(old_active_index); |
| 1232 TabContentsWrapper* new_tab = | 1234 TabContentsWrapper* new_tab = |
| 1233 active_index() == TabStripSelectionModel::kUnselectedIndex ? | 1235 active_index() == TabStripSelectionModel::kUnselectedIndex ? |
| 1234 NULL : GetTabContentsAt(active_index()); | 1236 NULL : GetTabContentsAt(active_index()); |
| 1235 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | 1237 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 1236 ActiveTabChanged(old_tab, new_tab, active_index(), true)); | 1238 ActiveTabChanged(old_tab, new_tab, active_index(), true)); |
| 1237 } | 1239 } |
| 1238 | 1240 |
| 1241 void TabStripModel::NotifySelectionChanged( | |
| 1242 const TabStripSelectionModel& old_selection_model) { | |
| 1243 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | |
| 1244 TabSelectionChanged(old_selection_model)); | |
| 1245 } | |
| 1246 | |
| 1247 void TabStripModel::NotifyIfActiveOrSelectionChanged( | |
| 1248 const TabStripSelectionModel& old_selection_model) { | |
| 1249 if (old_selection_model.active() != active_index()) | |
| 1250 NotifyActiveTabChanged(old_selection_model.active()); | |
| 1251 if (!selection_model().Equals(old_selection_model)) | |
| 1252 NotifySelectionChanged(old_selection_model); | |
| 1253 } | |
| 1254 | |
| 1239 void TabStripModel::SelectRelativeTab(bool next) { | 1255 void TabStripModel::SelectRelativeTab(bool next) { |
| 1240 // This may happen during automated testing or if a user somehow buffers | 1256 // This may happen during automated testing or if a user somehow buffers |
| 1241 // many key accelerators. | 1257 // many key accelerators. |
| 1242 if (contents_data_.empty()) | 1258 if (contents_data_.empty()) |
| 1243 return; | 1259 return; |
| 1244 | 1260 |
| 1245 int index = active_index(); | 1261 int index = active_index(); |
| 1246 int delta = next ? 1 : -1; | 1262 int delta = next ? 1 : -1; |
| 1247 index = (index + count() + delta) % count(); | 1263 index = (index + count() + delta) % count(); |
| 1248 ActivateTabAt(index, true); | 1264 ActivateTabAt(index, true); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1311 void TabStripModel::ForgetOpenersAndGroupsReferencing( | 1327 void TabStripModel::ForgetOpenersAndGroupsReferencing( |
| 1312 const NavigationController* tab) { | 1328 const NavigationController* tab) { |
| 1313 for (TabContentsDataVector::const_iterator i = contents_data_.begin(); | 1329 for (TabContentsDataVector::const_iterator i = contents_data_.begin(); |
| 1314 i != contents_data_.end(); ++i) { | 1330 i != contents_data_.end(); ++i) { |
| 1315 if ((*i)->group == tab) | 1331 if ((*i)->group == tab) |
| 1316 (*i)->group = NULL; | 1332 (*i)->group = NULL; |
| 1317 if ((*i)->opener == tab) | 1333 if ((*i)->opener == tab) |
| 1318 (*i)->opener = NULL; | 1334 (*i)->opener = NULL; |
| 1319 } | 1335 } |
| 1320 } | 1336 } |
| OLD | NEW |