Phosphor
Qt6 / Wayland library suite for window-management tools
Loading...
Searching...
No Matches
PlacementMap.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
7#include <PhosphorShell/phosphorshell_export.h>
8
10
11#include <QHash>
12#include <QList>
13#include <QObject>
14#include <QRect>
15#include <QSet>
16#include <QString>
17#include <QStringList>
18#include <QTimer>
19#include <QVariantList>
20#include <QVariantMap>
21
22QT_BEGIN_NAMESPACE
23class QQmlEngine;
24class QJSEngine;
25class QDBusServiceWatcher;
26QT_END_NAMESPACE
27
28namespace PhosphorShell {
29
30class PlacementMap;
31class PlacementMapBus;
32class Workspaces;
33
77class PHOSPHORSHELL_EXPORT PlacementMapScreen : public QObject
78{
79 Q_OBJECT
80
81 Q_PROPERTY(QString screenName READ screenName CONSTANT)
82 Q_PROPERTY(QString screenId READ screenId NOTIFY screenIdChanged)
83 Q_PROPERTY(int mode READ mode NOTIFY modeChanged)
84 Q_PROPERTY(qreal aspect READ aspect NOTIFY aspectChanged)
85 Q_PROPERTY(QRect workArea READ workArea NOTIFY workAreaChanged)
86 Q_PROPERTY(QVariantList cells READ cells NOTIFY cellsChanged)
87 Q_PROPERTY(QVariantList windows READ windows NOTIFY windowsChanged)
88 Q_PROPERTY(QVariantMap lens READ lens NOTIFY lensChanged)
89 Q_PROPERTY(int overflowLeft READ overflowLeft NOTIFY overflowChanged)
90 Q_PROPERTY(int overflowRight READ overflowRight NOTIFY overflowChanged)
91 Q_PROPERTY(int stripExtentPx READ stripExtentPx NOTIFY stripExtentChanged)
92 Q_PROPERTY(int desktopCount READ desktopCount NOTIFY desktopCountChanged)
93 Q_PROPERTY(int currentDesktop READ currentDesktop NOTIFY currentDesktopChanged)
94 Q_PROPERTY(bool urgent READ isUrgent NOTIFY urgentChanged)
95 Q_PROPERTY(QVariantList menuModel READ menuModel NOTIFY menuModelChanged)
97 Q_PROPERTY(int pinnedDesktop READ pinnedDesktop CONSTANT)
98
99public:
100 enum Mode {
101 None = -1,
105 };
106 Q_ENUM(Mode)
107
110 PlacementMapScreen(const QString& screenName, int desktopIndex, PlacementMap* parent);
112
113 [[nodiscard]] int pinnedDesktop() const;
114 [[nodiscard]] bool isPinned() const;
115
116 [[nodiscard]] QString screenName() const;
117 [[nodiscard]] QString screenId() const;
118 [[nodiscard]] int mode() const;
119 [[nodiscard]] bool layoutsAvailable() const;
120 [[nodiscard]] qreal aspect() const;
121 [[nodiscard]] QRect workArea() const;
122 [[nodiscard]] QVariantList cells() const;
123 [[nodiscard]] QVariantList windows() const;
125 Q_INVOKABLE void activateNavigationWindow(const QString& windowId);
127 Q_INVOKABLE void moveNavigationWindowToDesktop(const QString& windowId, int index);
128 Q_INVOKABLE void placeNavigationWindowInZone(const QString& windowId, const QString& zoneId);
129 [[nodiscard]] QVariantMap lens() const;
130 [[nodiscard]] int overflowLeft() const;
131 [[nodiscard]] int overflowRight() const;
132 [[nodiscard]] int stripExtentPx() const;
133 [[nodiscard]] int desktopCount() const;
134 [[nodiscard]] int currentDesktop() const;
135 [[nodiscard]] bool isUrgent() const;
136 [[nodiscard]] QVariantList menuModel() const;
137
141 Q_INVOKABLE QRect cellRect(const QString& id) const;
143 Q_INVOKABLE QString focusedCellId() const;
144
150 Q_INVOKABLE void activate(const QString& id);
155 Q_INVOKABLE void moveCell(const QString& fromId, const QString& toId);
160 Q_INVOKABLE void toggleFloat(const QString& id);
164 Q_INVOKABLE void moveToDesktop(const QString& id, int index);
172 Q_INVOKABLE void registerDropProxy(const QRect& miniatureScreenRect, const QVariantList& cellScreenRects);
174 Q_INVOKABLE void unregisterDropProxy();
177 Q_INVOKABLE void refreshMenu();
178 Q_INVOKABLE void setPlacementMode(int mode);
181 Q_INVOKABLE void applyMenuChoice(const QString& kind, const QString& id);
184 Q_INVOKABLE void scrollView(int delta);
187 Q_INVOKABLE void scrollViewByPx(int px);
189 Q_INVOKABLE void switchDesktop(int index);
190
193 void reseed();
199 Q_INVOKABLE void refreshGeometry();
203 void tileBatchReceived(const QList<PlacementMapParser::TileRect>& tiles);
214 void focusedWindowChanged(const QString& windowId);
216
217Q_SIGNALS:
233 void changed();
234
235private:
236 void resolveScreenId();
237 void fetchDeviceScale();
238 void fetchModeData();
239 void fetchSnappingLayout();
240 void fetchStrip();
241 void fetchVisibleStrip();
242 void stripFetchFinished();
243 void fetchCurrentTiles();
244 void fetchFocus();
245 void applyStrip(const PlacementMapParser::StripParse& parse);
246 void applyTiles(const QList<PlacementMapParser::TileRect>& tiles);
247 void stepFocusToward(const PlacementMapParser::Cell* cell);
248 void rebuildFromSource();
249 void setMode(int mode);
250 void setState(const PlacementMapParser::ScreenState& state);
251 void schedulePublish();
252 void publish();
253 // placementmap_actions.cpp
254 void activateWindow(const QString& windowId);
255 void setWindowFloating(const QString& windowId);
256 void fetchSnappingMenu();
257 void fetchTilingMenu();
258 void fetchScrollingMenu();
259 void setMenu(const QVariantList& menu);
260 void runVerb(const QString& id);
261 [[nodiscard]] QString verbTargetWindowId() const;
262 // placementmap_desktop.cpp
263 void fetchPinnedMode();
264 void fetchPinnedSnappingLayout();
265 void fetchDesktopWindows();
266 void applyDesktopWindows(const PhosphorProtocol::WindowStateList& states);
267 void clearPinnedSource();
268 void sendDropProxy(const QString& json);
269 [[nodiscard]] int wireDesktop() const;
270 [[nodiscard]] const PlacementMapParser::Cell* cellById(const QString& id) const;
271 [[nodiscard]] QString effectiveFocusedWindowId() const;
276 template<typename Reply, typename Fn, typename ErrFn>
277 void call(const QString& interface, const QString& method, const QVariantList& args, Fn&& onReply, ErrFn&& onError);
278 template<typename Reply, typename Fn>
279 void call(const QString& interface, const QString& method, const QVariantList& args, Fn&& onReply);
280
281 PlacementMap* m_map = nullptr;
282 QString m_screenName;
283 QString m_screenId;
284 // -1 for the live screen; the 0-based desktop for a pinned one.
285 int m_pinnedDesktop = -1;
286 // Pinned screens only: the desktop's occupants (snapping) or window ids
287 // (tiling / scrolling) from getWindowStatesForDesktop, and the
288 // coalescer behind windowStatesChanged.
289 QList<PlacementMapParser::Occupant> m_pinnedOccupants;
290 QStringList m_pinnedWindowIds;
291 QTimer m_pinnedRefetch;
292 // Scrolling.stripChanged is a wake-up per placement change, and a drag or
293 // the edge auto-scroll emits one per step. The settle timer folds a burst
294 // into one stripModelJson read (a daemon-side relayout each), and a
295 // wake-up landing while a read is in flight re-arms it once on the reply.
296 QTimer m_stripRefetch;
297 bool m_stripFetchInFlight = false;
298 bool m_stripRefetchWanted = false;
299 // The same wake-up reaches a screen in another mode as a mode re-read
300 // (a context epoch change can mean the desktop switched onto a scrolling
301 // context), coalesced on the same settle so a drag step on one screen
302 // does not cost every other screen a round trip per step.
303 QTimer m_modeRefetch;
304 // Both in the daemon's global coordinates; the published workArea and
305 // cellRect() subtract the screen origin.
306 QRect m_workArea;
307 QRect m_screenGeometry;
308 int m_mode = None;
309 // The engine's own focus for this screen (Tiling.managedFocusedWindow
310 // / focusedWindowChanged). Authoritative once the daemon has answered;
311 // until then the singleton's focus-request proxy stands in.
312 QString m_focusedWindowId;
313 bool m_focusFromDaemon = false;
314 // Bumped by reseed()/serviceLost(); in-flight replies carry the value
315 // they were sent under and are dropped on mismatch.
316 int m_generation = 0;
317 // The screen's getScreenStates row: the resolved ids behind `mode`
318 // and the daemon's (desktop, activity) for the assignment verbs.
319 PlacementMapParser::ScreenState m_state;
320 // Bumped per refreshMenu(); a fetch that started under an older value
321 // publishes nothing.
322 int m_menuGeneration = 0;
323 // The last drop-proxy payload sent, empty while none is registered.
324 QString m_dropProxyJson;
325
326 // Source data, before occupancy and focus are layered on.
327 QList<PlacementMapParser::Cell> m_source;
328 QList<PlacementMapParser::Cell> m_sourceWindows;
329 QList<PlacementMapParser::Cell> m_resolvedWindows;
330 QList<PlacementMapParser::TileRect> m_lastBatch;
331 QRectF m_sourceLens;
332 bool m_sourceVertical = false;
333 int m_sourceOverflowLeft = 0;
334 int m_sourceOverflowRight = 0;
335 int m_sourceStripExtentPx = 0;
336
337 // Resolved cells (occupancy + focus applied), the input to publish().
338 QList<PlacementMapParser::Cell> m_resolved;
339
340 // Published values, change-gated.
341 QVariantList m_cells;
342 QVariantList m_windows;
343 QVariantMap m_lens;
344 QRect m_publishedWorkArea;
345 qreal m_aspect = 0.0;
346 int m_overflowLeft = 0;
347 int m_overflowRight = 0;
348 int m_stripExtentPx = 0;
349 int m_desktopCount = 0;
350 int m_currentDesktop = -1;
351 bool m_urgent = false;
352 QVariantList m_menu;
353
354 QTimer m_coalesce;
355
356 // The output's logical scale from the daemon (Screen.getScreenInfo
357 // devicePixelRatio), which a snapping layout counting its fixed zones in
358 // device pixels is divided by. 0 until the daemon has answered. Re-read
359 // with the work area. Appended last (installed class).
360 qreal m_deviceScale = 0.0;
361};
362
377class PHOSPHORSHELL_EXPORT PlacementMap : public QObject
378{
379 Q_OBJECT
380
382 Q_PROPERTY(bool available READ isAvailable NOTIFY availableChanged)
383
384public:
385 explicit PlacementMap(QObject* parent = nullptr);
386 ~PlacementMap() override;
387
389 [[nodiscard]] static PlacementMap* create(QQmlEngine* engine, QJSEngine* scriptEngine);
390
393 Q_INVOKABLE PhosphorShell::PlacementMapScreen* forScreen(const QString& screenName);
407 Q_INVOKABLE PhosphorShell::PlacementMapScreen* forScreenDesktop(const QString& screenName, int desktopIndex);
408
409 [[nodiscard]] bool isAvailable() const;
410 [[nodiscard]] Workspaces* workspaces() const;
411
413 [[nodiscard]] QHash<QString, QStringList> occupancyForScreen(const QString& screenId) const;
416 [[nodiscard]] QList<PlacementMapParser::Occupant> occupantsForScreen(const QString& screenId) const;
420 [[nodiscard]] QString focusedWindowId() const;
422 [[nodiscard]] const QHash<QString, PlacementMapParser::WindowMeta>& windowMetadata() const;
425 [[nodiscard]] const QSet<QString>& urgentWindows() const;
427 [[nodiscard]] QString currentActivity() const;
428
429Q_SIGNALS:
431
432private:
433 // Screens issue their daemon calls through the singleton's bus.
434 friend class PlacementMapScreen;
435
436 struct WindowOccupancy
437 {
438 QString screenId;
439 QStringList zoneIds;
440 // Order of the last state change; higher is more recent.
441 quint64 seq = 0;
442 };
443
447 struct Capabilities
448 {
449 bool stripModel = true;
450 bool currentTiles = true;
451 bool focusQuery = true;
452 bool focusColumnAt = true;
453 bool scrollViewByPx = true;
454 bool windowMetadata = true;
455 bool urgentWindows = true;
456 bool activateWindow = true;
457 bool setWindowFloatingForScreen = true;
458 bool moveWindowToDesktop = true;
459 bool moveColumnTo = true;
460 bool dropProxy = true;
461 bool windowStatesForDesktop = true;
462 };
463
464 void setAvailable(bool available);
465 void seedWindowStates();
466 void seedUrgentWindows();
467 void seedActivity();
468 void applyWindowState(const QString& windowId, const QString& screenId, const QStringList& zoneIds, bool floating);
469 void forEachScreen(void (PlacementMapScreen::*fn)());
471 [[nodiscard]] static QString screenKey(const QString& screenName, int desktopIndex);
474 void requestMetadata(const QString& windowId);
477 void noteReferencedWindows(PlacementMapScreen* screen, const QSet<QString>& windowIds);
478 void pruneMetadata();
479
480 PlacementMapBus* m_bus = nullptr;
481 QDBusServiceWatcher* m_watcher = nullptr;
482 Workspaces* m_workspaces = nullptr;
483 // Keyed by screenKey(): the live screen under its name, the pinned
484 // ones under name + desktop.
485 QHash<QString, PlacementMapScreen*> m_screens;
486 QHash<QString, WindowOccupancy> m_occupancy;
487 quint64 m_occupancySeq = 0;
488 QString m_focusedWindowId;
489 QString m_activity;
490 QHash<QString, PlacementMapParser::WindowMeta> m_metadata;
491 QSet<QString> m_metadataPending;
492 QHash<PlacementMapScreen*, QSet<QString>> m_referenced;
493 QSet<QString> m_urgent;
494 QTimer m_prune;
495 Capabilities m_caps;
496 bool m_available = false;
497};
498
499} // namespace PhosphorShell
One screen's view of the placement engine, in work-area fractions.
Definition PlacementMap.h:78
void serviceLost()
The daemon left the bus: mode -1, no cells.
void tileBatchReceived(const QList< PlacementMapParser::TileRect > &tiles)
A windowsTileRequested batch arrived; ignored unless it names us.
QString focusedCellId() const
The id of the cell holding the focused window, or empty.
void moveNavigationWindowToDesktop(const QString &windowId, int index)
Move any window in the navigation model, including offscreen columns.
void unregisterDropProxy()
Withdraw the drop proxy, if one is registered.
void tilingChanged()
Tiling.tilingChanged for this screen.
void focusedWindowChanged(const QString &windowId)
Tiling.focusedWindowChanged for this screen: the engine's own focus, which supersedes the singleton's...
void scrollView(int delta)
Wheel over the map.
void refreshMenu()
Re-fetch menuModel for the current mode.
void moveCell(const QString &fromId, const QString &toId)
Drag of one cell onto another (A2 §1.5).
Mode
Definition PlacementMap.h:100
@ None
Definition PlacementMap.h:101
@ Tiling
Definition PlacementMap.h:103
@ Scrolling
Definition PlacementMap.h:104
@ Snapping
Definition PlacementMap.h:102
void switchDesktop(int index)
Switch to the desktop at this 0-based row of Workspaces.model.
void refreshGeometry()
Re-read the work area, the screen origin and the output's scale.
void occupancyChanged()
Occupancy (snapping) or focus (any mode) changed upstream.
void scrollViewByPx(int px)
Drag of the lens: pan the strip's view by px in strip space (Scrolling.scrollViewByPx).
void stripChanged()
Scrolling.stripChanged / stripContextChanged for this screen.
void toggleFloat(const QString &id)
Middle-click: float the cell's window.
PlacementMapScreen(const QString &screenName, PlacementMap *parent)
QVariantList menuModel() const
void placeNavigationWindowInZone(const QString &windowId, const QString &zoneId)
void refreshMode()
Re-read the mode for the current (screen, desktop, activity).
QRect cellRect(const QString &id) const
The cell's rect in this screen's logical coordinates, origin at its top-left (work-area fractions sca...
void activate(const QString &id)
Click on a cell.
void registerDropProxy(const QRect &miniatureScreenRect, const QVariantList &cellScreenRects)
Register the miniature as a drop proxy for the compositor's own window drag (WindowDrag....
void changed()
Coalesced: once per event-loop turn after any of the above.
QVariantList windows() const
void reseed()
Forget everything and re-resolve from the daemon.
void windowStatesChanged()
WindowTracking.windowStateChanged landed in the singleton's table (or the table was re-seeded).
void applyMenuChoice(const QString &kind, const QString &id)
Apply one menuModel entry: assign the layout / algorithm / template to this screen and desktop,...
void activateNavigationWindow(const QString &windowId)
Activate a window from the complete navigation model.
void moveToDesktop(const QString &id, int index)
Move the cell's window to the desktop at this 0-based row of Workspaces.model (WindowTracking....
QML singleton: the placement engine's geometry per screen.
Definition PlacementMap.h:378
const QSet< QString > & urgentWindows() const
Windows demanding attention (getUrgentWindows seeded, then windowUrgencyChanged).
PhosphorShell::PlacementMapScreen * forScreen(const QString &screenName)
The map for a QScreen name (PanelWindow.screen.name).
QString currentActivity() const
The current activity id (LayoutRegistry.getCurrentActivity).
QHash< QString, QStringList > occupancyForScreen(const QString &screenId) const
windowId → zone ids for non-floating windows on screenId.
friend class PlacementMapScreen
Definition PlacementMap.h:434
PlacementMap(QObject *parent=nullptr)
Whether the daemon is on the session bus right now.
QString focusedWindowId() const
The window the daemon last asked the compositor to focus or activate, or empty.
static PlacementMap * create(QQmlEngine *engine, QJSEngine *scriptEngine)
QML-singleton factory hook.
QList< PlacementMapParser::Occupant > occupantsForScreen(const QString &screenId) const
The non-floating windows on screenId, oldest state change first, so the last occupant of a zone is it...
PhosphorShell::PlacementMapScreen * forScreenDesktop(const QString &screenName, int desktopIndex)
The map for a QScreen name pinned to the 0-based desktopIndex (a row of Workspaces....
Workspaces * workspaces() const
const QHash< QString, PlacementMapParser::WindowMeta > & windowMetadata() const
WindowTracking.getWindowMetadata answers so far, by window id.
QML singleton exposing the compositor's workspaces.
Definition Workspaces.h:111
D-Bus marshalling for the shared tiling-family value types (see AutotileTypes.h; the file names preda...
Definition AutotileMarshalling.h:19
Definition PlacementMapParser.h:18
Definition Environment.h:11