6#include "phosphorengine_export.h"
17#include <QLoggingCategory>
26Q_DECLARE_EXPORTED_LOGGING_CATEGORY(lcPerScreenStates, PHOSPHORENGINE_EXPORT)
43template<
typename StateT>
46 static_assert(std::is_base_of_v<IPlacementState, StateT>,
47 "PerScreenStates<StateT>: StateT must implement PhosphorEngine::IPlacementState");
55 auto it = m_states.find(key);
56 if (it != m_states.end()) {
59 StateT* created = factory ? factory() :
nullptr;
61 m_states.insert(key, created);
69 return m_states.value(key);
74 return m_states.contains(key);
80 m_states.insert(key, state);
86 return m_states.take(key);
91 return m_states.size();
95 const QHash<PlacementStateKey, StateT*>&
states()
const
119 m_contextKeyResolver = std::move(resolver);
124 return m_windowMemberships.contains(windowId);
131 return primaryOf(m_windowMemberships.value(windowId));
135 std::optional<PlacementStateKey>
windowKey(
const QString& windowId)
const
137 auto it = m_windowMemberships.constFind(windowId);
138 if (it == m_windowMemberships.constEnd()) {
141 return primaryOf(it.value());
147 return m_windowMemberships.value(windowId);
156 return m_windowMemberships.value(windowId).contains(key);
165 m_windowMemberships.insert(windowId, {key});
172 QList<PlacementStateKey>& keys = m_windowMemberships[windowId];
173 if (!keys.contains(key)) {
183 auto it = m_windowMemberships.find(windowId);
184 if (it == m_windowMemberships.end()) {
187 it.value().removeAll(key);
188 if (it.value().isEmpty()) {
189 m_windowMemberships.erase(it);
196 m_windowMemberships.remove(windowId);
203 return primaryOf(m_windowMemberships.take(windowId));
209 return m_windowMemberships.keys();
219 for (
auto it = m_windowMemberships.cbegin(); it != m_windowMemberships.cend(); ++it) {
232 auto it = m_windowMemberships.constFind(windowId);
233 if (it == m_windowMemberships.constEnd()) {
240 return m_states.value(key);
253 if (oldKey == newKey) {
262 auto it = m_windowMemberships.find(windowId);
263 if (it != m_windowMemberships.end() && !it.value().contains(oldKey)) {
265 qCWarning(lcPerScreenStates) <<
"PerScreenStates::migrate: caller asserted" << windowId <<
"was on desktop"
267 <<
"but its memberships are" << it.value().size() <<
"key(s) not including"
268 <<
"that one — migrating the primary instead";
270 if (it == m_windowMemberships.end()) {
271 m_windowMemberships.insert(windowId, {newKey});
278 QList<PlacementStateKey>& keys = it.value();
279 const qsizetype at = keys.indexOf(keys.contains(oldKey) ? oldKey : primaryOf(keys));
284 if (keys.contains(newKey)) {
295 for (
auto it = m_windowMemberships.begin(); it != m_windowMemberships.end(); ++it) {
296 QList<PlacementStateKey>& keys = it.value();
297 const qsizetype at = keys.indexOf(oldKey);
301 if (keys.contains(newKey)) {
317 for (
auto it = m_states.begin(); it != m_states.end();) {
318 if (pred(it.key(), it.value())) {
320 onRemove(it.key(), it.value());
322 it = m_states.erase(it);
338 for (
auto it = m_windowMemberships.begin(); it != m_windowMemberships.end();) {
339 QList<PlacementStateKey>& keys = it.value();
340 const QString& windowId = it.key();
342 return pred(windowId, key);
344 if (keys.isEmpty()) {
345 it = m_windowMemberships.erase(it);
358 if (keys.isEmpty()) {
361 if (keys.size() == 1 || !m_contextKeyResolver) {
364 for (
const PlacementStateKey& key : keys) {
365 if (m_contextKeyResolver(key.
screenId) == key) {
376 QHash<PlacementStateKey, StateT*> m_states;
377 QHash<QString, QList<PlacementStateKey>> m_windowMemberships;
Exported because this is a header-only template used from every engine library, so the category has t...
Definition PerScreenStates.h:45
bool containsKey(const PlacementStateKey &key) const
Definition PerScreenStates.h:72
bool hasWindow(const QString &windowId) const
Definition PerScreenStates.h:122
StateT * takeState(const PlacementStateKey &key)
Remove and return the state at key (nullptr if absent). Does not delete.
Definition PerScreenStates.h:84
PlacementStateKey keyForWindow(const QString &windowId) const
The PRIMARY key for windowId, or a default-constructed key when untracked (an empty screenId marks "n...
Definition PerScreenStates.h:129
StateT * stateForKey(const PlacementStateKey &key) const
The state for key, or nullptr if none exists (never creates).
Definition PerScreenStates.h:67
void migrate(const QString &windowId, const PlacementStateKey &oldKey, const PlacementStateKey &newKey)
Move a window's reverse-map entry from oldKey to newKey.
Definition PerScreenStates.h:248
StateT * forKey(const PlacementStateKey &key, const std::function< StateT *()> &factory)
Lazily creates the state for key if absent.
Definition PerScreenStates.h:53
std::function< PlacementStateKey(const QString &screenId)> ContextKeyResolver
Resolves the key a screen is CURRENTLY showing.
Definition PerScreenStates.h:115
void removeWindow(const QString &windowId)
Drop EVERY membership for windowId (does not touch state objects).
Definition PerScreenStates.h:194
void removeStatesIf(const std::function< bool(const PlacementStateKey &, StateT *)> &pred, const std::function< void(const PlacementStateKey &, StateT *)> &onRemove)
Lockstep prune of the forward map: for every state matching pred, invoke onRemove (engine-specific te...
Definition PerScreenStates.h:314
StateT * forWindow(const QString &windowId, PlacementStateKey *outKey=nullptr) const
Resolve the state holding windowId's PRIMARY membership (no create).
Definition PerScreenStates.h:230
void insertState(const PlacementStateKey &key, StateT *state)
Insert/replace the state at key (caller retains ownership semantics).
Definition PerScreenStates.h:78
void setKeyForWindow(const QString &windowId, const PlacementStateKey &key)
Make key the window's ONLY membership.
Definition PerScreenStates.h:163
void rekeyWindows(const PlacementStateKey &oldKey, const PlacementStateKey &newKey)
Rewrite every membership equal to oldKey to newKey.
Definition PerScreenStates.h:293
int stateCount() const
Definition PerScreenStates.h:89
void addMembership(const QString &windowId, const PlacementStateKey &key)
Add key to the window's memberships, keeping the ones it has.
Definition PerScreenStates.h:170
bool hasMembership(const QString &windowId, const PlacementStateKey &key) const
Whether windowId belongs to the state at key specifically.
Definition PerScreenStates.h:154
void removeWindowsIf(const std::function< bool(const QString &, const PlacementStateKey &)> &pred)
Drop MEMBERSHIPS matching pred (e.g.
Definition PerScreenStates.h:333
void removeMembership(const QString &windowId, const PlacementStateKey &key)
Drop one membership.
Definition PerScreenStates.h:181
PlacementStateKey takeWindow(const QString &windowId)
Remove every membership and return the primary (default key when absent), mirroring QHash::take.
Definition PerScreenStates.h:201
QStringList trackedWindowIds() const
Every tracked window id, once each however many states hold it.
Definition PerScreenStates.h:207
QList< PlacementStateKey > membershipsForWindow(const QString &windowId) const
Every state this window belongs to, in the order they were added.
Definition PerScreenStates.h:145
void setContextKeyResolver(ContextKeyResolver resolver)
Definition PerScreenStates.h:117
void forEachMembership(const std::function< void(const QString &, const PlacementStateKey &)> &fn) const
Visit every (windowId, key) membership pair.
Definition PerScreenStates.h:214
const QHash< PlacementStateKey, StateT * > & states() const
Read-only view of the forward map for iteration.
Definition PerScreenStates.h:95
std::optional< PlacementStateKey > windowKey(const QString &windowId) const
The PRIMARY key for windowId, or nullopt when untracked.
Definition PerScreenStates.h:135
Definition DesktopRemap.h:12
Identity of a per-screen placement state: a window's placement is scoped to the (screen,...
Definition EngineTypes.h:25
int desktop
Definition EngineTypes.h:27
QString screenId
Definition EngineTypes.h:26