AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
AWindow.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12#pragma once
13
14#include "AUI/Views.h"
15#include "AUI/Common/AString.h"
16
17#include "AUI/Common/AObject.h"
18#include "AUI/Common/ASignal.h"
19
20#include "AUI/Platform/AWindowBase.h"
21#include "AUI/Thread/IEventLoop.h"
22#include "AUI/Util/AMetric.h"
23#include "AWindowNativePtr.h"
24#include <AUI/Enum/WindowStyle.h>
25#include <AUI/Enum/AScreenOrientation.h>
26
27#if AUI_PLATFORM_WIN
28#include <windows.h>
29
30#elif AUI_PLATFORM_ANDROID
31#include <jni.h>
32#elif AUI_PLATFORM_APPLE
33#else
34
35#endif
36
37class ARender;
38class AWindowManager;
39
44class API_AUI_VIEWS AWindow: public AWindowBase
45{
46 friend class OpenGLRenderingContext;
47 friend class CommonRenderingContext;
48 friend class SoftwareRenderingContext;
49 friend class AWindowManager;
50 friend struct painter;
51public:
52 AWindow(const AString& name = "My window", int width = 854_dp, int height = 500_dp, AWindow* parent = nullptr, WindowStyle ws = WindowStyle::DEFAULT) {
53 windowNativePreInit(name, width, height, parent, ws);
54 }
55 virtual ~AWindow();
56
57 [[nodiscard]]
58 unsigned frameMillis() const noexcept override {
59 return mFrameMillis;
60 }
61
62 void redraw();
63
64 void blockUserInput(bool blockUserInput = true) override;
65
73 static bool isRedrawWillBeEfficient();
74
75 void setIcon(const AImage& image);
76
80 void quit();
81
82 void setWindowStyle(WindowStyle ws);
83
87 void minimize();
88
92 bool isMinimized() const;
93
97 void maximize();
98
102 bool isMaximized() const;
103
107 void restore();
108
109 void flagRedraw() override;
110
118 void show();
119 void close();
120 void hide();
121
122 [[nodiscard]]
123 bool isClosed() const noexcept;
124
125#if AUI_PLATFORM_WIN
126 HWND getNativeHandle() { return mHandle; }
127#elif AUI_PLATFORM_ANDROID
128 jobject getNativeHandle() { return mHandle; }
129#elif AUI_PLATFORM_APPLE
130#else
131 AWindowNativePtr getNativeHandle() { return mHandle; }
132#endif
133
134 auto nativeHandle() const {
135 return mHandle;
136 }
137
138 const AString& getWindowTitle() const
139 {
140 return mWindowTitle;
141 }
142
143 glm::ivec2 getWindowPosition() const;
144
145 void setPosition(glm::ivec2 position) override;
146 void setSize(glm::ivec2 size) override;
147 void setGeometry(int x, int y, int width, int height) override;
148
149#if AUI_PLATFORM_LINUX
150 void applyGeometryToChildren() override;
151#endif
152
153 void onFocusAcquired() override;
154 void onFocusLost() override;
155
156 void onKeyDown(AInput::Key key) override;
157 void onKeyRepeat(AInput::Key key) override;
158
159 void setFocusNextViewOnTab(bool value) {
160 mFocusNextViewOnTab = value;
161 }
162
169 static _<AWindow> wrapViewToWindow(const _<AView>& view, const AString& title, int width = 854_dp, int height = 500_dp, AWindow* parent = nullptr, WindowStyle ws = WindowStyle::DEFAULT);
170
174 static AWindowBase* current();
175
182 [[nodiscard]] glm::ivec2 mapPositionTo(const glm::ivec2& position, _<AWindow> other);
183
189 [[nodiscard]] glm::ivec2 unmapPosition(const glm::ivec2& position);
190
196 [[nodiscard]] glm::ivec2 mapPosition(const glm::ivec2& position);
197
198 _<AOverlappingSurface> createOverlappingSurfaceImpl(const glm::ivec2& position, const glm::ivec2& size) override;
199
200 void closeOverlappingSurfaceImpl(AOverlappingSurface* surface) override;
201 virtual void onCloseButtonClicked();
202
203 void forceUpdateCursor() override;
204
205 void showTouchscreenKeyboardImpl() override;
206 void hideTouchscreenKeyboardImpl() override;
207
213 void moveToCenter();
214
220 void setMobileScreenOrientation(AScreenOrientation screenOrientation);
221
222signals:
223 emits<> closed;
224 emits<> shown;
225
231
236
241
246
247 bool consumesClick(const glm::ivec2& pos) override;
248
249 void onPointerMove(glm::vec2 pos, const APointerMoveEvent& event) override;
250
251protected:
252#if AUI_PLATFORM_WIN
253 HICON mIcon = nullptr;
254 virtual LRESULT winProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
255#elif AUI_PLATFORM_ANDROID
256#elif AUI_PLATFORM_APPLE
257#endif
258 AWindowNativePtr mHandle = 0; // on linux AWindowNativePtr is not a pointer type so using zero here
259 WindowStyle mWindowStyle = WindowStyle::DEFAULT;
260
261 virtual void doDrawWindow();
262 virtual void onClosed();
263
264 void windowNativePreInit(const AString& name, int width, int height, AWindow* parent, WindowStyle ws);
265
269 void allowDragNDrop();
270
275 AWindow(std::nullptr_t) {}
276
277 void createDevtoolsWindow() override;
278
279 float fetchDpiFromSystem() const override;
280
284 bool mFocusNextViewOnTab = false;
285
286private:
287#if AUI_PLATFORM_WIN
288
289#elif AUI_PLATFORM_ANDROID
290#elif AUI_PLATFORM_LINUX
294 struct {
295 uint32_t lo = 0;
296 uint32_t hi = 0;
297 /* XID */ unsigned long counter;
298 } mXsyncRequestCounter;
299 bool mWasMaximized = false;
300
301 void* mIC = nullptr; // input context
302#endif
303#ifdef AUI_PLATFORM_MACOS
304 bool mRedrawFlag = false;
305#else
306 bool mRedrawFlag = true;
307#endif
308 AString mWindowClass;
309 AWindow* mParentWindow;
310
311 unsigned mFrameMillis = 1;
312
316 _<AWindow> mSelfHolder;
317
318 AString mWindowTitle;
319
320#if AUI_PLATFORM_WIN
321 friend LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
322#elif AUI_PLATFORM_ANDROID
323#elif AUI_PLATFORM_APPLE
324#else
325 unsigned long xGetWindowProperty(unsigned long property, unsigned long type, unsigned char** value) const;
326 void xSendEventToWM(unsigned long atom, long a, long b, long c, long d, long e) const;
327#endif
328
329};
Definition: AOverlappingSurface.h:19
Represents a Unicode character string.
Definition: AString.h:37
bool consumesClick(const glm::ivec2 &pos) override
Determines whether this AView processes this click or passes it thru.
Definition: AViewContainerBase.cpp:369
void redraw()
Request window manager to redraw this AView.
Definition: AView.cpp:68
Definition: AWindowBase.h:33
virtual void blockUserInput(bool blockUserInput=true)
Enables or disables user input for this window.
Definition: AWindowBase.cpp:516
virtual _< AOverlappingSurface > createOverlappingSurfaceImpl(const glm::ivec2 &position, const glm::ivec2 &size)=0
void onPointerMove(glm::vec2 pos, const APointerMoveEvent &event) override
Handles pointer hover events.
Definition: AWindowBase.cpp:353
virtual void forceUpdateCursor()
Updates cursor by triggering onPointerMove on the same position (mMousePos).
Definition: AWindowBase.cpp:333
Definition: AWindowManager.h:23
Represents a window in the underlying windowing system.
Definition: AWindow.h:45
emits< glm::vec2 > moving
Window is moving.
Definition: AWindow.h:230
emits restored
Window is restored (shown from the taskbar, deiconified).
Definition: AWindow.h:245
AWindow(std::nullptr_t)
Constructor for custom initialization logic.
Definition: AWindow.h:275
emits maximized
Window is maximized.
Definition: AWindow.h:235
unsigned frameMillis() const noexcept override
Returns previous frame's rendering duration in millis.
Definition: AWindow.h:58
emits minimized
Window is minimized (hidden to the taskbar, iconified).
Definition: AWindow.h:240
Definition: CommonRenderingContext.h:28
Definition: OpenGLRenderingContext.h:23
Definition: SoftwareRenderingContext.h:17
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177
AScreenOrientation
Controls screen orientation.
Definition: AScreenOrientation.h:20
Pointing method move event.
Definition: APointerMoveEvent.h:21
Definition: Type.h:16
Definition: Size.h:22