Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(203)

Side by Side Diff: ui/chromeos/touch_exploration_controller.cc

Issue 296403011: Support double-tap to click in touch accessibility controller. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address more feedback Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "ui/chromeos/touch_exploration_controller.h" 5 #include "ui/chromeos/touch_exploration_controller.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "ui/aura/client/cursor_client.h" 8 #include "ui/aura/client/cursor_client.h"
9 #include "ui/aura/window.h" 9 #include "ui/aura/window.h"
10 #include "ui/aura/window_event_dispatcher.h"
10 #include "ui/aura/window_tree_host.h" 11 #include "ui/aura/window_tree_host.h"
11 #include "ui/events/event.h" 12 #include "ui/events/event.h"
13 #include "ui/events/event_processor.h"
12 14
13 namespace ui { 15 namespace ui {
14 16
15 TouchExplorationController::TouchExplorationController( 17 TouchExplorationController::TouchExplorationController(
16 aura::Window* root_window) 18 aura::Window* root_window)
17 : root_window_(root_window) { 19 : root_window_(root_window),
20 initial_touch_id_passthrough_mapping_(0),
21 state_(NO_FINGERS_DOWN),
22 event_handler_for_testing_(NULL) {
18 CHECK(root_window); 23 CHECK(root_window);
19 root_window->GetHost()->GetEventSource()->AddEventRewriter(this); 24 root_window->GetHost()->GetEventSource()->AddEventRewriter(this);
20 } 25 }
21 26
22 TouchExplorationController::~TouchExplorationController() { 27 TouchExplorationController::~TouchExplorationController() {
23 root_window_->GetHost()->GetEventSource()->RemoveEventRewriter(this); 28 root_window_->GetHost()->GetEventSource()->RemoveEventRewriter(this);
24 } 29 }
25 30
31 void TouchExplorationController::CallTapTimerNowForTesting() {
32 CHECK(tap_timer_.IsRunning());
James Cook 2014/06/05 18:04:41 Could this be a DCHECK?
dmazzoni 2014/06/05 23:32:24 OK. Honest question, though - how many people run
James Cook 2014/06/06 00:09:45 No one runs Chrome OS in debug. However, this func
dmazzoni 2014/06/06 15:27:22 Ah, of course - I wasn't thinking about this being
33 tap_timer_.Stop();
34 OnTapTimerFired();
35 }
36
37 void TouchExplorationController::SetEventHandlerForTesting(
38 ui::EventHandler* event_handler_for_testing) {
39 event_handler_for_testing_ = event_handler_for_testing;
40 }
41
26 ui::EventRewriteStatus TouchExplorationController::RewriteEvent( 42 ui::EventRewriteStatus TouchExplorationController::RewriteEvent(
27 const ui::Event& event, 43 const ui::Event& event,
28 scoped_ptr<ui::Event>* rewritten_event) { 44 scoped_ptr<ui::Event>* rewritten_event) {
29 if (!event.IsTouchEvent()) 45 if (!event.IsTouchEvent())
30 return ui::EVENT_REWRITE_CONTINUE; 46 return ui::EVENT_REWRITE_CONTINUE;
31
32 const ui::TouchEvent& touch_event = 47 const ui::TouchEvent& touch_event =
33 static_cast<const ui::TouchEvent&>(event); 48 static_cast<const ui::TouchEvent&>(event);
49
50 // If the tap timer should have fired by now but hasn't, run it now and
51 // stop the timer. This is important so that behavior is consistent with
52 // the timestamps of the events, and not dependent on the granularity of
53 // the timer.
54 if (tap_timer_.IsRunning() &&
55 touch_event.time_stamp() - initial_press_->time_stamp() >
56 gesture_detector_config_.double_tap_timeout) {
57 tap_timer_.Stop();
58 OnTapTimerFired();
59 // Note: this may change the state. We should now continue and process
60 // this event under this new state.
61 }
62
34 const ui::EventType type = touch_event.type(); 63 const ui::EventType type = touch_event.type();
35 const gfx::PointF& location = touch_event.location_f(); 64 const gfx::PointF& location = touch_event.location_f();
36 const int touch_id = touch_event.touch_id(); 65 const int touch_id = touch_event.touch_id();
37 const int flags = touch_event.flags(); 66
38 67 // Always update touch ids and touch locations, so we can use those
39 if (type == ui::ET_TOUCH_PRESSED) { 68 // no matter what state we're in.
40 touch_ids_.push_back(touch_id); 69 if (type == ui::ET_TOUCH_PRESSED) {
70 current_touch_ids_.push_back(touch_id);
41 touch_locations_.insert(std::pair<int, gfx::PointF>(touch_id, location)); 71 touch_locations_.insert(std::pair<int, gfx::PointF>(touch_id, location));
42 // If this is the first and only finger touching - rewrite the touch as a 72 } else if (type == ui::ET_TOUCH_RELEASED || type == ui::ET_TOUCH_CANCELLED) {
43 // mouse move. Otherwise let the it go through as is. 73 std::vector<int>::iterator it = std::find(
44 if (touch_ids_.size() == 1) { 74 current_touch_ids_.begin(), current_touch_ids_.end(), touch_id);
45 *rewritten_event = CreateMouseMoveEvent(location, flags); 75
76 // Can happen if touch exploration is enabled while fingers were down.
77 if (it == current_touch_ids_.end())
78 return ui::EVENT_REWRITE_CONTINUE;
79
80 current_touch_ids_.erase(it);
81 touch_locations_.erase(touch_id);
82 } else if (type == ui::ET_TOUCH_MOVED) {
83 std::vector<int>::iterator it = std::find(
84 current_touch_ids_.begin(), current_touch_ids_.end(), touch_id);
85
86 // Can happen if touch exploration is enabled while fingers were down.
87 if (it == current_touch_ids_.end())
88 return ui::EVENT_REWRITE_CONTINUE;
89
90 touch_locations_[*it] = location;
91 }
92
93 // The rest of the processing depends on what state we're in.
94 switch(state_) {
95 case NO_FINGERS_DOWN:
96 return OnNoFingersDown(touch_event, rewritten_event);
97 case SINGLE_TAP_PRESSED:
98 return OnSingleTapPressed(touch_event, rewritten_event);
99 case SINGLE_TAP_RELEASED:
100 return OnSingleTapReleased(touch_event, rewritten_event);
101 case DOUBLE_TAP_PRESSED:
102 return OnDoubleTapPressed(touch_event, rewritten_event);
103 case TOUCH_EXPLORATION:
104 return OnTouchExploration(touch_event, rewritten_event);
105 case PASSTHROUGH_MINUS_ONE:
106 return OnPassthroughMinusOne(touch_event, rewritten_event);
107 }
108
109 NOTREACHED();
110 return ui::EVENT_REWRITE_CONTINUE;
111 }
112
113 ui::EventRewriteStatus TouchExplorationController::NextDispatchEvent(
114 const ui::Event& last_event, scoped_ptr<ui::Event>* new_event) {
115 NOTREACHED();
116 return ui::EVENT_REWRITE_CONTINUE;
117 }
118
119 ui::EventRewriteStatus TouchExplorationController::OnNoFingersDown(
120 const ui::TouchEvent& event, scoped_ptr<ui::Event>* rewritten_event) {
121 const ui::EventType type = event.type();
122 if (type == ui::ET_TOUCH_PRESSED) {
123 initial_press_.reset(new TouchEvent(event));
124 tap_timer_.Start(FROM_HERE,
125 gesture_detector_config_.double_tap_timeout,
126 this,
127 &TouchExplorationController::OnTapTimerFired);
128 state_ = SINGLE_TAP_PRESSED;
129 return ui::EVENT_REWRITE_DISCARD;
130 }
131
132 NOTREACHED();
133 return ui::EVENT_REWRITE_CONTINUE;
134 }
135
136 ui::EventRewriteStatus TouchExplorationController::OnSingleTapPressed(
James Cook 2014/06/05 18:04:41 optional: What do you think of naming these method
dmazzoni 2014/06/05 23:32:24 Make sense. I'm fine with InSingleTapPressed, etc.
137 const ui::TouchEvent& event, scoped_ptr<ui::Event>* rewritten_event) {
138 const ui::EventType type = event.type();
139
140 if (type == ui::ET_TOUCH_PRESSED) {
141 // Adding a second finger within the timeout period switches to
142 // passthrough.
143 state_ = PASSTHROUGH_MINUS_ONE;
144 return OnPassthroughMinusOne(event, rewritten_event);
145 } else if (type == ui::ET_TOUCH_RELEASED || type == ui::ET_TOUCH_CANCELLED) {
146 DCHECK_EQ(0U, current_touch_ids_.size());
147 state_ = SINGLE_TAP_RELEASED;
148 return EVENT_REWRITE_DISCARD;
149 } else if (type == ui::ET_TOUCH_MOVED) {
150 // If the user moves far enough from the initial touch location (outside
151 // the "slop" region, jump to the touch exploration mode early.
152 // TODO(evy, lisayin): Add gesture recognition here instead -
153 // we should probably jump to gesture mode here if the velocity is
154 // high enough, and touch exploration if the velocity is lower.
155 float delta = (event.location() - initial_press_->location()).Length();
156 if (delta > gesture_detector_config_.touch_slop) {
46 EnterTouchToMouseMode(); 157 EnterTouchToMouseMode();
47 return ui::EVENT_REWRITE_REWRITTEN; 158 state_ = TOUCH_EXPLORATION;
159 return OnTouchExploration(event, rewritten_event);
48 } 160 }
49 return ui::EVENT_REWRITE_CONTINUE; 161
50 } else if (type == ui::ET_TOUCH_RELEASED || type == ui::ET_TOUCH_CANCELLED) { 162 return EVENT_REWRITE_DISCARD;
51 std::vector<int>::iterator it = 163 }
52 std::find(touch_ids_.begin(), touch_ids_.end(), touch_id); 164 NOTREACHED() << "Unexpected event type received.";
53 // We may fail to find the finger if the exploration mode was turned on 165 return ui::EVENT_REWRITE_CONTINUE;
54 // while the user had some fingers touching the screen. We simply ignore 166 }
55 // those fingers for the purposes of event transformation. 167
56 if (it == touch_ids_.end()) 168 ui::EventRewriteStatus TouchExplorationController::OnSingleTapReleased(
57 return ui::EVENT_REWRITE_CONTINUE; 169 const ui::TouchEvent& event, scoped_ptr<ui::Event>* rewritten_event) {
58 const bool first_finger_released = it == touch_ids_.begin(); 170 const ui::EventType type = event.type();
59 touch_ids_.erase(it); 171 if (type == ui::ET_TOUCH_PRESSED) {
60 int num_erased = touch_locations_.erase(touch_id); 172 // This is the second tap in a double-tap (or double tap-hold).
61 DCHECK_EQ(num_erased, 1); 173 // Rewrite at location of last touch exploration.
62 const int num_fingers_remaining = touch_ids_.size(); 174 ui::TouchEvent* rewritten_press_event = new ui::TouchEvent(
63 175 ui::ET_TOUCH_PRESSED,
64 if (num_fingers_remaining == 0) { 176 last_touch_exploration_location_,
65 *rewritten_event = CreateMouseMoveEvent(location, flags); 177 event.touch_id(),
66 return ui::EVENT_REWRITE_REWRITTEN; 178 event.time_stamp());
67 } 179 rewritten_press_event->set_flags(event.flags());
68 180 rewritten_event->reset(rewritten_press_event);
69 // If we are left with one finger - enter the mouse move mode. 181 state_ = DOUBLE_TAP_PRESSED;
70 const bool enter_mouse_move_mode = num_fingers_remaining == 1; 182 return ui::EVENT_REWRITE_REWRITTEN;
71 183 }
72 if (!enter_mouse_move_mode && !first_finger_released) { 184
73 // No special handling needed. 185 NOTREACHED();
74 return ui::EVENT_REWRITE_CONTINUE; 186 return ui::EVENT_REWRITE_CONTINUE;
75 } 187 }
76 188
77 // If the finger which was released was the first one, - we need to rewrite 189 ui::EventRewriteStatus TouchExplorationController::OnDoubleTapPressed(
78 // the release event as a release of the was second / now first finger. 190 const ui::TouchEvent& event, scoped_ptr<ui::Event>* rewritten_event) {
79 // This is the finger which will now be getting "substracted". 191 const ui::EventType type = event.type();
80 if (first_finger_released) { 192 if (type == ui::ET_TOUCH_PRESSED) {
81 int rewritten_release_id = touch_ids_[0]; 193 return ui::EVENT_REWRITE_DISCARD;
82 const gfx::PointF& rewritten_release_location = 194 } else if (type == ui::ET_TOUCH_RELEASED || type == ui::ET_TOUCH_CANCELLED) {
83 touch_locations_[rewritten_release_id]; 195 if (current_touch_ids_.size() != 0)
84 ui::TouchEvent* rewritten_release_event = new ui::TouchEvent( 196 return EVENT_REWRITE_DISCARD;
85 ui::ET_TOUCH_RELEASED, 197
86 rewritten_release_location, 198 // Rewrite at location of last touch exploration.
87 rewritten_release_id, 199 ui::TouchEvent* rewritten_release_event = new ui::TouchEvent(
88 event.time_stamp()); 200 ui::ET_TOUCH_RELEASED,
89 rewritten_release_event->set_flags(touch_event.flags()); 201 last_touch_exploration_location_,
90 rewritten_event->reset(rewritten_release_event); 202 event.touch_id(),
91 } else if (enter_mouse_move_mode) { 203 event.time_stamp());
92 // Dispatch the release event as is. 204 rewritten_release_event->set_flags(event.flags());
93 // TODO(mfomitchev): We can get rid of this clause once we have 205 rewritten_event->reset(rewritten_release_event);
94 // EVENT_REWRITE_DISPATCH_ANOTHER working without having to set 206 ResetToNoFingersDown();
95 // rewritten_event.
96 rewritten_event->reset(new ui::TouchEvent(touch_event));
97 }
98
99 if (enter_mouse_move_mode) {
100 // Since we are entering the mouse move mode - also dispatch a mouse move
101 // event at the location of the one remaining finger. (num_fingers == 1)
102 const gfx::PointF& mouse_move_location = touch_locations_[touch_ids_[0]];
103 next_dispatch_event_ =
104 CreateMouseMoveEvent(mouse_move_location, flags).Pass();
105 return ui::EVENT_REWRITE_DISPATCH_ANOTHER;
106 }
107 return ui::EVENT_REWRITE_REWRITTEN; 207 return ui::EVENT_REWRITE_REWRITTEN;
108 } else if (type == ui::ET_TOUCH_MOVED) { 208 } else if (type == ui::ET_TOUCH_MOVED) {
109 std::vector<int>::iterator it = 209 return ui::EVENT_REWRITE_DISCARD;
110 std::find(touch_ids_.begin(), touch_ids_.end(), touch_id); 210 }
111 // We may fail to find the finger if the exploration mode was turned on 211 NOTREACHED() << "Unexpected event type received.";
112 // while the user had some fingers touching the screen. We simply ignore 212 return ui::EVENT_REWRITE_CONTINUE;
113 // those fingers for the purposes of event transformation. 213 }
114 if (it == touch_ids_.end()) 214
115 return ui::EVENT_REWRITE_CONTINUE; 215 ui::EventRewriteStatus TouchExplorationController::OnTouchExploration(
116 touch_locations_[*it] = location; 216 const ui::TouchEvent& event, scoped_ptr<ui::Event>* rewritten_event) {
117 if (touch_ids_.size() == 1) { 217 const ui::EventType type = event.type();
118 // Touch moves are rewritten as mouse moves when there's only one finger 218 if (type == ui::ET_TOUCH_PRESSED) {
119 // touching the screen. 219 // Ignore any additional fingers when we're already in touch exploration
120 *rewritten_event = CreateMouseMoveEvent(location, flags).Pass(); 220 // mode. TODO(evy, lisayin): Support "split-tap" here instead.
121 return ui::EVENT_REWRITE_REWRITTEN; 221 return ui::EVENT_REWRITE_DISCARD;
222 } else if (type == ui::ET_TOUCH_RELEASED || type == ui::ET_TOUCH_CANCELLED) {
223 if (current_touch_ids_.size() == 0)
224 ResetToNoFingersDown();
225 } else if (type != ui::ET_TOUCH_MOVED) {
226 NOTREACHED() << "Unexpected event type received.";
227 return ui::EVENT_REWRITE_CONTINUE;
228 }
229
230 // Rewrite as a mouse-move event.
231 *rewritten_event = CreateMouseMoveEvent(event.location(), event.flags());
232 last_touch_exploration_location_ = event.location();
233 return ui::EVENT_REWRITE_REWRITTEN;
234 }
235
236 ui::EventRewriteStatus TouchExplorationController::OnPassthroughMinusOne(
237 const ui::TouchEvent& event, scoped_ptr<ui::Event>* rewritten_event) {
238 ui::EventType type = event.type();
239 gfx::PointF location = event.location_f();
240
241 if (type == ui::ET_TOUCH_RELEASED || type == ui::ET_TOUCH_CANCELLED) {
242 if (current_touch_ids_.size() == 0)
243 ResetToNoFingersDown();
244
245 if (initial_touch_id_passthrough_mapping_ == 0) {
246 if (event.touch_id() == initial_press_->touch_id()) {
247 initial_touch_id_passthrough_mapping_ = -1;
James Cook 2014/06/05 18:04:41 Use a named constant instead of -1.
dmazzoni 2014/06/05 23:32:24 Done.
248 } else {
249 // If the only finger now remaining is the first finger,
250 // rewrite as a move to the location of the first finger.
251 initial_touch_id_passthrough_mapping_ = event.touch_id();
252 ui::TouchEvent* rewritten_passthrough_event = new ui::TouchEvent(
253 ui::ET_TOUCH_MOVED,
254 touch_locations_[initial_press_->touch_id()],
255 initial_touch_id_passthrough_mapping_,
256 event.time_stamp());
257 rewritten_passthrough_event->set_flags(event.flags());
258 rewritten_event->reset(rewritten_passthrough_event);
259 return ui::EVENT_REWRITE_REWRITTEN;
260 }
122 } 261 }
123 if (touch_id == touch_ids_.front()) { 262 }
124 // Touch moves of the first finger are discarded when there's more than 263
125 // one finger touching. 264 if (event.touch_id() == initial_press_->touch_id()) {
265 if (initial_touch_id_passthrough_mapping_ <= 0)
James Cook 2014/06/05 18:04:41 Could compare directly to your named constant, sin
dmazzoni 2014/06/05 23:32:24 Done.
126 return ui::EVENT_REWRITE_DISCARD; 266 return ui::EVENT_REWRITE_DISCARD;
127 } 267
128 return ui::EVENT_REWRITE_CONTINUE; 268 ui::TouchEvent* rewritten_passthrough_event = new ui::TouchEvent(
129 } 269 type,
130 NOTREACHED() << "Unexpected event type received."; 270 location,
131 return ui::EVENT_REWRITE_CONTINUE; 271 initial_touch_id_passthrough_mapping_,
132 } 272 event.time_stamp());
133 273 rewritten_passthrough_event->set_flags(event.flags());
134 ui::EventRewriteStatus TouchExplorationController::NextDispatchEvent( 274 rewritten_event->reset(rewritten_passthrough_event);
135 const ui::Event& last_event, 275 return ui::EVENT_REWRITE_REWRITTEN;
136 scoped_ptr<ui::Event>* new_event) { 276 }
137 CHECK(next_dispatch_event_); 277
138 DCHECK(last_event.IsTouchEvent()); 278 return ui::EVENT_REWRITE_CONTINUE;
139 *new_event = next_dispatch_event_.Pass(); 279 }
140 // Enter the mouse move mode if needed 280
141 if ((*new_event)->IsMouseEvent()) 281 void TouchExplorationController::OnTapTimerFired() {
282 if (state_ != SINGLE_TAP_RELEASED && state_ != SINGLE_TAP_PRESSED)
283 return;
284
285 if (state_ == SINGLE_TAP_RELEASED) {
286 ResetToNoFingersDown();
287 } else {
142 EnterTouchToMouseMode(); 288 EnterTouchToMouseMode();
143 return ui::EVENT_REWRITE_REWRITTEN; 289 state_ = TOUCH_EXPLORATION;
290 }
291
292 scoped_ptr<ui::Event> mouse_move = CreateMouseMoveEvent(
293 initial_press_->location(), initial_press_->flags());
294 DispatchEvent(mouse_move.get());
295 last_touch_exploration_location_ = initial_press_->location();
296 }
297
298 void TouchExplorationController::DispatchEvent(ui::Event* event) {
299 if (event_handler_for_testing_) {
300 event_handler_for_testing_->OnEvent(event);
301 return;
302 }
303
304 ui::EventDispatchDetails result ALLOW_UNUSED =
305 root_window_->GetHost()->dispatcher()->OnEventFromSource(event);
144 } 306 }
145 307
146 scoped_ptr<ui::Event> TouchExplorationController::CreateMouseMoveEvent( 308 scoped_ptr<ui::Event> TouchExplorationController::CreateMouseMoveEvent(
147 const gfx::PointF& location, 309 const gfx::PointF& location,
148 int flags) { 310 int flags) {
149 return scoped_ptr<ui::Event>( 311 return scoped_ptr<ui::Event>(
150 new ui::MouseEvent( 312 new ui::MouseEvent(
151 ui::ET_MOUSE_MOVED, 313 ui::ET_MOUSE_MOVED,
152 location, 314 location,
153 location, 315 location,
154 flags | ui::EF_IS_SYNTHESIZED | ui::EF_TOUCH_ACCESSIBILITY, 316 flags | ui::EF_IS_SYNTHESIZED | ui::EF_TOUCH_ACCESSIBILITY,
155 0)); 317 0));
156 } 318 }
157 319
158 void TouchExplorationController::EnterTouchToMouseMode() { 320 void TouchExplorationController::EnterTouchToMouseMode() {
159 aura::client::CursorClient* cursor_client = 321 aura::client::CursorClient* cursor_client =
160 aura::client::GetCursorClient(root_window_); 322 aura::client::GetCursorClient(root_window_);
161 if (cursor_client && !cursor_client->IsMouseEventsEnabled()) 323 if (cursor_client && !cursor_client->IsMouseEventsEnabled())
162 cursor_client->EnableMouseEvents(); 324 cursor_client->EnableMouseEvents();
163 if (cursor_client && cursor_client->IsCursorVisible()) 325 if (cursor_client && cursor_client->IsCursorVisible())
164 cursor_client->HideCursor(); 326 cursor_client->HideCursor();
165 } 327 }
166 328
329 void TouchExplorationController::ResetToNoFingersDown() {
330 state_ = NO_FINGERS_DOWN;
331 initial_touch_id_passthrough_mapping_ = 0;
332 if (tap_timer_.IsRunning())
333 tap_timer_.Stop();
334 }
335
167 } // namespace ui 336 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698