Skip to content

Commit b336129

Browse files
committed
core/window: add QsWindow.devicePixelRatio
1 parent bc73d35 commit b336129

File tree

9 files changed

+48
-0
lines changed

9 files changed

+48
-0
lines changed

src/wayland/wlr_layershell.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ WaylandPanelInterface::WaylandPanelInterface(QObject* parent)
192192
QObject::connect(this->layer, &ProxyWindowBase::backerVisibilityChanged, this, &WaylandPanelInterface::backingWindowVisibleChanged);
193193
QObject::connect(this->layer, &ProxyWindowBase::heightChanged, this, &WaylandPanelInterface::heightChanged);
194194
QObject::connect(this->layer, &ProxyWindowBase::widthChanged, this, &WaylandPanelInterface::widthChanged);
195+
QObject::connect(this->layer, &ProxyWindowBase::devicePixelRatioChanged, this, &WaylandPanelInterface::devicePixelRatioChanged);
195196
QObject::connect(this->layer, &ProxyWindowBase::screenChanged, this, &WaylandPanelInterface::screenChanged);
196197
QObject::connect(this->layer, &ProxyWindowBase::windowTransformChanged, this, &WaylandPanelInterface::windowTransformChanged);
197198
QObject::connect(this->layer, &ProxyWindowBase::colorChanged, this, &WaylandPanelInterface::colorChanged);
@@ -218,10 +219,13 @@ void WaylandPanelInterface::onReload(QObject* oldInstance) {
218219
QQmlListProperty<QObject> WaylandPanelInterface::data() { return this->layer->data(); }
219220
ProxyWindowBase* WaylandPanelInterface::proxyWindow() const { return this->layer; }
220221
QQuickItem* WaylandPanelInterface::contentItem() const { return this->layer->contentItem(); }
222+
221223
bool WaylandPanelInterface::isBackingWindowVisible() const {
222224
return this->layer->isVisibleDirect();
223225
}
224226

227+
qreal WaylandPanelInterface::devicePixelRatio() const { return this->layer->devicePixelRatio(); }
228+
225229
// NOLINTBEGIN
226230
#define proxyPair(type, get, set) \
227231
type WaylandPanelInterface::get() const { return this->layer->get(); } \

src/wayland/wlr_layershell.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ class WaylandPanelInterface: public PanelWindowInterface {
146146
[[nodiscard]] qint32 height() const override;
147147
void setHeight(qint32 height) override;
148148

149+
[[nodiscard]] virtual qreal devicePixelRatio() const override;
150+
149151
[[nodiscard]] QuickshellScreenInfo* screen() const override;
150152
void setScreen(QuickshellScreenInfo* screen) override;
151153

src/window/floatingwindow.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ FloatingWindowInterface::FloatingWindowInterface(QObject* parent)
3232
QObject::connect(this->window, &ProxyWindowBase::backerVisibilityChanged, this, &FloatingWindowInterface::backingWindowVisibleChanged);
3333
QObject::connect(this->window, &ProxyWindowBase::heightChanged, this, &FloatingWindowInterface::heightChanged);
3434
QObject::connect(this->window, &ProxyWindowBase::widthChanged, this, &FloatingWindowInterface::widthChanged);
35+
QObject::connect(this->window, &ProxyWindowBase::devicePixelRatioChanged, this, &FloatingWindowInterface::devicePixelRatioChanged);
3536
QObject::connect(this->window, &ProxyWindowBase::screenChanged, this, &FloatingWindowInterface::screenChanged);
3637
QObject::connect(this->window, &ProxyWindowBase::windowTransformChanged, this, &FloatingWindowInterface::windowTransformChanged);
3738
QObject::connect(this->window, &ProxyWindowBase::colorChanged, this, &FloatingWindowInterface::colorChanged);
@@ -50,10 +51,13 @@ void FloatingWindowInterface::onReload(QObject* oldInstance) {
5051
QQmlListProperty<QObject> FloatingWindowInterface::data() { return this->window->data(); }
5152
ProxyWindowBase* FloatingWindowInterface::proxyWindow() const { return this->window; }
5253
QQuickItem* FloatingWindowInterface::contentItem() const { return this->window->contentItem(); }
54+
5355
bool FloatingWindowInterface::isBackingWindowVisible() const {
5456
return this->window->isVisibleDirect();
5557
}
5658

59+
qreal FloatingWindowInterface::devicePixelRatio() const { return this->window->devicePixelRatio(); }
60+
5761
// NOLINTBEGIN
5862
#define proxyPair(type, get, set) \
5963
type FloatingWindowInterface::get() const { return this->window->get(); } \

src/window/floatingwindow.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class FloatingWindowInterface: public WindowInterface {
4242
[[nodiscard]] qint32 height() const override;
4343
void setHeight(qint32 height) override;
4444

45+
[[nodiscard]] virtual qreal devicePixelRatio() const override;
46+
4547
[[nodiscard]] QuickshellScreenInfo* screen() const override;
4648
void setScreen(QuickshellScreenInfo* screen) override;
4749

src/window/proxywindow.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "proxywindow.hpp"
22

33
#include <private/qquickwindow_p.h>
4+
#include <qcoreevent.h>
45
#include <qevent.h>
56
#include <qnamespace.h>
67
#include <qobject.h>
@@ -191,6 +192,7 @@ void ProxyWindowBase::connectWindow() {
191192
QObject::connect(this->window, &QWindow::screenChanged, this, &ProxyWindowBase::screenChanged);
192193
QObject::connect(this->window, &QQuickWindow::colorChanged, this, &ProxyWindowBase::colorChanged);
193194
QObject::connect(this->window, &ProxiedWindow::exposed, this, &ProxyWindowBase::runLints);
195+
QObject::connect(this->window, &ProxiedWindow::devicePixelRatioChanged, this, &ProxyWindowBase::devicePixelRatioChanged);
194196
// clang-format on
195197
}
196198

@@ -213,6 +215,7 @@ void ProxyWindowBase::completeWindow() {
213215
emit this->yChanged();
214216
emit this->widthChanged();
215217
emit this->heightChanged();
218+
emit this->devicePixelRatioChanged();
216219

217220
this->mContentItem->setParentItem(this->window->contentItem());
218221
this->mContentItem->setWidth(this->width());
@@ -411,6 +414,12 @@ void ProxyWindowBase::setSurfaceFormat(QsSurfaceFormat format) {
411414
emit this->surfaceFormatChanged();
412415
}
413416

417+
qreal ProxyWindowBase::devicePixelRatio() const {
418+
if (this->window != nullptr) return this->window->devicePixelRatio();
419+
if (this->mScreen != nullptr) return this->mScreen->devicePixelRatio();
420+
return 1.0;
421+
}
422+
414423
void ProxyWindowBase::onMaskChanged() {
415424
if (this->window != nullptr) this->updateMask();
416425
}
@@ -456,6 +465,14 @@ void ProxyWindowAttached::setWindow(ProxyWindowBase* window) {
456465
emit this->windowChanged();
457466
}
458467

468+
bool ProxiedWindow::event(QEvent* event) {
469+
if (event->type() == QEvent::DevicePixelRatioChange) {
470+
emit this->devicePixelRatioChanged();
471+
}
472+
473+
return this->QQuickWindow::event(event);
474+
}
475+
459476
void ProxiedWindow::exposeEvent(QExposeEvent* event) {
460477
this->QQuickWindow::exposeEvent(event);
461478
emit this->exposed();

src/window/proxywindow.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class ProxyWindowBase: public Reloadable {
4444
Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged);
4545
Q_PROPERTY(qint32 width READ width WRITE setWidth NOTIFY widthChanged);
4646
Q_PROPERTY(qint32 height READ height WRITE setHeight NOTIFY heightChanged);
47+
Q_PROPERTY(qreal devicePixelRatio READ devicePixelRatio NOTIFY devicePixelRatioChanged);
4748
Q_PROPERTY(QuickshellScreenInfo* screen READ screen WRITE setScreen NOTIFY screenChanged);
4849
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged);
4950
Q_PROPERTY(PendingRegion* mask READ mask WRITE setMask NOTIFY maskChanged);
@@ -97,6 +98,8 @@ class ProxyWindowBase: public Reloadable {
9798
[[nodiscard]] virtual qint32 height() const;
9899
virtual void setHeight(qint32 height);
99100

101+
[[nodiscard]] qreal devicePixelRatio() const;
102+
100103
[[nodiscard]] virtual QuickshellScreenInfo* screen() const;
101104
virtual void setScreen(QuickshellScreenInfo* screen);
102105

@@ -122,6 +125,7 @@ class ProxyWindowBase: public Reloadable {
122125
void yChanged();
123126
void widthChanged();
124127
void heightChanged();
128+
void devicePixelRatioChanged();
125129
void windowTransformChanged();
126130
void screenChanged();
127131
void colorChanged();
@@ -192,8 +196,10 @@ class ProxiedWindow: public QQuickWindow {
192196

193197
signals:
194198
void exposed();
199+
void devicePixelRatioChanged();
195200

196201
protected:
202+
bool event(QEvent* event) override;
197203
void exposeEvent(QExposeEvent* event) override;
198204

199205
private:

src/window/windowinterface.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ class WindowInterface: public Reloadable {
5555
Q_PROPERTY(bool backingWindowVisible READ isBackingWindowVisible NOTIFY backingWindowVisibleChanged);
5656
Q_PROPERTY(qint32 width READ width WRITE setWidth NOTIFY widthChanged);
5757
Q_PROPERTY(qint32 height READ height WRITE setHeight NOTIFY heightChanged);
58+
/// The ratio between logical pixels and monitor pixels.
59+
///
60+
/// Qt's coordinate system works in logical pixels, which equal N monitor pixels
61+
/// depending on scale factor. This property returns the amount of monitor pixels
62+
/// in a logical pixel for the current window.
63+
Q_PROPERTY(qreal devicePixelRatio READ devicePixelRatio NOTIFY devicePixelRatioChanged);
5864
/// The screen that the window currently occupies.
5965
///
6066
/// This may be modified to move the window to the given screen.
@@ -147,6 +153,8 @@ class WindowInterface: public Reloadable {
147153
[[nodiscard]] virtual qint32 height() const = 0;
148154
virtual void setHeight(qint32 height) = 0;
149155

156+
[[nodiscard]] virtual qreal devicePixelRatio() const = 0;
157+
150158
[[nodiscard]] virtual QuickshellScreenInfo* screen() const = 0;
151159
virtual void setScreen(QuickshellScreenInfo* screen) = 0;
152160

@@ -171,6 +179,7 @@ class WindowInterface: public Reloadable {
171179
void backingWindowVisibleChanged();
172180
void widthChanged();
173181
void heightChanged();
182+
void devicePixelRatioChanged();
174183
void screenChanged();
175184
void windowTransformChanged();
176185
void colorChanged();

src/x11/panel_window.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ XPanelInterface::XPanelInterface(QObject* parent)
478478
QObject::connect(this->panel, &ProxyWindowBase::backerVisibilityChanged, this, &XPanelInterface::backingWindowVisibleChanged);
479479
QObject::connect(this->panel, &ProxyWindowBase::heightChanged, this, &XPanelInterface::heightChanged);
480480
QObject::connect(this->panel, &ProxyWindowBase::widthChanged, this, &XPanelInterface::widthChanged);
481+
QObject::connect(this->panel, &ProxyWindowBase::devicePixelRatioChanged, this, &XPanelInterface::devicePixelRatioChanged);
481482
QObject::connect(this->panel, &ProxyWindowBase::screenChanged, this, &XPanelInterface::screenChanged);
482483
QObject::connect(this->panel, &ProxyWindowBase::windowTransformChanged, this, &XPanelInterface::windowTransformChanged);
483484
QObject::connect(this->panel, &ProxyWindowBase::colorChanged, this, &XPanelInterface::colorChanged);
@@ -505,6 +506,7 @@ QQmlListProperty<QObject> XPanelInterface::data() { return this->panel->data();
505506
ProxyWindowBase* XPanelInterface::proxyWindow() const { return this->panel; }
506507
QQuickItem* XPanelInterface::contentItem() const { return this->panel->contentItem(); }
507508
bool XPanelInterface::isBackingWindowVisible() const { return this->panel->isVisibleDirect(); }
509+
qreal XPanelInterface::devicePixelRatio() const { return this->panel->devicePixelRatio(); }
508510

509511
// NOLINTBEGIN
510512
#define proxyPair(type, get, set) \

src/x11/panel_window.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ class XPanelInterface: public PanelWindowInterface {
127127
[[nodiscard]] qint32 height() const override;
128128
void setHeight(qint32 height) override;
129129

130+
[[nodiscard]] virtual qreal devicePixelRatio() const override;
131+
130132
[[nodiscard]] QuickshellScreenInfo* screen() const override;
131133
void setScreen(QuickshellScreenInfo* screen) override;
132134

0 commit comments

Comments
 (0)