input_event_activation_protector.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (c) 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. #ifndef UI_VIEWS_INPUT_EVENT_ACTIVATION_PROTECTOR_H_
  5. #define UI_VIEWS_INPUT_EVENT_ACTIVATION_PROTECTOR_H_
  6. #include "base/time/time.h"
  7. #include "ui/views/views_export.h"
  8. namespace ui {
  9. class Event;
  10. }
  11. namespace views {
  12. // The goal of this class is to prevent potentially unintentional user
  13. // interaction with a UI element.
  14. class VIEWS_EXPORT InputEventActivationProtector {
  15. public:
  16. InputEventActivationProtector() = default;
  17. InputEventActivationProtector(const InputEventActivationProtector&) = delete;
  18. InputEventActivationProtector& operator=(
  19. const InputEventActivationProtector&) = delete;
  20. ~InputEventActivationProtector() = default;
  21. // Updates the state of the protector based off of visibility changes. This
  22. // method must be called when the visibility of the view is changed.
  23. void VisibilityChanged(bool is_visible);
  24. // Returns true if the event is a mouse, touch, or pointer event that took
  25. // place within the double-click time interval after |view_shown_time_stamp_|.
  26. bool IsPossiblyUnintendedInteraction(const ui::Event& event);
  27. // Resets the state for click tracking.
  28. void ResetForTesting();
  29. // Integration tests can disable all input event activation protectors.
  30. static void DisableForTesting();
  31. private:
  32. // Timestamp of when the view being tracked is first shown.
  33. base::TimeTicks view_shown_time_stamp_;
  34. // Timestamp of the last event.
  35. base::TimeTicks last_event_timestamp_;
  36. // Number of repeated UI events with short intervals.
  37. size_t repeated_event_count_ = 0;
  38. };
  39. } // namespace views
  40. #endif // UI_VIEWS_INPUT_EVENT_ACTIVATION_PROTECTOR_H_