fix: dangling screen issues and incorrect warnings

This commit is contained in:
outfoxxed 2024-02-18 19:57:12 -08:00
parent a361930865
commit 55bcae4d62
Signed by: outfoxxed
GPG key ID: 4C88A185FB89301E
3 changed files with 41 additions and 41 deletions

View file

@ -45,6 +45,9 @@ void ProxyWindowBase::onReload(QObject* oldInstance) {
this->mContentItem->setWidth(this->width()); this->mContentItem->setWidth(this->width());
this->mContentItem->setHeight(this->height()); this->mContentItem->setHeight(this->height());
// without this the dangling screen pointer wont be updated to a real screen
emit this->screenChanged();
emit this->windowConnected(); emit this->windowConnected();
this->window->setVisible(this->mVisible); this->window->setVisible(this->mVisible);
} }

View file

@ -9,48 +9,33 @@
QuickShellScreenInfo::QuickShellScreenInfo(QObject* parent, QScreen* screen): QuickShellScreenInfo::QuickShellScreenInfo(QObject* parent, QScreen* screen):
QObject(parent), screen(screen) { QObject(parent), screen(screen) {
QObject::connect(
this->screen, if (this->screen != nullptr) {
&QScreen::geometryChanged, // clang-format off
this, QObject::connect(this->screen, &QScreen::geometryChanged, this, &QuickShellScreenInfo::geometryChanged);
&QuickShellScreenInfo::geometryChanged QObject::connect(this->screen, &QScreen::physicalDotsPerInchChanged, this, &QuickShellScreenInfo::physicalPixelDensityChanged);
); QObject::connect(this->screen, &QScreen::logicalDotsPerInchChanged, this, &QuickShellScreenInfo::logicalPixelDensityChanged);
QObject::connect( QObject::connect(this->screen, &QScreen::orientationChanged, this, &QuickShellScreenInfo::orientationChanged);
this->screen, QObject::connect(this->screen, &QScreen::primaryOrientationChanged, this, &QuickShellScreenInfo::primaryOrientationChanged);
&QScreen::physicalDotsPerInchChanged, QObject::connect(this->screen, &QObject::destroyed, this, &QuickShellScreenInfo::screenDestroyed);
this, // clang-format on
&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
);
} }
bool QuickShellScreenInfo::operator==(QuickShellScreenInfo& other) const { bool QuickShellScreenInfo::operator==(QuickShellScreenInfo& other) const {
return this->screen == other.screen; 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 { QString QuickShellScreenInfo::name() const {
if (this->screen == nullptr) { if (this->screen == nullptr) {
warnNull(); this->warnDangling();
return "{ DANGLING SCREEN POINTER }"; return "{ NULL SCREEN }";
} }
return this->screen->name(); return this->screen->name();
@ -58,7 +43,7 @@ QString QuickShellScreenInfo::name() const {
qint32 QuickShellScreenInfo::width() const { qint32 QuickShellScreenInfo::width() const {
if (this->screen == nullptr) { if (this->screen == nullptr) {
warnNull(); this->warnDangling();
return 0; return 0;
} }
@ -67,7 +52,7 @@ qint32 QuickShellScreenInfo::width() const {
qint32 QuickShellScreenInfo::height() const { qint32 QuickShellScreenInfo::height() const {
if (this->screen == nullptr) { if (this->screen == nullptr) {
warnNull(); this->warnDangling();
return 0; return 0;
} }
@ -76,7 +61,7 @@ qint32 QuickShellScreenInfo::height() const {
qreal QuickShellScreenInfo::physicalPixelDensity() const { qreal QuickShellScreenInfo::physicalPixelDensity() const {
if (this->screen == nullptr) { if (this->screen == nullptr) {
warnNull(); this->warnDangling();
return 0.0; return 0.0;
} }
@ -85,7 +70,7 @@ qreal QuickShellScreenInfo::physicalPixelDensity() const {
qreal QuickShellScreenInfo::logicalPixelDensity() const { qreal QuickShellScreenInfo::logicalPixelDensity() const {
if (this->screen == nullptr) { if (this->screen == nullptr) {
warnNull(); this->warnDangling();
return 0.0; return 0.0;
} }
@ -94,7 +79,7 @@ qreal QuickShellScreenInfo::logicalPixelDensity() const {
qreal QuickShellScreenInfo::devicePixelRatio() const { qreal QuickShellScreenInfo::devicePixelRatio() const {
if (this->screen == nullptr) { if (this->screen == nullptr) {
warnNull(); this->warnDangling();
return 0.0; return 0.0;
} }
@ -103,7 +88,7 @@ qreal QuickShellScreenInfo::devicePixelRatio() const {
Qt::ScreenOrientation QuickShellScreenInfo::orientation() const { Qt::ScreenOrientation QuickShellScreenInfo::orientation() const {
if (this->screen == nullptr) { if (this->screen == nullptr) {
warnNull(); this->warnDangling();
return Qt::PrimaryOrientation; return Qt::PrimaryOrientation;
} }
@ -112,9 +97,14 @@ Qt::ScreenOrientation QuickShellScreenInfo::orientation() const {
Qt::ScreenOrientation QuickShellScreenInfo::primaryOrientation() const { Qt::ScreenOrientation QuickShellScreenInfo::primaryOrientation() const {
if (this->screen == nullptr) { if (this->screen == nullptr) {
warnNull(); this->warnDangling();
return Qt::PrimaryOrientation; return Qt::PrimaryOrientation;
} }
return this->screen->primaryOrientation(); return this->screen->primaryOrientation();
} }
void QuickShellScreenInfo::screenDestroyed() {
this->screen = nullptr;
this->dangling = true;
}

View file

@ -28,7 +28,7 @@ class QuickShellScreenInfo: public QObject {
/// The name of the screen as seen by the operating system. /// The name of the screen as seen by the operating system.
/// ///
/// Usually something like `DP-1`, `HDMI-1`, `eDP-1`. /// 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 width READ width NOTIFY geometryChanged);
Q_PROPERTY(qint32 height READ height NOTIFY geometryChanged); Q_PROPERTY(qint32 height READ height NOTIFY geometryChanged);
/// The number of physical pixels per millimeter. /// The number of physical pixels per millimeter.
@ -57,10 +57,17 @@ public:
QScreen* screen; QScreen* screen;
private:
void warnDangling() const;
bool dangling = false;
signals: signals:
void geometryChanged(); void geometryChanged();
void physicalPixelDensityChanged(); void physicalPixelDensityChanged();
void logicalPixelDensityChanged(); void logicalPixelDensityChanged();
void orientationChanged(); void orientationChanged();
void primaryOrientationChanged(); void primaryOrientationChanged();
private slots:
void screenDestroyed();
}; };