gesture_event_details.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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_EVENT_DETAILS_H_
  5. #define UI_EVENTS_GESTURE_EVENT_DETAILS_H_
  6. #include <string.h>
  7. #include "base/check_op.h"
  8. #include "ui/events/event_constants.h"
  9. #include "ui/events/events_base_export.h"
  10. #include "ui/events/types/event_type.h"
  11. #include "ui/events/types/scroll_types.h"
  12. #include "ui/gfx/geometry/rect.h"
  13. #include "ui/gfx/geometry/rect_conversions.h"
  14. #include "ui/gfx/geometry/rect_f.h"
  15. namespace ui {
  16. struct EVENTS_BASE_EXPORT GestureEventDetails {
  17. public:
  18. GestureEventDetails();
  19. explicit GestureEventDetails(EventType type);
  20. GestureEventDetails(EventType type,
  21. float delta_x,
  22. float delta_y,
  23. ui::ScrollGranularity units =
  24. ui::ScrollGranularity::kScrollByPrecisePixel);
  25. // The caller is responsible for ensuring that the gesture data from |other|
  26. // is compatible and sufficient for that expected by gestures of |type|.
  27. GestureEventDetails(EventType type, const GestureEventDetails& other);
  28. EventType type() const { return type_; }
  29. GestureDeviceType device_type() const { return device_type_; }
  30. void set_device_type(GestureDeviceType device_type) {
  31. device_type_ = device_type;
  32. }
  33. bool is_source_touch_event_set_blocking() const {
  34. return is_source_touch_event_set_blocking_;
  35. }
  36. void set_is_source_touch_event_set_blocking(
  37. bool is_source_touch_event_set_blocking) {
  38. is_source_touch_event_set_blocking_ = is_source_touch_event_set_blocking;
  39. }
  40. EventPointerType primary_pointer_type() const {
  41. return primary_pointer_type_;
  42. }
  43. void set_primary_pointer_type(EventPointerType primary_pointer_type) {
  44. primary_pointer_type_ = primary_pointer_type;
  45. }
  46. uint32_t primary_unique_touch_event_id() const {
  47. return primary_unique_touch_event_id_;
  48. }
  49. void set_primary_unique_touch_event_id(uint32_t unique_touch_event_id) {
  50. primary_unique_touch_event_id_ = unique_touch_event_id;
  51. }
  52. int touch_points() const { return touch_points_; }
  53. void set_touch_points(int touch_points) {
  54. DCHECK_GT(touch_points, 0);
  55. touch_points_ = touch_points;
  56. }
  57. const gfx::Rect bounding_box() const {
  58. return ToEnclosingRect(bounding_box_);
  59. }
  60. const gfx::RectF& bounding_box_f() const {
  61. return bounding_box_;
  62. }
  63. void set_bounding_box(const gfx::RectF& box) { bounding_box_ = box; }
  64. float scroll_x_hint() const {
  65. DCHECK_EQ(ET_GESTURE_SCROLL_BEGIN, type_);
  66. return data_.scroll_begin.x_hint;
  67. }
  68. float scroll_y_hint() const {
  69. DCHECK_EQ(ET_GESTURE_SCROLL_BEGIN, type_);
  70. return data_.scroll_begin.y_hint;
  71. }
  72. ui::ScrollGranularity scroll_begin_units() const {
  73. DCHECK_EQ(ET_GESTURE_SCROLL_BEGIN, type_);
  74. return data_.scroll_begin.delta_hint_units;
  75. }
  76. float scroll_x() const {
  77. DCHECK_EQ(ET_GESTURE_SCROLL_UPDATE, type_);
  78. return data_.scroll_update.x;
  79. }
  80. float scroll_y() const {
  81. DCHECK_EQ(ET_GESTURE_SCROLL_UPDATE, type_);
  82. return data_.scroll_update.y;
  83. }
  84. ui::ScrollGranularity scroll_update_units() const {
  85. DCHECK_EQ(ET_GESTURE_SCROLL_UPDATE, type_);
  86. return data_.scroll_update.delta_units;
  87. }
  88. float velocity_x() const {
  89. DCHECK_EQ(ET_SCROLL_FLING_START, type_);
  90. return data_.fling_velocity.x;
  91. }
  92. float velocity_y() const {
  93. DCHECK_EQ(ET_SCROLL_FLING_START, type_);
  94. return data_.fling_velocity.y;
  95. }
  96. float first_finger_width() const {
  97. DCHECK_EQ(ET_GESTURE_TWO_FINGER_TAP, type_);
  98. return data_.first_finger_enclosing_rectangle.width;
  99. }
  100. float first_finger_height() const {
  101. DCHECK_EQ(ET_GESTURE_TWO_FINGER_TAP, type_);
  102. return data_.first_finger_enclosing_rectangle.height;
  103. }
  104. float scale() const {
  105. DCHECK_EQ(ET_GESTURE_PINCH_UPDATE, type_);
  106. return data_.scale;
  107. }
  108. bool swipe_left() const {
  109. DCHECK_EQ(ET_GESTURE_SWIPE, type_);
  110. return data_.swipe.left;
  111. }
  112. bool swipe_right() const {
  113. DCHECK_EQ(ET_GESTURE_SWIPE, type_);
  114. return data_.swipe.right;
  115. }
  116. bool swipe_up() const {
  117. DCHECK_EQ(ET_GESTURE_SWIPE, type_);
  118. return data_.swipe.up;
  119. }
  120. bool swipe_down() const {
  121. DCHECK_EQ(ET_GESTURE_SWIPE, type_);
  122. return data_.swipe.down;
  123. }
  124. int tap_count() const {
  125. DCHECK(type_ == ET_GESTURE_TAP ||
  126. type_ == ET_GESTURE_TAP_UNCONFIRMED ||
  127. type_ == ET_GESTURE_DOUBLE_TAP);
  128. return data_.tap_count;
  129. }
  130. void set_tap_count(int tap_count) {
  131. DCHECK_GE(tap_count, 0);
  132. DCHECK(type_ == ET_GESTURE_TAP ||
  133. type_ == ET_GESTURE_TAP_UNCONFIRMED ||
  134. type_ == ET_GESTURE_DOUBLE_TAP);
  135. data_.tap_count = tap_count;
  136. }
  137. void set_scale(float scale) {
  138. DCHECK_GE(scale, 0.0f);
  139. DCHECK_EQ(type_, ET_GESTURE_PINCH_UPDATE);
  140. data_.scale = scale;
  141. }
  142. // Supports comparison over internal structures for testing.
  143. bool operator==(const GestureEventDetails& other) const {
  144. return type_ == other.type_ &&
  145. !memcmp(&data_, &other.data_, sizeof(Details)) &&
  146. device_type_ == other.device_type_ &&
  147. touch_points_ == other.touch_points_ &&
  148. bounding_box_ == other.bounding_box_;
  149. }
  150. private:
  151. EventType type_;
  152. union Details {
  153. Details();
  154. struct { // SCROLL start details.
  155. // Distance that caused the scroll to start. Generally redundant with
  156. // the x/y values from the first scroll_update.
  157. float x_hint;
  158. float y_hint;
  159. ui::ScrollGranularity delta_hint_units;
  160. } scroll_begin;
  161. struct { // SCROLL delta.
  162. float x;
  163. float y;
  164. ui::ScrollGranularity delta_units;
  165. // Whether any previous scroll update in the current scroll sequence was
  166. // suppressed because the underlying touch was consumed.
  167. } scroll_update;
  168. float scale; // PINCH scale.
  169. struct { // FLING velocity.
  170. float x;
  171. float y;
  172. } fling_velocity;
  173. // Dimensions of the first finger's enclosing rectangle for
  174. // TWO_FINGER_TAP.
  175. struct {
  176. float width;
  177. float height;
  178. } first_finger_enclosing_rectangle;
  179. struct { // SWIPE direction.
  180. bool left;
  181. bool right;
  182. bool up;
  183. bool down;
  184. } swipe;
  185. // Tap information must be set for ET_GESTURE_TAP,
  186. // ET_GESTURE_TAP_UNCONFIRMED, and ET_GESTURE_DOUBLE_TAP events.
  187. int tap_count; // TAP repeat count.
  188. } data_;
  189. GestureDeviceType device_type_;
  190. bool is_source_touch_event_set_blocking_ = false;
  191. // The pointer type for the first touch point in the gesture.
  192. EventPointerType primary_pointer_type_ = EventPointerType::kUnknown;
  193. // The unique touch id for the first touch in the gesture.
  194. uint32_t primary_unique_touch_event_id_ = 0;
  195. int touch_points_; // Number of active touch points in the gesture.
  196. // Bounding box is an axis-aligned rectangle that contains all the
  197. // enclosing rectangles of the touch-points in the gesture.
  198. gfx::RectF bounding_box_;
  199. };
  200. } // namespace ui
  201. #endif // UI_EVENTS_GESTURE_EVENT_DETAILS_H_