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

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

Powered by Google App Engine
This is Rietveld 408576698