Phosphor
Qt6 / Wayland library suite for window-management tools
 
Loading...
Searching...
No Matches
InMemoryConfigStore.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 "IConfigStore.h"
7#include "phosphorscreenscore_export.h"
8
9namespace PhosphorScreens {
10
21class PHOSPHORSCREENSCORE_EXPORT InMemoryConfigStore final : public IConfigStore
22{
23 Q_OBJECT
24public:
32 static constexpr int DefaultMaxScreensPerPhysical = 8;
33
39 explicit InMemoryConfigStore(int maxScreensPerPhysical = DefaultMaxScreensPerPhysical, QObject* parent = nullptr)
40 : IConfigStore(parent)
41 , m_maxScreensPerPhysical(maxScreensPerPhysical)
42 {
43 }
44
45 QHash<QString, VirtualScreenConfig> loadAll() const override
46 {
47 return m_configs;
48 }
49
50 VirtualScreenConfig get(const QString& physicalScreenId) const override
51 {
52 return m_configs.value(physicalScreenId);
53 }
54
55 bool save(const QString& physicalScreenId, const VirtualScreenConfig& config) override
56 {
57 if (config.isEmpty()) {
58 return remove(physicalScreenId);
59 }
60 // Honour the IConfigStore contract: reject what
61 // VirtualScreenConfig::isValid rejects. Mirrors SettingsConfigStore's
62 // behaviour so tests exercise the same acceptance surface real
63 // consumers see. A 0 cap means "no cap" — the default — but callers
64 // can pass a production-matching cap via the constructor to catch
65 // over-limit regressions the lightweight default would otherwise hide.
66 if (!VirtualScreenConfig::isValid(config, physicalScreenId, m_maxScreensPerPhysical)) {
67 return false;
68 }
69 const auto it = m_configs.constFind(physicalScreenId);
70 // Exact operator== (not approxEqual). In-memory storage has no JSON
71 // round-trip to round off floats, so there's nothing for the
72 // tolerance window to absorb — but approxEqual is non-transitive,
73 // and using a non-transitive predicate as a dedup key can collapse
74 // a sequence of sub-tolerance drifts into a single equivalence class
75 // the caller didn't intend. Settings-backed stores keep approxEqual
76 // because they DO serialise through JSON; this store doesn't need it.
77 if (it != m_configs.constEnd() && it.value() == config) {
78 return true; // No-op write — don't fire changed().
79 }
80 m_configs.insert(physicalScreenId, config);
81 Q_EMIT changed();
82 return true;
83 }
84
85 bool remove(const QString& physicalScreenId) override
86 {
87 if (m_configs.remove(physicalScreenId) > 0) {
88 Q_EMIT changed();
89 }
90 return true;
91 }
92
93private:
94 QHash<QString, VirtualScreenConfig> m_configs;
95 int m_maxScreensPerPhysical;
96};
97
98} // namespace PhosphorScreens
Pluggable persistence for virtual-screen configurations.
Definition IConfigStore.h:37
Trivial IConfigStore that holds its state in process memory.
Definition InMemoryConfigStore.h:22
VirtualScreenConfig get(const QString &physicalScreenId) const override
Single-key read for callers that only need one entry (e.g.
Definition InMemoryConfigStore.h:50
bool remove(const QString &physicalScreenId) override
Drop the entry for physicalScreenId.
Definition InMemoryConfigStore.h:85
InMemoryConfigStore(int maxScreensPerPhysical=DefaultMaxScreensPerPhysical, QObject *parent=nullptr)
Construct with an optional maximum-virtual-screens-per-physical cap.
Definition InMemoryConfigStore.h:39
QHash< QString, VirtualScreenConfig > loadAll() const override
Snapshot every persisted virtual-screen config, keyed by physical screen ID.
Definition InMemoryConfigStore.h:45
bool save(const QString &physicalScreenId, const VirtualScreenConfig &config) override
Persist config for physicalScreenId.
Definition InMemoryConfigStore.h:55
Definition IWindowTrackingService.h:27
Configuration for how a physical screen is subdivided into virtual screens.
Definition VirtualScreen.h:124
bool isEmpty() const
Definition VirtualScreen.h:132