Phosphor
Qt6 / Wayland library suite for window-management tools
Loading...
Searching...
No Matches
WorkspaceMap.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 "phosphorworkspaces_export.h"
7
8#include <functional>
9
10#include <QHash>
11#include <QList>
12#include <QString>
13#include <QStringList>
14
15namespace PhosphorWorkspaces {
16
18struct PHOSPHORWORKSPACES_EXPORT WorkspaceEntry
19{
23 QString desktopId;
24 QString name;
30 QString namedId;
31 QString homeScreenId;
32
33 bool operator==(const WorkspaceEntry& other) const
34 {
35 return desktopId == other.desktopId && name == other.name && namedId == other.namedId
36 && homeScreenId == other.homeScreenId;
37 }
38};
39
48class PHOSPHORWORKSPACES_EXPORT WorkspaceMap
49{
50public:
51 // ── Screen order (slice concatenation order) ────────────────────────────
52 QStringList screenOrder() const;
57 void setScreenOrder(const QStringList& order);
58
59 // ── Slice access ────────────────────────────────────────────────────────
63 bool hasScreen(const QString& screenId) const;
68 bool knowsScreen(const QString& screenId) const;
69 QList<WorkspaceEntry> slice(const QString& screenId) const;
70 int sliceSize(const QString& screenId) const;
72 QString ownerOf(const QString& desktopId) const;
74 int sliceIndexOf(const QString& desktopId) const;
76 QStringList allDesktopIds() const;
79 WorkspaceEntry entryFor(const QString& desktopId) const;
80
81 // ── Mutation (reconciler only) ──────────────────────────────────────────
85 void insert(const QString& screenId, int sliceIndex, const WorkspaceEntry& entry);
100 bool remove(const QString& desktopId);
102 bool reorderWithinSlice(const QString& desktopId, int newSliceIndex);
106 bool reorderSlice(const QString& screenId, const QStringList& orderedIds);
109 bool transfer(const QString& desktopId, const QString& toScreenId, int sliceIndex);
110 void setName(const QString& desktopId, const QString& name);
111 void setNamedId(const QString& desktopId, const QString& namedId);
112 void setHomeScreen(const QString& desktopId, const QString& homeScreenId);
117 bool renameScreen(const QString& fromScreenId, const QString& toScreenId);
120 QList<WorkspaceEntry> takeSlice(const QString& screenId);
121 void clear();
122
123 // ── Fork-5 position arithmetic ──────────────────────────────────────────
130 uint globalPositionForInsert(const QString& screenId, int sliceIndex) const;
131
132 // ── Consistency ─────────────────────────────────────────────────────────
138 bool consistentWith(const QStringList& kwinIds) const;
142 QStringList repairAgainst(const QStringList& kwinIds);
143
144 // ── Serialization (public form + state file share this) ─────────────────
152 QString toJson(quint64 generation, const QHash<QString, int>& currentByScreen,
153 const std::function<int(const QString&)>& indexOf, bool includeState) const;
157 bool fromJson(const QString& json);
160 static constexpr int MaxDesktopIdLength = 256;
161
162 bool operator==(const WorkspaceMap& other) const
163 {
164 return m_screenOrder == other.m_screenOrder && m_slices == other.m_slices;
165 }
166
167private:
168 QStringList m_screenOrder;
169 QHash<QString, QList<WorkspaceEntry>> m_slices;
170 QHash<QString, QString> m_ownerOf;
171};
172
173} // namespace PhosphorWorkspaces
The per-monitor workspace ownership model: screenId → ordered slice of KWin desktop ids,...
Definition WorkspaceMap.h:49
void setScreenOrder(const QStringList &order)
Replace the screen order (left-to-right geometry, ties by id — computed by the caller,...
bool remove(const QString &desktopId)
Remove a desktop from whatever slice owns it.
QString ownerOf(const QString &desktopId) const
Owner screen of a desktop id, or empty when unowned.
QString toJson(quint64 generation, const QHash< QString, int > &currentByScreen, const std::function< int(const QString &)> &indexOf, bool includeState) const
Map JSON per plan §3.2/§3.3.
QStringList screenOrder() const
QStringList allDesktopIds() const
All owned desktop ids, slice-concatenated in screen order.
bool consistentWith(const QStringList &kwinIds) const
True when every invariant holds against kwinIds (each KWin id owned exactly once, no entry for a vani...
QList< WorkspaceEntry > slice(const QString &screenId) const
int sliceIndexOf(const QString &desktopId) const
Slice position (0-based) of a desktop within its owner, or -1.
void insert(const QString &screenId, int sliceIndex, const WorkspaceEntry &entry)
Insert an entry into a screen's slice at sliceIndex (clamped).
WorkspaceEntry entryFor(const QString &desktopId) const
The entry for a desktop id (owner looked up via the index); nullptr-like default entry when unowned.
bool transfer(const QString &desktopId, const QString &toScreenId, int sliceIndex)
Re-own a desktop to another screen at sliceIndex (clamped); keeps the entry's metadata.
void setHomeScreen(const QString &desktopId, const QString &homeScreenId)
bool knowsScreen(const QString &screenId) const
Whether this screen is part of the world at all: it holds a slice, or it appears in the screen order.
int sliceSize(const QString &screenId) const
void setName(const QString &desktopId, const QString &name)
bool reorderWithinSlice(const QString &desktopId, int newSliceIndex)
Move a desktop within its owner's slice to newSliceIndex (clamped).
bool fromJson(const QString &json)
Parse a state-file or public-form payload into this map.
uint globalPositionForInsert(const QString &screenId, int sliceIndex) const
Global 0-based insert position computed from the MAP alone: the sum of slice sizes of screens precedi...
bool renameScreen(const QString &fromScreenId, const QString &toScreenId)
Re-key a screen in place: its slice, its slot in the screen order and its owner rows move to toScreen...
static constexpr int MaxDesktopIdLength
The longest desktop id fromJson accepts.
Definition WorkspaceMap.h:160
bool operator==(const WorkspaceMap &other) const
Definition WorkspaceMap.h:162
bool reorderSlice(const QString &screenId, const QStringList &orderedIds)
Put a screen's slice in exactly the order orderedIds lists.
QList< WorkspaceEntry > takeSlice(const QString &screenId)
Drop a screen's slice entirely, returning its entries in order (used by hotplug migration,...
void setNamedId(const QString &desktopId, const QString &namedId)
QStringList repairAgainst(const QStringList &kwinIds)
Repair against the authoritative KWin id list: drop entries whose id vanished, report ids KWin has th...
bool hasScreen(const QString &screenId) const
Whether this screen HOLDS A SLICE.
Definition WindowTrackingService.h:46
One owned desktop in a screen's workspace slice.
Definition WorkspaceMap.h:19
QString desktopId
KWin's desktop id, an opaque string.
Definition WorkspaceMap.h:23
QString name
empty = dynamic; non-empty = named (destroy-exempt)
Definition WorkspaceMap.h:24
bool operator==(const WorkspaceEntry &other) const
Definition WorkspaceMap.h:33
QString homeScreenId
set only while displaced by hotplug
Definition WorkspaceMap.h:31
QString namedId
The persistent id of the named-workspace declaration this desktop realizes (NamedWorkspace::id).
Definition WorkspaceMap.h:30