spoken_feedback_enabler.cc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2018 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. #include "ash/accessibility/chromevox/spoken_feedback_enabler.h"
  5. #include "ash/accessibility/accessibility_controller_impl.h"
  6. #include "ash/shell.h"
  7. #include "base/numerics/safe_conversions.h"
  8. #include "ui/events/base_event_utils.h"
  9. namespace ash {
  10. namespace {
  11. // Delay between timer callbacks. Each one plays a tick sound.
  12. constexpr base::TimeDelta kTimerDelay = base::Milliseconds(500);
  13. // The number of ticks of the timer before the first sound is generated.
  14. constexpr int kTimerTicksOfFirstSoundFeedback = 6;
  15. // The number of ticks of the timer before toggling spoken feedback.
  16. constexpr int kTimerTicksToToggleSpokenFeedback = 10;
  17. } // namespace
  18. SpokenFeedbackEnabler::SpokenFeedbackEnabler() {
  19. start_time_ = ui::EventTimeForNow();
  20. timer_.Start(FROM_HERE, kTimerDelay, this, &SpokenFeedbackEnabler::OnTimer);
  21. }
  22. SpokenFeedbackEnabler::~SpokenFeedbackEnabler() {}
  23. void SpokenFeedbackEnabler::OnTimer() {
  24. base::TimeTicks now = ui::EventTimeForNow();
  25. int tick_count = base::ClampRound((now - start_time_) / kTimerDelay);
  26. AccessibilityControllerImpl* controller =
  27. Shell::Get()->accessibility_controller();
  28. CHECK(controller);
  29. if (tick_count >= kTimerTicksOfFirstSoundFeedback &&
  30. tick_count < kTimerTicksToToggleSpokenFeedback) {
  31. controller->PlaySpokenFeedbackToggleCountdown(tick_count);
  32. } else if (tick_count == kTimerTicksToToggleSpokenFeedback) {
  33. controller->SetSpokenFeedbackEnabled(
  34. !controller->spoken_feedback().enabled(), A11Y_NOTIFICATION_SHOW);
  35. timer_.Stop();
  36. }
  37. }
  38. } // namespace ash