OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "athena/wm/bezel_controller.h" | 5 #include "athena/wm/bezel_controller.h" |
6 | 6 |
7 #include "ui/aura/window.h" | 7 #include "ui/aura/window.h" |
8 #include "ui/events/event_handler.h" | 8 #include "ui/events/event_handler.h" |
9 #include "ui/gfx/display.h" | |
10 #include "ui/gfx/geometry/point_conversions.h" | |
11 #include "ui/gfx/screen.h" | |
12 #include "ui/wm/core/coordinate_conversion.h" | |
13 | 9 |
14 namespace athena { | 10 namespace athena { |
15 namespace { | 11 namespace { |
16 | 12 |
17 // Using bezel swipes, the first touch that is registered is usually within | 13 // Using bezel swipes, the first touch that is registered is usually within |
18 // 5-10 pixels from the edge, but sometimes as far as 29 pixels away. | 14 // 5-10 pixels from the edge, but sometimes as far as 29 pixels away. |
19 // So setting this width fairly high for now. | 15 // So setting this width fairly high for now. |
20 const float kBezelWidth = 20.0f; | 16 const float kBezelWidth = 20.0f; |
21 | 17 |
22 const float kScrollDeltaNone = 0; | 18 const float kScrollPositionNone = -100; |
23 | 19 |
24 bool ShouldProcessGesture(ui::EventType event_type) { | 20 bool ShouldProcessGesture(ui::EventType event_type) { |
25 return event_type == ui::ET_GESTURE_SCROLL_UPDATE || | 21 return event_type == ui::ET_GESTURE_SCROLL_UPDATE || |
26 event_type == ui::ET_GESTURE_SCROLL_BEGIN || | 22 event_type == ui::ET_GESTURE_SCROLL_BEGIN || |
27 event_type == ui::ET_GESTURE_BEGIN || | 23 event_type == ui::ET_GESTURE_BEGIN || |
28 event_type == ui::ET_GESTURE_END; | 24 event_type == ui::ET_GESTURE_END; |
29 } | 25 } |
30 | 26 |
31 gfx::Display GetDisplay(aura::Window* window) { | |
32 gfx::Screen* screen = gfx::Screen::GetScreenFor(window); | |
33 return screen->GetDisplayNearestWindow(window); | |
34 } | |
35 | |
36 float GetDistance(const gfx::PointF& location, | |
37 aura::Window* window, | |
38 BezelController::Bezel bezel) { | |
39 DCHECK(bezel == BezelController::BEZEL_LEFT || | |
40 bezel == BezelController::BEZEL_RIGHT); | |
41 // Convert location from window coordinates to screen coordinates. | |
42 gfx::Point point_in_screen(gfx::ToRoundedPoint(location)); | |
43 wm::ConvertPointToScreen(window, &point_in_screen); | |
44 return bezel == BezelController::BEZEL_LEFT | |
45 ? point_in_screen.x() | |
46 : point_in_screen.x() - GetDisplay(window).bounds().width(); | |
47 } | |
48 | |
49 } // namespace | 27 } // namespace |
50 | 28 |
51 BezelController::BezelController(aura::Window* container) | 29 BezelController::BezelController(aura::Window* container) |
52 : container_(container), | 30 : container_(container), |
53 state_(NONE), | 31 state_(NONE), |
54 scroll_bezel_(BEZEL_NONE), | 32 scroll_bezel_(BEZEL_NONE), |
55 scroll_target_(NULL), | 33 scroll_target_(NULL), |
56 left_right_delegate_(NULL) { | 34 left_right_delegate_(NULL) { |
57 } | 35 } |
58 | 36 |
59 void BezelController::SetState(BezelController::State state) { | 37 float BezelController::GetDistance(const gfx::PointF& position, |
60 // Use SetState(State, float) if |state| is one of the BEZEL_SCROLLING states. | 38 BezelController::Bezel bezel) { |
61 DCHECK_NE(state, BEZEL_SCROLLING_TWO_FINGERS); | 39 DCHECK(bezel == BEZEL_LEFT || bezel == BEZEL_RIGHT); |
62 DCHECK_NE(state, BEZEL_SCROLLING_ONE_FINGER); | 40 return bezel == BEZEL_LEFT |
63 SetState(state, kScrollDeltaNone); | 41 ? position.x() |
| 42 : position.x() - container_->GetBoundsInScreen().width(); |
64 } | 43 } |
65 | 44 |
66 void BezelController::SetState(BezelController::State state, | 45 void BezelController::SetState(BezelController::State state, |
67 float scroll_delta) { | 46 const gfx::PointF& scroll_position) { |
68 if (!left_right_delegate_ || state == state_) | 47 if (!left_right_delegate_ || state == state_) |
69 return; | 48 return; |
70 | 49 |
71 if (state == BEZEL_SCROLLING_TWO_FINGERS) | 50 if (state == BEZEL_SCROLLING_TWO_FINGERS) { |
72 left_right_delegate_->ScrollBegin(scroll_bezel_, scroll_delta); | 51 float delta = GetDistance(scroll_position, scroll_bezel_); |
73 else if (state_ == BEZEL_SCROLLING_TWO_FINGERS) | 52 left_right_delegate_->ScrollBegin(scroll_bezel_, delta); |
| 53 } else if (state_ == BEZEL_SCROLLING_TWO_FINGERS) { |
74 left_right_delegate_->ScrollEnd(); | 54 left_right_delegate_->ScrollEnd(); |
| 55 } |
75 state_ = state; | 56 state_ = state; |
76 if (state == NONE) { | 57 if (state == NONE) { |
77 scroll_bezel_ = BEZEL_NONE; | 58 scroll_bezel_ = BEZEL_NONE; |
78 scroll_target_ = NULL; | 59 scroll_target_ = NULL; |
79 } | 60 } |
80 } | 61 } |
81 | 62 |
82 // Only implemented for LEFT and RIGHT bezels ATM. | 63 // Only implemented for LEFT and RIGHT bezels ATM. |
83 BezelController::Bezel BezelController::GetBezel(const gfx::PointF& location) { | 64 BezelController::Bezel BezelController::GetBezel(const gfx::PointF& location) { |
84 int screen_width = GetDisplay(container_).bounds().width(); | |
85 if (location.x() < kBezelWidth) { | 65 if (location.x() < kBezelWidth) { |
86 return BEZEL_LEFT; | 66 return BEZEL_LEFT; |
87 } else if (location.x() > screen_width - kBezelWidth) { | 67 } else if (location.x() > |
| 68 container_->GetBoundsInScreen().width() - kBezelWidth) { |
88 return BEZEL_RIGHT; | 69 return BEZEL_RIGHT; |
89 } else { | 70 } else { |
90 return BEZEL_NONE; | 71 return BEZEL_NONE; |
91 } | 72 } |
92 } | 73 } |
93 | 74 |
94 void BezelController::OnGestureEvent(ui::GestureEvent* event) { | 75 void BezelController::OnGestureEvent(ui::GestureEvent* event) { |
95 // TODO(mfomitchev): Currently we aren't retargetting or consuming any of the | 76 // TODO (mfomitchev): Currently we aren't retargetting or consuming any of the |
96 // touch events. This means that content can prevent the generation of gesture | 77 // touch events. This means that content can prevent the generation of gesture |
97 // events and two-finger scroll won't work. Possible solution to this problem | 78 // events and two-finger scroll won't work. Possible solution to this problem |
98 // is hosting our own gesture recognizer or retargetting touch events at the | 79 // is hosting our own gesture recognizer or retargetting touch events at the |
99 // bezel. | 80 // bezel. |
100 | 81 |
101 if (!left_right_delegate_) | 82 if (!left_right_delegate_) |
102 return; | 83 return; |
103 | 84 |
104 ui::EventType type = event->type(); | 85 ui::EventType type = event->type(); |
105 if (!ShouldProcessGesture(type)) | 86 if (!ShouldProcessGesture(type)) |
106 return; | 87 return; |
107 | 88 |
108 if (scroll_target_ && event->target() != scroll_target_) | 89 if (scroll_target_ && event->target() != scroll_target_) |
109 return; | 90 return; |
110 | 91 |
111 const gfx::PointF& event_location = event->location_f(); | 92 const gfx::PointF& event_location = event->location_f(); |
112 const ui::GestureEventDetails& event_details = event->details(); | 93 const ui::GestureEventDetails& event_details = event->details(); |
113 int num_touch_points = event_details.touch_points(); | 94 int num_touch_points = event_details.touch_points(); |
114 float scroll_delta = kScrollDeltaNone; | |
115 if (scroll_bezel_ != BEZEL_NONE) { | |
116 aura::Window* target_window = static_cast<aura::Window*>(event->target()); | |
117 scroll_delta = GetDistance(event_location, target_window, scroll_bezel_); | |
118 } | |
119 | 95 |
120 if (type == ui::ET_GESTURE_BEGIN) { | 96 if (type == ui::ET_GESTURE_BEGIN) { |
121 if (num_touch_points > 2) { | 97 if (num_touch_points > 2) { |
122 SetState(IGNORE_CURRENT_SCROLL); | 98 SetState(IGNORE_CURRENT_SCROLL, |
| 99 gfx::Point(kScrollPositionNone, kScrollPositionNone)); |
123 return; | 100 return; |
124 } | 101 } |
125 BezelController::Bezel event_bezel = GetBezel(event->location_f()); | 102 BezelController::Bezel event_bezel = GetBezel(event->location_f()); |
126 switch (state_) { | 103 switch (state_) { |
127 case NONE: | 104 case NONE: |
128 scroll_bezel_ = event_bezel; | 105 scroll_bezel_ = event_bezel; |
129 scroll_target_ = event->target(); | 106 scroll_target_ = event->target(); |
130 if (event_bezel != BEZEL_LEFT && event_bezel != BEZEL_RIGHT) | 107 if (event_bezel != BEZEL_LEFT && event_bezel != BEZEL_RIGHT) { |
131 SetState(IGNORE_CURRENT_SCROLL); | 108 SetState(IGNORE_CURRENT_SCROLL, |
132 else | 109 gfx::Point(kScrollPositionNone, kScrollPositionNone)); |
133 SetState(BEZEL_GESTURE_STARTED); | 110 } else { |
| 111 SetState(BEZEL_GESTURE_STARTED, event_location); |
| 112 } |
134 break; | 113 break; |
135 case IGNORE_CURRENT_SCROLL: | 114 case IGNORE_CURRENT_SCROLL: |
136 break; | 115 break; |
137 case BEZEL_GESTURE_STARTED: | 116 case BEZEL_GESTURE_STARTED: |
138 case BEZEL_SCROLLING_ONE_FINGER: | 117 case BEZEL_SCROLLING_ONE_FINGER: |
139 DCHECK_EQ(num_touch_points, 2); | 118 DCHECK_EQ(num_touch_points, 2); |
140 DCHECK(scroll_target_); | 119 DCHECK(scroll_target_); |
141 DCHECK_NE(scroll_bezel_, BEZEL_NONE); | 120 DCHECK_NE(scroll_bezel_, BEZEL_NONE); |
142 | 121 |
143 if (event_bezel != scroll_bezel_) { | 122 if (event_bezel != scroll_bezel_) { |
144 SetState(IGNORE_CURRENT_SCROLL); | 123 SetState(IGNORE_CURRENT_SCROLL, |
| 124 gfx::Point(kScrollPositionNone, kScrollPositionNone)); |
145 return; | 125 return; |
146 } | 126 } |
147 if (state_ == BEZEL_SCROLLING_ONE_FINGER) | 127 if (state_ == BEZEL_SCROLLING_ONE_FINGER) |
148 SetState(BEZEL_SCROLLING_TWO_FINGERS); | 128 SetState(BEZEL_SCROLLING_TWO_FINGERS, event_location); |
149 break; | 129 break; |
150 case BEZEL_SCROLLING_TWO_FINGERS: | 130 case BEZEL_SCROLLING_TWO_FINGERS: |
151 // Should've exited above | 131 // Should've exited above |
152 NOTREACHED(); | 132 NOTREACHED(); |
153 break; | 133 break; |
154 } | 134 } |
155 } else if (type == ui::ET_GESTURE_END) { | 135 } else if (type == ui::ET_GESTURE_END) { |
156 if (state_ == NONE) | 136 if (state_ == NONE) |
157 return; | 137 return; |
158 | 138 |
159 CHECK(scroll_target_); | 139 CHECK(scroll_target_); |
160 if (num_touch_points == 1) { | 140 if (num_touch_points == 1) { |
161 SetState(NONE); | 141 SetState(NONE, gfx::Point(kScrollPositionNone, kScrollPositionNone)); |
162 } else { | 142 } else { |
163 SetState(IGNORE_CURRENT_SCROLL); | 143 SetState(IGNORE_CURRENT_SCROLL, |
| 144 gfx::Point(kScrollPositionNone, kScrollPositionNone)); |
164 } | 145 } |
165 } else if (type == ui::ET_GESTURE_SCROLL_BEGIN) { | 146 } else if (type == ui::ET_GESTURE_SCROLL_BEGIN) { |
166 DCHECK(state_ == IGNORE_CURRENT_SCROLL || state_ == BEZEL_GESTURE_STARTED); | 147 DCHECK(state_ == IGNORE_CURRENT_SCROLL || state_ == BEZEL_GESTURE_STARTED); |
167 if (state_ != BEZEL_GESTURE_STARTED) | 148 if (state_ != BEZEL_GESTURE_STARTED) |
168 return; | 149 return; |
169 | 150 |
170 if (num_touch_points == 1) { | 151 if (num_touch_points == 1) { |
171 SetState(BEZEL_SCROLLING_ONE_FINGER, scroll_delta); | 152 SetState(BEZEL_SCROLLING_ONE_FINGER, event_location); |
172 return; | 153 return; |
173 } | 154 } |
174 | 155 |
175 DCHECK_EQ(num_touch_points, 2); | 156 DCHECK_EQ(num_touch_points, 2); |
176 SetState(BEZEL_SCROLLING_TWO_FINGERS, scroll_delta); | 157 SetState(BEZEL_SCROLLING_TWO_FINGERS, event_location); |
177 if (left_right_delegate_->CanScroll()) | 158 if (left_right_delegate_->CanScroll()) |
178 event->SetHandled(); | 159 event->SetHandled(); |
179 } else if (type == ui::ET_GESTURE_SCROLL_UPDATE) { | 160 } else if (type == ui::ET_GESTURE_SCROLL_UPDATE) { |
180 if (state_ != BEZEL_SCROLLING_TWO_FINGERS) | 161 if (state_ != BEZEL_SCROLLING_TWO_FINGERS) |
181 return; | 162 return; |
182 | 163 |
| 164 float scroll_delta = GetDistance(event_location, scroll_bezel_); |
183 left_right_delegate_->ScrollUpdate(scroll_delta); | 165 left_right_delegate_->ScrollUpdate(scroll_delta); |
184 if (left_right_delegate_->CanScroll()) | 166 if (left_right_delegate_->CanScroll()) |
185 event->SetHandled(); | 167 event->SetHandled(); |
186 } | 168 } |
187 } | 169 } |
188 | 170 |
189 } // namespace athena | 171 } // namespace athena |
OLD | NEW |