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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 } | 150 } |
| 151 data->opener = &selected_contents->controller(); | 151 data->opener = &selected_contents->controller(); |
| 152 } | 152 } |
| 153 | 153 |
| 154 contents_data_.insert(contents_data_.begin() + index, data); | 154 contents_data_.insert(contents_data_.begin() + index, data); |
| 155 | 155 |
| 156 selection_model_.IncrementFrom(index); | 156 selection_model_.IncrementFrom(index); |
| 157 | 157 |
| 158 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | 158 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 159 TabInsertedAt(contents, index, active)); | 159 TabInsertedAt(contents, index, active)); |
| 160 | 160 TabStripSelectionModel old_model; |
| 161 old_model.Copy(selection_model_); | |
| 161 if (active) { | 162 if (active) { |
| 162 selection_model_.SetSelectedIndex(index); | 163 selection_model_.SetSelectedIndex(index); |
| 163 NotifyTabSelectedIfChanged(selected_contents, index, false); | 164 NotifyIfActiveOrSelectionChanged(selected_contents, false, old_model); |
| 164 } | 165 } |
| 165 } | 166 } |
| 166 | 167 |
| 167 TabContentsWrapper* TabStripModel::ReplaceTabContentsAt( | 168 TabContentsWrapper* TabStripModel::ReplaceTabContentsAt( |
| 168 int index, | 169 int index, |
| 169 TabContentsWrapper* new_contents) { | 170 TabContentsWrapper* new_contents) { |
| 170 DCHECK(ContainsIndex(index)); | 171 DCHECK(ContainsIndex(index)); |
| 171 TabContentsWrapper* old_contents = GetContentsAt(index); | 172 TabContentsWrapper* old_contents = GetContentsAt(index); |
| 172 | 173 |
| 173 ForgetOpenersAndGroupsReferencing(&(old_contents->controller())); | 174 ForgetOpenersAndGroupsReferencing(&(old_contents->controller())); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 214 closing_all_ = true; | 215 closing_all_ = true; |
| 215 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | 216 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 216 TabDetachedAt(removed_contents, index)); | 217 TabDetachedAt(removed_contents, index)); |
| 217 if (empty()) { | 218 if (empty()) { |
| 218 // TabDetachedAt() might unregister observers, so send |TabStripEmtpy()| in | 219 // TabDetachedAt() might unregister observers, so send |TabStripEmtpy()| in |
| 219 // a second pass. | 220 // a second pass. |
| 220 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, TabStripEmpty()); | 221 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, TabStripEmpty()); |
| 221 } else { | 222 } else { |
| 222 int old_active = active_index(); | 223 int old_active = active_index(); |
| 223 selection_model_.DecrementFrom(index); | 224 selection_model_.DecrementFrom(index); |
| 225 TabStripSelectionModel old_model; | |
| 226 old_model.Copy(selection_model_); | |
| 224 if (index == old_active) { | 227 if (index == old_active) { |
| 225 if (!selection_model_.empty()) { | 228 if (!selection_model_.empty()) { |
| 226 // A selected tab was removed, but there is still something selected. | 229 // A selected tab was removed, but there is still something selected. |
| 227 // Move the active and anchor to the first selected index. | 230 // Move the active and anchor to the first selected index. |
| 228 selection_model_.set_active(selection_model_.selected_indices()[0]); | 231 selection_model_.set_active(selection_model_.selected_indices()[0]); |
| 229 selection_model_.set_anchor(selection_model_.active()); | 232 selection_model_.set_anchor(selection_model_.active()); |
| 230 NotifyTabSelectedIfChanged(removed_contents, active_index(), false); | |
| 231 } else { | 233 } else { |
| 232 // The active tab was removed and nothing is selected. Reset the | 234 // The active tab was removed and nothing is selected. Reset the |
| 233 // selection and send out notification. | 235 // selection and send out notification. |
| 234 selection_model_.SetSelectedIndex(next_selected_index); | 236 selection_model_.SetSelectedIndex(next_selected_index); |
| 235 NotifyTabSelectedIfChanged(removed_contents, next_selected_index, | |
| 236 false); | |
| 237 } | 237 } |
| 238 NotifyIfActiveOrSelectionChanged(removed_contents, false, old_model); | |
| 238 } | 239 } |
| 239 } | 240 } |
| 240 return removed_contents; | 241 return removed_contents; |
| 241 } | 242 } |
| 242 | 243 |
| 243 void TabStripModel::ActivateTabAt(int index, bool user_gesture) { | 244 void TabStripModel::ActivateTabAt(int index, bool user_gesture) { |
| 244 DCHECK(ContainsIndex(index)); | 245 DCHECK(ContainsIndex(index)); |
| 245 bool had_multi = selection_model_.selected_indices().size() > 1; | 246 TabContentsWrapper* old_contents = GetActiveTabContents(); |
| 246 TabContentsWrapper* old_contents = | 247 TabStripSelectionModel old_model; |
| 247 (active_index() == TabStripSelectionModel::kUnselectedIndex) ? | 248 old_model.Copy(selection_model_); |
| 248 NULL : GetActiveTabContents(); | |
| 249 selection_model_.SetSelectedIndex(index); | 249 selection_model_.SetSelectedIndex(index); |
| 250 TabContentsWrapper* new_contents = GetContentsAt(index); | 250 NotifyIfActiveOrSelectionChanged(old_contents, user_gesture, old_model); |
| 251 if (old_contents != new_contents && old_contents) { | |
| 252 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | |
| 253 TabDeactivated(old_contents)); | |
| 254 } | |
| 255 if (old_contents != new_contents || had_multi) { | |
| 256 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | |
| 257 ActiveTabChanged(old_contents, new_contents, | |
| 258 active_index(), user_gesture)); | |
| 259 } | |
| 260 } | 251 } |
| 261 | 252 |
| 262 void TabStripModel::MoveTabContentsAt(int index, | 253 void TabStripModel::MoveTabContentsAt(int index, |
| 263 int to_position, | 254 int to_position, |
| 264 bool select_after_move) { | 255 bool select_after_move) { |
| 265 DCHECK(ContainsIndex(index)); | 256 DCHECK(ContainsIndex(index)); |
| 266 if (index == to_position) | 257 if (index == to_position) |
| 267 return; | 258 return; |
| 268 | 259 |
| 269 int first_non_mini_tab = IndexOfFirstNonMiniTab(); | 260 int first_non_mini_tab = IndexOfFirstNonMiniTab(); |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 561 return count(); | 552 return count(); |
| 562 } | 553 } |
| 563 | 554 |
| 564 int TabStripModel::ConstrainInsertionIndex(int index, bool mini_tab) { | 555 int TabStripModel::ConstrainInsertionIndex(int index, bool mini_tab) { |
| 565 return mini_tab ? std::min(std::max(0, index), IndexOfFirstNonMiniTab()) : | 556 return mini_tab ? std::min(std::max(0, index), IndexOfFirstNonMiniTab()) : |
| 566 std::min(count(), std::max(index, IndexOfFirstNonMiniTab())); | 557 std::min(count(), std::max(index, IndexOfFirstNonMiniTab())); |
| 567 } | 558 } |
| 568 | 559 |
| 569 void TabStripModel::ExtendSelectionTo(int index) { | 560 void TabStripModel::ExtendSelectionTo(int index) { |
| 570 DCHECK(ContainsIndex(index)); | 561 DCHECK(ContainsIndex(index)); |
| 571 int old_active = active_index(); | 562 TabContentsWrapper* old_contents = GetActiveTabContents(); |
| 563 TabStripSelectionModel old_model; | |
| 564 old_model.Copy(selection_model()); | |
| 572 selection_model_.SetSelectionFromAnchorTo(index); | 565 selection_model_.SetSelectionFromAnchorTo(index); |
| 573 // This may not have resulted in a change, but we assume it did. | 566 NotifyIfActiveOrSelectionChanged(old_contents, false, old_model); |
| 574 NotifyActiveTabChanged(old_active); | |
| 575 } | 567 } |
| 576 | 568 |
| 577 void TabStripModel::ToggleSelectionAt(int index) { | 569 void TabStripModel::ToggleSelectionAt(int index) { |
| 578 DCHECK(ContainsIndex(index)); | 570 DCHECK(ContainsIndex(index)); |
| 579 int old_active = active_index(); | 571 TabContentsWrapper* old_contents = GetActiveTabContents(); |
| 572 TabStripSelectionModel old_model; | |
| 573 old_model.Copy(selection_model()); | |
| 580 if (selection_model_.IsSelected(index)) { | 574 if (selection_model_.IsSelected(index)) { |
| 581 if (selection_model_.size() == 1) { | 575 if (selection_model_.size() == 1) { |
| 582 // One tab must be selected and this tab is currently selected so we can't | 576 // One tab must be selected and this tab is currently selected so we can't |
| 583 // unselect it. | 577 // unselect it. |
| 584 return; | 578 return; |
| 585 } | 579 } |
| 586 selection_model_.RemoveIndexFromSelection(index); | 580 selection_model_.RemoveIndexFromSelection(index); |
| 587 selection_model_.set_anchor(index); | 581 selection_model_.set_anchor(index); |
| 588 if (selection_model_.active() == TabStripSelectionModel::kUnselectedIndex) | 582 if (selection_model_.active() == TabStripSelectionModel::kUnselectedIndex) |
| 589 selection_model_.set_active(selection_model_.selected_indices()[0]); | 583 selection_model_.set_active(selection_model_.selected_indices()[0]); |
| 590 } else { | 584 } else { |
| 591 selection_model_.AddIndexToSelection(index); | 585 selection_model_.AddIndexToSelection(index); |
| 592 selection_model_.set_anchor(index); | 586 selection_model_.set_anchor(index); |
| 593 selection_model_.set_active(index); | 587 selection_model_.set_active(index); |
| 594 } | 588 } |
| 595 NotifyActiveTabChanged(old_active); | 589 NotifyIfActiveOrSelectionChanged(old_contents, false, old_model); |
| 596 } | 590 } |
| 597 | 591 |
| 598 void TabStripModel::AddSelectionFromAnchorTo(int index) { | 592 void TabStripModel::AddSelectionFromAnchorTo(int index) { |
| 599 int old_active = active_index(); | 593 TabContentsWrapper* old_contents = GetActiveTabContents(); |
| 594 TabStripSelectionModel old_model; | |
| 595 old_model.Copy(selection_model()); | |
| 600 selection_model_.AddSelectionFromAnchorTo(index); | 596 selection_model_.AddSelectionFromAnchorTo(index); |
| 601 NotifyActiveTabChanged(old_active); | 597 NotifyIfActiveOrSelectionChanged(old_contents, false, old_model); |
| 602 } | 598 } |
| 603 | 599 |
| 604 bool TabStripModel::IsTabSelected(int index) const { | 600 bool TabStripModel::IsTabSelected(int index) const { |
| 605 DCHECK(ContainsIndex(index)); | 601 DCHECK(ContainsIndex(index)); |
| 606 return selection_model_.IsSelected(index); | 602 return selection_model_.IsSelected(index); |
| 607 } | 603 } |
| 608 | 604 |
| 609 void TabStripModel::SetSelectionFromModel( | 605 void TabStripModel::SetSelectionFromModel( |
| 610 const TabStripSelectionModel& source) { | 606 const TabStripSelectionModel& source) { |
| 611 DCHECK_NE(TabStripSelectionModel::kUnselectedIndex, source.active()); | 607 DCHECK_NE(TabStripSelectionModel::kUnselectedIndex, source.active()); |
| 612 int old_active_index = active_index(); | 608 TabContentsWrapper* old_contents = GetActiveTabContents(); |
| 609 TabStripSelectionModel old_model; | |
| 610 old_model.Copy(selection_model()); | |
| 613 selection_model_.Copy(source); | 611 selection_model_.Copy(source); |
| 614 // This may not have resulted in a change, but we assume it did. | 612 NotifyIfActiveOrSelectionChanged(old_contents, false, old_model); |
| 615 NotifyActiveTabChanged(old_active_index); | |
| 616 } | 613 } |
| 617 | 614 |
| 618 void TabStripModel::AddTabContents(TabContentsWrapper* contents, | 615 void TabStripModel::AddTabContents(TabContentsWrapper* contents, |
| 619 int index, | 616 int index, |
| 620 PageTransition::Type transition, | 617 PageTransition::Type transition, |
| 621 int add_types) { | 618 int add_types) { |
| 622 // If the newly-opened tab is part of the same task as the parent tab, we want | 619 // If the newly-opened tab is part of the same task as the parent tab, we want |
| 623 // to inherit the parent's "group" attribute, so that if this tab is then | 620 // to inherit the parent's "group" attribute, so that if this tab is then |
| 624 // closed we'll jump back to the parent tab. | 621 // closed we'll jump back to the parent tab. |
| 625 bool inherit_group = (add_types & ADD_INHERIT_GROUP) == ADD_INHERIT_GROUP; | 622 bool inherit_group = (add_types & ADD_INHERIT_GROUP) == ADD_INHERIT_GROUP; |
| (...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1200 // and detach it. | 1197 // and detach it. |
| 1201 delete contents; | 1198 delete contents; |
| 1202 } | 1199 } |
| 1203 | 1200 |
| 1204 TabContentsWrapper* TabStripModel::GetContentsAt(int index) const { | 1201 TabContentsWrapper* TabStripModel::GetContentsAt(int index) const { |
| 1205 CHECK(ContainsIndex(index)) << | 1202 CHECK(ContainsIndex(index)) << |
| 1206 "Failed to find: " << index << " in: " << count() << " entries."; | 1203 "Failed to find: " << index << " in: " << count() << " entries."; |
| 1207 return contents_data_.at(index)->contents; | 1204 return contents_data_.at(index)->contents; |
| 1208 } | 1205 } |
| 1209 | 1206 |
| 1210 void TabStripModel::NotifyTabSelectedIfChanged(TabContentsWrapper* old_contents, | 1207 void TabStripModel::NotifyIfActiveOrSelectionChanged( |
| 1211 int to_index, | 1208 TabContentsWrapper* old_contents, |
| 1212 bool user_gesture) { | 1209 bool user_gesture, |
| 1213 TabContentsWrapper* new_contents = GetContentsAt(to_index); | 1210 const TabStripSelectionModel& old_model) { |
| 1214 if (old_contents == new_contents) | 1211 TabContentsWrapper* new_contents = GetContentsAt(active_index()); |
| 1215 return; | 1212 if (old_contents != new_contents) { |
| 1216 | 1213 TabContentsWrapper* last_selected_contents = old_contents; |
|
sky
2011/06/10 17:56:05
nit: just use old_contents instead of creating ano
dpapad
2011/06/10 18:21:37
Done.
| |
| 1217 TabContentsWrapper* last_selected_contents = old_contents; | 1214 if (last_selected_contents) { |
| 1218 if (last_selected_contents) { | 1215 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 1216 TabDeactivated(last_selected_contents)); | |
| 1217 } | |
| 1219 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | 1218 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 1220 TabDeactivated(last_selected_contents)); | 1219 ActiveTabChanged(last_selected_contents, new_contents, |
| 1220 active_index(), user_gesture)); | |
| 1221 } | 1221 } |
| 1222 | 1222 if (!selection_model().Equals(old_model)) { |
| 1223 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | 1223 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 1224 ActiveTabChanged(last_selected_contents, new_contents, | 1224 TabSelectionChanged(old_model)); |
| 1225 active_index(), user_gesture)); | 1225 } |
| 1226 } | |
| 1227 | |
| 1228 void TabStripModel::NotifyActiveTabChanged(int old_active_index) { | |
| 1229 TabContentsWrapper* old_tab = | |
| 1230 old_active_index == TabStripSelectionModel::kUnselectedIndex ? | |
| 1231 NULL : GetTabContentsAt(old_active_index); | |
| 1232 TabContentsWrapper* new_tab = | |
| 1233 active_index() == TabStripSelectionModel::kUnselectedIndex ? | |
| 1234 NULL : GetTabContentsAt(active_index()); | |
| 1235 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | |
| 1236 ActiveTabChanged(old_tab, new_tab, active_index(), true)); | |
| 1237 } | 1226 } |
| 1238 | 1227 |
| 1239 void TabStripModel::SelectRelativeTab(bool next) { | 1228 void TabStripModel::SelectRelativeTab(bool next) { |
| 1240 // This may happen during automated testing or if a user somehow buffers | 1229 // This may happen during automated testing or if a user somehow buffers |
| 1241 // many key accelerators. | 1230 // many key accelerators. |
| 1242 if (contents_data_.empty()) | 1231 if (contents_data_.empty()) |
| 1243 return; | 1232 return; |
| 1244 | 1233 |
| 1245 int index = active_index(); | 1234 int index = active_index(); |
| 1246 int delta = next ? 1 : -1; | 1235 int delta = next ? 1 : -1; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1311 void TabStripModel::ForgetOpenersAndGroupsReferencing( | 1300 void TabStripModel::ForgetOpenersAndGroupsReferencing( |
| 1312 const NavigationController* tab) { | 1301 const NavigationController* tab) { |
| 1313 for (TabContentsDataVector::const_iterator i = contents_data_.begin(); | 1302 for (TabContentsDataVector::const_iterator i = contents_data_.begin(); |
| 1314 i != contents_data_.end(); ++i) { | 1303 i != contents_data_.end(); ++i) { |
| 1315 if ((*i)->group == tab) | 1304 if ((*i)->group == tab) |
| 1316 (*i)->group = NULL; | 1305 (*i)->group = NULL; |
| 1317 if ((*i)->opener == tab) | 1306 if ((*i)->opener == tab) |
| 1318 (*i)->opener = NULL; | 1307 (*i)->opener = NULL; |
| 1319 } | 1308 } |
| 1320 } | 1309 } |
| OLD | NEW |