File: touch_accessibility_enabler.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (130 lines) | stat: -rw-r--r-- 4,328 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_ACCESSIBILITY_CHROMEVOX_TOUCH_ACCESSIBILITY_ENABLER_H_
#define ASH_ACCESSIBILITY_CHROMEVOX_TOUCH_ACCESSIBILITY_ENABLER_H_

#include <map>

#include "ash/ash_export.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time/tick_clock.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "ui/events/event.h"
#include "ui/events/event_handler.h"
#include "ui/events/gesture_detection/gesture_detector.h"

namespace aura {
class Window;
}

namespace ash {

// A delegate to handle commands in response to detected accessibility gesture
// events.
class TouchAccessibilityEnablerDelegate {
 public:
  virtual ~TouchAccessibilityEnablerDelegate() {}

  // Toggles spoken feedback.
  virtual void ToggleSpokenFeedback() {}
};

// TouchAccessibilityEnabler triggers turning spoken feedback on or off
// by holding down two fingers on the touch screen for several seconds.
class ASH_EXPORT TouchAccessibilityEnabler : public ui::EventHandler {
 public:
  TouchAccessibilityEnabler(aura::Window* root_window,
                            TouchAccessibilityEnablerDelegate* delegate);

  TouchAccessibilityEnabler(const TouchAccessibilityEnabler&) = delete;
  TouchAccessibilityEnabler& operator=(const TouchAccessibilityEnabler&) =
      delete;

  ~TouchAccessibilityEnabler() override;

  bool IsInNoFingersDownForTesting() { return state_ == NO_FINGERS_DOWN; }
  bool IsInOneFingerDownForTesting() { return state_ == ONE_FINGER_DOWN; }
  bool IsInTwoFingersDownForTesting() { return state_ == TWO_FINGERS_DOWN; }
  bool IsInWaitForNoFingersForTesting() {
    return state_ == WAIT_FOR_NO_FINGERS;
  }
  void TriggerOnTimerForTesting() { OnTimer(); }

  // When TouchExplorationController is running, it tells this class to
  // remove its event handler so that it can pass it the unrewritten events
  // directly. Otherwise, this class would only receive the rewritten events,
  // which would require entirely separate logic.
  void RemoveEventHandler();
  void AddEventHandler();
  void HandleTouchEvent(const ui::TouchEvent& event);

  // Expose a weak ptr so that TouchExplorationController can hold a reference
  // to this object without worrying about destruction order during shutdown.
  base::WeakPtr<TouchAccessibilityEnabler> GetWeakPtr();

 private:
  // Overridden from ui::EventHandler
  void OnTouchEvent(ui::TouchEvent* event) override;

  void StartTimer();
  void CancelTimer();
  void OnTimer();

  void ResetToNoFingersDown();

  // Returns the current time of the tick clock.
  base::TimeTicks Now();

  enum State {
    // No fingers are down.
    NO_FINGERS_DOWN,

    // One finger is down and it's possible this could be a two-finger-hold.
    ONE_FINGER_DOWN,

    // Two fingers are down and stationary and we will trigger enabling
    // spoken feedback after a delay.
    TWO_FINGERS_DOWN,

    // This is the "reject" state when we get anything other than two fingers
    // held down and stationary. Stay in this state until all fingers are
    // removed.
    WAIT_FOR_NO_FINGERS
  };

  raw_ptr<aura::Window> root_window_;

  // Called when we detect a long-press of two fingers. Not owned.
  raw_ptr<TouchAccessibilityEnablerDelegate> delegate_;

  // The current state.
  State state_;

  // Map of touch ids to their initial locations.
  std::map<int, gfx::PointF> touch_locations_;

  // A timer that triggers after two fingers are held down for a given duration.
  base::RetainingOneShotTimer timer_;

  // A default gesture detector config, so we can share the same
  // timeout and pixel slop constants.
  ui::GestureDetector::Config gesture_detector_config_;

  // When touch_accessibility_enabler gets time relative to real time during
  // testing, this clock is set to the simulated clock and used.
  raw_ptr<const base::TickClock> tick_clock_;

  // Whether or not we currently have an event handler installed. It can
  // be removed when TouchExplorationController is running.
  bool event_handler_installed_ = false;

  base::WeakPtrFactory<TouchAccessibilityEnabler> weak_factory_{this};
};

}  // namespace ash

#endif  // ASH_ACCESSIBILITY_CHROMEVOX_TOUCH_ACCESSIBILITY_ENABLER_H_