-
Notifications
You must be signed in to change notification settings - Fork 920
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4671 from guifel/guifel-1/mru-clycling-ctrl-tab
Most recently used tab with Ctrl-Tab cycling
- Loading branch information
Showing
23 changed files
with
465 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* Copyright (c) 2020 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef BRAVE_BROWSER_UI_BRAVE_BROWSER_WINDOW_H_ | ||
#define BRAVE_BROWSER_UI_BRAVE_BROWSER_WINDOW_H_ | ||
|
||
#include "chrome/browser/ui/browser_window.h" | ||
|
||
class BraveBrowserWindow : public BrowserWindow { | ||
public: | ||
~BraveBrowserWindow() override {} | ||
|
||
virtual void StartTabCycling() = 0; | ||
}; | ||
|
||
#endif // BRAVE_BROWSER_UI_BRAVE_BROWSER_WINDOW_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
source_set("tabs") { | ||
# Set due to //chrome/browser and //chrome/browser/ui circular dependencies. | ||
check_includes = false | ||
|
||
if (!is_android) { | ||
sources = [ | ||
"brave_tab_menu_model.cc", | ||
"brave_tab_menu_model.h", | ||
"brave_tab_strip_model.cc", | ||
"brave_tab_strip_model.h", | ||
] | ||
|
||
deps = [ | ||
"//brave/common/", | ||
"//chrome/app:generated_resources", | ||
"//components/prefs", | ||
"//components/sessions", | ||
"//content/public/browser", | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* Copyright (c) 2020 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "brave/browser/ui/tabs/brave_tab_strip_model.h" | ||
|
||
#include <algorithm> | ||
|
||
#include "brave/common/pref_names.h" | ||
#include "chrome/browser/profiles/profile.h" | ||
#include "chrome/browser/ui/browser_finder.h" | ||
#include "chrome/browser/ui/views/frame/browser_view.h" | ||
#include "components/prefs/pref_service.h" | ||
#include "content/public/browser/web_contents.h" | ||
|
||
BraveTabStripModel::BraveTabStripModel(TabStripModelDelegate* delegate, | ||
Profile* profile) | ||
: TabStripModel(delegate, profile) {} | ||
BraveTabStripModel::~BraveTabStripModel() {} | ||
|
||
void BraveTabStripModel::SelectRelativeTab(bool forward, | ||
UserGestureDetails detail) { | ||
if (contents_data_.empty()) | ||
return; | ||
|
||
bool is_mru_enabled = profile()->GetPrefs()->GetBoolean(kMRUCyclingEnabled); | ||
|
||
if (is_mru_enabled) { | ||
SelectMRUTab(forward, detail); | ||
} else { | ||
TabStripModel::SelectRelativeTab(forward, detail); | ||
} | ||
} | ||
|
||
void BraveTabStripModel::SelectMRUTab(bool forward, UserGestureDetails detail) { | ||
if (mru_cycle_list_.empty()) { | ||
// Start cycling | ||
|
||
Browser* browser = chrome::FindBrowserWithWebContents(GetWebContentsAt(0)); | ||
if (!browser) | ||
return; | ||
|
||
// Create a list of tab indexes sorted by time of last activation | ||
for (int i = 0; i < count(); ++i) { | ||
mru_cycle_list_.push_back(i); | ||
} | ||
|
||
std::sort(mru_cycle_list_.begin(), mru_cycle_list_.end(), | ||
[this](int a, int b) { | ||
return GetWebContentsAt(a)->GetLastActiveTime() > | ||
GetWebContentsAt(b)->GetLastActiveTime(); | ||
}); | ||
|
||
// Tell the cycling controller that we start cycling to handle tabs keys | ||
static_cast<BraveBrowserWindow*>(browser->window())->StartTabCycling(); | ||
} | ||
|
||
if (forward) { | ||
std::rotate(mru_cycle_list_.begin(), | ||
mru_cycle_list_.begin() + 1, | ||
mru_cycle_list_.end()); | ||
} else { | ||
std::rotate(mru_cycle_list_.rbegin(), | ||
mru_cycle_list_.rbegin() + 1, | ||
mru_cycle_list_.rend()); | ||
} | ||
|
||
ActivateTabAt(mru_cycle_list_[0], detail); | ||
} | ||
|
||
void BraveTabStripModel::StopMRUCycling() { | ||
mru_cycle_list_.clear(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* Copyright (c) 2020 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef BRAVE_BROWSER_UI_TABS_BRAVE_TAB_STRIP_MODEL_H_ | ||
#define BRAVE_BROWSER_UI_TABS_BRAVE_TAB_STRIP_MODEL_H_ | ||
|
||
#include <vector> | ||
|
||
#include "chrome/browser/ui/tabs/tab_strip_model.h" | ||
|
||
class BraveTabStripModel : public TabStripModel { | ||
public: | ||
explicit BraveTabStripModel(TabStripModelDelegate* delegate, | ||
Profile* profile); | ||
|
||
~BraveTabStripModel() override; | ||
|
||
BraveTabStripModel(const BraveTabStripModel&) = delete; | ||
BraveTabStripModel operator=(const BraveTabStripModel&) = delete; | ||
|
||
void SelectRelativeTab(bool forward, UserGestureDetails detail) override; | ||
|
||
// Set the next tab when doing a MRU cycling with Ctrl-tab | ||
void SelectMRUTab( | ||
bool forward, | ||
UserGestureDetails detail = UserGestureDetails(GestureType::kOther)); | ||
|
||
// Stop MRU cycling, called when releasing the Ctrl key | ||
void StopMRUCycling(); | ||
|
||
private: | ||
// List of tab indexes sorted by most recently used | ||
std::vector<int> mru_cycle_list_; | ||
}; | ||
|
||
#endif // BRAVE_BROWSER_UI_TABS_BRAVE_TAB_STRIP_MODEL_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright (c) 2020 The Brave Authors. All rights reserved. | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
# You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
source_set("browser_tests") { | ||
testonly = true | ||
defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ] | ||
|
||
sources = [ | ||
"brave_tab_strip_model_browsertest.cc", | ||
] | ||
|
||
deps = [ | ||
"//brave/common:pref_names", | ||
"//chrome/app:command_ids", | ||
"//chrome/browser", | ||
"//chrome/browser/ui", | ||
"//chrome/test:test_support_ui", | ||
"//components/prefs", | ||
"//content/test:test_support", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* Copyright (c) 2020 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "brave/common/pref_names.h" | ||
#include "chrome/app/chrome_command_ids.h" | ||
#include "chrome/browser/profiles/profile.h" | ||
#include "chrome/browser/ui/browser.h" | ||
#include "chrome/browser/ui/browser_commands.h" | ||
#include "chrome/test/base/in_process_browser_test.h" | ||
#include "components/prefs/pref_service.h" | ||
#include "content/public/test/browser_test.h" | ||
|
||
using BraveTabStripModelTest = InProcessBrowserTest; | ||
|
||
IN_PROC_BROWSER_TEST_F(BraveTabStripModelTest, MRUCyclingBasic) { | ||
TabStripModel* tab_strip_model = browser()->tab_strip_model(); | ||
|
||
// Open 3 tabs | ||
chrome::NewTab(browser()); | ||
chrome::NewTab(browser()); | ||
EXPECT_EQ(browser()->tab_strip_model()->count(), 3); | ||
EXPECT_EQ(browser()->tab_strip_model()->active_index(), 2); | ||
|
||
// Normal next tab expected by default, 2 -> 0 | ||
chrome::ExecuteCommand(browser(), IDC_SELECT_NEXT_TAB); | ||
EXPECT_EQ(tab_strip_model->active_index(), 0); | ||
|
||
// Activate MRU cycling | ||
browser()->profile()->GetPrefs()->SetBoolean(kMRUCyclingEnabled, true); | ||
|
||
// MRU cycling, 0 -> 2 | ||
chrome::ExecuteCommand(browser(), IDC_SELECT_NEXT_TAB); | ||
EXPECT_EQ(tab_strip_model->active_index(), 2); | ||
// Ctrl is not being released 2 -> 1 | ||
chrome::ExecuteCommand(browser(), IDC_SELECT_NEXT_TAB); | ||
EXPECT_EQ(tab_strip_model->active_index(), 1); | ||
// 1 -> 2 | ||
chrome::ExecuteCommand(browser(), IDC_SELECT_PREVIOUS_TAB); | ||
EXPECT_EQ(tab_strip_model->active_index(), 2); | ||
} | ||
|
||
// Check MRU Cycling is restarted when tab is closed during the mru cycling. | ||
// User can close current tab while cycling like this. | ||
// For example on linux, when user does "Ctrl + tab -> Ctrl + F4 -> Ctrl + tab", | ||
// second Ctrl + tab should restart mru cycling. | ||
IN_PROC_BROWSER_TEST_F(BraveTabStripModelTest, TabClosingWhileMRUCycling) { | ||
// Activate MRU cycling | ||
browser()->profile()->GetPrefs()->SetBoolean(kMRUCyclingEnabled, true); | ||
|
||
TabStripModel* tab_strip_model = browser()->tab_strip_model(); | ||
|
||
// Open 3 tabs | ||
chrome::NewTab(browser()); | ||
chrome::NewTab(browser()); | ||
chrome::NewTab(browser()); | ||
EXPECT_EQ(browser()->tab_strip_model()->count(), 4); | ||
EXPECT_EQ(browser()->tab_strip_model()->active_index(), 3); | ||
|
||
// MRU cycling, 3 -> 2 | ||
chrome::ExecuteCommand(browser(), IDC_SELECT_NEXT_TAB); | ||
EXPECT_EQ(tab_strip_model->active_index(), 2); | ||
|
||
// MRU cycling, 2 -> 1 | ||
chrome::ExecuteCommand(browser(), IDC_SELECT_NEXT_TAB); | ||
EXPECT_EQ(tab_strip_model->active_index(), 1); | ||
|
||
// Close current tab (index 1). | ||
chrome::ExecuteCommand(browser(), IDC_CLOSE_TAB); | ||
EXPECT_EQ(tab_strip_model->active_index(), 1); | ||
|
||
// New MRU cycling is started | ||
chrome::ExecuteCommand(browser(), IDC_SELECT_NEXT_TAB); | ||
EXPECT_EQ(tab_strip_model->active_index(), 2); | ||
} |
Oops, something went wrong.