touch_disposition_gesture_filter.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_TOUCH_DISPOSITION_GESTURE_FILTER_H_
  5. #define UI_EVENTS_GESTURE_DETECTION_TOUCH_DISPOSITION_GESTURE_FILTER_H_
  6. #include <stdint.h>
  7. #include "base/containers/queue.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "ui/events/gesture_detection/bitset_32.h"
  10. #include "ui/events/gesture_detection/gesture_detection_export.h"
  11. #include "ui/events/gesture_detection/gesture_event_data_packet.h"
  12. #include "ui/events/types/event_type.h"
  13. namespace ui {
  14. // Interface with which the |TouchDispositionGestureFilter| forwards gestures
  15. // for a given touch event.
  16. class GESTURE_DETECTION_EXPORT TouchDispositionGestureFilterClient {
  17. public:
  18. virtual void ForwardGestureEvent(const GestureEventData&) = 0;
  19. };
  20. // Given a stream of touch-derived gesture packets, produces a refined gesture
  21. // sequence based on the ack dispositions of the generating touch events.
  22. class GESTURE_DETECTION_EXPORT TouchDispositionGestureFilter {
  23. public:
  24. explicit TouchDispositionGestureFilter(
  25. TouchDispositionGestureFilterClient* client);
  26. TouchDispositionGestureFilter(const TouchDispositionGestureFilter&) = delete;
  27. TouchDispositionGestureFilter& operator=(
  28. const TouchDispositionGestureFilter&) = delete;
  29. ~TouchDispositionGestureFilter();
  30. // To be called upon production of touch-derived gestures by the platform,
  31. // *prior* to the generating touch being forward to the renderer. In
  32. // particular, |packet| contains [0, n] gestures that correspond to a given
  33. // touch event. It is imperative that a single packet is received for
  34. // *each* touch event, even those that did not produce a gesture.
  35. enum PacketResult {
  36. SUCCESS, // Packet successfully queued.
  37. INVALID_PACKET_ORDER, // Packets were received in the wrong order, i.e.,
  38. // TOUCH_BEGIN should always precede other packets.
  39. INVALID_PACKET_TYPE, // Packet had an invalid type.
  40. };
  41. PacketResult OnGesturePacket(const GestureEventDataPacket& packet);
  42. // OnTouchEventAck must be called upon receipt of every touch event ack.
  43. void OnTouchEventAck(uint32_t unique_touch_event_id,
  44. bool event_consumed,
  45. bool is_source_touch_event_set_blocking);
  46. // Whether there are any active gesture sequences still queued in the filter.
  47. bool IsEmpty() const;
  48. void ResetGestureHandlingState();
  49. private:
  50. // A single GestureSequence corresponds to all gestures created
  51. // between the first finger down and the last finger up, including gestures
  52. // generated by timeouts from a statinoary finger.
  53. using GestureSequence = base::queue<GestureEventDataPacket>;
  54. // Utility class for maintaining the touch and gesture handling state for the
  55. // current gesture sequence.
  56. class GestureHandlingState {
  57. public:
  58. GestureHandlingState();
  59. // To be called on each touch event ack.
  60. void OnTouchEventAck(bool event_consumed, bool is_touch_start_event);
  61. // Returns true iff the gesture should be dropped.
  62. bool Filter(EventType type);
  63. // Whether an event of |type| has been filtered from the current sequence.
  64. bool HasFilteredGestureType(EventType type) const;
  65. private:
  66. // True iff the sequence has had any touch down event consumed.
  67. bool start_touch_consumed_;
  68. // True iff the most recently ack'ed touch event was consumed.
  69. bool current_touch_consumed_;
  70. // Indicates whether the previous gesture of a given type was dropped.
  71. BitSet32 last_gesture_of_type_dropped_;
  72. // Indicates whether *any* previous gesture of a given type was dropped.
  73. BitSet32 any_gesture_of_type_dropped_;
  74. };
  75. void FilterAndSendPacket(const GestureEventDataPacket& packet);
  76. void SendGesture(const GestureEventData& gesture,
  77. const GestureEventDataPacket& packet);
  78. void CancelTapIfNecessary(const GestureEventDataPacket& packet);
  79. void CancelFlingIfNecessary(const GestureEventDataPacket& packet);
  80. void EndScrollIfNecessary(const GestureEventDataPacket& packet);
  81. void PopGestureSequence();
  82. void SendAckedEvents();
  83. GestureSequence& Head();
  84. GestureSequence& Tail();
  85. raw_ptr<TouchDispositionGestureFilterClient> client_;
  86. base::queue<GestureSequence> sequences_;
  87. GestureHandlingState state_;
  88. // Bookkeeping for inserting synthetic Gesture{Tap,Fling}Cancel events
  89. // when necessary, e.g., GestureTapCancel when scrolling begins, or
  90. // GestureFlingCancel when a user taps following a GestureFlingStart.
  91. int ending_event_motion_event_id_;
  92. MotionEvent::ToolType ending_event_primary_tool_type_;
  93. bool needs_tap_ending_event_;
  94. bool needs_show_press_event_;
  95. bool needs_fling_ending_event_;
  96. bool needs_scroll_ending_event_;
  97. };
  98. } // namespace ui
  99. #endif // UI_EVENTS_GESTURE_DETECTION_TOUCH_DISPOSITION_GESTURE_FILTER_H_