Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "ui/chromeos/touch_accessibility_enabler.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/time/default_tick_clock.h" | |
| 11 #include "ui/aura/window.h" | |
| 12 #include "ui/aura/window_event_dispatcher.h" | |
| 13 #include "ui/aura/window_tree_host.h" | |
| 14 #include "ui/events/event.h" | |
| 15 #include "ui/events/event_processor.h" | |
| 16 #include "ui/events/event_utils.h" | |
| 17 | |
| 18 namespace ui { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // Delay between timer callbacks. Each one plays a tick sound. | |
| 23 constexpr int kTimerDelayInMS = 500; | |
| 24 | |
| 25 // The number of ticks of the timer before toggling spoken feedback. | |
| 26 constexpr int kTimerTicksToToggleSpokenFeedback = 7; | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 TouchAccessibilityEnabler::TouchAccessibilityEnabler( | |
| 31 aura::Window* root_window, | |
| 32 TouchAccessibilityEnablerDelegate* delegate) | |
| 33 : root_window_(root_window), | |
| 34 delegate_(delegate), | |
| 35 state_(NO_FINGERS_DOWN), | |
| 36 tick_clock_(NULL) { | |
| 37 DCHECK(root_window); | |
| 38 DCHECK(delegate); | |
| 39 root_window_->AddPreTargetHandler(this); | |
| 40 } | |
| 41 | |
| 42 TouchAccessibilityEnabler::~TouchAccessibilityEnabler() { | |
| 43 root_window_->RemovePreTargetHandler(this); | |
| 44 } | |
| 45 | |
| 46 void TouchAccessibilityEnabler::OnTouchEvent(ui::TouchEvent* event) { | |
| 47 // Skip events rewritten by TouchExplorationController, it will hand | |
| 48 // us the unrewritten events directly. | |
| 49 if (!(event->flags() & ui::EF_TOUCH_ACCESSIBILITY)) | |
| 50 HandleTouchEvent(*event); | |
| 51 } | |
| 52 | |
| 53 void TouchAccessibilityEnabler::HandleTouchEvent(const ui::TouchEvent& event) { | |
|
oshima
2016/11/14 22:02:47
DCHECK(!(event.flags() & EF_TOUCH_ACCESSIBILITY));
dmazzoni
2016/11/14 23:17:16
Done.
| |
| 54 const ui::EventType type = event.type(); | |
| 55 const gfx::PointF& location = event.location_f(); | |
| 56 const int touch_id = event.touch_id(); | |
| 57 | |
| 58 if (type == ui::ET_TOUCH_PRESSED) { | |
| 59 touch_locations_.insert(std::pair<int, gfx::PointF>(touch_id, location)); | |
| 60 } else if (type == ui::ET_TOUCH_RELEASED || type == ui::ET_TOUCH_CANCELLED) { | |
| 61 auto iter = touch_locations_.find(touch_id); | |
| 62 | |
| 63 // Can happen if this object is constructed while fingers were down. | |
| 64 if (iter == touch_locations_.end()) | |
| 65 return; | |
| 66 | |
| 67 touch_locations_.erase(touch_id); | |
| 68 } else if (type == ui::ET_TOUCH_MOVED) { | |
| 69 auto iter = touch_locations_.find(touch_id); | |
| 70 | |
| 71 // Can happen if this object is constructed while fingers were down. | |
| 72 if (iter == touch_locations_.end()) | |
| 73 return; | |
| 74 | |
| 75 float delta = (location - iter->second).Length(); | |
| 76 if (delta > gesture_detector_config_.double_tap_slop) { | |
| 77 state_ = WAIT_FOR_NO_FINGERS; | |
| 78 CancelTimer(); | |
| 79 return; | |
| 80 } | |
| 81 } else { | |
| 82 NOTREACHED() << "Unexpected event type received: " << event.name(); | |
| 83 return; | |
| 84 } | |
| 85 | |
| 86 if (touch_locations_.size() == 0) { | |
| 87 state_ = NO_FINGERS_DOWN; | |
| 88 CancelTimer(); | |
| 89 return; | |
| 90 } | |
| 91 | |
| 92 if (touch_locations_.size() > 2) { | |
| 93 state_ = WAIT_FOR_NO_FINGERS; | |
| 94 CancelTimer(); | |
| 95 return; | |
| 96 } | |
| 97 | |
| 98 if (state_ == NO_FINGERS_DOWN && event.type() == ui::ET_TOUCH_PRESSED) { | |
| 99 state_ = ONE_FINGER_DOWN; | |
| 100 } else if (state_ == ONE_FINGER_DOWN && | |
| 101 event.type() == ui::ET_TOUCH_PRESSED) { | |
| 102 state_ = TWO_FINGERS_DOWN; | |
| 103 two_finger_start_time_ = Now(); | |
| 104 StartTimer(); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 base::TimeTicks TouchAccessibilityEnabler::Now() { | |
| 109 if (tick_clock_) { | |
| 110 // This is the same as what EventTimeForNow() does, but here we do it | |
| 111 // with a clock that can be replaced with a simulated clock for tests. | |
| 112 return tick_clock_->NowTicks(); | |
| 113 } | |
| 114 return ui::EventTimeForNow(); | |
| 115 } | |
| 116 | |
| 117 void TouchAccessibilityEnabler::StartTimer() { | |
| 118 if (timer_.IsRunning()) | |
| 119 return; | |
| 120 | |
| 121 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kTimerDelayInMS), | |
| 122 this, &ui::TouchAccessibilityEnabler::OnTimer); | |
| 123 } | |
| 124 | |
| 125 void TouchAccessibilityEnabler::CancelTimer() { | |
| 126 if (timer_.IsRunning()) | |
| 127 timer_.Stop(); | |
| 128 } | |
| 129 | |
| 130 void TouchAccessibilityEnabler::OnTimer() { | |
| 131 base::TimeTicks now = Now(); | |
| 132 double tick_count_f = | |
| 133 (now - two_finger_start_time_).InMillisecondsF() / kTimerDelayInMS; | |
| 134 int tick_count = roundf(tick_count_f); | |
|
oshima
2016/11/14 22:02:47
nit: include <math.h>
or use std::lround
dmazzoni
2016/11/14 23:17:16
Done.
| |
| 135 if (tick_count >= 1 && tick_count < kTimerTicksToToggleSpokenFeedback) { | |
| 136 delegate_->PlaySpokenFeedbackToggleCountdown(tick_count); | |
| 137 } | |
| 138 if (tick_count == kTimerTicksToToggleSpokenFeedback) { | |
| 139 delegate_->ToggleSpokenFeedback(); | |
| 140 state_ = WAIT_FOR_NO_FINGERS; | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 } // namespace ui | |
| OLD | NEW |