Phosphor
Qt6 / Wayland library suite for window-management tools
Loading...
Searching...
No Matches
Workspaces.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 fuddlesworth
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
6#include <PhosphorShell/phosphorshell_export.h>
7
8#include <QAbstractListModel>
9#include <QObject>
10#include <QPointer>
11#include <QString>
12#include <QStringList>
13
14QT_BEGIN_NAMESPACE
15class QQmlEngine;
16class QJSEngine;
17QT_END_NAMESPACE
18
19namespace PhosphorShell {
20
21class WorkspaceBackend;
22
39class PHOSPHORSHELL_EXPORT WorkspaceListModel : public QAbstractListModel
40{
41 Q_OBJECT
42
43public:
44 enum Role {
45 IdRole = Qt::UserRole + 1,
48 };
49 Q_ENUM(Role)
50
51
52 explicit WorkspaceListModel(WorkspaceBackend* backend, QObject* parent = nullptr);
53 ~WorkspaceListModel() override;
54
55 [[nodiscard]] int rowCount(const QModelIndex& parent = {}) const override;
56 [[nodiscard]] QVariant data(const QModelIndex& index, int role) const override;
57 [[nodiscard]] QHash<int, QByteArray> roleNames() const override;
58
59private:
60 void reload();
61
62 QPointer<WorkspaceBackend> m_backend;
63 QStringList m_ids;
64 QStringList m_names;
65 // 0-based row of the active workspace, or -1 when none is.
66 int m_activeRow = -1;
67};
68
107// Registered as a QML singleton imperatively in ShellEngine::load(), same
108// as Toplevels; the QML_* macros would need qt_add_qml_module, which this
109// library does not use.
110class PHOSPHORSHELL_EXPORT Workspaces : public QObject
111{
112 Q_OBJECT
113
115 Q_PROPERTY(QAbstractListModel* model READ model CONSTANT)
117 Q_PROPERTY(QString activeId READ activeId NOTIFY activeChanged)
118 Q_PROPERTY(QString activeName READ activeName NOTIFY activeChanged)
122 Q_PROPERTY(int activeIndex READ activeIndex NOTIFY activeChanged)
123 Q_PROPERTY(int count READ count NOTIFY countChanged)
124 Q_PROPERTY(bool supported READ isSupported NOTIFY supportedChanged)
125
126public:
129 explicit Workspaces(QObject* parent = nullptr);
132 explicit Workspaces(WorkspaceBackend* backend, QObject* parent = nullptr);
133 ~Workspaces() override;
134
136 [[nodiscard]] static Workspaces* create(QQmlEngine* engine, QJSEngine* scriptEngine);
137
138 [[nodiscard]] QAbstractListModel* model() const;
139 [[nodiscard]] QString activeId() const;
140 [[nodiscard]] QString activeName() const;
141 [[nodiscard]] int activeIndex() const;
142 [[nodiscard]] int count() const;
143 [[nodiscard]] bool isSupported() const;
144
148 Q_INVOKABLE void switchTo(const QString& id);
149
150Q_SIGNALS:
154
155private:
156 void sync();
157
158 QPointer<WorkspaceBackend> m_backend;
159 WorkspaceListModel* m_model = nullptr;
160 // Last-published values, so the notifications are change-gated rather
161 // than mirroring every update the backend makes.
162 QString m_activeId;
163 QString m_activeName;
164 int m_activeIndex = 0;
165 int m_count = 0;
166};
167
168} // namespace PhosphorShell
Incrementally-updating model of the compositor's workspaces.
Definition Workspaces.h:40
WorkspaceListModel(WorkspaceBackend *backend, QObject *parent=nullptr)
Null backend: an empty model.
QVariant data(const QModelIndex &index, int role) const override
QHash< int, QByteArray > roleNames() const override
Role
Definition Workspaces.h:44
@ IsActiveRole
Definition Workspaces.h:47
@ IdRole
Definition Workspaces.h:45
@ NameRole
Definition Workspaces.h:46
int rowCount(const QModelIndex &parent={}) const override
QString activeName() const
QAbstractListModel * model() const
static Workspaces * create(QQmlEngine *engine, QJSEngine *scriptEngine)
QML-singleton factory hook.
Workspaces(WorkspaceBackend *backend, QObject *parent=nullptr)
Over backend, which must outlive this object or be destroyed with it.
void switchTo(const QString &id)
Switch to the workspace with this identifier.
QString activeId() const
Workspaces(QObject *parent=nullptr)
Bind Repeater { model: Workspaces.model }.
Definition Environment.h:11