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" | 9 #include "ui/gfx/display.h" |
10 #include "ui/gfx/geometry/point_conversions.h" | 10 #include "ui/gfx/geometry/point_conversions.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 DCHECK(bezel == BezelController::BEZEL_LEFT || | 39 DCHECK(bezel == BezelController::BEZEL_LEFT || |
40 bezel == BezelController::BEZEL_RIGHT); | 40 bezel == BezelController::BEZEL_RIGHT); |
41 // Convert location from window coordinates to screen coordinates. | 41 // Convert location from window coordinates to screen coordinates. |
42 gfx::Point point_in_screen(gfx::ToRoundedPoint(location)); | 42 gfx::Point point_in_screen(gfx::ToRoundedPoint(location)); |
43 wm::ConvertPointToScreen(window, &point_in_screen); | 43 wm::ConvertPointToScreen(window, &point_in_screen); |
44 return bezel == BezelController::BEZEL_LEFT | 44 return bezel == BezelController::BEZEL_LEFT |
45 ? point_in_screen.x() | 45 ? point_in_screen.x() |
46 : point_in_screen.x() - GetDisplay(window).bounds().width(); | 46 : point_in_screen.x() - GetDisplay(window).bounds().width(); |
47 } | 47 } |
48 | 48 |
49 // Returns the bezel corresponding to the |location| in |window| or BEZEL_NONE | |
50 // if the location is outside of the bezel area. | |
51 // Only implemented for LEFT and RIGHT bezels. | |
52 BezelController::Bezel GetBezel(const gfx::PointF& location, | |
53 aura::Window* window) { | |
54 int screen_width = GetDisplay(window).bounds().width(); | |
55 gfx::Point point_in_screen(gfx::ToRoundedPoint(location)); | |
56 wm::ConvertPointToScreen(window, &point_in_screen); | |
57 if (point_in_screen.x() < kBezelWidth) | |
58 return BezelController::BEZEL_LEFT; | |
59 if (point_in_screen.x() > screen_width - kBezelWidth) | |
60 return BezelController::BEZEL_RIGHT; | |
61 return BezelController::BEZEL_NONE; | |
62 } | |
63 | |
64 } // namespace | 49 } // namespace |
65 | 50 |
66 BezelController::BezelController(aura::Window* container) | 51 BezelController::BezelController(aura::Window* container) |
67 : container_(container), | 52 : container_(container), |
68 state_(NONE), | 53 state_(NONE), |
69 scroll_bezel_(BEZEL_NONE), | 54 scroll_bezel_(BEZEL_NONE), |
70 scroll_target_(NULL), | 55 scroll_target_(NULL), |
71 left_right_delegate_(NULL) { | 56 left_right_delegate_(NULL) { |
72 } | 57 } |
73 | 58 |
74 void BezelController::SetState(BezelController::State state) { | 59 void BezelController::SetState(BezelController::State state) { |
75 // Use SetState(State, float) if |state| is one of the BEZEL_SCROLLING states. | 60 // Use SetState(State, float) if |state| is one of the BEZEL_SCROLLING states. |
76 DCHECK_NE(state, BEZEL_SCROLLING_TWO_FINGERS); | 61 DCHECK_NE(state, BEZEL_SCROLLING_TWO_FINGERS); |
77 DCHECK_NE(state, BEZEL_SCROLLING_ONE_FINGER); | 62 DCHECK_NE(state, BEZEL_SCROLLING_ONE_FINGER); |
78 SetState(state, kScrollDeltaNone); | 63 SetState(state, kScrollDeltaNone); |
79 } | 64 } |
80 | 65 |
81 void BezelController::SetState(BezelController::State state, | 66 void BezelController::SetState(BezelController::State state, |
82 float scroll_delta) { | 67 float scroll_delta) { |
83 if (!left_right_delegate_ || state == state_) | 68 if (!left_right_delegate_ || state == state_) |
84 return; | 69 return; |
85 | 70 |
86 if (state == BEZEL_SCROLLING_TWO_FINGERS) | 71 if (state == BEZEL_SCROLLING_TWO_FINGERS) |
87 left_right_delegate_->BezelScrollBegin(scroll_bezel_, scroll_delta); | 72 left_right_delegate_->ScrollBegin(scroll_bezel_, scroll_delta); |
88 else if (state_ == BEZEL_SCROLLING_TWO_FINGERS) | 73 else if (state_ == BEZEL_SCROLLING_TWO_FINGERS) |
89 left_right_delegate_->BezelScrollEnd(); | 74 left_right_delegate_->ScrollEnd(); |
90 state_ = state; | 75 state_ = state; |
91 if (state == NONE) { | 76 if (state == NONE) { |
92 scroll_bezel_ = BEZEL_NONE; | 77 scroll_bezel_ = BEZEL_NONE; |
93 scroll_target_ = NULL; | 78 scroll_target_ = NULL; |
94 } | 79 } |
95 } | 80 } |
96 | 81 |
| 82 // Only implemented for LEFT and RIGHT bezels ATM. |
| 83 BezelController::Bezel BezelController::GetBezel(const gfx::PointF& location) { |
| 84 int screen_width = GetDisplay(container_).bounds().width(); |
| 85 if (location.x() < kBezelWidth) { |
| 86 return BEZEL_LEFT; |
| 87 } else if (location.x() > screen_width - kBezelWidth) { |
| 88 return BEZEL_RIGHT; |
| 89 } else { |
| 90 return BEZEL_NONE; |
| 91 } |
| 92 } |
| 93 |
97 void BezelController::OnGestureEvent(ui::GestureEvent* event) { | 94 void BezelController::OnGestureEvent(ui::GestureEvent* event) { |
98 // TODO(mfomitchev): Currently we aren't retargetting or consuming any of the | 95 // TODO(mfomitchev): Currently we aren't retargetting or consuming any of the |
99 // touch events. This means that content can prevent the generation of gesture | 96 // touch events. This means that content can prevent the generation of gesture |
100 // events and two-finger scroll won't work. Possible solution to this problem | 97 // events and two-finger scroll won't work. Possible solution to this problem |
101 // is hosting our own gesture recognizer or retargetting touch events at the | 98 // is hosting our own gesture recognizer or retargetting touch events at the |
102 // bezel. | 99 // bezel. |
103 | 100 |
104 if (!left_right_delegate_) | 101 if (!left_right_delegate_) |
105 return; | 102 return; |
106 | 103 |
107 ui::EventType type = event->type(); | 104 ui::EventType type = event->type(); |
108 if (!ShouldProcessGesture(type)) | 105 if (!ShouldProcessGesture(type)) |
109 return; | 106 return; |
110 | 107 |
111 const ui::GestureEventDetails& event_details = event->details(); | |
112 int num_touch_points = event_details.touch_points(); | |
113 if (num_touch_points == 1 && type == ui::ET_GESTURE_BEGIN) { | |
114 // Reset the state when the first finger touches and starts a gesture. | |
115 // Normally, the state gets reset when the last finger is lifted and we | |
116 // receive ET_GESTURE_END. However ET_GESTURE_END doesn't always get | |
117 // dispatched. (E.g. if the gesture target was hidden or deleted). | |
118 // Since we can't rely on receiving ET_GESTURE_END when the last finger is | |
119 // lifted, we also reset the state on ET_GESTURE_BEGIN when the first | |
120 // finger touches the screen. | |
121 SetState(NONE); | |
122 } | |
123 | |
124 if (scroll_target_ && event->target() != scroll_target_) | 108 if (scroll_target_ && event->target() != scroll_target_) |
125 return; | 109 return; |
126 | 110 |
127 const gfx::PointF& event_location = event->location_f(); | 111 const gfx::PointF& event_location = event->location_f(); |
| 112 const ui::GestureEventDetails& event_details = event->details(); |
| 113 int num_touch_points = event_details.touch_points(); |
128 float scroll_delta = kScrollDeltaNone; | 114 float scroll_delta = kScrollDeltaNone; |
129 aura::Window* target_window = static_cast<aura::Window*>(event->target()); | 115 if (scroll_bezel_ != BEZEL_NONE) { |
130 if (scroll_bezel_ != BEZEL_NONE) | 116 aura::Window* target_window = static_cast<aura::Window*>(event->target()); |
131 scroll_delta = GetDistance(event_location, target_window, scroll_bezel_); | 117 scroll_delta = GetDistance(event_location, target_window, scroll_bezel_); |
| 118 } |
132 | 119 |
133 if (type == ui::ET_GESTURE_BEGIN) { | 120 if (type == ui::ET_GESTURE_BEGIN) { |
134 if (num_touch_points > 2) { | 121 if (num_touch_points > 2) { |
135 SetState(IGNORE_CURRENT_SCROLL); | 122 SetState(IGNORE_CURRENT_SCROLL); |
136 return; | 123 return; |
137 } | 124 } |
138 BezelController::Bezel event_bezel = | 125 BezelController::Bezel event_bezel = GetBezel(event->location_f()); |
139 GetBezel(event->location_f(), target_window); | |
140 switch (state_) { | 126 switch (state_) { |
141 case NONE: | 127 case NONE: |
142 scroll_bezel_ = event_bezel; | 128 scroll_bezel_ = event_bezel; |
143 scroll_target_ = event->target(); | 129 scroll_target_ = event->target(); |
144 if (event_bezel != BEZEL_LEFT && event_bezel != BEZEL_RIGHT) | 130 if (event_bezel != BEZEL_LEFT && event_bezel != BEZEL_RIGHT) |
145 SetState(IGNORE_CURRENT_SCROLL); | 131 SetState(IGNORE_CURRENT_SCROLL); |
146 else | 132 else |
147 SetState(BEZEL_GESTURE_STARTED); | 133 SetState(BEZEL_GESTURE_STARTED); |
148 break; | 134 break; |
149 case IGNORE_CURRENT_SCROLL: | 135 case IGNORE_CURRENT_SCROLL: |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 if (state_ != BEZEL_GESTURE_STARTED) | 167 if (state_ != BEZEL_GESTURE_STARTED) |
182 return; | 168 return; |
183 | 169 |
184 if (num_touch_points == 1) { | 170 if (num_touch_points == 1) { |
185 SetState(BEZEL_SCROLLING_ONE_FINGER, scroll_delta); | 171 SetState(BEZEL_SCROLLING_ONE_FINGER, scroll_delta); |
186 return; | 172 return; |
187 } | 173 } |
188 | 174 |
189 DCHECK_EQ(num_touch_points, 2); | 175 DCHECK_EQ(num_touch_points, 2); |
190 SetState(BEZEL_SCROLLING_TWO_FINGERS, scroll_delta); | 176 SetState(BEZEL_SCROLLING_TWO_FINGERS, scroll_delta); |
191 if (left_right_delegate_->BezelCanScroll()) | 177 if (left_right_delegate_->CanScroll()) |
192 event->SetHandled(); | 178 event->SetHandled(); |
193 } else if (type == ui::ET_GESTURE_SCROLL_UPDATE) { | 179 } else if (type == ui::ET_GESTURE_SCROLL_UPDATE) { |
194 if (state_ != BEZEL_SCROLLING_TWO_FINGERS) | 180 if (state_ != BEZEL_SCROLLING_TWO_FINGERS) |
195 return; | 181 return; |
196 | 182 |
197 left_right_delegate_->BezelScrollUpdate(scroll_delta); | 183 left_right_delegate_->ScrollUpdate(scroll_delta); |
198 if (left_right_delegate_->BezelCanScroll()) | 184 if (left_right_delegate_->CanScroll()) |
199 event->SetHandled(); | 185 event->SetHandled(); |
200 } | 186 } |
201 } | 187 } |
202 | 188 |
203 } // namespace athena | 189 } // namespace athena |
OLD | NEW |