scale_gesture_detector.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2014 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_EVENTS_GESTURE_DETECTION_SCALE_GESTURE_DETECTOR_H_
  5. #define UI_EVENTS_GESTURE_DETECTION_SCALE_GESTURE_DETECTOR_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/time/time.h"
  8. #include "ui/events/gesture_detection/gesture_detection_export.h"
  9. namespace ui {
  10. class MotionEvent;
  11. class ScaleGestureListener;
  12. // Port of ScaleGestureDetector.java from Android
  13. // * platform/frameworks/base/core/java/android/view/ScaleGestureDetector.java
  14. // * Change-Id: I3e7926a4f6f9ab4951f380bd004499c78b3bda69
  15. // * Please update the Change-Id as upstream Android changes are pulled.
  16. class GESTURE_DETECTION_EXPORT ScaleGestureDetector {
  17. public:
  18. struct GESTURE_DETECTION_EXPORT Config {
  19. Config();
  20. ~Config();
  21. // Distance the current span can deviate from the initial span before
  22. // scaling will start (in dips). The span is the diameter of the circle with
  23. // a radius of average pointer deviation from the focal point.
  24. float span_slop;
  25. // Minimum span needed to initiate a scaling gesture (in dips).
  26. float min_scaling_span;
  27. // Minimum pinch span change before pinch occurs (in dips). See
  28. // crbug.com/373318.
  29. float min_pinch_update_span_delta;
  30. // Whether the associated |ScaleGestureListener| should receive |OnScale|
  31. // callbacks when the user uses a stylus and presses the button.
  32. // Defaults to false.
  33. bool stylus_scale_enabled;
  34. };
  35. ScaleGestureDetector(const Config& config, ScaleGestureListener* listener);
  36. ScaleGestureDetector(const ScaleGestureDetector&) = delete;
  37. ScaleGestureDetector& operator=(const ScaleGestureDetector&) = delete;
  38. virtual ~ScaleGestureDetector();
  39. // Accepts MotionEvents and dispatches events to a |ScaleGestureListener|
  40. // when appropriate.
  41. //
  42. // Note: Applications should pass a complete and consistent event stream to
  43. // this method. A complete and consistent event stream involves all
  44. // MotionEvents from the initial Action::DOWN to the final Action::UP or
  45. // Action::CANCEL.
  46. //
  47. // Returns true if the event was processed and the detector wants to receive
  48. // the rest of the MotionEvents in this event stream.
  49. bool OnTouchEvent(const MotionEvent& event);
  50. // This method may be called by the owner when a a double-tap event has been
  51. // detected *for the same event stream* being fed to this instance of the
  52. // ScaleGestureDetector. As call order is important here, the double-tap
  53. // detector should always be offered events *before* the ScaleGestureDetector.
  54. bool OnDoubleTap(const MotionEvent& event);
  55. // Set whether the associated |ScaleGestureListener| should receive
  56. // OnScale callbacks when the user performs a doubletap followed by a swipe.
  57. bool IsInProgress() const;
  58. bool InAnchoredScaleMode() const;
  59. float GetFocusX() const;
  60. float GetFocusY() const;
  61. float GetCurrentSpan() const;
  62. float GetCurrentSpanX() const;
  63. float GetCurrentSpanY() const;
  64. float GetPreviousSpan() const;
  65. float GetPreviousSpanX() const;
  66. float GetPreviousSpanY() const;
  67. float GetScaleFactor() const;
  68. base::TimeDelta GetTimeDelta() const;
  69. base::TimeTicks GetEventTime() const;
  70. private:
  71. enum AnchoredScaleMode {
  72. ANCHORED_SCALE_MODE_NONE,
  73. ANCHORED_SCALE_MODE_DOUBLE_TAP,
  74. ANCHORED_SCALE_MODE_STYLUS
  75. };
  76. void ResetScaleWithSpan(float span);
  77. const raw_ptr<ScaleGestureListener> listener_;
  78. bool stylus_scale_enabled_;
  79. float focus_x_;
  80. float focus_y_;
  81. float curr_span_;
  82. float prev_span_;
  83. float initial_span_;
  84. float curr_span_x_;
  85. float curr_span_y_;
  86. float prev_span_x_;
  87. float prev_span_y_;
  88. base::TimeTicks curr_time_;
  89. base::TimeTicks prev_time_;
  90. bool in_progress_;
  91. float span_slop_;
  92. float min_span_;
  93. float anchored_scale_start_x_;
  94. float anchored_scale_start_y_;
  95. AnchoredScaleMode anchored_scale_mode_;
  96. bool event_before_or_above_starting_gesture_event_;
  97. };
  98. } // namespace ui
  99. #endif // UI_EVENTS_GESTURE_DETECTION_SCALE_GESTURE_DETECTOR_H_