Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / widget / headless / HeadlessWidget.h
blob9856991ef32f25f51942f8cd664a09bec2192c70
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 : 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 virtual nsresult Create(nsIWidget* aParent, nsNativeWidget aNativeParent,
54 const LayoutDeviceIntRect& aRect,
55 widget::InitData* aInitData = nullptr) override;
56 using nsBaseWidget::Create; // for Create signature not overridden here
57 virtual already_AddRefed<nsIWidget> CreateChild(
58 const LayoutDeviceIntRect& aRect, widget::InitData* aInitData = nullptr,
59 bool aForceUseIWidgetParent = false) override;
61 virtual nsIWidget* GetTopLevelWidget() override;
63 virtual void GetCompositorWidgetInitData(
64 mozilla::widget::CompositorWidgetInitData* aInitData) override;
66 virtual void Destroy() override;
67 virtual void Show(bool aState) override;
68 virtual bool IsVisible() const override;
69 virtual void Move(double aX, double aY) override;
70 virtual void Resize(double aWidth, double aHeight, bool aRepaint) override;
71 virtual void Resize(double aX, double aY, double aWidth, double aHeight,
72 bool aRepaint) override;
73 virtual nsSizeMode SizeMode() override { return mSizeMode; }
74 virtual void SetSizeMode(nsSizeMode aMode) override;
75 virtual nsresult MakeFullScreen(bool aFullScreen) override;
76 virtual void Enable(bool aState) override;
77 virtual bool IsEnabled() const override;
78 virtual void SetFocus(Raise, mozilla::dom::CallerType aCallerType) override;
79 virtual void Invalidate(const LayoutDeviceIntRect& aRect) override {
80 // TODO: see if we need to do anything here.
82 virtual nsresult SetTitle(const nsAString& title) override {
83 // Headless widgets have no title, so just ignore it.
84 return NS_OK;
86 virtual nsresult SetNonClientMargins(
87 const LayoutDeviceIntMargin& margins) override {
88 // Headless widgets have no chrome margins, so just ignore the call.
89 return NS_OK;
91 virtual LayoutDeviceIntPoint WidgetToScreenOffset() override;
92 virtual void SetInputContext(const InputContext& aContext,
93 const InputContextAction& aAction) override {
94 mInputContext = aContext;
96 virtual InputContext GetInputContext() override { return mInputContext; }
98 virtual WindowRenderer* GetWindowRenderer() override;
100 void SetCompositorWidgetDelegate(CompositorWidgetDelegate* delegate) override;
102 [[nodiscard]] virtual nsresult AttachNativeKeyEvent(
103 WidgetKeyboardEvent& aEvent) override;
104 MOZ_CAN_RUN_SCRIPT virtual bool GetEditCommands(
105 NativeKeyBindingsType aType, const WidgetKeyboardEvent& aEvent,
106 nsTArray<CommandInt>& aCommands) override;
108 virtual nsresult DispatchEvent(WidgetGUIEvent* aEvent,
109 nsEventStatus& aStatus) override;
111 virtual nsresult SynthesizeNativeMouseEvent(
112 LayoutDeviceIntPoint aPoint, NativeMouseMessage aNativeMessage,
113 mozilla::MouseButton aButton, nsIWidget::Modifiers aModifierFlags,
114 nsIObserver* aObserver) override;
115 virtual nsresult SynthesizeNativeMouseMove(LayoutDeviceIntPoint aPoint,
116 nsIObserver* aObserver) override {
117 return SynthesizeNativeMouseEvent(
118 aPoint, NativeMouseMessage::Move, mozilla::MouseButton::eNotPressed,
119 nsIWidget::Modifiers::NO_MODIFIERS, aObserver);
122 virtual nsresult SynthesizeNativeMouseScrollEvent(
123 LayoutDeviceIntPoint aPoint, uint32_t aNativeMessage, double aDeltaX,
124 double aDeltaY, double aDeltaZ, uint32_t aModifierFlags,
125 uint32_t aAdditionalFlags, nsIObserver* aObserver) override;
127 virtual nsresult SynthesizeNativeTouchPoint(uint32_t aPointerId,
128 TouchPointerState aPointerState,
129 LayoutDeviceIntPoint aPoint,
130 double aPointerPressure,
131 uint32_t aPointerOrientation,
132 nsIObserver* aObserver) override;
134 virtual nsresult SynthesizeNativeTouchPadPinch(
135 TouchpadGesturePhase aEventPhase, float aScale,
136 LayoutDeviceIntPoint aPoint, int32_t aModifierFlags) override;
138 virtual nsresult SynthesizeNativeTouchpadPan(TouchpadGesturePhase aEventPhase,
139 LayoutDeviceIntPoint aPoint,
140 double aDeltaX, double aDeltaY,
141 int32_t aModifierFlags,
142 nsIObserver* aObserver) override;
144 private:
145 ~HeadlessWidget();
146 bool mEnabled;
147 bool mVisible;
148 bool mDestroyed;
149 bool mAlwaysOnTop;
150 nsIWidget* mTopLevel;
151 HeadlessCompositorWidget* mCompositorWidget;
152 nsSizeMode mSizeMode;
153 // The size mode before entering fullscreen mode.
154 nsSizeMode mLastSizeMode;
155 // The last size mode set while the window was visible.
156 nsSizeMode mEffectiveSizeMode;
157 mozilla::ScreenCoord mLastPinchSpan;
158 InputContext mInputContext;
159 mozilla::UniquePtr<mozilla::MultiTouchInput> mSynthesizedTouchInput;
160 // In headless there is no window manager to track window bounds
161 // across size mode changes, so we must track it to emulate.
162 LayoutDeviceIntRect mRestoreBounds;
163 void ApplySizeModeSideEffects();
164 // Move while maintaining size mode.
165 void MoveInternal(int32_t aX, int32_t aY);
166 // Resize while maintaining size mode.
167 void ResizeInternal(int32_t aWidth, int32_t aHeight, bool aRepaint);
168 // Similarly, we must track the active window ourselves in order
169 // to dispatch (de)activation events properly.
170 void RaiseWindow();
171 // The top level widgets are tracked for window ordering. They are
172 // stored in order of activation where the last element is always the
173 // currently active widget.
174 static StaticAutoPtr<nsTArray<HeadlessWidget*>> sActiveWindows;
175 // Get the most recently activated widget or null if there are none.
176 static already_AddRefed<HeadlessWidget> GetActiveWindow();
179 } // namespace widget
180 } // namespace mozilla
182 #endif