OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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 "athena/wm/bezel_controller.h" | |
6 | |
7 #include "ui/aura/window.h" | |
8 #include "ui/events/event_handler.h" | |
9 | |
10 namespace athena { | |
11 namespace { | |
12 | |
13 // Using bezel swipes, the first touch that is registered is usually within | |
14 // 5-10 pixels from the edge, but sometimes as far as 29 pixels away. | |
15 // So setting this width fairly high for now. | |
16 const float kBezelWidth = 20.0f; | |
17 | |
18 const float kScrollPositionNone = -100; | |
19 | |
20 bool ShouldProcessGesture(ui::EventType event_type) { | |
21 return event_type == ui::ET_GESTURE_SCROLL_UPDATE || | |
22 event_type == ui::ET_GESTURE_SCROLL_BEGIN || | |
23 event_type == ui::ET_GESTURE_BEGIN; | |
24 } | |
25 | |
26 } // namespace | |
27 | |
28 BezelController::BezelController(aura::Window* container) | |
29 : container_(container), | |
30 num_fingers_down_(0), | |
31 state_(NO_FINGERS_DOWN), | |
32 scroll_bezel_(BEZEL_NONE), | |
33 left_right_delegate_(NULL) { | |
34 } | |
35 | |
36 float BezelController::GetDistance(const gfx::PointF& position, | |
37 BezelController::Bezel bezel) { | |
38 DCHECK(bezel == BEZEL_LEFT || bezel == BEZEL_RIGHT); | |
39 return bezel == BEZEL_LEFT | |
40 ? position.x() | |
41 : position.x() - container_->GetBoundsInScreen().width(); | |
42 } | |
43 | |
44 void BezelController::SetState(BezelController::State state, | |
45 const gfx::PointF& scroll_position) { | |
46 if (!left_right_delegate_ || state == state_) | |
47 return; | |
48 | |
49 if (state == BEZEL_SCROLLING_TWO_FINGERS) { | |
50 float delta = GetDistance(scroll_position, scroll_bezel_); | |
51 left_right_delegate_->ScrollBegin(scroll_bezel_, delta); | |
52 } else if (state_ == BEZEL_SCROLLING_TWO_FINGERS) { | |
53 left_right_delegate_->ScrollEnd(); | |
54 } | |
55 state_ = state; | |
56 if (state_ == IGNORE || state == NO_FINGERS_DOWN) | |
57 scroll_bezel_ = BEZEL_NONE; | |
58 } | |
59 | |
60 // Only implemented for LEFT and RIGHT bezels ATM. | |
61 BezelController::Bezel BezelController::GetBezel(const gfx::PointF& location) { | |
62 if (location.x() < kBezelWidth) { | |
63 return BEZEL_LEFT; | |
64 } else if (location.x() > | |
65 container_->GetBoundsInScreen().width() - kBezelWidth) { | |
66 return BEZEL_RIGHT; | |
67 } else { | |
68 return BEZEL_NONE; | |
69 } | |
70 } | |
71 | |
72 void BezelController::OnTouchEvent(ui::TouchEvent* event) { | |
73 if (!left_right_delegate_) | |
74 return; | |
75 | |
76 // TODO (mfomitchev): Currently we aren't retargetting or consuming any of the | |
77 // touch events. This means that content can prevent the generation of gesture | |
78 // events and two-finger scroll won't work. Possible solution to this problem | |
79 // is hosting our own gesture recognizer or retargetting touch events at the | |
80 // bezel. | |
81 | |
82 ui::EventType type = event->type(); | |
83 if (type == ui::ET_TOUCH_PRESSED) { | |
84 num_fingers_down_++; | |
85 BezelController::Bezel event_bezel = GetBezel(event->location_f()); | |
86 | |
87 if (num_fingers_down_ > 2 || | |
88 (event_bezel != BEZEL_LEFT && event_bezel != BEZEL_RIGHT)) { | |
89 SetState(IGNORE, gfx::Point(kScrollPositionNone, kScrollPositionNone)); | |
90 return; | |
91 } | |
92 | |
93 switch (state_) { | |
94 case NO_FINGERS_DOWN: | |
95 SetState(BEZEL_GESTURE_STARTED, | |
96 gfx::Point(kScrollPositionNone, kScrollPositionNone)); | |
97 scroll_bezel_ = event_bezel; | |
98 return; | |
99 case IGNORE: | |
100 case BEZEL_GESTURE_STARTED: | |
101 return; | |
102 case BEZEL_SCROLLING_ONE_FINGER: | |
103 if (event_bezel != scroll_bezel_) | |
104 SetState(IGNORE, | |
105 gfx::Point(kScrollPositionNone, kScrollPositionNone)); | |
106 return; | |
107 case BEZEL_SCROLLING_TWO_FINGERS: | |
108 // Should've exited above | |
109 NOTREACHED(); | |
110 return; | |
111 } | |
112 } else if (type == ui::ET_TOUCH_RELEASED || type == ui::ET_TOUCH_CANCELLED) { | |
113 num_fingers_down_--; | |
114 DCHECK_NE(state_, NO_FINGERS_DOWN); | |
115 // The finger beeing lifted gets counted for touch_points. | |
116 if (num_fingers_down_ == 0) { | |
117 SetState(NO_FINGERS_DOWN, | |
118 gfx::Point(kScrollPositionNone, kScrollPositionNone)); | |
119 } else { | |
120 // Lifting fingers means the user is doing a gesture other than 2-finger | |
121 // scrolling. | |
122 SetState(IGNORE, | |
123 gfx::Point(kScrollPositionNone, kScrollPositionNone)); | |
124 } | |
125 } | |
126 } | |
sadrul
2014/07/22 22:34:16
Can you look into not overriding OnTouchEvent(), a
mfomitchev
2014/07/22 22:51:26
I tried doing that at first, but there is a a coup
sadrul
2014/07/23 16:56:19
If the location of the GESTURE_BEGIN event is not
mfomitchev
2014/07/23 21:51:54
Done.
| |
127 | |
128 void BezelController::OnGestureEvent(ui::GestureEvent* event) { | |
129 if (!left_right_delegate_) | |
130 return; | |
131 | |
132 ui::EventType type = event->type(); | |
133 if (!ShouldProcessGesture(type)) | |
134 return; | |
135 | |
136 const gfx::PointF& event_location = event->location_f(); | |
137 const ui::GestureEventDetails& event_details = event->details(); | |
138 | |
139 if (type == ui::ET_GESTURE_BEGIN && event_details.touch_points() == 2 && | |
140 state_ == BEZEL_SCROLLING_ONE_FINGER) { | |
141 SetState(BEZEL_SCROLLING_TWO_FINGERS, event_location); | |
142 } else if (type == ui::ET_GESTURE_SCROLL_BEGIN) { | |
143 DCHECK(state_ == IGNORE || state_ == BEZEL_GESTURE_STARTED); | |
144 if (state_ != BEZEL_GESTURE_STARTED) | |
145 return; | |
146 | |
147 if (num_fingers_down_ == 1) { | |
148 SetState(BEZEL_SCROLLING_ONE_FINGER, event_location); | |
149 return; | |
150 } | |
151 | |
152 DCHECK_EQ(num_fingers_down_, 2); | |
153 SetState(BEZEL_SCROLLING_TWO_FINGERS, event_location); | |
154 if (left_right_delegate_->CanScroll()) { | |
155 event->SetHandled(); | |
156 event->StopPropagation(); | |
157 } | |
158 } else if (type == ui::ET_GESTURE_SCROLL_UPDATE) { | |
159 if (state_ != BEZEL_SCROLLING_TWO_FINGERS) | |
160 return; | |
161 | |
162 float scroll_delta = GetDistance(event_location, scroll_bezel_); | |
163 left_right_delegate_->ScrollUpdate(scroll_delta); | |
164 if (left_right_delegate_->CanScroll()) { | |
165 event->SetHandled(); | |
166 event->StopPropagation(); | |
167 } | |
168 } | |
169 } | |
170 | |
171 } // namespace athena | |
OLD | NEW |