1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_dom_TouchEvent_h_
7 #define mozilla_dom_TouchEvent_h_
9 #include "mozilla/dom/Touch.h"
10 #include "mozilla/dom/TouchEventBinding.h"
11 #include "mozilla/dom/UIEvent.h"
12 #include "mozilla/Attributes.h"
13 #include "mozilla/EventForwards.h"
14 #include "mozilla/TouchEvents.h"
15 #include "nsJSEnvironment.h"
16 #include "nsStringFwd.h"
17 #include "nsWrapperCache.h"
22 class TouchList final
: public nsISupports
, public nsWrapperCache
{
24 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
25 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(TouchList
)
27 explicit TouchList(nsISupports
* aParent
) : mParent(aParent
) {
28 nsJSContext::LikelyShortLivingObjectCreated();
30 TouchList(nsISupports
* aParent
, const WidgetTouchEvent::TouchArray
& aTouches
)
31 : mParent(aParent
), mPoints(aTouches
.Clone()) {
32 nsJSContext::LikelyShortLivingObjectCreated();
35 void Append(Touch
* aPoint
) { mPoints
.AppendElement(aPoint
); }
37 virtual JSObject
* WrapObject(JSContext
* aCx
,
38 JS::Handle
<JSObject
*> aGivenProto
) override
;
40 nsISupports
* GetParentObject() const { return mParent
; }
42 static bool PrefEnabled(JSContext
* aCx
, JSObject
* aGlobal
);
44 uint32_t Length() const { return mPoints
.Length(); }
45 Touch
* Item(uint32_t aIndex
) const { return mPoints
.SafeElementAt(aIndex
); }
46 Touch
* IndexedGetter(uint32_t aIndex
, bool& aFound
) const {
47 aFound
= aIndex
< mPoints
.Length();
51 return mPoints
[aIndex
];
54 void Clear() { mPoints
.Clear(); }
57 ~TouchList() = default;
59 nsCOMPtr
<nsISupports
> mParent
;
60 WidgetTouchEvent::TouchArray mPoints
;
63 class TouchEvent
: public UIEvent
{
65 TouchEvent(EventTarget
* aOwner
, nsPresContext
* aPresContext
,
66 WidgetTouchEvent
* aEvent
);
68 NS_DECL_ISUPPORTS_INHERITED
69 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(TouchEvent
, UIEvent
)
71 virtual JSObject
* WrapObjectInternal(
72 JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) override
{
73 return TouchEvent_Binding::Wrap(aCx
, this, aGivenProto
);
76 already_AddRefed
<TouchList
> CopyTouches(
77 const Sequence
<OwningNonNull
<Touch
>>& aTouches
);
80 // TargetTouches() populates mTargetTouches from widget event's touch list.
81 TouchList
* TargetTouches();
82 // GetExistingTargetTouches just returns the existing target touches list.
83 TouchList
* GetExistingTargetTouches() { return mTargetTouches
; }
84 TouchList
* ChangedTouches();
91 void InitTouchEvent(const nsAString
& aType
, bool aCanBubble
, bool aCancelable
,
92 nsGlobalWindowInner
* aView
, int32_t aDetail
,
93 bool aCtrlKey
, bool aAltKey
, bool aShiftKey
,
94 bool aMetaKey
, TouchList
* aTouches
,
95 TouchList
* aTargetTouches
, TouchList
* aChangedTouches
);
97 static bool PlatformSupportsTouch();
98 static bool PrefEnabled(JSContext
* aCx
, JSObject
* aGlobal
);
99 static bool PrefEnabled(nsIDocShell
* aDocShell
);
100 static bool LegacyAPIEnabled(JSContext
* aCx
, JSObject
* aGlobal
);
101 static bool LegacyAPIEnabled(nsIDocShell
* aDocShell
, bool aCallerIsSystem
);
103 static already_AddRefed
<TouchEvent
> Constructor(const GlobalObject
& aGlobal
,
104 const nsAString
& aType
,
105 const TouchEventInit
& aParam
);
108 ~TouchEvent() = default;
110 void AssignTouchesToWidgetEvent(TouchList
* aList
, bool aCheckDuplicates
);
112 RefPtr
<TouchList
> mTouches
;
113 RefPtr
<TouchList
> mTargetTouches
;
114 RefPtr
<TouchList
> mChangedTouches
;
118 } // namespace mozilla
120 already_AddRefed
<mozilla::dom::TouchEvent
> NS_NewDOMTouchEvent(
121 mozilla::dom::EventTarget
* aOwner
, nsPresContext
* aPresContext
,
122 mozilla::WidgetTouchEvent
* aEvent
);
124 #endif // mozilla_dom_TouchEvent_h_