blob: c292499ca1c0576de5da4161a9d639ee6ea8a1c9 [file] [log] [blame]
Ryan Daum6fc68b8c2018-01-19 16:27:181// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chromecast/graphics/cast_system_gesture_event_handler.h"
6
7#include "ui/aura/window.h"
8#include "ui/display/display.h"
9#include "ui/display/screen.h"
10#include "ui/events/event.h"
11#include "ui/gfx/geometry/point.h"
12#include "ui/gfx/geometry/rect.h"
13#include "ui/wm/core/coordinate_conversion.h"
14
15namespace chromecast {
16
17namespace {
18
19// The number of pixels from the very left or right of the screen to consider as
20// a valid origin for the left or right swipe gesture.
21constexpr int kSideGestureStartWidth = 10;
22
23// The number of pixels from the very top or bottom of the screen to consider as
24// a valid origin for the top or bottom swipe gesture.
25constexpr int kSideGestureStartHeight = 10;
26
27} // namespace
28
29CastSystemGestureEventHandler::CastSystemGestureEventHandler(
30 aura::Window* root_window)
31 : EventHandler(),
32 root_window_(root_window),
33 current_swipe_(CastSideSwipeOrigin::NONE) {
34 DCHECK(root_window);
35 root_window->AddPreTargetHandler(this);
36}
37
38CastSystemGestureEventHandler::~CastSystemGestureEventHandler() {
39 DCHECK(swipe_gesture_handlers_.empty());
40 root_window_->RemovePreTargetHandler(this);
41}
42
43CastSideSwipeOrigin CastSystemGestureEventHandler::GetDragPosition(
44 const gfx::Point& point,
45 const gfx::Rect& screen_bounds) const {
46 if (point.y() < (screen_bounds.y() + kSideGestureStartHeight)) {
47 return CastSideSwipeOrigin::TOP;
48 }
49 if (point.x() < (screen_bounds.x() + kSideGestureStartWidth)) {
50 return CastSideSwipeOrigin::LEFT;
51 }
52 if (point.x() >
53 (screen_bounds.x() + screen_bounds.width() - kSideGestureStartWidth)) {
54 return CastSideSwipeOrigin::RIGHT;
55 }
56 if (point.y() >
57 (screen_bounds.y() + screen_bounds.height() - kSideGestureStartHeight)) {
58 return CastSideSwipeOrigin::BOTTOM;
59 }
60 return CastSideSwipeOrigin::NONE;
61}
62
63void CastSystemGestureEventHandler::OnGestureEvent(ui::GestureEvent* event) {
64 gfx::Point gesture_location(event->location());
65 aura::Window* target = static_cast<aura::Window*>(event->target());
66
67 // Convert the event's point to the point on the physical screen.
68 // For cast on root window this is likely going to be identical, but put it
69 // through the ScreenPositionClient just to be sure.
70 ::wm::ConvertPointToScreen(target, &gesture_location);
71 gfx::Rect screen_bounds = display::Screen::GetScreen()
72 ->GetDisplayNearestPoint(gesture_location)
73 .bounds();
74 CastSideSwipeOrigin side_swipe_origin =
75 GetDragPosition(gesture_location, screen_bounds);
76 if (side_swipe_origin != CastSideSwipeOrigin::NONE ||
77 current_swipe_ != CastSideSwipeOrigin::NONE) {
78 switch (event->type()) {
79 case ui::ET_GESTURE_SCROLL_BEGIN:
80 current_swipe_ = side_swipe_origin;
81 for (auto* side_swipe_handler : swipe_gesture_handlers_) {
82 side_swipe_handler->OnSideSwipeBegin(side_swipe_origin, event);
83 }
84 break;
85 case ui::ET_GESTURE_SCROLL_END:
86 case ui::ET_SCROLL_FLING_START:
87 for (auto* side_swipe_handler : swipe_gesture_handlers_) {
88 side_swipe_handler->OnSideSwipeEnd(current_swipe_, event);
89 }
90 current_swipe_ = CastSideSwipeOrigin::NONE;
91 break;
92 default:
93 break;
94 }
95 }
96}
97
98void CastSystemGestureEventHandler::AddSideSwipeGestureHandler(
99 CastSideSwipeGestureHandlerInterface* handler) {
100 swipe_gesture_handlers_.push_back(handler);
101}
102
103void CastSystemGestureEventHandler::RemoveSideSwipeGestureHandler(
104 CastSideSwipeGestureHandlerInterface* handler) {
105 auto find = std::find(swipe_gesture_handlers_.begin(),
106 swipe_gesture_handlers_.end(), handler);
107 if (find != swipe_gesture_handlers_.end())
108 swipe_gesture_handlers_.erase(find);
109}
110
111} // namespace chromecast