Chromium Code Reviews| Index: ui/chromeos/touch_accessibility_enabler.cc |
| diff --git a/ui/chromeos/touch_accessibility_enabler.cc b/ui/chromeos/touch_accessibility_enabler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..363069ab43e5c16b120317f1d8be8c97782a6b5f |
| --- /dev/null |
| +++ b/ui/chromeos/touch_accessibility_enabler.cc |
| @@ -0,0 +1,156 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ui/chromeos/touch_accessibility_enabler.h" |
| + |
| +#include <utility> |
| + |
| +#include "base/logging.h" |
| +#include "base/time/default_tick_clock.h" |
| +#include "ui/aura/window.h" |
| +#include "ui/aura/window_event_dispatcher.h" |
| +#include "ui/aura/window_tree_host.h" |
| +#include "ui/events/event.h" |
| +#include "ui/events/event_processor.h" |
| +#include "ui/events/event_utils.h" |
| + |
| +namespace ui { |
| + |
| +namespace { |
| + |
| +// Delay between timer callbacks. Each one plays a tick sound. |
| +const int kTimerDelayInMS = 500; |
|
oshima
2016/11/08 18:14:43
nit: constexpr
dmazzoni
2016/11/08 20:43:47
Done.
|
| + |
| +// The number of ticks of the timer before toggling spoken feedback. |
| +const int kTimerTicksToToggleSpokenFeedback = 7; |
| + |
| +} // namespace |
| + |
| +TouchAccessibilityEnabler::TouchAccessibilityEnabler( |
| + aura::Window* root_window, |
| + TouchAccessibilityEnablerDelegate* delegate) |
| + : root_window_(root_window), |
| + delegate_(delegate), |
| + state_(NO_FINGERS_DOWN), |
| + tick_clock_(NULL) { |
| + DCHECK(root_window); |
|
oshima
2016/11/08 18:14:43
delegate too?
dmazzoni
2016/11/08 20:43:47
Done.
|
| + root_window->GetHost()->GetEventSource()->AddEventRewriter(this); |
| +} |
| + |
| +TouchAccessibilityEnabler::~TouchAccessibilityEnabler() { |
| + root_window_->GetHost()->GetEventSource()->RemoveEventRewriter(this); |
| +} |
| + |
| +ui::EventRewriteStatus TouchAccessibilityEnabler::RewriteEvent( |
| + const ui::Event& event, |
| + std::unique_ptr<ui::Event>* rewritten_event) { |
|
oshima
2016/11/08 18:14:43
so eventhandler doesn't work?
dmazzoni
2016/11/08 20:43:47
Oh, good catch! Originally I did rewrite an event
dmazzoni
2016/11/08 21:18:04
Actually after testing some more, it doesn't work
sadrul
2016/11/10 20:39:03
I would like to understand more, because this is a
|
| + if (!event.IsTouchEvent()) |
| + return ui::EVENT_REWRITE_CONTINUE; |
| + |
| + const ui::TouchEvent& touch_event = static_cast<const ui::TouchEvent&>(event); |
| + HandleTouchEvent(touch_event); |
| + return ui::EVENT_REWRITE_CONTINUE; |
| +} |
| + |
| +ui::EventRewriteStatus TouchAccessibilityEnabler::NextDispatchEvent( |
| + const ui::Event& last_event, |
| + std::unique_ptr<ui::Event>* new_event) { |
| + NOTREACHED(); |
| + return ui::EVENT_REWRITE_CONTINUE; |
| +} |
| + |
| +void TouchAccessibilityEnabler::HandleTouchEvent(const ui::TouchEvent& event) { |
| + const ui::EventType type = event.type(); |
| + const gfx::PointF& location = event.location_f(); |
| + const int touch_id = event.touch_id(); |
| + |
| + if (type == ui::ET_TOUCH_PRESSED) { |
| + touch_ids_.push_back(touch_id); |
| + touch_locations_.insert(std::pair<int, gfx::PointF>(touch_id, location)); |
| + } else if (type == ui::ET_TOUCH_RELEASED || type == ui::ET_TOUCH_CANCELLED) { |
| + auto iter = std::find(touch_ids_.begin(), touch_ids_.end(), touch_id); |
| + |
| + // Can happen if this object is constructed while fingers were down. |
| + if (iter == touch_ids_.end()) |
| + return; |
| + |
| + touch_ids_.erase(iter); |
| + touch_locations_.erase(touch_id); |
| + } else if (type == ui::ET_TOUCH_MOVED) { |
| + auto iter = std::find(touch_ids_.begin(), touch_ids_.end(), touch_id); |
| + |
| + // Can happen if this object is constructed while fingers were down. |
| + if (iter == touch_ids_.end()) |
| + return; |
| + |
| + float delta = (location - touch_locations_[*iter]).Length(); |
| + if (delta > gesture_detector_config_.double_tap_slop) { |
| + state_ = WAIT_FOR_NO_FINGERS; |
| + CancelTimer(); |
| + return; |
| + } |
| + } else { |
| + NOTREACHED() << "Unexpected event type received: " << event.name(); |
| + return; |
| + } |
| + |
| + if (touch_ids_.size() == 0) { |
| + state_ = NO_FINGERS_DOWN; |
| + CancelTimer(); |
| + return; |
| + } |
| + |
| + if (touch_ids_.size() > 2) { |
| + state_ = WAIT_FOR_NO_FINGERS; |
| + CancelTimer(); |
| + return; |
| + } |
| + |
| + if (state_ == NO_FINGERS_DOWN && event.type() == ui::ET_TOUCH_PRESSED) { |
| + state_ = ONE_FINGER_DOWN; |
| + } else if (state_ == ONE_FINGER_DOWN && |
| + event.type() == ui::ET_TOUCH_PRESSED) { |
| + state_ = TWO_FINGERS_DOWN; |
| + two_finger_start_time_ = Now(); |
| + StartTimer(); |
| + } |
| +} |
| + |
| +base::TimeTicks TouchAccessibilityEnabler::Now() { |
| + if (tick_clock_) { |
| + // This is the same as what EventTimeForNow() does, but here we do it |
| + // with a clock that can be replaced with a simulated clock for tests. |
| + return tick_clock_->NowTicks(); |
| + } |
| + return ui::EventTimeForNow(); |
| +} |
| + |
| +void TouchAccessibilityEnabler::StartTimer() { |
| + if (timer_.IsRunning()) |
| + return; |
| + |
| + timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kTimerDelayInMS), |
| + this, &ui::TouchAccessibilityEnabler::OnTimer); |
| +} |
| + |
| +void TouchAccessibilityEnabler::CancelTimer() { |
| + if (timer_.IsRunning()) |
| + timer_.Stop(); |
| +} |
| + |
| +void TouchAccessibilityEnabler::OnTimer() { |
| + base::TimeTicks now = Now(); |
| + double tick_count_f = |
| + (now - two_finger_start_time_).InMillisecondsF() / kTimerDelayInMS; |
| + int tick_count = roundf(tick_count_f); |
| + if (tick_count >= 1 && tick_count < kTimerTicksToToggleSpokenFeedback) { |
| + delegate_->PlaySpokenFeedbackToggleCountdown(tick_count); |
| + } |
| + if (tick_count == kTimerTicksToToggleSpokenFeedback) { |
| + delegate_->ToggleSpokenFeedback(); |
| + state_ = WAIT_FOR_NO_FINGERS; |
| + } |
| +} |
| + |
| +} // namespace ui |