Bug 1885489 - Part 5: Add SnapshotIterator::readInt32(). r=iain
[gecko.git] / widget / TouchEvents.h
blob5259276d52fba357c8be8074e7027a7a3de8aac5
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_TouchEvents_h__
7 #define mozilla_TouchEvents_h__
9 #include <stdint.h>
11 #include "mozilla/dom/Touch.h"
12 #include "mozilla/MouseEvents.h"
13 #include "mozilla/RefPtr.h"
14 #include "nsTArray.h"
16 namespace mozilla {
18 /******************************************************************************
19 * mozilla::WidgetGestureNotifyEvent
21 * This event is the first event generated when the user touches
22 * the screen with a finger, and it's meant to decide what kind
23 * of action we'll use for that touch interaction.
25 * The event is dispatched to the layout and based on what is underneath
26 * the initial contact point it's then decided if we should pan
27 * (finger scrolling) or drag the target element.
28 ******************************************************************************/
30 class WidgetGestureNotifyEvent : public WidgetGUIEvent {
31 public:
32 virtual WidgetGestureNotifyEvent* AsGestureNotifyEvent() override {
33 return this;
36 WidgetGestureNotifyEvent(bool aIsTrusted, EventMessage aMessage,
37 nsIWidget* aWidget,
38 const WidgetEventTime* aTime = nullptr)
39 : WidgetGUIEvent(aIsTrusted, aMessage, aWidget, eGestureNotifyEventClass,
40 aTime),
41 mPanDirection(ePanNone),
42 mDisplayPanFeedback(false) {}
44 virtual WidgetEvent* Duplicate() const override {
45 // XXX Looks like this event is handled only in PostHandleEvent() of
46 // EventStateManager. Therefore, it might be possible to handle this
47 // in PreHandleEvent() and not to dispatch as a DOM event into the DOM
48 // tree like ContentQueryEvent. Then, this event doesn't need to
49 // support Duplicate().
50 MOZ_ASSERT(mClass == eGestureNotifyEventClass,
51 "Duplicate() must be overridden by sub class");
52 // Not copying widget, it is a weak reference.
53 WidgetGestureNotifyEvent* result =
54 new WidgetGestureNotifyEvent(false, mMessage, nullptr, this);
55 result->AssignGestureNotifyEventData(*this, true);
56 result->mFlags = mFlags;
57 return result;
60 typedef int8_t PanDirectionType;
61 enum PanDirection : PanDirectionType {
62 ePanNone,
63 ePanVertical,
64 ePanHorizontal,
65 ePanBoth
68 PanDirection mPanDirection;
69 bool mDisplayPanFeedback;
71 void AssignGestureNotifyEventData(const WidgetGestureNotifyEvent& aEvent,
72 bool aCopyTargets) {
73 AssignGUIEventData(aEvent, aCopyTargets);
75 mPanDirection = aEvent.mPanDirection;
76 mDisplayPanFeedback = aEvent.mDisplayPanFeedback;
80 /******************************************************************************
81 * mozilla::WidgetSimpleGestureEvent
82 ******************************************************************************/
84 class WidgetSimpleGestureEvent : public WidgetMouseEventBase {
85 public:
86 virtual WidgetSimpleGestureEvent* AsSimpleGestureEvent() override {
87 return this;
90 WidgetSimpleGestureEvent(bool aIsTrusted, EventMessage aMessage,
91 nsIWidget* aWidget,
92 const WidgetEventTime* aTime = nullptr)
93 : WidgetMouseEventBase(aIsTrusted, aMessage, aWidget,
94 eSimpleGestureEventClass, aTime),
95 mAllowedDirections(0),
96 mDirection(0),
97 mClickCount(0),
98 mDelta(0.0) {}
100 WidgetSimpleGestureEvent(const WidgetSimpleGestureEvent& aOther)
101 : WidgetMouseEventBase(aOther.IsTrusted(), aOther.mMessage,
102 aOther.mWidget, eSimpleGestureEventClass),
103 mAllowedDirections(aOther.mAllowedDirections),
104 mDirection(aOther.mDirection),
105 mClickCount(0),
106 mDelta(aOther.mDelta) {}
108 virtual WidgetEvent* Duplicate() const override {
109 MOZ_ASSERT(mClass == eSimpleGestureEventClass,
110 "Duplicate() must be overridden by sub class");
111 // Not copying widget, it is a weak reference.
112 WidgetSimpleGestureEvent* result =
113 new WidgetSimpleGestureEvent(false, mMessage, nullptr, this);
114 result->AssignSimpleGestureEventData(*this, true);
115 result->mFlags = mFlags;
116 return result;
119 // See SimpleGestureEvent.webidl for values
120 uint32_t mAllowedDirections;
121 // See SimpleGestureEvent.webidl for values
122 uint32_t mDirection;
123 // The number of taps for tap events
124 uint32_t mClickCount;
125 // Delta for magnify and rotate events
126 double mDelta;
128 // XXX Not tested by test_assign_event_data.html
129 void AssignSimpleGestureEventData(const WidgetSimpleGestureEvent& aEvent,
130 bool aCopyTargets) {
131 AssignMouseEventBaseData(aEvent, aCopyTargets);
133 // mAllowedDirections isn't copied
134 mDirection = aEvent.mDirection;
135 mDelta = aEvent.mDelta;
136 mClickCount = aEvent.mClickCount;
140 /******************************************************************************
141 * mozilla::WidgetTouchEvent
142 ******************************************************************************/
144 class WidgetTouchEvent final : public WidgetInputEvent {
145 public:
146 typedef nsTArray<RefPtr<mozilla::dom::Touch>> TouchArray;
147 typedef AutoTArray<RefPtr<mozilla::dom::Touch>, 10> AutoTouchArray;
148 typedef AutoTouchArray::base_type TouchArrayBase;
150 WidgetTouchEvent* AsTouchEvent() override { return this; }
152 MOZ_COUNTED_DEFAULT_CTOR(WidgetTouchEvent)
154 WidgetTouchEvent(const WidgetTouchEvent& aOther)
155 : WidgetInputEvent(aOther.IsTrusted(), aOther.mMessage, aOther.mWidget,
156 eTouchEventClass) {
157 MOZ_COUNT_CTOR(WidgetTouchEvent);
158 mModifiers = aOther.mModifiers;
159 mTimeStamp = aOther.mTimeStamp;
160 mTouches.AppendElements(aOther.mTouches);
161 mInputSource = aOther.mInputSource;
162 mButton = aOther.mButton;
163 mButtons = aOther.mButtons;
164 mFlags.mCancelable = mMessage != eTouchCancel;
165 mFlags.mHandledByAPZ = aOther.mFlags.mHandledByAPZ;
168 WidgetTouchEvent(WidgetTouchEvent&& aOther)
169 : WidgetInputEvent(std::move(aOther)) {
170 MOZ_COUNT_CTOR(WidgetTouchEvent);
171 mModifiers = aOther.mModifiers;
172 mTimeStamp = aOther.mTimeStamp;
173 mTouches = std::move(aOther.mTouches);
174 mInputSource = aOther.mInputSource;
175 mButton = aOther.mButton;
176 mButtons = aOther.mButtons;
177 mFlags = aOther.mFlags;
180 WidgetTouchEvent& operator=(WidgetTouchEvent&&) = default;
182 WidgetTouchEvent(bool aIsTrusted, EventMessage aMessage, nsIWidget* aWidget,
183 const WidgetEventTime* aTime = nullptr)
184 : WidgetInputEvent(aIsTrusted, aMessage, aWidget, eTouchEventClass,
185 aTime) {
186 MOZ_COUNT_CTOR(WidgetTouchEvent);
187 mFlags.mCancelable = mMessage != eTouchCancel;
190 MOZ_COUNTED_DTOR_OVERRIDE(WidgetTouchEvent)
192 WidgetEvent* Duplicate() const override {
193 MOZ_ASSERT(mClass == eTouchEventClass,
194 "Duplicate() must be overridden by sub class");
195 // Not copying widget, it is a weak reference.
196 WidgetTouchEvent* result =
197 new WidgetTouchEvent(false, mMessage, nullptr, this);
198 result->AssignTouchEventData(*this, true);
199 result->mFlags = mFlags;
200 return result;
203 TouchArray mTouches;
204 uint16_t mInputSource = 5; // MouseEvent_Binding::MOZ_SOURCE_TOUCH
205 int16_t mButton = eNotPressed;
206 int16_t mButtons = 0;
208 void AssignTouchEventData(const WidgetTouchEvent& aEvent, bool aCopyTargets) {
209 AssignInputEventData(aEvent, aCopyTargets);
211 // Assign*EventData() assume that they're called only new instance.
212 MOZ_ASSERT(mTouches.IsEmpty());
213 mTouches.AppendElements(aEvent.mTouches);
214 mInputSource = aEvent.mInputSource;
218 } // namespace mozilla
220 #endif // mozilla_TouchEvents_h__