Skip to content

Commit

Permalink
Update ui::PointerEvent to support mouse wheel and capture change eve…
Browse files Browse the repository at this point in the history
…nts.

BUG=638630
TEST=mus_ws_unittests events_unittests blink_converters_unittests
     ash_unittests views_mus_unittests

Review-Url: https://codereview.chromium.org/2256343003
Cr-Commit-Position: refs/heads/master@{#414812}
  • Loading branch information
riaj authored and Commit bot committed Aug 26, 2016
1 parent a34e954 commit 70fa957
Show file tree
Hide file tree
Showing 17 changed files with 143 additions and 107 deletions.
26 changes: 13 additions & 13 deletions ash/aura/pointer_watcher_adapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "ash/shell.h"
#include "ui/aura/client/screen_position_client.h"
#include "ui/aura/window.h"
#include "ui/display/screen.h"
#include "ui/events/event.h"
#include "ui/events/event_constants.h"
#include "ui/gfx/geometry/point.h"
Expand Down Expand Up @@ -43,18 +44,12 @@ void PointerWatcherAdapter::RemovePointerWatcher(
}

void PointerWatcherAdapter::OnMouseEvent(ui::MouseEvent* event) {
if (event->type() == ui::ET_MOUSE_CAPTURE_CHANGED) {
FOR_EACH_OBSERVER(views::PointerWatcher, non_move_watchers_,
OnMouseCaptureChanged());
FOR_EACH_OBSERVER(views::PointerWatcher, move_watchers_,
OnMouseCaptureChanged());
return;
}

// For compatibility with the mus version, don't send drags.
if (event->type() != ui::ET_MOUSE_PRESSED &&
event->type() != ui::ET_MOUSE_RELEASED &&
event->type() != ui::ET_MOUSE_MOVED)
event->type() != ui::ET_MOUSE_MOVED &&
event->type() != ui::ET_MOUSEWHEEL &&
event->type() != ui::ET_MOUSE_CAPTURE_CHANGED)
return;

DCHECK(ui::PointerEvent::CanConvertFrom(*event));
Expand All @@ -73,10 +68,15 @@ void PointerWatcherAdapter::OnTouchEvent(ui::TouchEvent* event) {

gfx::Point PointerWatcherAdapter::GetLocationInScreen(
const ui::LocatedEvent& event) const {
aura::Window* target = static_cast<aura::Window*>(event.target());
gfx::Point location_in_screen = event.location();
aura::client::GetScreenPositionClient(target->GetRootWindow())
->ConvertPointToScreen(target, &location_in_screen);
gfx::Point location_in_screen;
if (event.type() == ui::ET_MOUSE_CAPTURE_CHANGED) {
location_in_screen = display::Screen::GetScreen()->GetCursorScreenPoint();
} else {
aura::Window* target = static_cast<aura::Window*>(event.target());
location_in_screen = event.location();
aura::client::GetScreenPositionClient(target->GetRootWindow())
->ConvertPointToScreen(target, &location_in_screen);
}
return location_in_screen;
}

Expand Down
52 changes: 34 additions & 18 deletions ash/aura/pointer_watcher_adapter_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,41 @@ namespace ash {

using PointerWatcherAdapterTest = test::AshTestBase;

// Records calls to OnPointerEventObserved() in |pointer_event_count_| and
// calls to OnMouseCaptureChanged() to |capture_changed_count_|.
// Records calls to OnPointerEventObserved() in |mouse_wheel_event_count| for a
// mouse wheel event, in |capture_changed_count_| for a mouse capture change
// event and in |pointer_event_count_| for all other pointer events.
class TestPointerWatcher : public views::PointerWatcher {
public:
explicit TestPointerWatcher(bool wants_moves) {
WmShell::Get()->AddPointerWatcher(this, wants_moves);
}
~TestPointerWatcher() override { WmShell::Get()->RemovePointerWatcher(this); }

void ClearCounts() { pointer_event_count_ = capture_changed_count_ = 0; }
void ClearCounts() {
pointer_event_count_ = capture_changed_count_ = mouse_wheel_event_count_ =
0;
}

int pointer_event_count() const { return pointer_event_count_; }
int capture_changed_count() const { return capture_changed_count_; }
int mouse_wheel_event_count() const { return mouse_wheel_event_count_; }

// views::PointerWatcher:
void OnPointerEventObserved(const ui::PointerEvent& event,
const gfx::Point& location_in_screen,
views::Widget* target) override {
pointer_event_count_++;
if (event.type() == ui::ET_POINTER_WHEEL_CHANGED)
mouse_wheel_event_count_++;
else if (event.type() == ui::ET_POINTER_CAPTURE_CHANGED)
capture_changed_count_++;
else
pointer_event_count_++;
}
void OnMouseCaptureChanged() override { capture_changed_count_++; }

private:
int pointer_event_count_ = 0;
int capture_changed_count_ = 0;
int mouse_wheel_event_count_ = 0;

DISALLOW_COPY_AND_ASSIGN(TestPointerWatcher);
};
Expand All @@ -52,15 +62,21 @@ class TestHelper {
// Used to verify call counts.
void ExpectCallCount(int non_move_pointer_event_count,
int non_move_capture_changed_count,
int non_move_mouse_wheel_event_count,
int move_pointer_event_count,
int move_capture_changed_count) {
int move_capture_changed_count,
int move_mouse_wheel_event_count) {
EXPECT_EQ(non_move_pointer_event_count,
non_move_watcher_.pointer_event_count());
EXPECT_EQ(non_move_capture_changed_count,
non_move_watcher_.capture_changed_count());
EXPECT_EQ(non_move_mouse_wheel_event_count,
non_move_watcher_.mouse_wheel_event_count());
EXPECT_EQ(move_pointer_event_count, move_watcher_.pointer_event_count());
EXPECT_EQ(move_capture_changed_count,
move_watcher_.capture_changed_count());
EXPECT_EQ(move_mouse_wheel_event_count,
move_watcher_.mouse_wheel_event_count());

non_move_watcher_.ClearCounts();
move_watcher_.ClearCounts();
Expand All @@ -78,39 +94,39 @@ TEST_F(PointerWatcherAdapterTest, MouseEvents) {

// Move: only the move PointerWatcher should get the event.
GetEventGenerator().MoveMouseTo(gfx::Point(10, 10));
helper.ExpectCallCount(0, 0, 1, 0);
helper.ExpectCallCount(0, 0, 0, 1, 0, 0);

// Press: both.
GetEventGenerator().PressLeftButton();
helper.ExpectCallCount(1, 0, 1, 0);
helper.ExpectCallCount(1, 0, 0, 1, 0, 0);

// Drag: none.
GetEventGenerator().MoveMouseTo(gfx::Point(20, 30));
helper.ExpectCallCount(0, 0, 0, 0);
helper.ExpectCallCount(0, 0, 0, 0, 0, 0);

// Release: both (aura generates a capture event here).
GetEventGenerator().ReleaseLeftButton();
helper.ExpectCallCount(1, 1, 1, 1);
helper.ExpectCallCount(1, 1, 0, 1, 1, 0);

// Exit: none.
GetEventGenerator().SendMouseExit();
helper.ExpectCallCount(0, 0, 0, 0);
helper.ExpectCallCount(0, 0, 0, 0, 0, 0);

// Enter: none.
ui::MouseEvent enter_event(ui::ET_MOUSE_ENTERED, gfx::Point(), gfx::Point(),
ui::EventTimeForNow(), 0, 0);
GetEventGenerator().Dispatch(&enter_event);
helper.ExpectCallCount(0, 0, 0, 0);
helper.ExpectCallCount(0, 0, 0, 0, 0, 0);

// Wheel: none
// Wheel: both
GetEventGenerator().MoveMouseWheel(10, 11);
helper.ExpectCallCount(0, 0, 0, 0);
helper.ExpectCallCount(0, 0, 1, 0, 0, 1);

// Capture: both.
ui::MouseEvent capture_event(ui::ET_MOUSE_CAPTURE_CHANGED, gfx::Point(),
gfx::Point(), ui::EventTimeForNow(), 0, 0);
GetEventGenerator().Dispatch(&capture_event);
helper.ExpectCallCount(0, 1, 0, 1);
helper.ExpectCallCount(0, 1, 0, 0, 1, 0);
}

TEST_F(PointerWatcherAdapterTest, TouchEvents) {
Expand All @@ -119,16 +135,16 @@ TEST_F(PointerWatcherAdapterTest, TouchEvents) {
// Press: both.
const int touch_id = 11;
GetEventGenerator().PressTouchId(touch_id);
helper.ExpectCallCount(1, 0, 1, 0);
helper.ExpectCallCount(1, 0, 0, 1, 0, 0);

// Drag: none.
GetEventGenerator().MoveTouchId(gfx::Point(20, 30), touch_id);
helper.ExpectCallCount(0, 0, 0, 0);
helper.ExpectCallCount(0, 0, 0, 0, 0, 0);

// Release: both (contrary to mouse above, touch does not implicitly generate
// capture).
GetEventGenerator().ReleaseTouchId(touch_id);
helper.ExpectCallCount(1, 0, 1, 0);
helper.ExpectCallCount(1, 0, 0, 1, 0, 0);
}

} // namespace ash
7 changes: 0 additions & 7 deletions ash/shared/immersive_fullscreen_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,6 @@ void ImmersiveFullscreenController::OnPointerEventObserved(
}
}

void ImmersiveFullscreenController::OnMouseCaptureChanged() {
const ui::MouseEvent event(ui::ET_MOUSE_CAPTURE_CHANGED, gfx::Point(),
gfx::Point(), ui::EventTimeForNow(), 0, 0);
OnMouseEvent(event, display::Screen::GetScreen()->GetCursorScreenPoint(),
nullptr);
}

////////////////////////////////////////////////////////////////////////////////
// views::WidgetObserver overrides:

Expand Down
1 change: 0 additions & 1 deletion ash/shared/immersive_fullscreen_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ class ASH_EXPORT ImmersiveFullscreenController
void OnPointerEventObserved(const ui::PointerEvent& event,
const gfx::Point& location_in_screen,
views::Widget* target) override;
void OnMouseCaptureChanged() override;

// views::WidgetObserver overrides:
void OnWidgetDestroying(views::Widget* widget) override;
Expand Down
13 changes: 6 additions & 7 deletions mojo/converters/blink/blink_input_events_type_converters.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ std::unique_ptr<blink::WebInputEvent> BuildWebKeyboardEvent(
}

std::unique_ptr<blink::WebInputEvent> BuildWebMouseWheelEventFrom(
const ui::MouseWheelEvent& event) {
const ui::PointerEvent& event) {
std::unique_ptr<blink::WebMouseWheelEvent> web_event(
new blink::WebMouseWheelEvent);
web_event->type = blink::WebInputEvent::MouseWheel;
Expand All @@ -149,8 +149,8 @@ std::unique_ptr<blink::WebInputEvent> BuildWebMouseWheelEventFrom(
// TODO(rjkroege): Update the following code once Blink supports
// DOM Level 3 wheel events
// (http://www.w3.org/TR/DOM-Level-3-Events/#events-wheelevents)
web_event->deltaX = event.x_offset();
web_event->deltaY = event.y_offset();
web_event->deltaX = event.pointer_details().offset.x();
web_event->deltaY = event.pointer_details().offset.y();

web_event->wheelTicksX = web_event->deltaX / ui::MouseWheelEvent::kWheelDelta;
web_event->wheelTicksY = web_event->deltaY / ui::MouseWheelEvent::kWheelDelta;
Expand Down Expand Up @@ -219,8 +219,7 @@ std::unique_ptr<blink::WebInputEvent> BuildWebTouchEvent(
std::unique_ptr<blink::WebInputEvent>
TypeConverter<std::unique_ptr<blink::WebInputEvent>, ui::Event>::Convert(
const ui::Event& event) {
DCHECK(event.IsKeyEvent() || event.IsPointerEvent() ||
event.IsMouseWheelEvent());
DCHECK(event.IsKeyEvent() || event.IsPointerEvent());
switch (event.type()) {
case ui::ET_POINTER_DOWN:
case ui::ET_POINTER_UP:
Expand All @@ -233,8 +232,8 @@ TypeConverter<std::unique_ptr<blink::WebInputEvent>, ui::Event>::Convert(
return BuildWebTouchEvent(*event.AsPointerEvent());
else
return nullptr;
case ui::ET_MOUSEWHEEL:
return BuildWebMouseWheelEventFrom(*event.AsMouseWheelEvent());
case ui::ET_POINTER_WHEEL_CHANGED:
return BuildWebMouseWheelEventFrom(*event.AsPointerEvent());
case ui::ET_KEY_PRESSED:
case ui::ET_KEY_RELEASED:
return BuildWebKeyboardEvent(*event.AsKeyEvent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ TEST(BlinkInputEventsConvertersTest, WheelEvent) {
ui::MouseEvent(ui::ET_MOUSEWHEEL, gfx::Point(), gfx::Point(),
base::TimeTicks(), 0, 0),
kDeltaX, kDeltaY);
ui::PointerEvent pointer_event(ui_event);
const std::unique_ptr<blink::WebInputEvent> web_event(
TypeConverter<std::unique_ptr<blink::WebInputEvent>, ui::Event>::Convert(
ui_event));
pointer_event));
ASSERT_TRUE(web_event);
ASSERT_EQ(blink::WebInputEvent::MouseWheel, web_event->type);
ASSERT_EQ(0, web_event->modifiers);
Expand Down
28 changes: 7 additions & 21 deletions services/ui/ws/event_dispatcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,6 @@ bool IsLocationInNonclientArea(const ServerWindow* target,
return true;
}

uint32_t PointerId(const ui::LocatedEvent& event) {
if (event.IsPointerEvent())
return event.AsPointerEvent()->pointer_id();
if (event.IsMouseWheelEvent())
return ui::PointerEvent::kMousePointerId;

NOTREACHED();
return 0;
}

} // namespace

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -288,12 +278,9 @@ void EventDispatcher::ProcessEvent(const ui::Event& event,
return;
}

if (event.IsPointerEvent() || event.IsMouseWheelEvent()) {
ProcessLocatedEvent(*event.AsLocatedEvent());
return;
}

NOTREACHED();
DCHECK(event.IsPointerEvent());
ProcessPointerEvent(*event.AsPointerEvent());
return;
}

void EventDispatcher::ProcessKeyEvent(const ui::KeyEvent& event,
Expand All @@ -317,10 +304,9 @@ void EventDispatcher::ProcessKeyEvent(const ui::KeyEvent& event,
EventDispatcherDelegate::AcceleratorPhase::POST);
}

void EventDispatcher::ProcessLocatedEvent(const ui::LocatedEvent& event) {
DCHECK(event.IsPointerEvent() || event.IsMouseWheelEvent());
const bool is_mouse_event =
event.IsMousePointerEvent() || event.IsMouseWheelEvent();
void EventDispatcher::ProcessPointerEvent(const ui::PointerEvent& event) {
DCHECK(event.IsPointerEvent());
const bool is_mouse_event = event.IsMousePointerEvent();

if (is_mouse_event) {
mouse_pointer_last_location_ = event.root_location();
Expand Down Expand Up @@ -348,7 +334,7 @@ void EventDispatcher::ProcessLocatedEvent(const ui::LocatedEvent& event) {
return;
}

const int32_t pointer_id = PointerId(event);
const int32_t pointer_id = event.pointer_id();
if (!IsTrackingPointer(pointer_id) ||
!pointer_targets_[pointer_id].is_pointer_down) {
const bool any_pointers_down = AreAnyPointersDown();
Expand Down
2 changes: 1 addition & 1 deletion services/ui/ws/event_dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class EventDispatcher : public ServerWindowObserver {
// when no buttons on the mouse are down.
// This also generates exit events as appropriate. For example, if the mouse
// moves between one window to another an exit is generated on the first.
void ProcessLocatedEvent(const ui::LocatedEvent& event);
void ProcessPointerEvent(const ui::PointerEvent& event);

// Adds |pointer_target| to |pointer_targets_|.
void StartTrackingPointer(int32_t pointer_id,
Expand Down
8 changes: 2 additions & 6 deletions services/ui/ws/event_dispatcher_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,8 @@ void RunMouseEventTests(EventDispatcher* dispatcher,
const MouseEventTest& test = tests[i];
ASSERT_FALSE(dispatcher_delegate->has_queued_events())
<< " unexpected queued events before running " << i;
if (test.input_event.IsMouseWheelEvent())
dispatcher->ProcessEvent(test.input_event,
EventDispatcher::AcceleratorMatchPhase::ANY);
else
dispatcher->ProcessEvent(ui::PointerEvent(test.input_event),
EventDispatcher::AcceleratorMatchPhase::ANY);
dispatcher->ProcessEvent(ui::PointerEvent(test.input_event),
EventDispatcher::AcceleratorMatchPhase::ANY);

std::unique_ptr<DispatchedEventDetails> details =
dispatcher_delegate->GetAndAdvanceDispatchedEventDetails();
Expand Down
5 changes: 3 additions & 2 deletions services/ui/ws/platform_display.cc
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ void DefaultPlatformDisplay::DispatchEvent(ui::Event* event) {
if (event->IsScrollEvent()) {
// TODO(moshayedi): crbug.com/602859. Dispatch scroll events as
// they are once we have proper support for scroll events.
delegate_->OnEvent(ui::MouseWheelEvent(*event->AsScrollEvent()));
} else if (event->IsMouseEvent() && !event->IsMouseWheelEvent()) {
delegate_->OnEvent(
ui::PointerEvent(ui::MouseWheelEvent(*event->AsScrollEvent())));
} else if (event->IsMouseEvent()) {
delegate_->OnEvent(ui::PointerEvent(*event->AsMouseEvent()));
} else if (event->IsTouchEvent()) {
delegate_->OnEvent(ui::PointerEvent(*event->AsTouchEvent()));
Expand Down
Loading

0 comments on commit 70fa957

Please sign in to comment.