Commit 9e5d22bc authored by Scott Violet's avatar Scott Violet Committed by Commit Bot

chromeos: minor cleanup of TabScrubber

Biggest non-style/c++11 change is to convert from NotificationRegistrar to
BrowserListObserver.

This should not result in any functional change.

BUG=889097,268984
TEST=covered by tests

Change-Id: I8ea1c89f28c66f312fbcfda925591f96852d2173
Reviewed-on: https://chromium-review.googlesource.com/c/1298123Reviewed-by: default avatarEvan Stade <estade@chromium.org>
Commit-Queue: Scott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#602421}
parent 3b59644a
...@@ -10,26 +10,22 @@ ...@@ -10,26 +10,22 @@
#include "ash/shell.h" #include "ash/shell.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h" #include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h" #include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/views/frame/browser_view.h" #include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/immersive_mode_controller.h"
#include "chrome/browser/ui/views/tabs/glow_hover_controller.h" #include "chrome/browser/ui/views/tabs/glow_hover_controller.h"
#include "chrome/browser/ui/views/tabs/tab.h" #include "chrome/browser/ui/views/tabs/tab.h"
#include "chrome/browser/ui/views/tabs/tab_strip.h" #include "chrome/browser/ui/views/tabs/tab_strip.h"
#include "chrome/browser/ui/views/tabs/tab_style.h" #include "chrome/browser/ui/views/tabs/tab_style.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "ui/aura/window.h"
#include "ui/events/event.h" #include "ui/events/event.h"
#include "ui/events/event_utils.h" #include "ui/events/event_utils.h"
#include "ui/events/gesture_detection/gesture_configuration.h" #include "ui/events/gesture_detection/gesture_configuration.h"
namespace { namespace {
const int64_t kActivationDelayMS = 200;
inline float Clamp(float value, float low, float high) { inline float Clamp(float value, float low, float high) {
return std::min(high, std::max(value, low)); return std::min(high, std::max(value, low));
} }
...@@ -78,29 +74,16 @@ bool TabScrubber::IsActivationPending() { ...@@ -78,29 +74,16 @@ bool TabScrubber::IsActivationPending() {
return activate_timer_.IsRunning(); return activate_timer_.IsRunning();
} }
TabScrubber::TabScrubber() TabScrubber::TabScrubber() {
: scrubbing_(false),
browser_(nullptr),
tab_strip_(nullptr),
swipe_x_(-1),
swipe_y_(-1),
swipe_direction_(LEFT),
highlighted_tab_(-1),
activation_delay_(kActivationDelayMS),
use_default_activation_delay_(true),
weak_ptr_factory_(this) {
// TODO(mash): Add window server API to observe swipe gestures. Observing // TODO(mash): Add window server API to observe swipe gestures. Observing
// gestures on browser windows is not sufficient, as this feature works when // gestures on browser windows is not sufficient, as this feature works when
// the cursor is over the shelf, desktop, etc. https://crbug.com/796366 // the cursor is over the shelf, desktop, etc. https://crbug.com/796366
ash::Shell::Get()->AddPreTargetHandler(this); ash::Shell::Get()->AddPreTargetHandler(this);
registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSED, BrowserList::AddObserver(this);
content::NotificationService::AllSources());
} }
TabScrubber::~TabScrubber() { TabScrubber::~TabScrubber() {
// Note: The weak_ptr_factory_ should invalidate its weak pointers before BrowserList::RemoveObserver(this);
// any other members are destroyed.
weak_ptr_factory_.InvalidateWeakPtrs();
} }
void TabScrubber::OnScrollEvent(ui::ScrollEvent* event) { void TabScrubber::OnScrollEvent(ui::ScrollEvent* event) {
...@@ -122,8 +105,7 @@ void TabScrubber::OnScrollEvent(ui::ScrollEvent* event) { ...@@ -122,8 +105,7 @@ void TabScrubber::OnScrollEvent(ui::ScrollEvent* event) {
return; return;
} }
BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow( BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser);
browser->window()->GetNativeWindow());
TabStrip* tab_strip = browser_view->tabstrip(); TabStrip* tab_strip = browser_view->tabstrip();
if (tab_strip->IsAnimating()) { if (tab_strip->IsAnimating()) {
...@@ -138,7 +120,7 @@ void TabScrubber::OnScrollEvent(ui::ScrollEvent* event) { ...@@ -138,7 +120,7 @@ void TabScrubber::OnScrollEvent(ui::ScrollEvent* event) {
// left, positive means right. // left, positive means right.
float x_offset = event->x_offset(); float x_offset = event->x_offset();
if (!scrubbing_) { if (!scrubbing_) {
BeginScrub(browser, browser_view, x_offset); BeginScrub(browser_view, x_offset);
} else if (highlighted_tab_ == -1) { } else if (highlighted_tab_ == -1) {
// Has the direction of the swipe changed while scrubbing? // Has the direction of the swipe changed while scrubbing?
Direction direction = (x_offset < 0) ? LEFT : RIGHT; Direction direction = (x_offset < 0) ? LEFT : RIGHT;
...@@ -174,18 +156,17 @@ void TabScrubber::OnScrollEvent(ui::ScrollEvent* event) { ...@@ -174,18 +156,17 @@ void TabScrubber::OnScrollEvent(ui::ScrollEvent* event) {
} }
} }
void TabScrubber::Observe(int type, void TabScrubber::OnBrowserRemoved(Browser* browser) {
const content::NotificationSource& source, if (browser != browser_)
const content::NotificationDetails& details) { return;
if (content::Source<Browser>(source).ptr() == browser_) {
activate_timer_.Stop(); activate_timer_.Stop();
swipe_x_ = -1; swipe_x_ = -1;
swipe_y_ = -1; swipe_y_ = -1;
scrubbing_ = false; scrubbing_ = false;
highlighted_tab_ = -1; highlighted_tab_ = -1;
browser_ = nullptr; browser_ = nullptr;
tab_strip_ = nullptr; tab_strip_ = nullptr;
}
} }
void TabScrubber::OnTabAdded(int index) { void TabScrubber::OnTabAdded(int index) {
...@@ -229,15 +210,13 @@ Browser* TabScrubber::GetActiveBrowser() { ...@@ -229,15 +210,13 @@ Browser* TabScrubber::GetActiveBrowser() {
return browser; return browser;
} }
void TabScrubber::BeginScrub(Browser* browser, void TabScrubber::BeginScrub(BrowserView* browser_view, float x_offset) {
BrowserView* browser_view,
float x_offset) {
DCHECK(browser);
DCHECK(browser_view); DCHECK(browser_view);
DCHECK(browser_view->browser());
tab_strip_ = browser_view->tabstrip(); tab_strip_ = browser_view->tabstrip();
scrubbing_ = true; scrubbing_ = true;
browser_ = browser; browser_ = browser_view->browser();
Direction direction = (x_offset < 0) ? LEFT : RIGHT; Direction direction = (x_offset < 0) ? LEFT : RIGHT;
ScrubDirectionChanged(direction); ScrubDirectionChanged(direction);
...@@ -256,8 +235,7 @@ void TabScrubber::FinishScrub(bool activate) { ...@@ -256,8 +235,7 @@ void TabScrubber::FinishScrub(bool activate) {
activate_timer_.Stop(); activate_timer_.Stop();
if (browser_ && browser_->window()) { if (browser_ && browser_->window()) {
BrowserView* browser_view = BrowserView::GetBrowserViewForNativeWindow( BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser_);
browser_->window()->GetNativeWindow());
TabStrip* tab_strip = browser_view->tabstrip(); TabStrip* tab_strip = browser_view->tabstrip();
if (activate && highlighted_tab_ != -1) { if (activate && highlighted_tab_ != -1) {
Tab* tab = tab_strip->tab_at(highlighted_tab_); Tab* tab = tab_strip->tab_at(highlighted_tab_);
...@@ -279,16 +257,11 @@ void TabScrubber::FinishScrub(bool activate) { ...@@ -279,16 +257,11 @@ void TabScrubber::FinishScrub(bool activate) {
} }
void TabScrubber::ScheduleFinishScrubIfNeeded() { void TabScrubber::ScheduleFinishScrubIfNeeded() {
int delay = use_default_activation_delay_ const base::TimeDelta delay = base::TimeDelta::FromMilliseconds(
? ui::GestureConfiguration::GetInstance() use_default_activation_delay_ ? 200 : 0);
->tab_scrub_activation_delay_in_ms() activate_timer_.Start(FROM_HERE, delay,
: activation_delay_; base::BindRepeating(&TabScrubber::FinishScrub,
base::Unretained(this), true));
if (delay >= 0) {
activate_timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(delay),
base::Bind(&TabScrubber::FinishScrub,
weak_ptr_factory_.GetWeakPtr(), true));
}
} }
void TabScrubber::ScrubDirectionChanged(Direction direction) { void TabScrubber::ScrubDirectionChanged(Direction direction) {
......
...@@ -9,13 +9,13 @@ ...@@ -9,13 +9,13 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/timer/timer.h" #include "base/timer/timer.h"
#include "chrome/browser/ui/views/frame/immersive_mode_controller.h" #include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/views/tabs/tab_strip_observer.h" #include "chrome/browser/ui/views/tabs/tab_strip_observer.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "ui/events/event_handler.h" #include "ui/events/event_handler.h"
class Browser; class Browser;
class BrowserView;
class ImmersiveRevealedLock;
class Tab; class Tab;
class TabStrip; class TabStrip;
...@@ -25,7 +25,7 @@ class Point; ...@@ -25,7 +25,7 @@ class Point;
// Class to enable quick tab switching via horizontal 3 finger swipes. // Class to enable quick tab switching via horizontal 3 finger swipes.
class TabScrubber : public ui::EventHandler, class TabScrubber : public ui::EventHandler,
public content::NotificationObserver, public BrowserListObserver,
public TabStripObserver { public TabStripObserver {
public: public:
enum Direction { LEFT, RIGHT }; enum Direction { LEFT, RIGHT };
...@@ -39,25 +39,20 @@ class TabScrubber : public ui::EventHandler, ...@@ -39,25 +39,20 @@ class TabScrubber : public ui::EventHandler,
int index, int index,
TabScrubber::Direction direction); TabScrubber::Direction direction);
void set_activation_delay(int activation_delay) {
activation_delay_ = activation_delay;
use_default_activation_delay_ = false;
}
int activation_delay() const { return activation_delay_; }
int highlighted_tab() const { return highlighted_tab_; } int highlighted_tab() const { return highlighted_tab_; }
bool IsActivationPending(); bool IsActivationPending();
private: private:
friend class TabScrubberTest;
TabScrubber(); TabScrubber();
~TabScrubber() override; ~TabScrubber() override;
// ui::EventHandler overrides: // ui::EventHandler overrides:
void OnScrollEvent(ui::ScrollEvent* event) override; void OnScrollEvent(ui::ScrollEvent* event) override;
// content::NotificationObserver overrides: // BrowserListObserver overrides:
void Observe(int type, void OnBrowserRemoved(Browser* browser) override;
const content::NotificationSource& source,
const content::NotificationDetails& details) override;
// TabStripObserver overrides. // TabStripObserver overrides.
void OnTabAdded(int index) override; void OnTabAdded(int index) override;
...@@ -66,7 +61,7 @@ class TabScrubber : public ui::EventHandler, ...@@ -66,7 +61,7 @@ class TabScrubber : public ui::EventHandler,
Browser* GetActiveBrowser(); Browser* GetActiveBrowser();
void BeginScrub(Browser* browser, BrowserView* browser_view, float x_offset); void BeginScrub(BrowserView* browser_view, float x_offset);
void FinishScrub(bool activate); void FinishScrub(bool activate);
void ScheduleFinishScrubIfNeeded(); void ScheduleFinishScrubIfNeeded();
...@@ -81,32 +76,28 @@ class TabScrubber : public ui::EventHandler, ...@@ -81,32 +76,28 @@ class TabScrubber : public ui::EventHandler,
void UpdateHighlightedTab(Tab* new_tab, int new_index); void UpdateHighlightedTab(Tab* new_tab, int new_index);
// Are we currently scrubbing?. // Are we currently scrubbing?.
bool scrubbing_; bool scrubbing_ = false;
// The last browser we used for scrubbing, NULL if |scrubbing_| is // The last browser we used for scrubbing, NULL if |scrubbing_| is false and
// false and there is no pending work. // there is no pending work.
Browser* browser_; Browser* browser_ = nullptr;
// The TabStrip of the active browser we're scrubbing. // The TabStrip of the active browser we're scrubbing.
TabStrip* tab_strip_; TabStrip* tab_strip_ = nullptr;
// The current accumulated x and y positions of a swipe, in // The current accumulated x and y positions of a swipe, in the coordinates
// the coordinates of the TabStrip of |browser_| // of the TabStrip of |browser_|.
float swipe_x_; float swipe_x_ = -1;
float swipe_y_; int swipe_y_ = -1;
// The direction the current swipe is headed. // The direction the current swipe is headed.
Direction swipe_direction_; Direction swipe_direction_ = LEFT;
// The index of the tab that is currently highlighted. // The index of the tab that is currently highlighted.
int highlighted_tab_; int highlighted_tab_ = -1;
// Timer to control a delayed activation of the |highlighted_tab_|. // Timer to control a delayed activation of the |highlighted_tab_|.
base::RetainingOneShotTimer activate_timer_; base::RetainingOneShotTimer activate_timer_;
// Time to wait in ms before newly selected tab becomes active. // True if the default activation delay should be used with |activate_timer_|.
int activation_delay_; // A value of false means the |activate_timer_| gets a zero delay.
// Set if activation_delay had been explicitly set. bool use_default_activation_delay_ = true;
bool use_default_activation_delay_;
// Forces the tabs to be revealed if we are in immersive fullscreen. // Forces the tabs to be revealed if we are in immersive fullscreen.
std::unique_ptr<ImmersiveRevealedLock> immersive_reveal_lock_; std::unique_ptr<ImmersiveRevealedLock> immersive_reveal_lock_;
content::NotificationRegistrar registrar_;
base::WeakPtrFactory<TabScrubber> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(TabScrubber); DISALLOW_COPY_AND_ASSIGN(TabScrubber);
}; };
......
...@@ -76,6 +76,8 @@ class ImmersiveRevealEndedWaiter : public ImmersiveModeController::Observer { ...@@ -76,6 +76,8 @@ class ImmersiveRevealEndedWaiter : public ImmersiveModeController::Observer {
DISALLOW_COPY_AND_ASSIGN(ImmersiveRevealEndedWaiter); DISALLOW_COPY_AND_ASSIGN(ImmersiveRevealEndedWaiter);
}; };
} // namespace
class TabScrubberTest : public InProcessBrowserTest, class TabScrubberTest : public InProcessBrowserTest,
public TabStripModelObserver { public TabStripModelObserver {
public: public:
...@@ -86,7 +88,7 @@ class TabScrubberTest : public InProcessBrowserTest, ...@@ -86,7 +88,7 @@ class TabScrubberTest : public InProcessBrowserTest,
} }
void SetUpOnMainThread() override { void SetUpOnMainThread() override {
TabScrubber::GetInstance()->set_activation_delay(0); TabScrubber::GetInstance()->use_default_activation_delay_ = false;
// Disable external monitor scaling of coordinates. // Disable external monitor scaling of coordinates.
ash::Shell* shell = ash::Shell::Get(); ash::Shell* shell = ash::Shell::Get();
...@@ -267,8 +269,6 @@ class TabScrubberTest : public InProcessBrowserTest, ...@@ -267,8 +269,6 @@ class TabScrubberTest : public InProcessBrowserTest,
DISALLOW_COPY_AND_ASSIGN(TabScrubberTest); DISALLOW_COPY_AND_ASSIGN(TabScrubberTest);
}; };
} // namespace
// Swipe a single tab in each direction. // Swipe a single tab in each direction.
IN_PROC_BROWSER_TEST_F(TabScrubberTest, Single) { IN_PROC_BROWSER_TEST_F(TabScrubberTest, Single) {
AddTabs(browser(), 1); AddTabs(browser(), 1);
......
...@@ -75,7 +75,6 @@ void AuraTestBase::SetUp() { ...@@ -75,7 +75,6 @@ void AuraTestBase::SetUp() {
gesture_config->set_semi_long_press_time_in_ms(400); gesture_config->set_semi_long_press_time_in_ms(400);
gesture_config->set_show_press_delay_in_ms(5); gesture_config->set_show_press_delay_in_ms(5);
gesture_config->set_swipe_enabled(true); gesture_config->set_swipe_enabled(true);
gesture_config->set_tab_scrub_activation_delay_in_ms(200);
gesture_config->set_two_finger_tap_enabled(true); gesture_config->set_two_finger_tap_enabled(true);
gesture_config->set_velocity_tracker_strategy( gesture_config->set_velocity_tracker_strategy(
ui::VelocityTracker::Strategy::LSQ2_RESTRICTED); ui::VelocityTracker::Strategy::LSQ2_RESTRICTED);
......
...@@ -67,7 +67,6 @@ GestureConfiguration::GestureConfiguration() ...@@ -67,7 +67,6 @@ GestureConfiguration::GestureConfiguration()
// 2 * max_touch_move_in_pixels_for_click_. // 2 * max_touch_move_in_pixels_for_click_.
span_slop_(30), span_slop_(30),
swipe_enabled_(false), swipe_enabled_(false),
tab_scrub_activation_delay_in_ms_(200),
two_finger_tap_enabled_(false), two_finger_tap_enabled_(false),
velocity_tracker_strategy_(VelocityTracker::Strategy::STRATEGY_DEFAULT) { velocity_tracker_strategy_(VelocityTracker::Strategy::STRATEGY_DEFAULT) {
} }
......
...@@ -161,14 +161,6 @@ class GESTURE_DETECTION_EXPORT GestureConfiguration { ...@@ -161,14 +161,6 @@ class GESTURE_DETECTION_EXPORT GestureConfiguration {
float span_slop() const { return span_slop_; } float span_slop() const { return span_slop_; }
bool swipe_enabled() const { return swipe_enabled_; } bool swipe_enabled() const { return swipe_enabled_; }
void set_swipe_enabled(bool val) { swipe_enabled_ = val; } void set_swipe_enabled(bool val) { swipe_enabled_ = val; }
// TODO(davemoore): Move into chrome/browser/ui.
int tab_scrub_activation_delay_in_ms() const {
return tab_scrub_activation_delay_in_ms_;
}
void set_tab_scrub_activation_delay_in_ms(int val) {
tab_scrub_activation_delay_in_ms_ = val;
}
bool two_finger_tap_enabled() const { return two_finger_tap_enabled_; } bool two_finger_tap_enabled() const { return two_finger_tap_enabled_; }
void set_two_finger_tap_enabled(bool val) { two_finger_tap_enabled_ = val; } void set_two_finger_tap_enabled(bool val) { two_finger_tap_enabled_ = val; }
VelocityTracker::Strategy velocity_tracker_strategy() const { VelocityTracker::Strategy velocity_tracker_strategy() const {
...@@ -254,9 +246,6 @@ class GESTURE_DETECTION_EXPORT GestureConfiguration { ...@@ -254,9 +246,6 @@ class GESTURE_DETECTION_EXPORT GestureConfiguration {
int show_press_delay_in_ms_; int show_press_delay_in_ms_;
float span_slop_; float span_slop_;
bool swipe_enabled_; bool swipe_enabled_;
// TODO(davemoore): Move into chrome/browser/ui.
int tab_scrub_activation_delay_in_ms_;
bool two_finger_tap_enabled_; bool two_finger_tap_enabled_;
VelocityTracker::Strategy velocity_tracker_strategy_; VelocityTracker::Strategy velocity_tracker_strategy_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment