From b336129c34991968e4c741085a4160826e23c62e Mon Sep 17 00:00:00 2001 From: outfoxxed Date: Wed, 22 Jan 2025 03:33:46 -0800 Subject: [PATCH] core/window: add QsWindow.devicePixelRatio --- src/wayland/wlr_layershell.cpp | 4 ++++ src/wayland/wlr_layershell.hpp | 2 ++ src/window/floatingwindow.cpp | 4 ++++ src/window/floatingwindow.hpp | 2 ++ src/window/proxywindow.cpp | 17 +++++++++++++++++ src/window/proxywindow.hpp | 6 ++++++ src/window/windowinterface.hpp | 9 +++++++++ src/x11/panel_window.cpp | 2 ++ src/x11/panel_window.hpp | 2 ++ 9 files changed, 48 insertions(+) diff --git a/src/wayland/wlr_layershell.cpp b/src/wayland/wlr_layershell.cpp index 010d0f77..1a338522 100644 --- a/src/wayland/wlr_layershell.cpp +++ b/src/wayland/wlr_layershell.cpp @@ -192,6 +192,7 @@ WaylandPanelInterface::WaylandPanelInterface(QObject* parent) QObject::connect(this->layer, &ProxyWindowBase::backerVisibilityChanged, this, &WaylandPanelInterface::backingWindowVisibleChanged); QObject::connect(this->layer, &ProxyWindowBase::heightChanged, this, &WaylandPanelInterface::heightChanged); QObject::connect(this->layer, &ProxyWindowBase::widthChanged, this, &WaylandPanelInterface::widthChanged); + QObject::connect(this->layer, &ProxyWindowBase::devicePixelRatioChanged, this, &WaylandPanelInterface::devicePixelRatioChanged); QObject::connect(this->layer, &ProxyWindowBase::screenChanged, this, &WaylandPanelInterface::screenChanged); QObject::connect(this->layer, &ProxyWindowBase::windowTransformChanged, this, &WaylandPanelInterface::windowTransformChanged); QObject::connect(this->layer, &ProxyWindowBase::colorChanged, this, &WaylandPanelInterface::colorChanged); @@ -218,10 +219,13 @@ void WaylandPanelInterface::onReload(QObject* oldInstance) { QQmlListProperty WaylandPanelInterface::data() { return this->layer->data(); } ProxyWindowBase* WaylandPanelInterface::proxyWindow() const { return this->layer; } QQuickItem* WaylandPanelInterface::contentItem() const { return this->layer->contentItem(); } + bool WaylandPanelInterface::isBackingWindowVisible() const { return this->layer->isVisibleDirect(); } +qreal WaylandPanelInterface::devicePixelRatio() const { return this->layer->devicePixelRatio(); } + // NOLINTBEGIN #define proxyPair(type, get, set) \ type WaylandPanelInterface::get() const { return this->layer->get(); } \ diff --git a/src/wayland/wlr_layershell.hpp b/src/wayland/wlr_layershell.hpp index 32aeecdb..c3448706 100644 --- a/src/wayland/wlr_layershell.hpp +++ b/src/wayland/wlr_layershell.hpp @@ -146,6 +146,8 @@ public: [[nodiscard]] qint32 height() const override; void setHeight(qint32 height) override; + [[nodiscard]] virtual qreal devicePixelRatio() const override; + [[nodiscard]] QuickshellScreenInfo* screen() const override; void setScreen(QuickshellScreenInfo* screen) override; diff --git a/src/window/floatingwindow.cpp b/src/window/floatingwindow.cpp index 918b186d..761bc2d4 100644 --- a/src/window/floatingwindow.cpp +++ b/src/window/floatingwindow.cpp @@ -32,6 +32,7 @@ FloatingWindowInterface::FloatingWindowInterface(QObject* parent) QObject::connect(this->window, &ProxyWindowBase::backerVisibilityChanged, this, &FloatingWindowInterface::backingWindowVisibleChanged); QObject::connect(this->window, &ProxyWindowBase::heightChanged, this, &FloatingWindowInterface::heightChanged); QObject::connect(this->window, &ProxyWindowBase::widthChanged, this, &FloatingWindowInterface::widthChanged); + QObject::connect(this->window, &ProxyWindowBase::devicePixelRatioChanged, this, &FloatingWindowInterface::devicePixelRatioChanged); QObject::connect(this->window, &ProxyWindowBase::screenChanged, this, &FloatingWindowInterface::screenChanged); QObject::connect(this->window, &ProxyWindowBase::windowTransformChanged, this, &FloatingWindowInterface::windowTransformChanged); QObject::connect(this->window, &ProxyWindowBase::colorChanged, this, &FloatingWindowInterface::colorChanged); @@ -50,10 +51,13 @@ void FloatingWindowInterface::onReload(QObject* oldInstance) { QQmlListProperty FloatingWindowInterface::data() { return this->window->data(); } ProxyWindowBase* FloatingWindowInterface::proxyWindow() const { return this->window; } QQuickItem* FloatingWindowInterface::contentItem() const { return this->window->contentItem(); } + bool FloatingWindowInterface::isBackingWindowVisible() const { return this->window->isVisibleDirect(); } +qreal FloatingWindowInterface::devicePixelRatio() const { return this->window->devicePixelRatio(); } + // NOLINTBEGIN #define proxyPair(type, get, set) \ type FloatingWindowInterface::get() const { return this->window->get(); } \ diff --git a/src/window/floatingwindow.hpp b/src/window/floatingwindow.hpp index 36f933b4..7dd0d4ed 100644 --- a/src/window/floatingwindow.hpp +++ b/src/window/floatingwindow.hpp @@ -42,6 +42,8 @@ public: [[nodiscard]] qint32 height() const override; void setHeight(qint32 height) override; + [[nodiscard]] virtual qreal devicePixelRatio() const override; + [[nodiscard]] QuickshellScreenInfo* screen() const override; void setScreen(QuickshellScreenInfo* screen) override; diff --git a/src/window/proxywindow.cpp b/src/window/proxywindow.cpp index d89a6344..f75fc641 100644 --- a/src/window/proxywindow.cpp +++ b/src/window/proxywindow.cpp @@ -1,6 +1,7 @@ #include "proxywindow.hpp" #include +#include #include #include #include @@ -191,6 +192,7 @@ void ProxyWindowBase::connectWindow() { QObject::connect(this->window, &QWindow::screenChanged, this, &ProxyWindowBase::screenChanged); QObject::connect(this->window, &QQuickWindow::colorChanged, this, &ProxyWindowBase::colorChanged); QObject::connect(this->window, &ProxiedWindow::exposed, this, &ProxyWindowBase::runLints); + QObject::connect(this->window, &ProxiedWindow::devicePixelRatioChanged, this, &ProxyWindowBase::devicePixelRatioChanged); // clang-format on } @@ -213,6 +215,7 @@ void ProxyWindowBase::completeWindow() { emit this->yChanged(); emit this->widthChanged(); emit this->heightChanged(); + emit this->devicePixelRatioChanged(); this->mContentItem->setParentItem(this->window->contentItem()); this->mContentItem->setWidth(this->width()); @@ -411,6 +414,12 @@ void ProxyWindowBase::setSurfaceFormat(QsSurfaceFormat format) { emit this->surfaceFormatChanged(); } +qreal ProxyWindowBase::devicePixelRatio() const { + if (this->window != nullptr) return this->window->devicePixelRatio(); + if (this->mScreen != nullptr) return this->mScreen->devicePixelRatio(); + return 1.0; +} + void ProxyWindowBase::onMaskChanged() { if (this->window != nullptr) this->updateMask(); } @@ -456,6 +465,14 @@ void ProxyWindowAttached::setWindow(ProxyWindowBase* window) { emit this->windowChanged(); } +bool ProxiedWindow::event(QEvent* event) { + if (event->type() == QEvent::DevicePixelRatioChange) { + emit this->devicePixelRatioChanged(); + } + + return this->QQuickWindow::event(event); +} + void ProxiedWindow::exposeEvent(QExposeEvent* event) { this->QQuickWindow::exposeEvent(event); emit this->exposed(); diff --git a/src/window/proxywindow.hpp b/src/window/proxywindow.hpp index 6c7946b1..6f02e05a 100644 --- a/src/window/proxywindow.hpp +++ b/src/window/proxywindow.hpp @@ -44,6 +44,7 @@ class ProxyWindowBase: public Reloadable { Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged); Q_PROPERTY(qint32 width READ width WRITE setWidth NOTIFY widthChanged); Q_PROPERTY(qint32 height READ height WRITE setHeight NOTIFY heightChanged); + Q_PROPERTY(qreal devicePixelRatio READ devicePixelRatio NOTIFY devicePixelRatioChanged); Q_PROPERTY(QuickshellScreenInfo* screen READ screen WRITE setScreen NOTIFY screenChanged); Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged); Q_PROPERTY(PendingRegion* mask READ mask WRITE setMask NOTIFY maskChanged); @@ -97,6 +98,8 @@ public: [[nodiscard]] virtual qint32 height() const; virtual void setHeight(qint32 height); + [[nodiscard]] qreal devicePixelRatio() const; + [[nodiscard]] virtual QuickshellScreenInfo* screen() const; virtual void setScreen(QuickshellScreenInfo* screen); @@ -122,6 +125,7 @@ signals: void yChanged(); void widthChanged(); void heightChanged(); + void devicePixelRatioChanged(); void windowTransformChanged(); void screenChanged(); void colorChanged(); @@ -192,8 +196,10 @@ public: signals: void exposed(); + void devicePixelRatioChanged(); protected: + bool event(QEvent* event) override; void exposeEvent(QExposeEvent* event) override; private: diff --git a/src/window/windowinterface.hpp b/src/window/windowinterface.hpp index 8603de31..2aca7a76 100644 --- a/src/window/windowinterface.hpp +++ b/src/window/windowinterface.hpp @@ -55,6 +55,12 @@ class WindowInterface: public Reloadable { Q_PROPERTY(bool backingWindowVisible READ isBackingWindowVisible NOTIFY backingWindowVisibleChanged); Q_PROPERTY(qint32 width READ width WRITE setWidth NOTIFY widthChanged); Q_PROPERTY(qint32 height READ height WRITE setHeight NOTIFY heightChanged); + /// The ratio between logical pixels and monitor pixels. + /// + /// Qt's coordinate system works in logical pixels, which equal N monitor pixels + /// depending on scale factor. This property returns the amount of monitor pixels + /// in a logical pixel for the current window. + Q_PROPERTY(qreal devicePixelRatio READ devicePixelRatio NOTIFY devicePixelRatioChanged); /// The screen that the window currently occupies. /// /// This may be modified to move the window to the given screen. @@ -147,6 +153,8 @@ public: [[nodiscard]] virtual qint32 height() const = 0; virtual void setHeight(qint32 height) = 0; + [[nodiscard]] virtual qreal devicePixelRatio() const = 0; + [[nodiscard]] virtual QuickshellScreenInfo* screen() const = 0; virtual void setScreen(QuickshellScreenInfo* screen) = 0; @@ -171,6 +179,7 @@ signals: void backingWindowVisibleChanged(); void widthChanged(); void heightChanged(); + void devicePixelRatioChanged(); void screenChanged(); void windowTransformChanged(); void colorChanged(); diff --git a/src/x11/panel_window.cpp b/src/x11/panel_window.cpp index bda4aa54..ac182f76 100644 --- a/src/x11/panel_window.cpp +++ b/src/x11/panel_window.cpp @@ -478,6 +478,7 @@ XPanelInterface::XPanelInterface(QObject* parent) QObject::connect(this->panel, &ProxyWindowBase::backerVisibilityChanged, this, &XPanelInterface::backingWindowVisibleChanged); QObject::connect(this->panel, &ProxyWindowBase::heightChanged, this, &XPanelInterface::heightChanged); QObject::connect(this->panel, &ProxyWindowBase::widthChanged, this, &XPanelInterface::widthChanged); + QObject::connect(this->panel, &ProxyWindowBase::devicePixelRatioChanged, this, &XPanelInterface::devicePixelRatioChanged); QObject::connect(this->panel, &ProxyWindowBase::screenChanged, this, &XPanelInterface::screenChanged); QObject::connect(this->panel, &ProxyWindowBase::windowTransformChanged, this, &XPanelInterface::windowTransformChanged); QObject::connect(this->panel, &ProxyWindowBase::colorChanged, this, &XPanelInterface::colorChanged); @@ -505,6 +506,7 @@ QQmlListProperty XPanelInterface::data() { return this->panel->data(); ProxyWindowBase* XPanelInterface::proxyWindow() const { return this->panel; } QQuickItem* XPanelInterface::contentItem() const { return this->panel->contentItem(); } bool XPanelInterface::isBackingWindowVisible() const { return this->panel->isVisibleDirect(); } +qreal XPanelInterface::devicePixelRatio() const { return this->panel->devicePixelRatio(); } // NOLINTBEGIN #define proxyPair(type, get, set) \ diff --git a/src/x11/panel_window.hpp b/src/x11/panel_window.hpp index 12645589..11041f22 100644 --- a/src/x11/panel_window.hpp +++ b/src/x11/panel_window.hpp @@ -127,6 +127,8 @@ public: [[nodiscard]] qint32 height() const override; void setHeight(qint32 height) override; + [[nodiscard]] virtual qreal devicePixelRatio() const override; + [[nodiscard]] QuickshellScreenInfo* screen() const override; void setScreen(QuickshellScreenInfo* screen) override;