Commit 90496b3b authored by sangwoo.ko's avatar sangwoo.ko Committed by Commit Bot

Activate the next/previous tab on horizonal scroll

This CL allows us to use mouse with horizontal scroll function,
such as tilting wheel or separate wheel for h-scroll.

Bug: 2384
Change-Id: I770c629bb2391343ce66a398e4d73ad69dde569e
Reviewed-on: https://chromium-review.googlesource.com/c/1414537
Commit-Queue: Sang Woo Ko <sangwoo108@chromium.org>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#628588}
parent 7df72cf5
......@@ -1341,7 +1341,7 @@ float TabStrip::GetHoverOpacityForRadialHighlight() const {
}
///////////////////////////////////////////////////////////////////////////////
// TabStrip, views::View overrides:
// TabStrip, views::AccessiblePaneView overrides:
void TabStrip::Layout() {
// Only do a layout if our size changed.
......@@ -1352,6 +1352,34 @@ void TabStrip::Layout() {
DoLayout();
}
bool TabStrip::OnMouseWheel(const ui::MouseWheelEvent& event) {
// Change the selected tab on horizontal wheel scrolls.
// Honor wheel scrolls over the tabs themselves, but not the NTB/surrounding
// space.
if ((!event.x_offset() && !event.IsShiftDown()) || tab_count() < 2 ||
!FindTabHitByPoint(event.location()))
return false;
accumulated_horizontal_scroll_ +=
(event.x_offset() ? event.x_offset() : event.y_offset());
int horizontal_offset =
accumulated_horizontal_scroll_ / ui::MouseWheelEvent::kWheelDelta;
if (!horizontal_offset)
return true;
accumulated_horizontal_scroll_ %= ui::MouseWheelEvent::kWheelDelta;
int new_active_index =
(controller_->GetActiveIndex() + horizontal_offset) % tab_count();
if (new_active_index < 0)
new_active_index += tab_count();
DCHECK(IsValidModelIndex(new_active_index));
controller_->SelectTab(new_active_index);
return true;
}
void TabStrip::PaintChildren(const views::PaintInfo& paint_info) {
// The view order doesn't match the paint order (tabs_ contains the tab
// ordering). Additionally we need to paint the tabs that are closing in
......
......@@ -290,8 +290,9 @@ class TabStrip : public views::AccessiblePaneView,
// MouseWatcherListener:
void MouseMovedOutOfHost() override;
// views::View:
// views::AccessiblePaneView:
void Layout() override;
bool OnMouseWheel(const ui::MouseWheelEvent& event) override;
void PaintChildren(const views::PaintInfo& paint_info) override;
void OnPaint(gfx::Canvas* canvas) override;
const char* GetClassName() const override;
......@@ -711,6 +712,10 @@ class TabStrip : public views::AccessiblePaneView,
// Number of mouse moves.
int mouse_move_count_ = 0;
// Accumulatated offsets from thumb wheel. Used to throttle horizontal
// scroll from thumb wheel.
int accumulated_horizontal_scroll_ = 0;
// Timer used when a tab is closed and we need to relayout. Only used when a
// tab close comes from a touch device.
base::OneShotTimer resize_layout_timer_;
......
......@@ -896,4 +896,50 @@ TEST_P(TabStripTest, EventsOnClosingTab) {
EXPECT_EQ(first_tab, tab_strip_->GetEventHandlerForPoint(tab_center));
}
// Switch selected tabs on horizontal scroll events.
TEST_P(TabStripTest, HorizontalScroll) {
tab_strip_->SetBounds(0, 0, 200, 20);
for (int i = 0; i < 3; i++)
controller_->AddTab(i, true /* is_active */);
Tab* tab = tab_strip_->tab_at(0);
gfx::Point tab_center = tab->bounds().CenterPoint();
for (int i = 0; i < tab_strip_->tab_count(); ++i) {
ui::MouseWheelEvent wheel_event(
gfx::Vector2d(ui::MouseWheelEvent::kWheelDelta, 0), tab_center,
tab_center, ui::EventTimeForNow(), 0, 0);
tab_strip_->OnMouseWheel(wheel_event);
EXPECT_EQ(i, controller_->GetActiveIndex());
}
controller_->SelectTab(0);
for (int i = tab_strip_->tab_count() - 1; i >= 0; --i) {
ui::MouseWheelEvent wheel_event(
gfx::Vector2d(-ui::MouseWheelEvent::kWheelDelta, 0), tab_center,
tab_center, ui::EventTimeForNow(), 0, 0);
tab_strip_->OnMouseWheel(wheel_event);
EXPECT_EQ(i, controller_->GetActiveIndex());
}
// When offset is smaller than kWheelDelta, we don't scroll immediately.
// We wait offset until accumulated offset gets bigger than kWheelDelta.
const int small_offset = ui::MouseWheelEvent::kWheelDelta / 3;
int next_accumulated_offset = small_offset;
while (next_accumulated_offset < ui::MouseWheelEvent::kWheelDelta) {
ui::MouseWheelEvent wheel_event(gfx::Vector2d(small_offset, 0), tab_center,
tab_center, ui::EventTimeForNow(), 0, 0);
tab_strip_->OnMouseWheel(wheel_event);
EXPECT_EQ(0, controller_->GetActiveIndex());
next_accumulated_offset += small_offset;
}
ui::MouseWheelEvent wheel_event(gfx::Vector2d(small_offset, 0), tab_center,
tab_center, ui::EventTimeForNow(), 0, 0);
tab_strip_->OnMouseWheel(wheel_event);
EXPECT_EQ(1, controller_->GetActiveIndex());
}
INSTANTIATE_TEST_SUITE_P(, TabStripTest, ::testing::Values(false, true));
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