AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AWindow.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 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 IPlatformAbstraction;
   48    friend class CommonRenderingContext;
   49    friend class SoftwareRenderingContext;
   50    friend class AWindowManager;
   51    friend struct painter;
   52public:
   53    AWindow(const AString& name = "My window", int width = 854_dp, int height = 500_dp, AWindow* parent = nullptr, WindowStyle ws = WindowStyle::DEFAULT) {
   54        windowNativePreInit(name, width, height, parent, ws);
   55    }
   56    virtual ~AWindow();
   57
   58    [[nodiscard]]
   59    unsigned frameMillis() const noexcept override {
   60        return mFrameMillis;
   61    }
   62
   63    void redraw() override;
   64
   65    void blockUserInput(bool blockUserInput = true) override;
   66
   74    static bool isRedrawWillBeEfficient();
   75
   76    void setIcon(const AImage& image);
   77
   81    void quit();
   82
   83    void setWindowStyle(WindowStyle ws);
   84    WindowStyle windowStyle() const { return mWindowStyle; }
   85
   89    void minimize();
   90
   94    bool isMinimized() const;
   95
   99    void maximize();
  100
  104    bool isMaximized() const;
  105
  109    void restore();
  110
  111    void flagRedraw() override;
  112
  120    void show();
  121    void close();
  122    void hide();
  123
  124    [[nodiscard]]
  125    bool isClosed() const noexcept;
  126
  127#if AUI_PLATFORM_WIN
  128    HWND getNativeHandle() { return mHandle; }
  129#elif AUI_PLATFORM_ANDROID
  130    jobject getNativeHandle() { return mHandle; }
  131#elif AUI_PLATFORM_APPLE
  132#else
  133    AWindowNativePtr getNativeHandle() { return mHandle; }
  134#endif
  135
  136    auto nativeHandle() const {
  137        return mHandle;
  138    }
  139
  140    const AString& getWindowTitle() const
  141    {
  142        return mWindowTitle;
  143    }
  144
  145    glm::ivec2 getWindowPosition() const;
  146
  147    void setPosition(glm::ivec2 position) override;
  148    void setSize(glm::ivec2 size) override;
  149    void setGeometry(int x, int y, int width, int height) override;
  150
  151#if AUI_PLATFORM_LINUX
  152    void applyGeometryToChildren() override;
  153#endif
  154
  155    void onFocusAcquired() override;
  156    void onFocusLost() override;
  157
  158    void onKeyDown(AInput::Key key) override;
  159    void onKeyRepeat(AInput::Key key) override;
  160
  161    void setFocusNextViewOnTab(bool value) {
  162        mFocusNextViewOnTab = value;
  163    }
  164
  175    static _<AWindow> wrapViewToWindow(const _<AView>& view, const AString& title, int width = 854_dp, int height = 500_dp, AWindow* parent = nullptr, WindowStyle ws = WindowStyle::DEFAULT);
  176
  180    static AWindowBase* current();
  181
  188    [[nodiscard]] glm::ivec2 mapPositionTo(const glm::ivec2& position, _<AWindow> other);
  189
  195    [[nodiscard]] glm::ivec2 unmapPosition(const glm::ivec2& position);
  196
  202    [[nodiscard]] glm::ivec2 mapPosition(const glm::ivec2& position);
  203
  204    _<AOverlappingSurface> createOverlappingSurfaceImpl(const glm::ivec2& position, const glm::ivec2& size) override;
  205
  206    void closeOverlappingSurfaceImpl(AOverlappingSurface* surface) override;
  207    virtual void onCloseButtonClicked();
  208
  209    void forceUpdateCursor() override;
  210
  211    void showTouchscreenKeyboardImpl() override;
  212    void hideTouchscreenKeyboardImpl() override;
  213
  219    void moveToCenter();
  220
  226    void setMobileScreenOrientation(AScreenOrientation screenOrientation);
  227
  228signals:
  229    emits<> closed;
  230    emits<> shown;
  231
  237
  242
  247
  252
  253    bool consumesClick(const glm::ivec2& pos) override;
  254
  255    void onPointerMove(glm::vec2 pos, const APointerMoveEvent& event) override;
  256
  257protected:
  258#if AUI_PLATFORM_WIN
  259    HICON mIcon = nullptr;
  260    virtual LRESULT winProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  261#elif AUI_PLATFORM_ANDROID
  262#elif AUI_PLATFORM_APPLE
  263#endif
  264    AWindowNativePtr mHandle = 0; // on linux AWindowNativePtr is not a pointer type so using zero here
  265    WindowStyle mWindowStyle = WindowStyle::DEFAULT;
  266
  267    virtual void doDrawWindow();
  268    virtual void onClosed();
  269
  270    void windowNativePreInit(const AString& name, int width, int height, AWindow* parent, WindowStyle ws);
  271
  275    void allowDragNDrop();
  276
  281    AWindow(std::nullptr_t) {}
  282
  283    void createDevtoolsWindow() override;
  284
  285    float fetchDpiFromSystem() const override;
  286
  290    bool mFocusNextViewOnTab = false;
  291
  292private:
  293#if AUI_PLATFORM_WIN
  294
  295#elif AUI_PLATFORM_ANDROID
  296#elif AUI_PLATFORM_LINUX
  297    bool mWasMaximized = false;
  298#endif
  299#ifdef AUI_PLATFORM_MACOS
  300    bool mRedrawFlag = false;
  301#else
  302    bool mRedrawFlag = true;
  303#endif
  304    AString mWindowClass;
  305    AWindow* mParentWindow;
  306
  307    unsigned mFrameMillis = 1;
  308
  312    _<AWindow> mSelfHolder;
  313
  314    AString mWindowTitle;
  315
  316#if AUI_PLATFORM_WIN
  317    friend LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  318#endif
  319#if AUI_PLATFORM_LINUX
  320    unsigned long xGetWindowProperty(unsigned long property, unsigned long type, unsigned char** value) const;
  321    void xSendEventToWM(unsigned long atom, long a, long b, long c, long d, long e) const;
  322#endif
  323
  324};
