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 25 matching lines...) Expand all Loading... | |
| 199 InternalCloseTabs(closing_tabs, CLOSE_NONE); | 200 InternalCloseTabs(closing_tabs, CLOSE_NONE); |
| 200 } | 201 } |
| 201 | 202 |
| 202 TabContentsWrapper* TabStripModel::DetachTabContentsAt(int index) { | 203 TabContentsWrapper* TabStripModel::DetachTabContentsAt(int index) { |
| 203 if (contents_data_.empty()) | 204 if (contents_data_.empty()) |
| 204 return NULL; | 205 return NULL; |
| 205 | 206 |
| 206 DCHECK(ContainsIndex(index)); | 207 DCHECK(ContainsIndex(index)); |
| 207 | 208 |
| 208 TabContentsWrapper* removed_contents = GetContentsAt(index); | 209 TabContentsWrapper* removed_contents = GetContentsAt(index); |
| 210 bool was_selected = IsTabSelected(index); | |
| 209 int next_selected_index = order_controller_->DetermineNewSelectedIndex(index); | 211 int next_selected_index = order_controller_->DetermineNewSelectedIndex(index); |
| 210 delete contents_data_.at(index); | 212 delete contents_data_.at(index); |
| 211 contents_data_.erase(contents_data_.begin() + index); | 213 contents_data_.erase(contents_data_.begin() + index); |
| 212 ForgetOpenersAndGroupsReferencing(&(removed_contents->controller())); | 214 ForgetOpenersAndGroupsReferencing(&(removed_contents->controller())); |
| 213 if (empty()) | 215 if (empty()) |
| 214 closing_all_ = true; | 216 closing_all_ = true; |
| 215 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | 217 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 216 TabDetachedAt(removed_contents, index)); | 218 TabDetachedAt(removed_contents, index)); |
| 217 if (empty()) { | 219 if (empty()) { |
| 218 // TabDetachedAt() might unregister observers, so send |TabStripEmtpy()| in | 220 // TabDetachedAt() might unregister observers, so send |TabStripEmtpy()| in |
| 219 // a second pass. | 221 // a second pass. |
| 220 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, TabStripEmpty()); | 222 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, TabStripEmpty()); |
| 221 } else { | 223 } else { |
| 222 int old_active = active_index(); | 224 int old_active = active_index(); |
| 223 selection_model_.DecrementFrom(index); | 225 selection_model_.DecrementFrom(index); |
| 226 TabStripSelectionModel old_model; | |
| 227 old_model.Copy(selection_model_); | |
| 224 if (index == old_active) { | 228 if (index == old_active) { |
| 225 if (!selection_model_.empty()) { | 229 if (!selection_model_.empty()) { |
| 226 // A selected tab was removed, but there is still something selected. | 230 // The active tab was removed, but there is still something selected. |
| 227 // Move the active and anchor to the first selected index. | 231 // Move the active and anchor to the first selected index. |
| 228 selection_model_.set_active(selection_model_.selected_indices()[0]); | 232 selection_model_.set_active(selection_model_.selected_indices()[0]); |
| 229 selection_model_.set_anchor(selection_model_.active()); | 233 selection_model_.set_anchor(selection_model_.active()); |
| 230 NotifyTabSelectedIfChanged(removed_contents, active_index(), false); | |
| 231 } else { | 234 } else { |
| 232 // The active tab was removed and nothing is selected. Reset the | 235 // The active tab was removed and nothing is selected. Reset the |
| 233 // selection and send out notification. | 236 // selection and send out notification. |
| 234 selection_model_.SetSelectedIndex(next_selected_index); | 237 selection_model_.SetSelectedIndex(next_selected_index); |
| 235 NotifyTabSelectedIfChanged(removed_contents, next_selected_index, | |
| 236 false); | |
| 237 } | 238 } |
| 239 NotifyIfActiveTabChanged(removed_contents, false); | |
| 240 } | |
| 241 | |
| 242 if (was_selected) { | |
|
dpapad
2011/06/13 21:36:18
Now this will trigger a notification when a not ac
sky
2011/06/13 22:20:16
It's pretty subtle as to why you need this. Please
dpapad
2011/06/14 16:58:23
Done.
| |
| 243 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | |
| 244 TabSelectionChanged(old_model)); | |
| 238 } | 245 } |
| 239 } | 246 } |
| 240 return removed_contents; | 247 return removed_contents; |
| 241 } | 248 } |
| 242 | 249 |
| 243 void TabStripModel::ActivateTabAt(int index, bool user_gesture) { | 250 void TabStripModel::ActivateTabAt(int index, bool user_gesture) { |
| 244 DCHECK(ContainsIndex(index)); | 251 DCHECK(ContainsIndex(index)); |
| 245 bool had_multi = selection_model_.selected_indices().size() > 1; | 252 TabContentsWrapper* old_contents = GetActiveTabContents(); |
| 246 TabContentsWrapper* old_contents = | 253 TabStripSelectionModel old_model; |
| 247 (active_index() == TabStripSelectionModel::kUnselectedIndex) ? | 254 old_model.Copy(selection_model_); |
| 248 NULL : GetActiveTabContents(); | |
| 249 selection_model_.SetSelectedIndex(index); | 255 selection_model_.SetSelectedIndex(index); |
| 250 TabContentsWrapper* new_contents = GetContentsAt(index); | 256 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 } | 257 } |
| 261 | 258 |
| 262 void TabStripModel::MoveTabContentsAt(int index, | 259 void TabStripModel::MoveTabContentsAt(int index, |
| 263 int to_position, | 260 int to_position, |
| 264 bool select_after_move) { | 261 bool select_after_move) { |
| 265 DCHECK(ContainsIndex(index)); | 262 DCHECK(ContainsIndex(index)); |
| 266 if (index == to_position) | 263 if (index == to_position) |
| 267 return; | 264 return; |
| 268 | 265 |
| 269 int first_non_mini_tab = IndexOfFirstNonMiniTab(); | 266 int first_non_mini_tab = IndexOfFirstNonMiniTab(); |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 561 return count(); | 558 return count(); |
| 562 } | 559 } |
| 563 | 560 |
| 564 int TabStripModel::ConstrainInsertionIndex(int index, bool mini_tab) { | 561 int TabStripModel::ConstrainInsertionIndex(int index, bool mini_tab) { |
| 565 return mini_tab ? std::min(std::max(0, index), IndexOfFirstNonMiniTab()) : | 562 return mini_tab ? std::min(std::max(0, index), IndexOfFirstNonMiniTab()) : |
| 566 std::min(count(), std::max(index, IndexOfFirstNonMiniTab())); | 563 std::min(count(), std::max(index, IndexOfFirstNonMiniTab())); |
| 567 } | 564 } |
| 568 | 565 |
| 569 void TabStripModel::ExtendSelectionTo(int index) { | 566 void TabStripModel::ExtendSelectionTo(int index) { |
| 570 DCHECK(ContainsIndex(index)); | 567 DCHECK(ContainsIndex(index)); |
| 571 int old_active = active_index(); | 568 TabContentsWrapper* old_contents = GetActiveTabContents(); |
| 569 TabStripSelectionModel old_model; | |
| 570 old_model.Copy(selection_model()); | |
| 572 selection_model_.SetSelectionFromAnchorTo(index); | 571 selection_model_.SetSelectionFromAnchorTo(index); |
| 573 // This may not have resulted in a change, but we assume it did. | 572 NotifyIfActiveOrSelectionChanged(old_contents, false, old_model); |
| 574 NotifyActiveTabChanged(old_active); | |
| 575 } | 573 } |
| 576 | 574 |
| 577 void TabStripModel::ToggleSelectionAt(int index) { | 575 void TabStripModel::ToggleSelectionAt(int index) { |
| 578 DCHECK(ContainsIndex(index)); | 576 DCHECK(ContainsIndex(index)); |
| 579 int old_active = active_index(); | 577 TabContentsWrapper* old_contents = GetActiveTabContents(); |
| 578 TabStripSelectionModel old_model; | |
| 579 old_model.Copy(selection_model()); | |
| 580 if (selection_model_.IsSelected(index)) { | 580 if (selection_model_.IsSelected(index)) { |
| 581 if (selection_model_.size() == 1) { | 581 if (selection_model_.size() == 1) { |
| 582 // One tab must be selected and this tab is currently selected so we can't | 582 // One tab must be selected and this tab is currently selected so we can't |
| 583 // unselect it. | 583 // unselect it. |
| 584 return; | 584 return; |
| 585 } | 585 } |
| 586 selection_model_.RemoveIndexFromSelection(index); | 586 selection_model_.RemoveIndexFromSelection(index); |
| 587 selection_model_.set_anchor(index); | 587 selection_model_.set_anchor(index); |
| 588 if (selection_model_.active() == TabStripSelectionModel::kUnselectedIndex) | 588 if (selection_model_.active() == TabStripSelectionModel::kUnselectedIndex) |
| 589 selection_model_.set_active(selection_model_.selected_indices()[0]); | 589 selection_model_.set_active(selection_model_.selected_indices()[0]); |
| 590 } else { | 590 } else { |
| 591 selection_model_.AddIndexToSelection(index); | 591 selection_model_.AddIndexToSelection(index); |
| 592 selection_model_.set_anchor(index); | 592 selection_model_.set_anchor(index); |
| 593 selection_model_.set_active(index); | 593 selection_model_.set_active(index); |
| 594 } | 594 } |
| 595 NotifyActiveTabChanged(old_active); | 595 NotifyIfActiveOrSelectionChanged(old_contents, false, old_model); |
| 596 } | 596 } |
| 597 | 597 |
| 598 void TabStripModel::AddSelectionFromAnchorTo(int index) { | 598 void TabStripModel::AddSelectionFromAnchorTo(int index) { |
| 599 int old_active = active_index(); | 599 TabContentsWrapper* old_contents = GetActiveTabContents(); |
| 600 TabStripSelectionModel old_model; | |
| 601 old_model.Copy(selection_model()); | |
| 600 selection_model_.AddSelectionFromAnchorTo(index); | 602 selection_model_.AddSelectionFromAnchorTo(index); |
| 601 NotifyActiveTabChanged(old_active); | 603 NotifyIfActiveOrSelectionChanged(old_contents, false, old_model); |
| 602 } | 604 } |
| 603 | 605 |
| 604 bool TabStripModel::IsTabSelected(int index) const { | 606 bool TabStripModel::IsTabSelected(int index) const { |
| 605 DCHECK(ContainsIndex(index)); | 607 DCHECK(ContainsIndex(index)); |
| 606 return selection_model_.IsSelected(index); | 608 return selection_model_.IsSelected(index); |
| 607 } | 609 } |
| 608 | 610 |
| 609 void TabStripModel::SetSelectionFromModel( | 611 void TabStripModel::SetSelectionFromModel( |
| 610 const TabStripSelectionModel& source) { | 612 const TabStripSelectionModel& source) { |
| 611 DCHECK_NE(TabStripSelectionModel::kUnselectedIndex, source.active()); | 613 DCHECK_NE(TabStripSelectionModel::kUnselectedIndex, source.active()); |
| 612 int old_active_index = active_index(); | 614 TabContentsWrapper* old_contents = GetActiveTabContents(); |
| 615 TabStripSelectionModel old_model; | |
| 616 old_model.Copy(selection_model()); | |
| 613 selection_model_.Copy(source); | 617 selection_model_.Copy(source); |
| 614 // This may not have resulted in a change, but we assume it did. | 618 NotifyIfActiveOrSelectionChanged(old_contents, false, old_model); |
| 615 NotifyActiveTabChanged(old_active_index); | |
| 616 } | 619 } |
| 617 | 620 |
| 618 void TabStripModel::AddTabContents(TabContentsWrapper* contents, | 621 void TabStripModel::AddTabContents(TabContentsWrapper* contents, |
| 619 int index, | 622 int index, |
| 620 PageTransition::Type transition, | 623 PageTransition::Type transition, |
| 621 int add_types) { | 624 int add_types) { |
| 622 // 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 |
| 623 // 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 |
| 624 // closed we'll jump back to the parent tab. | 627 // closed we'll jump back to the parent tab. |
| 625 bool inherit_group = (add_types & ADD_INHERIT_GROUP) == ADD_INHERIT_GROUP; | 628 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. | 1203 // and detach it. |
| 1201 delete contents; | 1204 delete contents; |
| 1202 } | 1205 } |
| 1203 | 1206 |
| 1204 TabContentsWrapper* TabStripModel::GetContentsAt(int index) const { | 1207 TabContentsWrapper* TabStripModel::GetContentsAt(int index) const { |
| 1205 CHECK(ContainsIndex(index)) << | 1208 CHECK(ContainsIndex(index)) << |
| 1206 "Failed to find: " << index << " in: " << count() << " entries."; | 1209 "Failed to find: " << index << " in: " << count() << " entries."; |
| 1207 return contents_data_.at(index)->contents; | 1210 return contents_data_.at(index)->contents; |
| 1208 } | 1211 } |
| 1209 | 1212 |
| 1210 void TabStripModel::NotifyTabSelectedIfChanged(TabContentsWrapper* old_contents, | 1213 void TabStripModel::NotifyIfActiveTabChanged( |
| 1211 int to_index, | 1214 TabContentsWrapper* old_contents, |
| 1212 bool user_gesture) { | 1215 bool user_gesture) { |
| 1213 TabContentsWrapper* new_contents = GetContentsAt(to_index); | 1216 TabContentsWrapper* new_contents = GetContentsAt(active_index()); |
| 1214 if (old_contents == new_contents) | 1217 if (old_contents != new_contents) { |
| 1215 return; | 1218 if (old_contents) { |
| 1216 | 1219 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 1217 TabContentsWrapper* last_selected_contents = old_contents; | 1220 TabDeactivated(old_contents)); |
| 1218 if (last_selected_contents) { | 1221 } |
| 1219 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | 1222 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 1220 TabDeactivated(last_selected_contents)); | 1223 ActiveTabChanged(old_contents, new_contents, |
| 1224 active_index(), user_gesture)); | |
| 1221 } | 1225 } |
| 1222 | |
| 1223 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | |
| 1224 ActiveTabChanged(last_selected_contents, new_contents, | |
| 1225 active_index(), user_gesture)); | |
| 1226 } | 1226 } |
| 1227 | 1227 |
| 1228 void TabStripModel::NotifyActiveTabChanged(int old_active_index) { | 1228 void TabStripModel::NotifyIfActiveOrSelectionChanged( |
| 1229 TabContentsWrapper* old_tab = | 1229 TabContentsWrapper* old_contents, |
| 1230 old_active_index == TabStripSelectionModel::kUnselectedIndex ? | 1230 bool user_gesture, |
| 1231 NULL : GetTabContentsAt(old_active_index); | 1231 const TabStripSelectionModel& old_model) { |
| 1232 TabContentsWrapper* new_tab = | 1232 NotifyIfActiveTabChanged(old_contents, user_gesture); |
| 1233 active_index() == TabStripSelectionModel::kUnselectedIndex ? | 1233 |
| 1234 NULL : GetTabContentsAt(active_index()); | 1234 if (!selection_model().Equals(old_model)) { |
| 1235 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | 1235 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 1236 ActiveTabChanged(old_tab, new_tab, active_index(), true)); | 1236 TabSelectionChanged(old_model)); |
| 1237 } | |
| 1237 } | 1238 } |
| 1238 | 1239 |
| 1239 void TabStripModel::SelectRelativeTab(bool next) { | 1240 void TabStripModel::SelectRelativeTab(bool next) { |
| 1240 // This may happen during automated testing or if a user somehow buffers | 1241 // This may happen during automated testing or if a user somehow buffers |
| 1241 // many key accelerators. | 1242 // many key accelerators. |
| 1242 if (contents_data_.empty()) | 1243 if (contents_data_.empty()) |
| 1243 return; | 1244 return; |
| 1244 | 1245 |
| 1245 int index = active_index(); | 1246 int index = active_index(); |
| 1246 int delta = next ? 1 : -1; | 1247 int delta = next ? 1 : -1; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1311 void TabStripModel::ForgetOpenersAndGroupsReferencing( | 1312 void TabStripModel::ForgetOpenersAndGroupsReferencing( |
| 1312 const NavigationController* tab) { | 1313 const NavigationController* tab) { |
| 1313 for (TabContentsDataVector::const_iterator i = contents_data_.begin(); | 1314 for (TabContentsDataVector::const_iterator i = contents_data_.begin(); |
| 1314 i != contents_data_.end(); ++i) { | 1315 i != contents_data_.end(); ++i) { |
| 1315 if ((*i)->group == tab) | 1316 if ((*i)->group == tab) |
| 1316 (*i)->group = NULL; | 1317 (*i)->group = NULL; |
| 1317 if ((*i)->opener == tab) | 1318 if ((*i)->opener == tab) |
| 1318 (*i)->opener = NULL; | 1319 (*i)->opener = NULL; |
| 1319 } | 1320 } |
| 1320 } | 1321 } |
| OLD | NEW |