diff --git a/src/cpp/proxywindow.cpp b/src/cpp/proxywindow.cpp index b06717c3..c6d52067 100644 --- a/src/cpp/proxywindow.cpp +++ b/src/cpp/proxywindow.cpp @@ -45,6 +45,9 @@ void ProxyWindowBase::onReload(QObject* oldInstance) { this->mContentItem->setWidth(this->width()); this->mContentItem->setHeight(this->height()); + // without this the dangling screen pointer wont be updated to a real screen + emit this->screenChanged(); + emit this->windowConnected(); this->window->setVisible(this->mVisible); } diff --git a/src/cpp/qmlscreen.cpp b/src/cpp/qmlscreen.cpp index 2f2ddcf4..b72d4c45 100644 --- a/src/cpp/qmlscreen.cpp +++ b/src/cpp/qmlscreen.cpp @@ -9,48 +9,33 @@ QuickShellScreenInfo::QuickShellScreenInfo(QObject* parent, QScreen* screen): QObject(parent), screen(screen) { - QObject::connect( - this->screen, - &QScreen::geometryChanged, - this, - &QuickShellScreenInfo::geometryChanged - ); - QObject::connect( - this->screen, - &QScreen::physicalDotsPerInchChanged, - this, - &QuickShellScreenInfo::physicalPixelDensityChanged - ); - QObject::connect( - this->screen, - &QScreen::logicalDotsPerInchChanged, - this, - &QuickShellScreenInfo::logicalPixelDensityChanged - ); - QObject::connect( - this->screen, - &QScreen::orientationChanged, - this, - &QuickShellScreenInfo::orientationChanged - ); - QObject::connect( - this->screen, - &QScreen::primaryOrientationChanged, - this, - &QuickShellScreenInfo::primaryOrientationChanged - ); + + if (this->screen != nullptr) { + // clang-format off + QObject::connect(this->screen, &QScreen::geometryChanged, this, &QuickShellScreenInfo::geometryChanged); + QObject::connect(this->screen, &QScreen::physicalDotsPerInchChanged, this, &QuickShellScreenInfo::physicalPixelDensityChanged); + QObject::connect(this->screen, &QScreen::logicalDotsPerInchChanged, this, &QuickShellScreenInfo::logicalPixelDensityChanged); + QObject::connect(this->screen, &QScreen::orientationChanged, this, &QuickShellScreenInfo::orientationChanged); + QObject::connect(this->screen, &QScreen::primaryOrientationChanged, this, &QuickShellScreenInfo::primaryOrientationChanged); + QObject::connect(this->screen, &QObject::destroyed, this, &QuickShellScreenInfo::screenDestroyed); + // clang-format on + } } bool QuickShellScreenInfo::operator==(QuickShellScreenInfo& other) const { return this->screen == other.screen; } -void warnNull() { qWarning() << "attempted to use dangling screen object"; } +void QuickShellScreenInfo::warnDangling() const { + if (this->dangling) { + qWarning() << "attempted to use dangling screen object"; + } +} QString QuickShellScreenInfo::name() const { if (this->screen == nullptr) { - warnNull(); - return "{ DANGLING SCREEN POINTER }"; + this->warnDangling(); + return "{ NULL SCREEN }"; } return this->screen->name(); @@ -58,7 +43,7 @@ QString QuickShellScreenInfo::name() const { qint32 QuickShellScreenInfo::width() const { if (this->screen == nullptr) { - warnNull(); + this->warnDangling(); return 0; } @@ -67,7 +52,7 @@ qint32 QuickShellScreenInfo::width() const { qint32 QuickShellScreenInfo::height() const { if (this->screen == nullptr) { - warnNull(); + this->warnDangling(); return 0; } @@ -76,7 +61,7 @@ qint32 QuickShellScreenInfo::height() const { qreal QuickShellScreenInfo::physicalPixelDensity() const { if (this->screen == nullptr) { - warnNull(); + this->warnDangling(); return 0.0; } @@ -85,7 +70,7 @@ qreal QuickShellScreenInfo::physicalPixelDensity() const { qreal QuickShellScreenInfo::logicalPixelDensity() const { if (this->screen == nullptr) { - warnNull(); + this->warnDangling(); return 0.0; } @@ -94,7 +79,7 @@ qreal QuickShellScreenInfo::logicalPixelDensity() const { qreal QuickShellScreenInfo::devicePixelRatio() const { if (this->screen == nullptr) { - warnNull(); + this->warnDangling(); return 0.0; } @@ -103,7 +88,7 @@ qreal QuickShellScreenInfo::devicePixelRatio() const { Qt::ScreenOrientation QuickShellScreenInfo::orientation() const { if (this->screen == nullptr) { - warnNull(); + this->warnDangling(); return Qt::PrimaryOrientation; } @@ -112,9 +97,14 @@ Qt::ScreenOrientation QuickShellScreenInfo::orientation() const { Qt::ScreenOrientation QuickShellScreenInfo::primaryOrientation() const { if (this->screen == nullptr) { - warnNull(); + this->warnDangling(); return Qt::PrimaryOrientation; } return this->screen->primaryOrientation(); } + +void QuickShellScreenInfo::screenDestroyed() { + this->screen = nullptr; + this->dangling = true; +} diff --git a/src/cpp/qmlscreen.hpp b/src/cpp/qmlscreen.hpp index be45819d..c878a64e 100644 --- a/src/cpp/qmlscreen.hpp +++ b/src/cpp/qmlscreen.hpp @@ -28,7 +28,7 @@ class QuickShellScreenInfo: public QObject { /// The name of the screen as seen by the operating system. /// /// Usually something like `DP-1`, `HDMI-1`, `eDP-1`. - Q_PROPERTY(QString name READ name); + Q_PROPERTY(QString name READ name CONSTANT); Q_PROPERTY(qint32 width READ width NOTIFY geometryChanged); Q_PROPERTY(qint32 height READ height NOTIFY geometryChanged); /// The number of physical pixels per millimeter. @@ -57,10 +57,17 @@ public: QScreen* screen; +private: + void warnDangling() const; + bool dangling = false; + signals: void geometryChanged(); void physicalPixelDensityChanged(); void logicalPixelDensityChanged(); void orientationChanged(); void primaryOrientationChanged(); + +private slots: + void screenDestroyed(); };