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 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 } | 151 } |
| 152 data->opener = &selected_contents->controller(); | 152 data->opener = &selected_contents->controller(); |
| 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 TabStripSelectionModel old_model; |
| 162 if (active) { | 162 old_model.Copy(selection_model_); |
| 163 if (active) | |
| 163 selection_model_.SetSelectedIndex(index); | 164 selection_model_.SetSelectedIndex(index); |
| 164 NotifyTabSelectedIfChanged(selected_contents, index, false); | 165 NotifyIfActiveOrSelectionChanged(old_model, false); |
|
sky
2011/06/06 21:01:14
Can 165 be moved into the if block?
dpapad
2011/06/06 22:17:11
Done. Putting it in the if block seems reasonable.
| |
| 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 |
| 174 ForgetOpenersAndGroupsReferencing(&(old_contents->controller())); | 174 ForgetOpenersAndGroupsReferencing(&(old_contents->controller())); |
| 175 | 175 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 213 ForgetOpenersAndGroupsReferencing(&(removed_contents->controller())); | 213 ForgetOpenersAndGroupsReferencing(&(removed_contents->controller())); |
| 214 if (empty()) | 214 if (empty()) |
| 215 closing_all_ = true; | 215 closing_all_ = true; |
| 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 TabStripSelectionModel old_model; |
| 224 old_model.Copy(selection_model_); | |
|
sky
2011/06/06 21:01:14
Don't you want to copy the model after decrementin
dpapad
2011/06/06 22:17:11
Copying the model after decrementing, results in n
sky
2011/06/06 22:42:16
Did you update 226 as well?
dpapad
2011/06/07 20:19:56
I moved lines 223-224 right after 225 and the beha
| |
| 224 selection_model_.DecrementFrom(index); | 225 selection_model_.DecrementFrom(index); |
| 225 if (index == old_active) { | 226 if (index == old_model.active()) { |
| 226 if (!selection_model_.empty()) { | 227 if (!selection_model_.empty()) { |
| 227 // A selected tab was removed, but there is still something selected. | 228 // A selected tab was removed, but there is still something selected. |
| 228 // Move the active and anchor to the first selected index. | 229 // Move the active and anchor to the first selected index. |
| 229 selection_model_.set_active(selection_model_.selected_indices()[0]); | 230 selection_model_.set_active(selection_model_.selected_indices()[0]); |
| 230 selection_model_.set_anchor(selection_model_.active()); | 231 selection_model_.set_anchor(selection_model_.active()); |
| 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, | |
| 237 false); | |
| 238 } | 236 } |
| 237 NotifyIfActiveOrSelectionChanged(old_model, false); | |
|
sky
2011/06/06 21:01:14
Won't this end up passing the wrong contents to th
dpapad
2011/06/06 22:17:11
It will pass indices that might no longer exist to
sky
2011/06/06 22:42:16
The problem is NotifyIfActiveOrSelectionChanges di
dpapad
2011/06/07 20:19:56
Done. I changed the notify functions I had added a
| |
| 239 } | 238 } |
| 240 } | 239 } |
| 241 return removed_contents; | 240 return removed_contents; |
| 242 } | 241 } |
| 243 | 242 |
| 244 void TabStripModel::ActivateTabAt(int index, bool user_gesture) { | 243 void TabStripModel::ActivateTabAt(int index, bool user_gesture) { |
| 245 DCHECK(ContainsIndex(index)); | 244 DCHECK(ContainsIndex(index)); |
| 246 bool had_multi = selection_model_.selected_indices().size() > 1; | 245 TabStripSelectionModel old_model; |
| 246 old_model.Copy(selection_model_); | |
| 247 TabContentsWrapper* old_contents = | 247 TabContentsWrapper* old_contents = |
| 248 (active_index() == TabStripSelectionModel::kUnselectedIndex) ? | 248 (active_index() == TabStripSelectionModel::kUnselectedIndex) ? |
| 249 NULL : GetSelectedTabContents(); | 249 NULL : GetSelectedTabContents(); |
| 250 selection_model_.SetSelectedIndex(index); | 250 selection_model_.SetSelectedIndex(index); |
| 251 TabContentsWrapper* new_contents = GetContentsAt(index); | 251 TabContentsWrapper* new_contents = GetContentsAt(index); |
| 252 if (old_contents != new_contents && old_contents) { | 252 if (old_contents != new_contents && old_contents) { |
| 253 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | 253 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 254 TabDeselected(old_contents)); | 254 TabDeselected(old_contents)); |
| 255 } | 255 } |
| 256 if (old_contents != new_contents || had_multi) { | 256 NotifyIfActiveOrSelectionChanged(old_model, user_gesture); |
| 257 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | |
| 258 ActiveTabChanged(old_contents, new_contents, | |
| 259 active_index(), user_gesture)); | |
| 260 } | |
| 261 } | 257 } |
| 262 | 258 |
| 263 void TabStripModel::MoveTabContentsAt(int index, | 259 void TabStripModel::MoveTabContentsAt(int index, |
| 264 int to_position, | 260 int to_position, |
| 265 bool select_after_move) { | 261 bool select_after_move) { |
| 266 DCHECK(ContainsIndex(index)); | 262 DCHECK(ContainsIndex(index)); |
| 267 if (index == to_position) | 263 if (index == to_position) |
| 268 return; | 264 return; |
| 269 | 265 |
| 270 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... | |
| 562 return count(); | 558 return count(); |
| 563 } | 559 } |
| 564 | 560 |
| 565 int TabStripModel::ConstrainInsertionIndex(int index, bool mini_tab) { | 561 int TabStripModel::ConstrainInsertionIndex(int index, bool mini_tab) { |
| 566 return mini_tab ? std::min(std::max(0, index), IndexOfFirstNonMiniTab()) : | 562 return mini_tab ? std::min(std::max(0, index), IndexOfFirstNonMiniTab()) : |
| 567 std::min(count(), std::max(index, IndexOfFirstNonMiniTab())); | 563 std::min(count(), std::max(index, IndexOfFirstNonMiniTab())); |
| 568 } | 564 } |
| 569 | 565 |
| 570 void TabStripModel::ExtendSelectionTo(int index) { | 566 void TabStripModel::ExtendSelectionTo(int index) { |
| 571 DCHECK(ContainsIndex(index)); | 567 DCHECK(ContainsIndex(index)); |
| 572 int old_active = active_index(); | 568 TabStripSelectionModel old_selection_model; |
| 569 old_selection_model.Copy(selection_model()); | |
| 573 selection_model_.SetSelectionFromAnchorTo(index); | 570 selection_model_.SetSelectionFromAnchorTo(index); |
| 574 // This may not have resulted in a change, but we assume it did. | 571 NotifyIfActiveOrSelectionChanged(old_selection_model, true); |
| 575 NotifyActiveTabChanged(old_active); | |
| 576 } | 572 } |
| 577 | 573 |
| 578 void TabStripModel::ToggleSelectionAt(int index) { | 574 void TabStripModel::ToggleSelectionAt(int index) { |
| 579 DCHECK(ContainsIndex(index)); | 575 DCHECK(ContainsIndex(index)); |
| 580 int old_active = active_index(); | 576 TabStripSelectionModel old_selection_model; |
| 577 old_selection_model.Copy(selection_model()); | |
| 581 if (selection_model_.IsSelected(index)) { | 578 if (selection_model_.IsSelected(index)) { |
| 582 if (selection_model_.size() == 1) { | 579 if (selection_model_.size() == 1) { |
| 583 // One tab must be selected and this tab is currently selected so we can't | 580 // One tab must be selected and this tab is currently selected so we can't |
| 584 // unselect it. | 581 // unselect it. |
| 585 return; | 582 return; |
| 586 } | 583 } |
| 587 selection_model_.RemoveIndexFromSelection(index); | 584 selection_model_.RemoveIndexFromSelection(index); |
| 588 selection_model_.set_anchor(index); | 585 selection_model_.set_anchor(index); |
| 589 if (selection_model_.active() == TabStripSelectionModel::kUnselectedIndex) | 586 if (selection_model_.active() == TabStripSelectionModel::kUnselectedIndex) |
| 590 selection_model_.set_active(selection_model_.selected_indices()[0]); | 587 selection_model_.set_active(selection_model_.selected_indices()[0]); |
| 591 } else { | 588 } else { |
| 592 selection_model_.AddIndexToSelection(index); | 589 selection_model_.AddIndexToSelection(index); |
| 593 selection_model_.set_anchor(index); | 590 selection_model_.set_anchor(index); |
| 594 selection_model_.set_active(index); | 591 selection_model_.set_active(index); |
| 595 } | 592 } |
| 596 NotifyActiveTabChanged(old_active); | 593 NotifyIfActiveOrSelectionChanged(old_selection_model, true); |
| 597 } | 594 } |
| 598 | 595 |
| 599 void TabStripModel::AddSelectionFromAnchorTo(int index) { | 596 void TabStripModel::AddSelectionFromAnchorTo(int index) { |
| 600 int old_active = active_index(); | 597 TabStripSelectionModel old_selection_model; |
| 598 old_selection_model.Copy(selection_model()); | |
| 601 selection_model_.AddSelectionFromAnchorTo(index); | 599 selection_model_.AddSelectionFromAnchorTo(index); |
| 602 NotifyActiveTabChanged(old_active); | 600 NotifyIfActiveOrSelectionChanged(old_selection_model, true); |
| 603 } | 601 } |
| 604 | 602 |
| 605 bool TabStripModel::IsTabSelected(int index) const { | 603 bool TabStripModel::IsTabSelected(int index) const { |
| 606 DCHECK(ContainsIndex(index)); | 604 DCHECK(ContainsIndex(index)); |
| 607 return selection_model_.IsSelected(index); | 605 return selection_model_.IsSelected(index); |
| 608 } | 606 } |
| 609 | 607 |
| 610 void TabStripModel::SetSelectionFromModel( | 608 void TabStripModel::SetSelectionFromModel( |
| 611 const TabStripSelectionModel& source) { | 609 const TabStripSelectionModel& source) { |
| 612 DCHECK_NE(TabStripSelectionModel::kUnselectedIndex, source.active()); | 610 DCHECK_NE(TabStripSelectionModel::kUnselectedIndex, source.active()); |
| 613 int old_active_index = active_index(); | 611 TabStripSelectionModel old_selection_model; |
| 612 old_selection_model.Copy(selection_model()); | |
| 614 selection_model_.Copy(source); | 613 selection_model_.Copy(source); |
| 615 // This may not have resulted in a change, but we assume it did. | 614 NotifyIfActiveOrSelectionChanged(old_selection_model, true); |
| 616 NotifyActiveTabChanged(old_active_index); | |
| 617 } | 615 } |
| 618 | 616 |
| 619 void TabStripModel::AddTabContents(TabContentsWrapper* contents, | 617 void TabStripModel::AddTabContents(TabContentsWrapper* contents, |
| 620 int index, | 618 int index, |
| 621 PageTransition::Type transition, | 619 PageTransition::Type transition, |
| 622 int add_types) { | 620 int add_types) { |
| 623 // If the newly-opened tab is part of the same task as the parent tab, we want | 621 // 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 | 622 // to inherit the parent's "group" attribute, so that if this tab is then |
| 625 // closed we'll jump back to the parent tab. | 623 // closed we'll jump back to the parent tab. |
| 626 bool inherit_group = (add_types & ADD_INHERIT_GROUP) == ADD_INHERIT_GROUP; | 624 bool inherit_group = (add_types & ADD_INHERIT_GROUP) == ADD_INHERIT_GROUP; |
| (...skipping 591 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1218 if (last_selected_contents) { | 1216 if (last_selected_contents) { |
| 1219 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | 1217 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 1220 TabDeselected(last_selected_contents)); | 1218 TabDeselected(last_selected_contents)); |
| 1221 } | 1219 } |
| 1222 | 1220 |
| 1223 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | 1221 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 1224 ActiveTabChanged(last_selected_contents, new_contents, | 1222 ActiveTabChanged(last_selected_contents, new_contents, |
| 1225 active_index(), user_gesture)); | 1223 active_index(), user_gesture)); |
| 1226 } | 1224 } |
| 1227 | 1225 |
| 1228 void TabStripModel::NotifyActiveTabChanged(int old_active_index) { | 1226 void TabStripModel::NotifyActiveTabChanged(int old_active_index, |
| 1227 bool user_gesture) { | |
| 1229 TabContentsWrapper* old_tab = | 1228 TabContentsWrapper* old_tab = |
| 1230 old_active_index == TabStripSelectionModel::kUnselectedIndex ? | 1229 old_active_index == TabStripSelectionModel::kUnselectedIndex ? |
| 1231 NULL : GetTabContentsAt(old_active_index); | 1230 NULL : GetTabContentsAt(old_active_index); |
| 1232 TabContentsWrapper* new_tab = | 1231 TabContentsWrapper* new_tab = |
| 1233 active_index() == TabStripSelectionModel::kUnselectedIndex ? | 1232 active_index() == TabStripSelectionModel::kUnselectedIndex ? |
| 1234 NULL : GetTabContentsAt(active_index()); | 1233 NULL : GetTabContentsAt(active_index()); |
| 1235 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | 1234 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, |
| 1236 ActiveTabChanged(old_tab, new_tab, active_index(), true)); | 1235 ActiveTabChanged(old_tab, new_tab, active_index(), |
| 1236 user_gesture)); | |
| 1237 } | |
| 1238 | |
| 1239 void TabStripModel::NotifySelectionChanged( | |
| 1240 const TabStripSelectionModel& old_selection_model) { | |
| 1241 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, | |
| 1242 TabSelectionChanged(old_selection_model)); | |
| 1243 } | |
| 1244 | |
| 1245 void TabStripModel::NotifyIfActiveOrSelectionChanged( | |
| 1246 const TabStripSelectionModel& old_selection_model, bool user_gesture) { | |
| 1247 if (old_selection_model.active() != active_index()) | |
| 1248 NotifyActiveTabChanged(old_selection_model.active(), user_gesture); | |
| 1249 if (!selection_model().Equals(old_selection_model)) | |
| 1250 NotifySelectionChanged(old_selection_model); | |
| 1237 } | 1251 } |
| 1238 | 1252 |
| 1239 void TabStripModel::SelectRelativeTab(bool next) { | 1253 void TabStripModel::SelectRelativeTab(bool next) { |
| 1240 // This may happen during automated testing or if a user somehow buffers | 1254 // This may happen during automated testing or if a user somehow buffers |
| 1241 // many key accelerators. | 1255 // many key accelerators. |
| 1242 if (contents_data_.empty()) | 1256 if (contents_data_.empty()) |
| 1243 return; | 1257 return; |
| 1244 | 1258 |
| 1245 int index = active_index(); | 1259 int index = active_index(); |
| 1246 int delta = next ? 1 : -1; | 1260 int delta = next ? 1 : -1; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1311 void TabStripModel::ForgetOpenersAndGroupsReferencing( | 1325 void TabStripModel::ForgetOpenersAndGroupsReferencing( |
| 1312 const NavigationController* tab) { | 1326 const NavigationController* tab) { |
| 1313 for (TabContentsDataVector::const_iterator i = contents_data_.begin(); | 1327 for (TabContentsDataVector::const_iterator i = contents_data_.begin(); |
| 1314 i != contents_data_.end(); ++i) { | 1328 i != contents_data_.end(); ++i) { |
| 1315 if ((*i)->group == tab) | 1329 if ((*i)->group == tab) |
| 1316 (*i)->group = NULL; | 1330 (*i)->group = NULL; |
| 1317 if ((*i)->opener == tab) | 1331 if ((*i)->opener == tab) |
| 1318 (*i)->opener = NULL; | 1332 (*i)->opener = NULL; |
| 1319 } | 1333 } |
| 1320 } | 1334 } |
| OLD | NEW |