input_event_activation_protector.cc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #include "ui/views/input_event_activation_protector.h"
  5. #include "ui/events/event.h"
  6. #include "ui/views/metrics.h"
  7. namespace views {
  8. namespace {
  9. bool g_disable_for_testing = false;
  10. } // namespace
  11. void InputEventActivationProtector::VisibilityChanged(bool is_visible) {
  12. if (is_visible)
  13. view_shown_time_stamp_ = base::TimeTicks::Now();
  14. }
  15. bool InputEventActivationProtector::IsPossiblyUnintendedInteraction(
  16. const ui::Event& event) {
  17. if (g_disable_for_testing)
  18. return false;
  19. if (view_shown_time_stamp_ == base::TimeTicks()) {
  20. // The UI was never shown, ignore. This can happen in tests.
  21. return false;
  22. }
  23. // Don't let key repeats close the dialog, they might've been held when the
  24. // dialog pops up.
  25. if (event.IsKeyEvent() && event.AsKeyEvent()->is_repeat())
  26. return true;
  27. if (!event.IsMouseEvent() && !event.IsTouchEvent())
  28. return false;
  29. const base::TimeDelta kShortInterval =
  30. base::Milliseconds(GetDoubleClickInterval());
  31. const bool short_event_after_last_event =
  32. event.time_stamp() < last_event_timestamp_ + kShortInterval;
  33. last_event_timestamp_ = event.time_stamp();
  34. // Unintended if the user has been clicking with short intervals.
  35. if (short_event_after_last_event) {
  36. repeated_event_count_++;
  37. return true;
  38. }
  39. repeated_event_count_ = 0;
  40. // Unintended if the user clicked right after the UI showed.
  41. return event.time_stamp() < view_shown_time_stamp_ + kShortInterval;
  42. }
  43. void InputEventActivationProtector::ResetForTesting() {
  44. view_shown_time_stamp_ = base::TimeTicks();
  45. last_event_timestamp_ = base::TimeTicks();
  46. repeated_event_count_ = 0;
  47. }
  48. void InputEventActivationProtector::DisableForTesting() {
  49. g_disable_for_testing = true;
  50. }
  51. } // namespace views