Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / widget / headless / HeadlessWidget.h
blob39833c28e40c61e354119cde429b8389056bafac
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 HEADLESSWIDGET_H
7 #define HEADLESSWIDGET_H
9 #include "mozilla/widget/InProcessCompositorWidget.h"
10 #include "nsBaseWidget.h"
11 #include "CompositorWidget.h"
12 #include "mozilla/dom/WheelEventBinding.h"
14 // The various synthesized event values are hardcoded to avoid pulling
15 // in the platform specific widget code.
16 #if defined(MOZ_WIDGET_GTK)
17 # define MOZ_HEADLESS_SCROLL_MULTIPLIER 3
18 # define MOZ_HEADLESS_SCROLL_DELTA_MODE \
19 mozilla::dom::WheelEvent_Binding::DOM_DELTA_LINE
20 #elif defined(XP_WIN)
21 # define MOZ_HEADLESS_SCROLL_MULTIPLIER \
22 .025 // default scroll lines (3) / WHEEL_DELTA (120)
23 # define MOZ_HEADLESS_SCROLL_DELTA_MODE \
24 mozilla::dom::WheelEvent_Binding::DOM_DELTA_LINE
25 #elif defined(XP_MACOSX)
26 # define MOZ_HEADLESS_SCROLL_MULTIPLIER 1
27 # define MOZ_HEADLESS_SCROLL_DELTA_MODE \
28 mozilla::dom::WheelEvent_Binding::DOM_DELTA_PIXEL
29 #elif defined(ANDROID)
30 # define MOZ_HEADLESS_SCROLL_MULTIPLIER 1
31 # define MOZ_HEADLESS_SCROLL_DELTA_MODE \
32 mozilla::dom::WheelEvent_Binding::DOM_DELTA_LINE
33 #else
34 # define MOZ_HEADLESS_SCROLL_MULTIPLIER -1
35 # define MOZ_HEADLESS_SCROLL_DELTA_MODE -1
36 #endif
38 namespace mozilla {
39 enum class NativeKeyBindingsType : uint8_t;
40 namespace widget {
42 class HeadlessWidget final : public nsBaseWidget {
43 public:
44 HeadlessWidget();
46 NS_INLINE_DECL_REFCOUNTING_INHERITED(HeadlessWidget, nsBaseWidget)
48 void* GetNativeData(uint32_t aDataType) override {
49 // Headless widgets have no native data.
50 return nullptr;
53 nsresult Create(nsIWidget* aParent, const LayoutDeviceIntRect& aRect,
54 widget::InitData* aInitData = nullptr) override;
55 using nsBaseWidget::Create; // for Create signature not overridden here
57 void GetCompositorWidgetInitData(
58 mozilla::widget::CompositorWidgetInitData* aInitData) override;
60 void Destroy() override;
61 void Show(bool aState) override;
62 bool IsVisible() const override;
63 void Move(double aX, double aY) override;
64 void Resize(double aWidth, double aHeight, bool aRepaint) override;
65 void Resize(double aX, double aY, double aWidth, double aHeight,
66 bool aRepaint) override;
67 nsSizeMode SizeMode() override { return mSizeMode; }
68 void SetSizeMode(nsSizeMode aMode) override;
69 nsresult MakeFullScreen(bool aFullScreen) override;
70 void Enable(bool aState) override;
71 bool IsEnabled() const override;
72 void SetFocus(Raise, mozilla::dom::CallerType aCallerType) override;
73 void Invalidate(const LayoutDeviceIntRect& aRect) override {
74 // TODO: see if we need to do anything here.
76 nsresult SetTitle(const nsAString& title) override {
77 // Headless widgets have no title, so just ignore it.
78 return NS_OK;
80 LayoutDeviceIntPoint WidgetToScreenOffset() override;
81 void SetInputContext(const InputContext& aContext,
82 const InputContextAction& aAction) override {
83 mInputContext = aContext;
85 InputContext GetInputContext() override { return mInputContext; }
87 WindowRenderer* GetWindowRenderer() override;
89 void SetCompositorWidgetDelegate(CompositorWidgetDelegate* delegate) override;
91 [[nodiscard]] nsresult AttachNativeKeyEvent(
92 WidgetKeyboardEvent& aEvent) override;
93 MOZ_CAN_RUN_SCRIPT bool GetEditCommands(
94 NativeKeyBindingsType aType, const WidgetKeyboardEvent& aEvent,
95 nsTArray<CommandInt>& aCommands) override;
97 nsresult DispatchEvent(WidgetGUIEvent* aEvent,
98 nsEventStatus& aStatus) override;
100 nsresult SynthesizeNativeMouseEvent(LayoutDeviceIntPoint aPoint,
101 NativeMouseMessage aNativeMessage,
102 mozilla::MouseButton aButton,
103 nsIWidget::Modifiers aModifierFlags,
104 nsIObserver* aObserver) override;
105 nsresult SynthesizeNativeMouseMove(LayoutDeviceIntPoint aPoint,
106 nsIObserver* aObserver) override {
107 return SynthesizeNativeMouseEvent(
108 aPoint, NativeMouseMessage::Move, mozilla::MouseButton::eNotPressed,
109 nsIWidget::Modifiers::NO_MODIFIERS, aObserver);
112 nsresult SynthesizeNativeMouseScrollEvent(
113 LayoutDeviceIntPoint aPoint, uint32_t aNativeMessage, double aDeltaX,
114 double aDeltaY, double aDeltaZ, uint32_t aModifierFlags,
115 uint32_t aAdditionalFlags, nsIObserver* aObserver) override;
117 nsresult SynthesizeNativeTouchPoint(uint32_t aPointerId,
118 TouchPointerState aPointerState,
119 LayoutDeviceIntPoint aPoint,
120 double aPointerPressure,
121 uint32_t aPointerOrientation,
122 nsIObserver* aObserver) override;
124 nsresult SynthesizeNativeTouchPadPinch(TouchpadGesturePhase aEventPhase,
125 float aScale,
126 LayoutDeviceIntPoint aPoint,
127 int32_t aModifierFlags) override;
129 nsresult SynthesizeNativeTouchpadPan(TouchpadGesturePhase aEventPhase,
130 LayoutDeviceIntPoint aPoint,
131 double aDeltaX, double aDeltaY,
132 int32_t aModifierFlags,
133 nsIObserver* aObserver) override;
135 private:
136 ~HeadlessWidget();
137 bool mEnabled;
138 bool mVisible;
139 bool mDestroyed;
140 bool mAlwaysOnTop;
141 HeadlessCompositorWidget* mCompositorWidget;
142 nsSizeMode mSizeMode;
143 // The size mode before entering fullscreen mode.
144 nsSizeMode mLastSizeMode;
145 // The last size mode set while the window was visible.
146 nsSizeMode mEffectiveSizeMode;
147 mozilla::ScreenCoord mLastPinchSpan;
148 InputContext mInputContext;
149 mozilla::UniquePtr<mozilla::MultiTouchInput> mSynthesizedTouchInput;
150 // In headless there is no window manager to track window bounds
151 // across size mode changes, so we must track it to emulate.
152 LayoutDeviceIntRect mRestoreBounds;
153 void ApplySizeModeSideEffects();
154 // Move while maintaining size mode.
155 void MoveInternal(int32_t aX, int32_t aY);
156 // Resize while maintaining size mode.
157 void ResizeInternal(int32_t aWidth, int32_t aHeight, bool aRepaint);
158 // Similarly, we must track the active window ourselves in order
159 // to dispatch (de)activation events properly.
160 void RaiseWindow();
161 // The top level widgets are tracked for window ordering. They are
162 // stored in order of activation where the last element is always the
163 // currently active widget.
164 static StaticAutoPtr<nsTArray<HeadlessWidget*>> sActiveWindows;
165 // Get the most recently activated widget or null if there are none.
166 static already_AddRefed<HeadlessWidget> GetActiveWindow();
169 } // namespace widget
170 } // namespace mozilla
172 #endif