Skip to content

Commit

Permalink
Turn SessionLockSurface into a QML attached property
Browse files Browse the repository at this point in the history
  • Loading branch information
plfiorini committed Jan 17, 2024
1 parent 97ca079 commit e4a8825
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 45 deletions.
5 changes: 3 additions & 2 deletions src/interfaces/declarative/qtshellintegrationplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
#include <QQmlExtensionPlugin>
#include <QtQml>

#include "../sessionlocksurface.h"
#include "../layersurface.h"
#include "../sessionlocksurface.h"

QML_DECLARE_TYPEINFO(Liri::QtShellIntegration::LayerSurface, QML_HAS_ATTACHED_PROPERTIES)
QML_DECLARE_TYPEINFO(Liri::QtShellIntegration::SessionLockSurface, QML_HAS_ATTACHED_PROPERTIES)

class Plugin : public QQmlExtensionPlugin
{
Expand All @@ -17,8 +18,8 @@ class Plugin : public QQmlExtensionPlugin
void registerTypes(const char *uri) override {
Q_ASSERT(QLatin1String(uri) == QLatin1String("Liri.QtShellIntegration"));

qmlRegisterType<Liri::QtShellIntegration::SessionLockSurface>(uri, 1, 0, "SessionLockSurface");
qmlRegisterType<Liri::QtShellIntegration::LayerSurface>(uri, 1, 0, "LayerSurface");
qmlRegisterType<Liri::QtShellIntegration::SessionLockSurface>(uri, 1, 0, "SessionLockSurface");
}
};

Expand Down
61 changes: 26 additions & 35 deletions src/interfaces/sessionlocksurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ namespace QtShellIntegration {
typedef QHash<QWindow *, SessionLockSurface *> SessionLockSurfaceMap;
Q_GLOBAL_STATIC(SessionLockSurfaceMap, globalLockSurfaces)

SessionLockSurface::SessionLockSurface(QObject *parent)
SessionLockSurface::SessionLockSurface(QWindow *window, QObject *parent)
: QObject(parent)
, d_ptr(new SessionLockSurfacePrivate())
{
Q_D(SessionLockSurface);
d->window = window;
globalLockSurfaces->insert(window, this);
}

SessionLockSurface::~SessionLockSurface()
Expand All @@ -26,55 +29,43 @@ SessionLockSurface::~SessionLockSurface()
globalLockSurfaces->remove(d->window);
}

bool SessionLockSurface::isInitialized() const
QWindow *SessionLockSurface::window() const
{
Q_D(const SessionLockSurface);
return d->initialized;
}

void SessionLockSurface::initialize()
{
Q_D(SessionLockSurface);

if (d->initialized)
return;

if (!d->window) {
qCWarning(lcQtShellIntegration, "Window not assigned to SessionLockSurface, failed to initialize");
return;
}

d->initialized = true;
return d->window;
}

QWindow *SessionLockSurface::window() const
bool SessionLockSurface::isEnabled() const
{
Q_D(const SessionLockSurface);
return d->window;
return d->enabled;
}

void SessionLockSurface::setWindow(QWindow *window)
void SessionLockSurface::setEnabled(bool enabled)
{
Q_D(SessionLockSurface);

if (d->window == window)
return;

if (d->initialized) {
qCWarning(lcQtShellIntegration, "Unable to change SessionLockSurface::window after initialization");
return;

if (!d->initialized) {
if (d->enabled == enabled)
return;

d->initialized = true;
d->enabled = enabled;
Q_EMIT enabledChanged(enabled);
}

d->window = window;
Q_EMIT windowChanged(d->window);

if (!globalLockSurfaces->contains(d->window))
globalLockSurfaces->insert(d->window, this);
}

SessionLockSurface *SessionLockSurface::get(QWindow *window)
{
return globalLockSurfaces->value(window, nullptr);
if (globalLockSurfaces->contains(window))
return globalLockSurfaces->value(window);
else
return new SessionLockSurface(window);
}

SessionLockSurface *SessionLockSurface::qmlAttachedProperties(QObject *object)
{
return get(qobject_cast<QWindow *>(object));
}

} // QtShellIntegration
Expand Down
21 changes: 13 additions & 8 deletions src/interfaces/sessionlocksurface.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,30 @@ class LIRIQTSHELLINTEGRATION_EXPORT SessionLockSurface : public QObject
{
Q_OBJECT
QML_ELEMENT
QML_UNCREATABLE("Cannot instantiate SessionLockSurface")
QML_ATTACHED(SessionLockSurface)
Q_DECLARE_PRIVATE(SessionLockSurface)
Q_PROPERTY(QWindow *window READ window WRITE setWindow NOTIFY windowChanged)
Q_PROPERTY(QWindow *window READ window CONSTANT)
Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged)
public:
SessionLockSurface(QObject *parent = nullptr);
~SessionLockSurface();

bool isInitialized() const;

void initialize();

QWindow *window() const;
void setWindow(QWindow *window);

bool isEnabled() const;
void setEnabled(bool enabled);

static SessionLockSurface *get(QWindow *window);

static SessionLockSurface *qmlAttachedProperties(QObject *object);

Q_SIGNALS:
void windowChanged(QWindow *window);
void enabledChanged(bool enabled);
void unlockRequested();

protected:
SessionLockSurface(QWindow *window, QObject *parent = nullptr);

private:
QScopedPointer<SessionLockSurfacePrivate> const d_ptr;
};
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/sessionlocksurface_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class SessionLockSurfacePrivate

bool initialized = false;
QWindow *window = nullptr;
bool enabled = false;
};

} // namespace QtShellIntegration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ QWaylandExtSessionLockSurface::QWaylandExtSessionLockSurface(QWaylandExtSessionL
qCWarning(lcQpaWayland) << "Cannot find LockSurface interface on window" << window->window();
return;
}
if (!interface->isEnabled()) {
qCWarning(lcQpaWayland) << "LockSurface is disabled on window" << window->window();
return;
}

init(lock->get_lock_surface(window->wlSurface(), window->waylandScreen()->output()));

Expand Down

0 comments on commit e4a8825

Please sign in to comment.