Phosphor
Qt6 / Wayland library suite for window-management tools
 
Loading...
Searching...
No Matches
Registry.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 <QHash>
7#include <QKeySequence>
8#include <QObject>
9#include <QPointer>
10#include <QString>
11#include <QVector>
12
13#include <functional>
14
15#include "phosphorshortcuts_export.h"
16
17namespace PhosphorShortcuts {
18
19class IBackend;
20
32class PHOSPHORSHORTCUTS_EXPORT Registry : public QObject
33{
34 Q_OBJECT
35public:
36 struct Binding
37 {
38 QString id;
39 QKeySequence defaultSeq;
40 QKeySequence currentSeq;
41 QString description;
42 };
43
44 explicit Registry(IBackend* backend, QObject* parent = nullptr);
45 ~Registry() override;
46
66 void bind(const QString& id, const QKeySequence& defaultSeq, const QString& description = {},
67 std::function<void()> callback = {}, bool persistent = true);
68
76 void rebind(const QString& id, const QKeySequence& seq);
77
85 void unbind(const QString& id);
86
100 void flush();
101
102 QKeySequence shortcut(const QString& id) const;
103
126 QStringList effectiveTriggers(const QString& id) const;
127
137 QVector<Binding> bindings(bool persistentOnly = false) const;
138
139Q_SIGNALS:
147 void triggered(QString id);
148
152 void ready();
153
159 void triggersChanged(QString id);
160
161private Q_SLOTS:
162 void onBackendActivated(QString id);
163 void onBackendReady();
164
165private:
166 struct Entry
167 {
168 Binding binding;
169 std::function<void()> callback;
170 // Last values successfully pushed to the backend. Used to decide
171 // whether flush() should re-register (default changed) or update-only
172 // (current changed), and to short-circuit no-op flushes.
173 QKeySequence lastSentDefault;
174 QKeySequence lastSentCurrent;
175 bool registered = false; // has registerShortcut been sent yet?
176 // Persistent flag last forwarded to the backend. flush() re-registers
177 // when this drifts from `persistent` so a re-bind() that flips the flag
178 // without changing the key still reaches the backend — the backend's
179 // crash-purge decision (KGlobalAccelBackend) depends on the latest
180 // value. Only meaningful once `registered` is true.
181 bool lastSentPersistent = true;
182 bool persistent = true; // surfaces in bindings(persistentOnly=true)
183 };
184
185 QPointer<IBackend> m_backend;
186 QHash<QString, Entry> m_entries;
187};
188
189} // namespace PhosphorShortcuts
Pluggable global shortcut backend.
Definition IBackend.h:32
Consumer-facing facade over IBackend.
Definition Registry.h:33
void ready()
Forwarded from IBackend::ready().
QVector< Binding > bindings(bool persistentOnly=false) const
Enumerate registered bindings, sorted by id for deterministic output.
void rebind(const QString &id, const QKeySequence &seq)
Change the active binding for an already-registered id.
void flush()
Forward queued bind/rebind ops to the backend.
QKeySequence shortcut(const QString &id) const
void unbind(const QString &id)
Drop a binding entirely.
void bind(const QString &id, const QKeySequence &defaultSeq, const QString &description={}, std::function< void()> callback={}, bool persistent=true)
Register a shortcut.
void triggered(QString id)
Emitted on every activation, regardless of whether the binding has a callback.
Registry(IBackend *backend, QObject *parent=nullptr)
void triggersChanged(QString id)
Forwarded from IBackend::triggersChanged for ids this registry owns — the effective binding changed o...
QStringList effectiveTriggers(const QString &id) const
Display strings for the key(s) the user EFFECTIVELY has to press for an id right now.
Definition Factory.h:12
Definition Registry.h:37
QString description
Definition Registry.h:41
QKeySequence defaultSeq
Definition Registry.h:39
QKeySequence currentSeq
Definition Registry.h:40
QString id
Definition Registry.h:38