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 HandleTouchEvent(event); | |
| 48 } | |
| 49 | |
| 50 void TouchAccessibilityEnabler::HandleTouchEvent(const ui::TouchEvent* event) { | |
| 51 const ui::EventType type = event->type(); | |
| 52 const gfx::PointF& location = event->location_f(); | |
| 53 const int touch_id = event->touch_id(); | |
| 54 | |
| 55 if (type == ui::ET_TOUCH_PRESSED) { | |
| 56 touch_ids_.push_back(touch_id); | |
| 57 touch_locations_.insert(std::pair<int, gfx::PointF>(touch_id, location)); | |
| 58 } else if (type == ui::ET_TOUCH_RELEASED || type == ui::ET_TOUCH_CANCELLED) { | |
| 59 auto iter = std::find(touch_ids_.begin(), touch_ids_.end(), touch_id); | |
| 60 | |
| 61 // Can happen if this object is constructed while fingers were down. | |
| 62 if (iter == touch_ids_.end()) | |
| 63 return; | |
| 64 | |
| 65 touch_ids_.erase(iter); | |
| 66 touch_locations_.erase(touch_id); | |
| 67 } else if (type == ui::ET_TOUCH_MOVED) { | |
| 68 auto iter = std::find(touch_ids_.begin(), touch_ids_.end(), touch_id); | |
| 69 | |
| 70 // Can happen if this object is constructed while fingers were down. | |
| 71 if (iter == touch_ids_.end()) | |
| 72 return; | |
| 73 | |
| 74 float delta = (location - touch_locations_[*iter]).Length(); | |
| 75 if (delta > gesture_detector_config_.double_tap_slop) { | |
| 76 state_ = WAIT_FOR_NO_FINGERS; | |
| 77 CancelTimer(); | |
| 78 return; | |
| 79 } | |
| 80 } else { | |
| 81 NOTREACHED() << "Unexpected event type received: " << event->name(); | |
| 82 return; | |
| 83 } | |
| 84 | |
| 85 if (touch_ids_.size() == 0) { | |
| 86 state_ = NO_FINGERS_DOWN; | |
| 87 CancelTimer(); | |
| 88 return; | |
| 89 } | |
| 90 | |
| 91 if (touch_ids_.size() > 2) { | |
|
sadrul
2016/11/12 01:51:53
I think you can just use |touch_locations_| to tra
dmazzoni
2016/11/14 20:33:54
Good idea, done.
| |
| 92 state_ = WAIT_FOR_NO_FINGERS; | |
| 93 CancelTimer(); | |
| 94 return; | |
| 95 } | |
| 96 | |
| 97 if (state_ == NO_FINGERS_DOWN && event->type() == ui::ET_TOUCH_PRESSED) { | |
| 98 state_ = ONE_FINGER_DOWN; | |
| 99 } else if (state_ == ONE_FINGER_DOWN && | |
| 100 event->type() == ui::ET_TOUCH_PRESSED) { | |
| 101 state_ = TWO_FINGERS_DOWN; | |
| 102 two_finger_start_time_ = Now(); | |
| 103 StartTimer(); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 base::TimeTicks TouchAccessibilityEnabler::Now() { | |
| 108 if (tick_clock_) { | |
| 109 // This is the same as what EventTimeForNow() does, but here we do it | |
| 110 // with a clock that can be replaced with a simulated clock for tests. | |
| 111 return tick_clock_->NowTicks(); | |
| 112 } | |
| 113 return ui::EventTimeForNow(); | |
| 114 } | |
| 115 | |
| 116 void TouchAccessibilityEnabler::StartTimer() { | |
| 117 if (timer_.IsRunning()) | |
| 118 return; | |
| 119 | |
| 120 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kTimerDelayInMS), | |
| 121 this, &ui::TouchAccessibilityEnabler::OnTimer); | |
| 122 } | |
| 123 | |
| 124 void TouchAccessibilityEnabler::CancelTimer() { | |
| 125 if (timer_.IsRunning()) | |
| 126 timer_.Stop(); | |
| 127 } | |
| 128 | |
| 129 void TouchAccessibilityEnabler::OnTimer() { | |
| 130 base::TimeTicks now = Now(); | |
| 131 double tick_count_f = | |
| 132 (now - two_finger_start_time_).InMillisecondsF() / kTimerDelayInMS; | |
| 133 int tick_count = roundf(tick_count_f); | |
| 134 if (tick_count >= 1 && tick_count < kTimerTicksToToggleSpokenFeedback) { | |
| 135 delegate_->PlaySpokenFeedbackToggleCountdown(tick_count); | |
| 136 } | |
| 137 if (tick_count == kTimerTicksToToggleSpokenFeedback) { | |
| 138 delegate_->ToggleSpokenFeedback(); | |
| 139 state_ = WAIT_FOR_NO_FINGERS; | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 } // namespace ui | |
| OLD | NEW |