Phosphor
Qt6 / Wayland library suite for window-management tools
Loading...
Searching...
No Matches
SnapState.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 "phosphorsnapengine_export.h"
9
10#include <QHash>
11#include <QObject>
12#include <QSet>
13#include <QString>
14#include <QStringList>
15
16#include <optional>
17
18namespace PhosphorEngine {
19class DesktopRemap;
20}
21
22namespace PhosphorSnapEngine {
23
35class PHOSPHORSNAPENGINE_EXPORT SnapState : public QObject, public PhosphorEngine::IPlacementState
36{
37 Q_OBJECT
38
39public:
40 explicit SnapState(const QString& screenId, QObject* parent = nullptr);
41 ~SnapState() override;
42
43 SnapState(const SnapState&) = delete;
44 SnapState& operator=(const SnapState&) = delete;
45
55 {
56 m_windowRegistry = registry;
57 }
58
59 // ═══════════════════════════════════════════════════════════════════════════
60 // IPlacementState
61 // ═══════════════════════════════════════════════════════════════════════════
62
63 QString screenId() const override;
64 int windowCount() const override;
65 QStringList managedWindows() const override;
66 bool containsWindow(const QString& windowId) const override;
67 bool isFloating(const QString& windowId) const override;
68 QStringList floatingWindows() const override;
69 QString placementIdForWindow(const QString& windowId) const override;
70
71 // ═══════════════════════════════════════════════════════════════════════════
72 // Zone Assignment CRUD
73 // ═══════════════════════════════════════════════════════════════════════════
74
75 void assignWindowToZone(const QString& windowId, const QString& zoneId, const QString& screenId,
76 int virtualDesktop);
77 void assignWindowToZones(const QString& windowId, const QStringList& zoneIds, const QString& screenId,
78 int virtualDesktop);
80 {
81 bool wasAssigned = false;
82 bool lastUsedZoneCleared = false;
83 };
84 UnassignResult unassignWindow(const QString& windowId);
97 bool clearScreenAndDesktop(const QString& windowId);
98
99 QString zoneForWindow(const QString& windowId) const;
100 QStringList zonesForWindow(const QString& windowId) const;
101 QStringList windowsInZone(const QString& zoneId) const;
102 QStringList snappedWindows() const;
103 bool isWindowSnapped(const QString& windowId) const;
104
121 void noteFocused(const QString& windowId);
122 QString lastSnappedFocus() const
123 {
124 return m_lastSnappedFocus;
125 }
126 QString lastFloatingFocus() const
127 {
128 return m_lastFloatingFocus;
129 }
130
131 QString screenForWindow(const QString& windowId) const;
132
145 void recordResidence(const QString& windowId, const QString& screenId, int virtualDesktop);
146 int desktopForWindow(const QString& windowId) const;
147
156 bool reassignDesktop(const QString& windowId, int virtualDesktop);
157
164 QStringList windowsOnScreenAndDesktop(const QString& screenId, int virtualDesktop) const;
165
166 const QHash<QString, QString>& screenAssignments() const
167 {
168 return m_windowScreenAssignments;
169 }
170 const QHash<QString, int>& desktopAssignments() const
171 {
172 return m_windowDesktopAssignments;
173 }
174 void setScreenAssignments(const QHash<QString, QString>& s)
175 {
176 if (m_windowScreenAssignments == s) {
177 return;
178 }
179 m_windowScreenAssignments = s;
180 Q_EMIT stateChanged();
181 }
182
183 // ═══════════════════════════════════════════════════════════════════════════
184 // Floating State
185 // ═══════════════════════════════════════════════════════════════════════════
186
187 void setFloating(const QString& windowId, bool floating);
188
195 void setFloatingOnScreen(const QString& windowId, const QString& screenId, int virtualDesktop);
196
198 UnassignResult unsnapForFloat(const QString& windowId);
199 QString preFloatZone(const QString& windowId) const;
200 QStringList preFloatZones(const QString& windowId) const;
201 QString preFloatScreen(const QString& windowId) const;
204 bool clearPreFloatZone(const QString& windowId);
205 void addPreFloatZone(const QString& windowId, const QStringList& zoneIds);
206 void addPreFloatScreen(const QString& windowId, const QString& screenId);
207
208 const QHash<QString, QStringList>& preFloatZoneAssignments() const
209 {
210 return m_preFloatZoneAssignments;
211 }
212 const QHash<QString, QString>& preFloatScreenAssignments() const
213 {
214 return m_preFloatScreenAssignments;
215 }
216 void setPreFloatScreenAssignments(const QHash<QString, QString>& a)
217 {
218 if (m_preFloatScreenAssignments == a) {
219 return;
220 }
221 m_preFloatScreenAssignments = a;
222 Q_EMIT stateChanged();
223 }
224
225 // ═══════════════════════════════════════════════════════════════════════════
226 // Window Lifecycle
227 // ═══════════════════════════════════════════════════════════════════════════
228
229 void windowClosed(const QString& windowId);
230 bool isEmpty() const;
231 void clear();
232
243 void migrateWindowTo(SnapState* target, const QString& windowId, const QString& newScreenId);
244
254 bool removeWindowData(const QString& windowId);
255
256 // ═══════════════════════════════════════════════════════════════════════════
257 // Last-Used Zone Tracking
258 //
259 // Each per-(screen,desktop,activity) store tracks its OWN last-used zone, so a
260 // window opening on monitor A only ever restores to a zone A was last snapped to
261 // (never monitor B's). The on-disk `LastUsedZoneId` persists a single id; the
262 // facade picks the representative store by lastUsedSeq() (most-recently updated).
263 // ═══════════════════════════════════════════════════════════════════════════
264
266 void updateLastUsedZone(const QString& zoneId, const QString& screenId, const QString& windowClass,
267 int virtualDesktop);
268
270 void restoreLastUsedZone(const QString& zoneId, const QString& screenId, const QString& zoneClass, int desktop);
271
272 QString lastUsedZoneId() const
273 {
274 return m_lastUsedZoneId;
275 }
276 QString lastUsedScreenId() const
277 {
278 return m_lastUsedScreenId;
279 }
280 QString lastUsedZoneClass() const
281 {
282 return m_lastUsedZoneClass;
283 }
284 int lastUsedDesktop() const
285 {
286 return m_lastUsedDesktop;
287 }
288
292 void renumberDesktopsAfterRemoval(int removedDesktop);
301 quint64 lastUsedSeq() const
302 {
303 return m_lastUsedSeq;
304 }
305 void retagLastUsedZoneClass(const QString& newClass)
306 {
307 if (m_lastUsedZoneClass == newClass) {
308 return;
309 }
310 m_lastUsedZoneClass = newClass;
311 Q_EMIT stateChanged();
312 }
313
314 // ═══════════════════════════════════════════════════════════════════════════
315 // Auto-Snap Bookkeeping
316 // ═══════════════════════════════════════════════════════════════════════════
317
318 void recordSnapIntent(const QString& windowClass, bool wasUserInitiated);
319 const QSet<QString>& userSnappedClasses() const
320 {
321 return m_userSnappedClasses;
322 }
323 void setUserSnappedClasses(const QSet<QString>& classes)
324 {
325 if (m_userSnappedClasses == classes) {
326 return;
327 }
328 m_userSnappedClasses = classes;
329 Q_EMIT stateChanged();
330 }
331
332 void markAsAutoSnapped(const QString& windowId);
333 bool isAutoSnapped(const QString& windowId) const;
334 bool clearAutoSnapped(const QString& windowId);
335
336 // ═══════════════════════════════════════════════════════════════════════════
337 // Occupied Zone Queries
338 // ═══════════════════════════════════════════════════════════════════════════
339
343 QSet<QString> buildOccupiedZoneSet(const QString& screenFilter = {}, int desktopFilter = 0) const;
344
346 int pruneStaleAssignments(const QSet<QString>& aliveWindowIds);
347
348 // ═══════════════════════════════════════════════════════════════════════════
349 // State Access (for persistence layer)
350 // ═══════════════════════════════════════════════════════════════════════════
351
352 const QHash<QString, QStringList>& zoneAssignments() const
353 {
354 return m_windowZoneAssignments;
355 }
356
357Q_SIGNALS:
358 void windowAssigned(const QString& windowId, const QString& zoneId);
359 void windowUnassigned(const QString& windowId);
360 void floatingChanged(const QString& windowId, bool floating);
362
363private:
370 QString canonicalizeForLookup(const QString& rawWindowId) const;
371
379 UnassignResult clearZoneAssignment(const QString& windowId, bool preserveScreenAndDesktop);
380
381 QSet<QString> allManagedWindowIds() const
382 {
383 QSet<QString> all;
384 all.reserve(m_windowZoneAssignments.size() + m_floatingWindows.size() + m_autoSnappedWindows.size());
385 for (auto it = m_windowZoneAssignments.constBegin(); it != m_windowZoneAssignments.constEnd(); ++it) {
386 all.insert(it.key());
387 }
388 all.unite(m_floatingWindows);
389 all.unite(m_autoSnappedWindows);
390 return all;
391 }
392
393 QString m_screenId;
394
396 PhosphorEngine::IWindowRegistry* m_windowRegistry = nullptr;
397
398 QHash<QString, QStringList> m_windowZoneAssignments;
399 QHash<QString, QString> m_windowScreenAssignments;
400 QHash<QString, int> m_windowDesktopAssignments;
401 QSet<QString> m_floatingWindows;
402 QHash<QString, QStringList> m_preFloatZoneAssignments;
403 QHash<QString, QString> m_preFloatScreenAssignments;
404
405 QString m_lastSnappedFocus;
406 QString m_lastFloatingFocus;
407
408 QString m_lastUsedZoneId;
409 QString m_lastUsedScreenId;
410 QString m_lastUsedZoneClass;
411 int m_lastUsedDesktop = 0;
413 quint64 m_lastUsedSeq = 0;
414
415 QSet<QString> m_userSnappedClasses;
416 QSet<QString> m_autoSnappedWindows;
417};
418
419} // namespace PhosphorSnapEngine
How KWin's desktop positions moved in one change of its desktop list: for each 1-based position in th...
Definition DesktopRemap.h:29
Per-screen placement state contract.
Definition IPlacementState.h:28
Definition IWindowRegistry.h:49
void setFloatingOnScreen(const QString &windowId, const QString &screenId, int virtualDesktop)
Mark a window floating AND record its screen+desktop without a zone.
void setUserSnappedClasses(const QSet< QString > &classes)
Definition SnapState.h:323
SnapState(const QString &screenId, QObject *parent=nullptr)
void windowClosed(const QString &windowId)
QString preFloatScreen(const QString &windowId) const
bool isFloating(const QString &windowId) const override
Whether the window is floating (excluded from placement).
void markAsAutoSnapped(const QString &windowId)
void assignWindowToZones(const QString &windowId, const QStringList &zoneIds, const QString &screenId, int virtualDesktop)
QStringList zonesForWindow(const QString &windowId) const
QString lastSnappedFocus() const
Definition SnapState.h:122
QString lastUsedZoneClass() const
Definition SnapState.h:280
bool clearPreFloatZone(const QString &windowId)
Returns true when an entry was actually removed, so callers can gate their dirty-marking on a real mu...
SnapState(const SnapState &)=delete
UnassignResult unassignWindow(const QString &windowId)
QStringList managedWindows() const override
All windows managed by this state (tiled + floating).
bool removeWindowData(const QString &windowId)
Remove ALL of windowId's per-window data from this store (zone/screen/ desktop assignment,...
void setScreenAssignments(const QHash< QString, QString > &s)
Definition SnapState.h:174
QString screenId() const override
Screen this state object manages.
bool containsWindow(const QString &windowId) const override
Whether the window is in this state's managed set.
bool clearScreenAndDesktop(const QString &windowId)
Drop ONLY the screen/desktop residence entries, leaving the zone assignment, the floating bit and the...
QString preFloatZone(const QString &windowId) const
void restoreLastUsedZone(const QString &zoneId, const QString &screenId, const QString &zoneClass, int desktop)
Restore last-used zone fields from persistence without emitting stateChanged.
const QSet< QString > & userSnappedClasses() const
Definition SnapState.h:319
UnassignResult unsnapForFloat(const QString &windowId)
Save zone assignment before floating for later restore.
void setWindowRegistry(PhosphorEngine::IWindowRegistry *registry)
Attach the daemon's shared window registry so every windowId-keyed accessor can canonicalize the inco...
Definition SnapState.h:54
void noteFocused(const QString &windowId)
Layer-side focus memory for switch-focus-between-floating-and-tiling.
QStringList windowsOnScreenAndDesktop(const QString &screenId, int virtualDesktop) const
Windows with a recorded desktop assignment (snapped, or floated-on-screen via setFloatingOnScreen / u...
QString lastUsedScreenId() const
Definition SnapState.h:276
QSet< QString > buildOccupiedZoneSet(const QString &screenFilter={}, int desktopFilter=0) const
Build the set of zone IDs currently occupied by snapped windows.
void addPreFloatZone(const QString &windowId, const QStringList &zoneIds)
const QHash< QString, int > & desktopAssignments() const
Definition SnapState.h:170
int pruneStaleAssignments(const QSet< QString > &aliveWindowIds)
Remove zone/screen/desktop assignments for windows not in the alive set.
void retagLastUsedZoneClass(const QString &newClass)
Definition SnapState.h:305
const QHash< QString, QStringList > & preFloatZoneAssignments() const
Definition SnapState.h:208
QStringList snappedWindows() const
quint64 lastUsedSeq() const
Monotonic stamp bumped every time this store's last-used zone is set to a non-empty value (via update...
Definition SnapState.h:301
void setPreFloatScreenAssignments(const QHash< QString, QString > &a)
Definition SnapState.h:216
bool isWindowSnapped(const QString &windowId) const
QStringList windowsInZone(const QString &zoneId) const
QStringList preFloatZones(const QString &windowId) const
void addPreFloatScreen(const QString &windowId, const QString &screenId)
void migrateWindowTo(SnapState *target, const QString &windowId, const QString &newScreenId)
Move windowId's per-window placement entries (zone assignment, live screen, desktop,...
QString placementIdForWindow(const QString &windowId) const override
Opaque placement identifier for the window's current slot.
QString lastUsedZoneId() const
Definition SnapState.h:272
bool reassignDesktop(const QString &windowId, int virtualDesktop)
Re-stamp a snapped window's virtual-desktop membership to virtualDesktop, keeping its zone and screen...
void renumberDesktopsAfterRemoval(int removedDesktop)
Shift every desktop number this store records (each window's desktop and the last-used zone's) the wa...
void recordSnapIntent(const QString &windowClass, bool wasUserInitiated)
void remapDesktops(const PhosphorEngine::DesktopRemap &remap)
The same for any change of KWin's desktop list: every number moves to the position remap gives its de...
void updateLastUsedZone(const QString &zoneId, const QString &screenId, const QString &windowClass, int virtualDesktop)
Update last-used zone and emit stateChanged.
QString zoneForWindow(const QString &windowId) const
const QHash< QString, QString > & screenAssignments() const
Definition SnapState.h:166
void assignWindowToZone(const QString &windowId, const QString &zoneId, const QString &screenId, int virtualDesktop)
void windowAssigned(const QString &windowId, const QString &zoneId)
bool clearAutoSnapped(const QString &windowId)
SnapState & operator=(const SnapState &)=delete
int desktopForWindow(const QString &windowId) const
QStringList floatingWindows() const override
All currently-floating windows.
int windowCount() const override
Total number of managed windows (tiled + floating).
void setFloating(const QString &windowId, bool floating)
void floatingChanged(const QString &windowId, bool floating)
QString screenForWindow(const QString &windowId) const
const QHash< QString, QString > & preFloatScreenAssignments() const
Definition SnapState.h:212
void windowUnassigned(const QString &windowId)
int lastUsedDesktop() const
Definition SnapState.h:284
void recordResidence(const QString &windowId, const QString &screenId, int virtualDesktop)
Record screen + desktop residence WITHOUT a zone assignment and without touching the floating set.
const QHash< QString, QStringList > & zoneAssignments() const
Definition SnapState.h:352
QString lastFloatingFocus() const
Definition SnapState.h:126
bool isAutoSnapped(const QString &windowId) const
Definition DesktopRemap.h:12
Definition SnapStateResolver.h:16
bool wasAssigned
Definition SnapState.h:81
bool lastUsedZoneCleared
Definition SnapState.h:82