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

Unified Diff: chrome/browser/ui/tabs/mru_tab_controller.cc

Issue 10117016: Implementation for switching between recently used tabs using ctrl tilde or quoteleft. Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Uploading patch for review after rebase. Created 8 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/tabs/mru_tab_controller.cc
diff --git a/chrome/browser/ui/tabs/mru_tab_controller.cc b/chrome/browser/ui/tabs/mru_tab_controller.cc
new file mode 100644
index 0000000000000000000000000000000000000000..5c172c077ab4b56941b3f75027d9022efacab90a
--- /dev/null
+++ b/chrome/browser/ui/tabs/mru_tab_controller.cc
@@ -0,0 +1,103 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/ui/tabs/mru_tab_controller.h"
+#include "chrome/browser/ui/tabs/tab_strip_model.h"
+
+#include "chrome/browser/ui/tab_contents/tab_contents.h"
+
+///////////////////////////////////////////////////////////////////////////////
+// MRUTabController, public:
+
+///////////////////////////////////////////////////////////////////////////////
+// MRUTabController, TabStripModelObserver implementation:
+
+MRUTabController::MRUTabController(TabStripModel* tab_strip_model)
+ : tab_strip_model_(tab_strip_model),
+ can_update_stack_(true) {
+ tab_strip_model_->AddObserver(this);
+}
+
+MRUTabController::~MRUTabController() {
+ tabs_mru_list_.clear();
+ if (tab_strip_model_)
+ tab_strip_model_->RemoveObserver(this);
+}
+
+void MRUTabController::TabInsertedAt(TabContents* contents,
+ int index,
+ bool foreground) {
+ if (foreground)
+ tabs_mru_list_.push_front(contents);
+ else
+ tabs_mru_list_.push_back(contents);
+}
+
+void MRUTabController::TabDetachedAt(TabContents* contents,
+ int index) {
+ // Remove the index if found in MRU List
+ std::list<TabContents*>::iterator it =
+ std::find(tabs_mru_list_.begin(), tabs_mru_list_.end(), contents);
+ if (it != tabs_mru_list_.end()) {
+ tabs_mru_list_.erase(it);
+ }
+}
+
+void MRUTabController::ActiveTabChanged(TabContents* old_contents,
+ TabContents* new_contents,
+ int index,
+ bool user_gesture) {
+ if (!can_update_stack_)
+ return;
+
+ // Bring the activated tab content to the front of the list
+ std::list<TabContents*>::iterator it =
+ std::find(tabs_mru_list_.begin(), tabs_mru_list_.end(), new_contents);
+ if (it != tabs_mru_list_.end()) {
+ tabs_mru_list_.push_front(*it);
+ tabs_mru_list_.erase(it);
+ }
+}
+
+void MRUTabController::TabReplacedAt(TabStripModel* tab_strip_model,
+ TabContents* old_contents,
+ TabContents* new_contents,
+ int index) {
+ // Replace the tab contents accordingly in the list. If the tab contents are
+ // not found add to the end of the list.
+ std::list<TabContents*>::iterator it =
+ std::find(tabs_mru_list_.begin(), tabs_mru_list_.end(), old_contents);
+ if (it != tabs_mru_list_.end() && *it != tabs_mru_list_.front()) {
+ tabs_mru_list_.insert(it, new_contents);
+ tabs_mru_list_.erase(it);
+ } else {
+ NOTREACHED();
+ }
+}
+
+void MRUTabController::TabStripEmpty() {
+ tabs_mru_list_.clear();
+}
+
+TabContents* MRUTabController::GetPreviousMRUTab() {
+ // Only if there are more than one open tab we can switch.
+ if (tabs_mru_list_.size() < 2)
+ return NULL;
+
+ std::list<TabContents*>::iterator it = tabs_mru_list_.begin();
+ return *(++it);
+}
+
+void MRUTabController::PauseStackUpdates() {
+ can_update_stack_ = false;
+}
+
+void MRUTabController::CommitActiveTabChanges() {
+ if (can_update_stack_)
+ return;
+
+ can_update_stack_ = true;
+ TabContents* active_tab = tab_strip_model_->GetActiveTabContents();
+ ActiveTabChanged(active_tab, active_tab, 0, true);
+}

Powered by Google App Engine
This is Rietveld 408576698