Owning image representation.
Definition AImage.h:25
Definition AOverlappingSurface.h:19
Represents a Unicode character string.
Definition AString.h:38
auto size() const
Size, including content area, border and padding.
Definition AView.h:114
auto position() const
Top left corner's position relative to top left corner's position of the parent AView.
Definition AView.h:102
Definition AWindowManager.h:21
void onPointerMove(glm::vec2 pos, const APointerMoveEvent &event) override
Handles pointer hover events.
glm::ivec2 mapPositionTo(const glm::ivec2 &position, _< AWindow > other)
Translates coordinates from the coordinate space of this window to the coordinate space of another wi...
void moveToCenter()
Moves the window to the center of monitor.
void allowDragNDrop()
Enables drag-n-drop for this window.
void minimize()
Minimizes window (hide window to the taskbar, iconifies)
void forceUpdateCursor() override
Updates cursor by triggering onPointerMove on the same position (mMousePos).
bool isMaximized() const
bool consumesClick(const glm::ivec2 &pos) override
Determines whether this AView processes this click or passes it thru.
void restore()
Restores window (shows window from taskbar)
static AWindowBase * current()
bool mFocusNextViewOnTab
defines if the next view must be focused on tab button pressed
Definition AWindow.h:290
void blockUserInput(bool blockUserInput=true) override
Enables or disables user input for this window.
emits< glm::vec2 > moving
Window is moving.
Definition AWindow.h:236
emits restored
Window is restored (shown from the taskbar, deiconified).
Definition AWindow.h:251
glm::ivec2 mapPosition(const glm::ivec2 &position)
Translates coordinates from the monitor's coordinate space to the coordinate space of this window.
AWindow(std::nullptr_t)
Constructor for custom initialization logic.
Definition AWindow.h:281
emits maximized
Window is maximized.
Definition AWindow.h:241
void setMobileScreenOrientation(AScreenOrientation screenOrientation)
Controls mobile device's screen orientation when this window is on the foreground.
static bool isRedrawWillBeEfficient()
Checks whether last monitor frame is displayed and redraw will be efficient. If some object often upd...
_< AOverlappingSurface > createOverlappingSurfaceImpl(const glm::ivec2 &position, const glm::ivec2 &size) override
unsigned frameMillis() const noexcept override
Returns previous frame's rendering duration in millis.
Definition AWindow.h:59
static _< AWindow > wrapViewToWindow(const _< AView > &view, const AString &title, int width=854_dp, int height=500_dp, AWindow *parent=nullptr, WindowStyle ws=WindowStyle::DEFAULT)
Wraps your AView to window.
bool isMinimized() const
glm::ivec2 unmapPosition(const glm::ivec2 &position)
Translates coordinates from the coordinate space of this window to the coordinate space of the monito...
void maximize()
Maximizes window (makes window fullscreen)
emits minimized
Window is minimized (hidden to the taskbar, iconified).
Definition AWindow.h:246
void quit()
Removes window from AWindowManager.
void show()
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:179
AScreenOrientation
Controls screen orientation.
Definition AScreenOrientation.h:20
ASignal< Args... > emits
A signal declaration.
Definition ASignal.h:577
Pointing method move event.
Definition APointerMoveEvent.h:21