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 Phosphor::Shortcuts {
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
75 void rebind(const QString& id, const QKeySequence& seq);
76
84 void unbind(const QString& id);
85
98 void flush();
99
100 QKeySequence shortcut(const QString& id) const;
101
111 QVector<Binding> bindings(bool persistentOnly = false) const;
112
113Q_SIGNALS:
121 void triggered(QString id);
122
126 void ready();
127
128private Q_SLOTS:
129 void onBackendActivated(QString id);
130 void onBackendReady();
131
132private:
133 struct Entry
134 {
135 Binding binding;
136 std::function<void()> callback;
137 // Last values successfully pushed to the backend. Used to decide
138 // whether flush() should re-register (default changed) or update-only
139 // (current changed), and to short-circuit no-op flushes.
140 QKeySequence lastSentDefault;
141 QKeySequence lastSentCurrent;
142 bool registered = false; // has registerShortcut been sent yet?
143 // Persistent flag last forwarded to the backend. flush() re-registers
144 // when this drifts from `persistent` so a re-bind() that flips the flag
145 // without changing the key still reaches the backend — the backend's
146 // crash-purge decision (KGlobalAccelBackend) depends on the latest
147 // value. Only meaningful once `registered` is true.
148 bool lastSentPersistent = true;
149 bool persistent = true; // surfaces in bindings(persistentOnly=true)
150 };
151
152 QPointer<IBackend> m_backend;
153 QHash<QString, Entry> m_entries;
154};
155
156} // namespace Phosphor::Shortcuts
Pluggable global shortcut backend.
Definition IBackend.h:29
Consumer-facing facade over IBackend.
Definition Registry.h:33
QKeySequence shortcut(const QString &id) const
void flush()
Forward queued bind/rebind ops to the backend.
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.
void unbind(const QString &id)
Drop a binding entirely.
void rebind(const QString &id, const QKeySequence &seq)
Change the active binding for an already-registered id.
void ready()
Forwarded from IBackend::ready().
Registry(IBackend *backend, QObject *parent=nullptr)
QVector< Binding > bindings(bool persistentOnly=false) const
Enumerate registered bindings, sorted by id for deterministic output.
Definition Factory.h:12
QString id
Definition Registry.h:38
QKeySequence defaultSeq
Definition Registry.h:39
QString description
Definition Registry.h:41
QKeySequence currentSeq
Definition Registry.h:40