Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(245)

Side by Side Diff: chrome/browser/tabs/tab_strip_model.cc

Issue 7033048: Multi-tab: Adding new Notification when tab selection changes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebasing Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 old_model.Copy(selection_model_);
162 if (active) { 163 if (active) {
163 selection_model_.SetSelectedIndex(index); 164 selection_model_.SetSelectedIndex(index);
164 NotifyTabSelectedIfChanged(selected_contents, index, false); 165 NotifyIfActiveTabChanged(selected_contents, active_index(), false);
sky 2011/06/07 20:32:44 You always end up invoking both of these methods a
dpapad 2011/06/07 23:20:09 Done. Combined to a single method named NotifyIfAc
166 NotifyIfSelectionChanged(old_model);
165 } 167 }
166 } 168 }
167 169
168 TabContentsWrapper* TabStripModel::ReplaceTabContentsAt( 170 TabContentsWrapper* TabStripModel::ReplaceTabContentsAt(
169 int index, 171 int index,
170 TabContentsWrapper* new_contents) { 172 TabContentsWrapper* new_contents) {
171 DCHECK(ContainsIndex(index)); 173 DCHECK(ContainsIndex(index));
172 TabContentsWrapper* old_contents = GetContentsAt(index); 174 TabContentsWrapper* old_contents = GetContentsAt(index);
173 175
174 ForgetOpenersAndGroupsReferencing(&(old_contents->controller())); 176 ForgetOpenersAndGroupsReferencing(&(old_contents->controller()));
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 ForgetOpenersAndGroupsReferencing(&(removed_contents->controller())); 215 ForgetOpenersAndGroupsReferencing(&(removed_contents->controller()));
214 if (empty()) 216 if (empty())
215 closing_all_ = true; 217 closing_all_ = true;
216 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, 218 FOR_EACH_OBSERVER(TabStripModelObserver, observers_,
217 TabDetachedAt(removed_contents, index)); 219 TabDetachedAt(removed_contents, index));
218 if (empty()) { 220 if (empty()) {
219 // TabDetachedAt() might unregister observers, so send |TabStripEmtpy()| in 221 // TabDetachedAt() might unregister observers, so send |TabStripEmtpy()| in
220 // a second pass. 222 // a second pass.
221 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, TabStripEmpty()); 223 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, TabStripEmpty());
222 } else { 224 } else {
223 int old_active = active_index(); 225 TabStripSelectionModel old_model;
226 old_model.Copy(selection_model_);
sky 2011/06/07 20:32:44 I'm still confused by this. You've notified the ob
dpapad 2011/06/07 23:20:09 Done.
224 selection_model_.DecrementFrom(index); 227 selection_model_.DecrementFrom(index);
225 if (index == old_active) { 228 if (index == old_model.active()) {
226 if (!selection_model_.empty()) { 229 if (!selection_model_.empty()) {
227 // A selected tab was removed, but there is still something selected. 230 // A selected tab was removed, but there is still something selected.
228 // Move the active and anchor to the first selected index. 231 // Move the active and anchor to the first selected index.
229 selection_model_.set_active(selection_model_.selected_indices()[0]); 232 selection_model_.set_active(selection_model_.selected_indices()[0]);
230 selection_model_.set_anchor(selection_model_.active()); 233 selection_model_.set_anchor(selection_model_.active());
231 NotifyTabSelectedIfChanged(removed_contents, active_index(), false);
232 } else { 234 } else {
233 // The active tab was removed and nothing is selected. Reset the 235 // The active tab was removed and nothing is selected. Reset the
234 // selection and send out notification. 236 // selection and send out notification.
235 selection_model_.SetSelectedIndex(next_selected_index); 237 selection_model_.SetSelectedIndex(next_selected_index);
236 NotifyTabSelectedIfChanged(removed_contents, next_selected_index,
237 false);
238 } 238 }
239 NotifyIfActiveTabChanged(removed_contents, active_index(), false);
240 NotifyIfSelectionChanged(old_model);
239 } 241 }
240 } 242 }
241 return removed_contents; 243 return removed_contents;
242 } 244 }
243 245
244 void TabStripModel::ActivateTabAt(int index, bool user_gesture) { 246 void TabStripModel::ActivateTabAt(int index, bool user_gesture) {
245 DCHECK(ContainsIndex(index)); 247 DCHECK(ContainsIndex(index));
246 bool had_multi = selection_model_.selected_indices().size() > 1; 248 TabContentsWrapper* old_contents = GetSelectedTabContents();
247 TabContentsWrapper* old_contents = 249 TabStripSelectionModel old_model;
248 (active_index() == TabStripSelectionModel::kUnselectedIndex) ? 250 old_model.Copy(selection_model_);
249 NULL : GetSelectedTabContents();
250 selection_model_.SetSelectedIndex(index); 251 selection_model_.SetSelectedIndex(index);
251 TabContentsWrapper* new_contents = GetContentsAt(index); 252 NotifyIfActiveTabChanged(old_contents, active_index(), user_gesture);
252 if (old_contents != new_contents && old_contents) { 253 NotifyIfSelectionChanged(old_model);
253 FOR_EACH_OBSERVER(TabStripModelObserver, observers_,
254 TabDeactivated(old_contents));
255 }
256 if (old_contents != new_contents || had_multi) {
257 FOR_EACH_OBSERVER(TabStripModelObserver, observers_,
258 ActiveTabChanged(old_contents, new_contents,
259 active_index(), user_gesture));
260 }
261 } 254 }
262 255
263 void TabStripModel::MoveTabContentsAt(int index, 256 void TabStripModel::MoveTabContentsAt(int index,
264 int to_position, 257 int to_position,
265 bool select_after_move) { 258 bool select_after_move) {
266 DCHECK(ContainsIndex(index)); 259 DCHECK(ContainsIndex(index));
267 if (index == to_position) 260 if (index == to_position)
268 return; 261 return;
269 262
270 int first_non_mini_tab = IndexOfFirstNonMiniTab(); 263 int first_non_mini_tab = IndexOfFirstNonMiniTab();
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
562 return count(); 555 return count();
563 } 556 }
564 557
565 int TabStripModel::ConstrainInsertionIndex(int index, bool mini_tab) { 558 int TabStripModel::ConstrainInsertionIndex(int index, bool mini_tab) {
566 return mini_tab ? std::min(std::max(0, index), IndexOfFirstNonMiniTab()) : 559 return mini_tab ? std::min(std::max(0, index), IndexOfFirstNonMiniTab()) :
567 std::min(count(), std::max(index, IndexOfFirstNonMiniTab())); 560 std::min(count(), std::max(index, IndexOfFirstNonMiniTab()));
568 } 561 }
569 562
570 void TabStripModel::ExtendSelectionTo(int index) { 563 void TabStripModel::ExtendSelectionTo(int index) {
571 DCHECK(ContainsIndex(index)); 564 DCHECK(ContainsIndex(index));
572 int old_active = active_index(); 565 TabContentsWrapper* old_contents = GetSelectedTabContents();
566 TabStripSelectionModel old_model;
567 old_model.Copy(selection_model());
573 selection_model_.SetSelectionFromAnchorTo(index); 568 selection_model_.SetSelectionFromAnchorTo(index);
574 // This may not have resulted in a change, but we assume it did. 569 NotifyIfActiveTabChanged(old_contents, active_index(), true);
575 NotifyActiveTabChanged(old_active); 570 NotifyIfSelectionChanged(old_model);
576 } 571 }
577 572
578 void TabStripModel::ToggleSelectionAt(int index) { 573 void TabStripModel::ToggleSelectionAt(int index) {
579 DCHECK(ContainsIndex(index)); 574 DCHECK(ContainsIndex(index));
580 int old_active = active_index(); 575 TabContentsWrapper* old_contents = GetSelectedTabContents();
576 TabStripSelectionModel old_model;
577 old_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 NotifyIfActiveTabChanged(old_contents, active_index(), true);
594 NotifyIfSelectionChanged(old_model);
597 } 595 }
598 596
599 void TabStripModel::AddSelectionFromAnchorTo(int index) { 597 void TabStripModel::AddSelectionFromAnchorTo(int index) {
600 int old_active = active_index(); 598 TabContentsWrapper* old_contents = GetSelectedTabContents();
599 TabStripSelectionModel old_model;
600 old_model.Copy(selection_model());
601 selection_model_.AddSelectionFromAnchorTo(index); 601 selection_model_.AddSelectionFromAnchorTo(index);
602 NotifyActiveTabChanged(old_active); 602 NotifyIfActiveTabChanged(old_contents, active_index(), true);
603 NotifyIfSelectionChanged(old_model);
603 } 604 }
604 605
605 bool TabStripModel::IsTabSelected(int index) const { 606 bool TabStripModel::IsTabSelected(int index) const {
606 DCHECK(ContainsIndex(index)); 607 DCHECK(ContainsIndex(index));
607 return selection_model_.IsSelected(index); 608 return selection_model_.IsSelected(index);
608 } 609 }
609 610
610 void TabStripModel::SetSelectionFromModel( 611 void TabStripModel::SetSelectionFromModel(
611 const TabStripSelectionModel& source) { 612 const TabStripSelectionModel& source) {
612 DCHECK_NE(TabStripSelectionModel::kUnselectedIndex, source.active()); 613 DCHECK_NE(TabStripSelectionModel::kUnselectedIndex, source.active());
613 int old_active_index = active_index(); 614 TabContentsWrapper* old_contents = GetSelectedTabContents();
615 TabStripSelectionModel old_model;
616 old_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 NotifyIfActiveTabChanged(old_contents, active_index(), true);
616 NotifyActiveTabChanged(old_active_index); 619 NotifyIfSelectionChanged(old_model);
617 } 620 }
618 621
619 void TabStripModel::AddTabContents(TabContentsWrapper* contents, 622 void TabStripModel::AddTabContents(TabContentsWrapper* contents,
620 int index, 623 int index,
621 PageTransition::Type transition, 624 PageTransition::Type transition,
622 int add_types) { 625 int add_types) {
623 // If the newly-opened tab is part of the same task as the parent tab, we want 626 // 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 627 // to inherit the parent's "group" attribute, so that if this tab is then
625 // closed we'll jump back to the parent tab. 628 // closed we'll jump back to the parent tab.
626 bool inherit_group = (add_types & ADD_INHERIT_GROUP) == ADD_INHERIT_GROUP; 629 bool inherit_group = (add_types & ADD_INHERIT_GROUP) == ADD_INHERIT_GROUP;
(...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after
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(TabContentsWrapper* old_contents,
1211 int to_index, 1214 int to_index,
sky 2011/06/07 20:32:44 You always pass in to_index = to active_index(). C
dpapad 2011/06/07 23:20:09 Done.
1212 bool user_gesture) { 1215 bool user_gesture) {
1213 TabContentsWrapper* new_contents = GetContentsAt(to_index); 1216 TabContentsWrapper* new_contents = GetContentsAt(to_index);
1214 if (old_contents == new_contents) 1217 if (old_contents == new_contents)
1215 return; 1218 return;
1216 1219
1217 TabContentsWrapper* last_selected_contents = old_contents; 1220 TabContentsWrapper* last_selected_contents = old_contents;
1218 if (last_selected_contents) { 1221 if (last_selected_contents) {
1219 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, 1222 FOR_EACH_OBSERVER(TabStripModelObserver, observers_,
1220 TabDeactivated(last_selected_contents)); 1223 TabDeactivated(last_selected_contents));
1221 } 1224 }
1222 1225
1223 FOR_EACH_OBSERVER(TabStripModelObserver, observers_, 1226 FOR_EACH_OBSERVER(TabStripModelObserver, observers_,
1224 ActiveTabChanged(last_selected_contents, new_contents, 1227 ActiveTabChanged(last_selected_contents, new_contents,
1225 active_index(), user_gesture)); 1228 active_index(), user_gesture));
1226 } 1229 }
1227 1230
1228 void TabStripModel::NotifyActiveTabChanged(int old_active_index) { 1231 void TabStripModel::NotifyIfSelectionChanged(
1229 TabContentsWrapper* old_tab = 1232 const TabStripSelectionModel& old_selection_model) {
1230 old_active_index == TabStripSelectionModel::kUnselectedIndex ? 1233 if (!selection_model().Equals(old_selection_model)) {
1231 NULL : GetTabContentsAt(old_active_index); 1234 FOR_EACH_OBSERVER(TabStripModelObserver, observers_,
1232 TabContentsWrapper* new_tab = 1235 TabSelectionChanged(old_selection_model));
1233 active_index() == TabStripSelectionModel::kUnselectedIndex ? 1236 }
1234 NULL : GetTabContentsAt(active_index());
1235 FOR_EACH_OBSERVER(TabStripModelObserver, observers_,
1236 ActiveTabChanged(old_tab, new_tab, active_index(), true));
1237 } 1237 }
1238 1238
1239 void TabStripModel::SelectRelativeTab(bool next) { 1239 void TabStripModel::SelectRelativeTab(bool next) {
1240 // This may happen during automated testing or if a user somehow buffers 1240 // This may happen during automated testing or if a user somehow buffers
1241 // many key accelerators. 1241 // many key accelerators.
1242 if (contents_data_.empty()) 1242 if (contents_data_.empty())
1243 return; 1243 return;
1244 1244
1245 int index = active_index(); 1245 int index = active_index();
1246 int delta = next ? 1 : -1; 1246 int delta = next ? 1 : -1;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
1311 void TabStripModel::ForgetOpenersAndGroupsReferencing( 1311 void TabStripModel::ForgetOpenersAndGroupsReferencing(
1312 const NavigationController* tab) { 1312 const NavigationController* tab) {
1313 for (TabContentsDataVector::const_iterator i = contents_data_.begin(); 1313 for (TabContentsDataVector::const_iterator i = contents_data_.begin();
1314 i != contents_data_.end(); ++i) { 1314 i != contents_data_.end(); ++i) {
1315 if ((*i)->group == tab) 1315 if ((*i)->group == tab)
1316 (*i)->group = NULL; 1316 (*i)->group = NULL;
1317 if ((*i)->opener == tab) 1317 if ((*i)->opener == tab)
1318 (*i)->opener = NULL; 1318 (*i)->opener = NULL;
1319 } 1319 }
1320 } 1320 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698