Allow replacing default shortcuts.

This commit is contained in:
John Preston 2025-02-17 14:30:47 +04:00
parent 74b71b92b6
commit 2e45d9fc6b

View file

@ -168,6 +168,7 @@ private:
void set(const QKeySequence &result, Command command, bool replace);
void remove(const QString &keys);
void remove(const QKeySequence &keys);
void remove(const QKeySequence &keys, Command command);
void unregister(base::unique_qptr<QAction> shortcut);
void pruneListened();
@ -293,7 +294,7 @@ void Manager::change(
Command command,
std::optional<Command> restore) {
if (!was.isEmpty()) {
remove(was);
remove(was, command);
}
if (!now.isEmpty()) {
set(now, command, true);
@ -397,6 +398,7 @@ bool Manager::readCustomFile() {
const auto entry = (*i).toObject();
const auto keys = entry.constFind(u"keys"_q);
const auto command = entry.constFind(u"command"_q);
const auto removed = entry.constFind(u"removed"_q);
if (keys == entry.constEnd()
|| command == entry.constEnd()
|| !(*keys).isString()
@ -410,7 +412,11 @@ bool Manager::readCustomFile() {
const auto name = (*command).toString();
const auto i = CommandByName.find(name);
if (i != end(CommandByName)) {
set((*keys).toString(), i->second, true);
if (removed != entry.constEnd() && removed->toBool()) {
remove((*keys).toString(), i->second);
} else {
set((*keys).toString(), i->second, true);
}
} else {
LOG(("Shortcut Warning: "
"could not find shortcut command handler '%1'"
@ -565,12 +571,36 @@ void Manager::writeCustomFile() {
}
}
}
for (const auto &[sequence, command] : _defaults) {
if (!_shortcuts.contains(sequence)) {
const auto has = [&](not_null<QObject*> shortcut, Command command) {
for (auto i = _commandByObject.findFirst(shortcut)
; i != end(_commandByObject) && i->first == shortcut
; ++i) {
if (i->second == command) {
return true;
}
}
return false;
};
for (const auto &[sequence, commands] : _defaults) {
const auto i = _shortcuts.find(sequence);
if (i == end(_shortcuts)) {
QJsonObject entry;
entry.insert(u"keys"_q, sequence.toString().toLower());
entry.insert(u"command"_q, QJsonValue());
shortcuts.append(entry);
continue;
}
for (const auto command : commands) {
if (!has(i->second.get(), command)) {
const auto j = CommandNames().find(command);
if (j != CommandNames().end()) {
QJsonObject entry;
entry.insert(u"keys"_q, sequence.toString().toLower());
entry.insert(u"command"_q, j->second);
entry.insert(u"removed"_q, true);
shortcuts.append(entry);
}
}
}
}
@ -669,6 +699,17 @@ void Manager::remove(const QKeySequence &keys) {
}
}
void Manager::remove(const QKeySequence &keys, Command command) {
const auto i = _shortcuts.find(keys);
if (i != end(_shortcuts)) {
_commandByObject.remove(i->second.get(), command);
if (!_commandByObject.contains(i->second.get())) {
unregister(std::move(i->second));
_shortcuts.erase(i);
}
}
}
void Manager::unregister(base::unique_qptr<QAction> shortcut) {
if (shortcut) {
_commandByObject.removeAll(shortcut.get());