From 2e45d9fc6bd8b22dc74e6681e95595873c29a717 Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 17 Feb 2025 14:30:47 +0400 Subject: [PATCH] Allow replacing default shortcuts. --- Telegram/SourceFiles/core/shortcuts.cpp | 49 +++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/Telegram/SourceFiles/core/shortcuts.cpp b/Telegram/SourceFiles/core/shortcuts.cpp index fe7809b84..371ef6a6d 100644 --- a/Telegram/SourceFiles/core/shortcuts.cpp +++ b/Telegram/SourceFiles/core/shortcuts.cpp @@ -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 shortcut); void pruneListened(); @@ -293,7 +294,7 @@ void Manager::change( Command command, std::optional 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 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 shortcut) { if (shortcut) { _commandByObject.removeAll(shortcut.get());