2018-11-16 12:15:14 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "core/shortcuts.h"
|
|
|
|
|
|
|
|
#include "mainwindow.h"
|
|
|
|
#include "mainwidget.h"
|
2019-06-06 11:20:21 +00:00
|
|
|
#include "window/window_controller.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/application.h"
|
2018-11-16 12:15:14 +00:00
|
|
|
#include "media/player/media_player_instance.h"
|
2019-09-17 16:13:12 +00:00
|
|
|
#include "base/platform/base_platform_info.h"
|
2021-01-10 03:52:18 +00:00
|
|
|
#include "platform/platform_specific.h"
|
2018-11-16 12:15:14 +00:00
|
|
|
#include "base/parse_helper.h"
|
|
|
|
|
2023-01-18 07:58:57 +00:00
|
|
|
#include <QAction>
|
2021-10-19 13:00:21 +00:00
|
|
|
#include <QShortcut>
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtCore/QJsonDocument>
|
|
|
|
#include <QtCore/QJsonObject>
|
|
|
|
#include <QtCore/QJsonArray>
|
|
|
|
|
2018-11-16 12:15:14 +00:00
|
|
|
namespace Shortcuts {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kCountLimit = 256; // How many shortcuts can be in json file.
|
|
|
|
|
|
|
|
rpl::event_stream<not_null<Request*>> RequestsStream;
|
|
|
|
|
|
|
|
const auto AutoRepeatCommands = base::flat_set<Command>{
|
|
|
|
Command::MediaPrevious,
|
|
|
|
Command::MediaNext,
|
|
|
|
Command::ChatPrevious,
|
|
|
|
Command::ChatNext,
|
2018-12-29 13:09:45 +00:00
|
|
|
Command::ChatFirst,
|
|
|
|
Command::ChatLast,
|
2018-11-16 12:15:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto MediaCommands = base::flat_set<Command>{
|
|
|
|
Command::MediaPlay,
|
|
|
|
Command::MediaPause,
|
|
|
|
Command::MediaPlayPause,
|
|
|
|
Command::MediaStop,
|
|
|
|
Command::MediaPrevious,
|
|
|
|
Command::MediaNext,
|
|
|
|
};
|
|
|
|
|
2018-12-13 08:48:31 +00:00
|
|
|
const auto SupportCommands = base::flat_set<Command>{
|
|
|
|
Command::SupportReloadTemplates,
|
|
|
|
Command::SupportToggleMuted,
|
|
|
|
Command::SupportScrollToCurrent,
|
2018-12-30 12:50:23 +00:00
|
|
|
Command::SupportHistoryBack,
|
|
|
|
Command::SupportHistoryForward,
|
2018-12-13 08:48:31 +00:00
|
|
|
};
|
|
|
|
|
2018-11-16 12:15:14 +00:00
|
|
|
const auto CommandByName = base::flat_map<QString, Command>{
|
2022-11-29 21:46:36 +00:00
|
|
|
{ u"close_telegram"_q , Command::Close },
|
|
|
|
{ u"lock_telegram"_q , Command::Lock },
|
|
|
|
{ u"minimize_telegram"_q , Command::Minimize },
|
|
|
|
{ u"quit_telegram"_q , Command::Quit },
|
|
|
|
|
|
|
|
{ u"media_play"_q , Command::MediaPlay },
|
|
|
|
{ u"media_pause"_q , Command::MediaPause },
|
|
|
|
{ u"media_playpause"_q , Command::MediaPlayPause },
|
|
|
|
{ u"media_stop"_q , Command::MediaStop },
|
|
|
|
{ u"media_previous"_q , Command::MediaPrevious },
|
|
|
|
{ u"media_next"_q , Command::MediaNext },
|
|
|
|
|
|
|
|
{ u"search"_q , Command::Search },
|
|
|
|
|
|
|
|
{ u"previous_chat"_q , Command::ChatPrevious },
|
|
|
|
{ u"next_chat"_q , Command::ChatNext },
|
|
|
|
{ u"first_chat"_q , Command::ChatFirst },
|
|
|
|
{ u"last_chat"_q , Command::ChatLast },
|
|
|
|
{ u"self_chat"_q , Command::ChatSelf },
|
|
|
|
|
|
|
|
{ u"previous_folder"_q , Command::FolderPrevious },
|
|
|
|
{ u"next_folder"_q , Command::FolderNext },
|
|
|
|
{ u"all_chats"_q , Command::ShowAllChats },
|
|
|
|
|
|
|
|
{ u"folder1"_q , Command::ShowFolder1 },
|
|
|
|
{ u"folder2"_q , Command::ShowFolder2 },
|
|
|
|
{ u"folder3"_q , Command::ShowFolder3 },
|
|
|
|
{ u"folder4"_q , Command::ShowFolder4 },
|
|
|
|
{ u"folder5"_q , Command::ShowFolder5 },
|
|
|
|
{ u"folder6"_q , Command::ShowFolder6 },
|
|
|
|
{ u"last_folder"_q , Command::ShowFolderLast },
|
|
|
|
|
|
|
|
{ u"show_archive"_q , Command::ShowArchive },
|
|
|
|
{ u"show_contacts"_q , Command::ShowContacts },
|
|
|
|
|
|
|
|
{ u"read_chat"_q , Command::ReadChat },
|
2020-09-16 12:14:35 +00:00
|
|
|
|
2019-12-23 15:14:12 +00:00
|
|
|
// Shortcuts that have no default values.
|
2022-11-29 21:46:36 +00:00
|
|
|
{ u"message"_q , Command::JustSendMessage },
|
|
|
|
{ u"message_silently"_q , Command::SendSilentMessage },
|
|
|
|
{ u"message_scheduled"_q , Command::ScheduleMessage },
|
2019-12-23 15:14:12 +00:00
|
|
|
//
|
2018-11-16 12:15:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto CommandNames = base::flat_map<Command, QString>{
|
2022-11-29 21:46:36 +00:00
|
|
|
{ Command::Close , u"close_telegram"_q },
|
|
|
|
{ Command::Lock , u"lock_telegram"_q },
|
|
|
|
{ Command::Minimize , u"minimize_telegram"_q },
|
|
|
|
{ Command::Quit , u"quit_telegram"_q },
|
|
|
|
|
|
|
|
{ Command::MediaPlay , u"media_play"_q },
|
|
|
|
{ Command::MediaPause , u"media_pause"_q },
|
|
|
|
{ Command::MediaPlayPause , u"media_playpause"_q },
|
|
|
|
{ Command::MediaStop , u"media_stop"_q },
|
|
|
|
{ Command::MediaPrevious , u"media_previous"_q },
|
|
|
|
{ Command::MediaNext , u"media_next"_q },
|
|
|
|
|
|
|
|
{ Command::Search , u"search"_q },
|
|
|
|
|
|
|
|
{ Command::ChatPrevious , u"previous_chat"_q },
|
|
|
|
{ Command::ChatNext , u"next_chat"_q },
|
|
|
|
{ Command::ChatFirst , u"first_chat"_q },
|
|
|
|
{ Command::ChatLast , u"last_chat"_q },
|
|
|
|
{ Command::ChatSelf , u"self_chat"_q },
|
|
|
|
|
|
|
|
{ Command::FolderPrevious , u"previous_folder"_q },
|
|
|
|
{ Command::FolderNext , u"next_folder"_q },
|
|
|
|
{ Command::ShowAllChats , u"all_chats"_q },
|
|
|
|
|
|
|
|
{ Command::ShowFolder1 , u"folder1"_q },
|
|
|
|
{ Command::ShowFolder2 , u"folder2"_q },
|
|
|
|
{ Command::ShowFolder3 , u"folder3"_q },
|
|
|
|
{ Command::ShowFolder4 , u"folder4"_q },
|
|
|
|
{ Command::ShowFolder5 , u"folder5"_q },
|
|
|
|
{ Command::ShowFolder6 , u"folder6"_q },
|
|
|
|
{ Command::ShowFolderLast , u"last_folder"_q },
|
|
|
|
|
|
|
|
{ Command::ShowArchive , u"show_archive"_q },
|
|
|
|
{ Command::ShowContacts , u"show_contacts"_q },
|
|
|
|
|
|
|
|
{ Command::ReadChat , u"read_chat"_q },
|
2018-11-16 12:15:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Manager {
|
|
|
|
public:
|
|
|
|
void fill();
|
|
|
|
void clear();
|
|
|
|
|
2023-01-18 07:58:57 +00:00
|
|
|
[[nodiscard]] std::vector<Command> lookup(
|
|
|
|
not_null<QObject*> object) const;
|
2018-11-16 12:15:14 +00:00
|
|
|
void toggleMedia(bool toggled);
|
2018-12-13 08:48:31 +00:00
|
|
|
void toggleSupport(bool toggled);
|
2023-01-18 07:58:57 +00:00
|
|
|
void listen(not_null<QWidget*> widget);
|
2018-11-16 12:15:14 +00:00
|
|
|
|
2023-01-18 07:58:57 +00:00
|
|
|
[[nodiscard]] const QStringList &errors() const;
|
2018-11-16 12:15:14 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void fillDefaults();
|
|
|
|
void writeDefaultFile();
|
|
|
|
bool readCustomFile();
|
|
|
|
|
2020-03-30 19:34:07 +00:00
|
|
|
void set(const QString &keys, Command command, bool replace = false);
|
2018-11-16 12:15:14 +00:00
|
|
|
void remove(const QString &keys);
|
2023-01-18 07:58:57 +00:00
|
|
|
void unregister(base::unique_qptr<QAction> shortcut);
|
2018-11-16 12:15:14 +00:00
|
|
|
|
|
|
|
QStringList _errors;
|
|
|
|
|
2023-01-18 07:58:57 +00:00
|
|
|
base::flat_map<QKeySequence, base::unique_qptr<QAction>> _shortcuts;
|
|
|
|
base::flat_multi_map<not_null<QObject*>, Command> _commandByObject;
|
2018-11-16 12:15:14 +00:00
|
|
|
|
2023-01-18 07:58:57 +00:00
|
|
|
base::flat_set<QAction*> _mediaShortcuts;
|
|
|
|
base::flat_set<QAction*> _supportShortcuts;
|
2018-11-16 12:15:14 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
QString DefaultFilePath() {
|
2022-11-29 21:46:36 +00:00
|
|
|
return cWorkingDir() + u"tdata/shortcuts-default.json"_q;
|
2018-11-16 12:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString CustomFilePath() {
|
2022-11-29 21:46:36 +00:00
|
|
|
return cWorkingDir() + u"tdata/shortcuts-custom.json"_q;
|
2018-11-16 12:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DefaultFileIsValid() {
|
|
|
|
QFile file(DefaultFilePath());
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto error = QJsonParseError{ 0, QJsonParseError::NoError };
|
|
|
|
const auto document = QJsonDocument::fromJson(
|
|
|
|
base::parse::stripComments(file.readAll()),
|
|
|
|
&error);
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
if (error.error != QJsonParseError::NoError || !document.isArray()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto shortcuts = document.array();
|
|
|
|
if (shortcuts.isEmpty() || !(*shortcuts.constBegin()).isObject()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto versionObject = (*shortcuts.constBegin()).toObject();
|
2022-11-29 21:46:36 +00:00
|
|
|
const auto version = versionObject.constFind(u"version"_q);
|
2018-11-16 12:15:14 +00:00
|
|
|
if (version == versionObject.constEnd()
|
|
|
|
|| !(*version).isString()
|
|
|
|
|| (*version).toString() != QString::number(AppVersion)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteDefaultCustomFile() {
|
|
|
|
const auto path = CustomFilePath();
|
|
|
|
auto input = QFile(":/misc/default_shortcuts-custom.json");
|
|
|
|
auto output = QFile(path);
|
|
|
|
if (input.open(QIODevice::ReadOnly) && output.open(QIODevice::WriteOnly)) {
|
|
|
|
output.write(input.readAll());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::fill() {
|
|
|
|
fillDefaults();
|
|
|
|
|
|
|
|
if (!DefaultFileIsValid()) {
|
|
|
|
writeDefaultFile();
|
|
|
|
}
|
|
|
|
if (!readCustomFile()) {
|
|
|
|
WriteDefaultCustomFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::clear() {
|
|
|
|
_errors.clear();
|
|
|
|
_shortcuts.clear();
|
2023-01-18 07:58:57 +00:00
|
|
|
_commandByObject.clear();
|
2018-11-16 12:15:14 +00:00
|
|
|
_mediaShortcuts.clear();
|
2018-12-13 08:48:31 +00:00
|
|
|
_supportShortcuts.clear();
|
2018-11-16 12:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const QStringList &Manager::errors() const {
|
|
|
|
return _errors;
|
|
|
|
}
|
|
|
|
|
2023-01-18 07:58:57 +00:00
|
|
|
std::vector<Command> Manager::lookup(not_null<QObject*> object) const {
|
2020-03-30 19:34:07 +00:00
|
|
|
auto result = std::vector<Command>();
|
2023-01-18 07:58:57 +00:00
|
|
|
auto i = _commandByObject.findFirst(object);
|
|
|
|
const auto end = _commandByObject.end();
|
|
|
|
for (; i != end && (i->first == object); ++i) {
|
2020-03-30 19:34:07 +00:00
|
|
|
result.push_back(i->second);
|
|
|
|
}
|
|
|
|
return result;
|
2018-11-16 12:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::toggleMedia(bool toggled) {
|
|
|
|
for (const auto shortcut : _mediaShortcuts) {
|
|
|
|
shortcut->setEnabled(toggled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 08:48:31 +00:00
|
|
|
void Manager::toggleSupport(bool toggled) {
|
|
|
|
for (const auto shortcut : _supportShortcuts) {
|
|
|
|
shortcut->setEnabled(toggled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 07:58:57 +00:00
|
|
|
void Manager::listen(not_null<QWidget*> widget) {
|
|
|
|
for (const auto &[keys, shortcut] : _shortcuts) {
|
|
|
|
widget->addAction(shortcut.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:15:14 +00:00
|
|
|
bool Manager::readCustomFile() {
|
|
|
|
// read custom shortcuts from file if it exists or write an empty custom shortcuts file
|
|
|
|
QFile file(CustomFilePath());
|
|
|
|
if (!file.exists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto guard = gsl::finally([&] {
|
|
|
|
if (!_errors.isEmpty()) {
|
2022-11-29 21:46:36 +00:00
|
|
|
_errors.push_front((u"While reading file '%1'..."_q
|
2018-11-16 12:15:14 +00:00
|
|
|
).arg(file.fileName()));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
2022-11-29 21:46:36 +00:00
|
|
|
_errors.push_back(u"Could not read the file!"_q);
|
2018-11-16 12:15:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
auto error = QJsonParseError{ 0, QJsonParseError::NoError };
|
|
|
|
const auto document = QJsonDocument::fromJson(
|
|
|
|
base::parse::stripComments(file.readAll()),
|
|
|
|
&error);
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
if (error.error != QJsonParseError::NoError) {
|
2022-11-29 21:46:36 +00:00
|
|
|
_errors.push_back((u"Failed to parse! Error: %2"_q
|
2018-11-16 12:15:14 +00:00
|
|
|
).arg(error.errorString()));
|
|
|
|
return true;
|
|
|
|
} else if (!document.isArray()) {
|
2022-11-29 21:46:36 +00:00
|
|
|
_errors.push_back(u"Failed to parse! Error: array expected"_q);
|
2018-11-16 12:15:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const auto shortcuts = document.array();
|
|
|
|
auto limit = kCountLimit;
|
|
|
|
for (auto i = shortcuts.constBegin(), e = shortcuts.constEnd(); i != e; ++i) {
|
|
|
|
if (!(*i).isObject()) {
|
2022-11-29 21:46:36 +00:00
|
|
|
_errors.push_back(u"Bad entry! Error: object expected"_q);
|
2018-11-16 12:15:14 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const auto entry = (*i).toObject();
|
2022-11-29 21:46:36 +00:00
|
|
|
const auto keys = entry.constFind(u"keys"_q);
|
|
|
|
const auto command = entry.constFind(u"command"_q);
|
2018-11-16 12:15:14 +00:00
|
|
|
if (keys == entry.constEnd()
|
|
|
|
|| command == entry.constEnd()
|
|
|
|
|| !(*keys).isString()
|
|
|
|
|| (!(*command).isString() && !(*command).isNull())) {
|
|
|
|
_errors.push_back(qsl("Bad entry! "
|
|
|
|
"{\"keys\": \"...\", \"command\": [ \"...\" | null ]} "
|
|
|
|
"expected."));
|
|
|
|
} else if ((*command).isNull()) {
|
|
|
|
remove((*keys).toString());
|
|
|
|
} else {
|
|
|
|
const auto name = (*command).toString();
|
|
|
|
const auto i = CommandByName.find(name);
|
|
|
|
if (i != end(CommandByName)) {
|
2020-03-30 19:34:07 +00:00
|
|
|
set((*keys).toString(), i->second, true);
|
2018-11-16 12:15:14 +00:00
|
|
|
} else {
|
|
|
|
LOG(("Shortcut Warning: "
|
|
|
|
"could not find shortcut command handler '%1'"
|
|
|
|
).arg(name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!--limit) {
|
2022-11-29 21:46:36 +00:00
|
|
|
_errors.push_back(u"Too many entries! Limit is %1"_q.arg(
|
|
|
|
kCountLimit));
|
2018-11-16 12:15:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::fillDefaults() {
|
2022-11-29 21:46:36 +00:00
|
|
|
const auto ctrl = Platform::IsMac() ? u"meta"_q : u"ctrl"_q;
|
|
|
|
|
|
|
|
set(u"ctrl+w"_q, Command::Close);
|
|
|
|
set(u"ctrl+f4"_q, Command::Close);
|
|
|
|
set(u"ctrl+l"_q, Command::Lock);
|
|
|
|
set(u"ctrl+m"_q, Command::Minimize);
|
|
|
|
set(u"ctrl+q"_q, Command::Quit);
|
|
|
|
|
|
|
|
set(u"media play"_q, Command::MediaPlay);
|
|
|
|
set(u"media pause"_q, Command::MediaPause);
|
|
|
|
set(u"toggle media play/pause"_q, Command::MediaPlayPause);
|
|
|
|
set(u"media stop"_q, Command::MediaStop);
|
|
|
|
set(u"media previous"_q, Command::MediaPrevious);
|
|
|
|
set(u"media next"_q, Command::MediaNext);
|
|
|
|
|
|
|
|
set(u"ctrl+f"_q, Command::Search);
|
|
|
|
set(u"search"_q, Command::Search);
|
|
|
|
set(u"find"_q, Command::Search);
|
|
|
|
|
|
|
|
set(u"ctrl+pgdown"_q, Command::ChatNext);
|
|
|
|
set(u"alt+down"_q, Command::ChatNext);
|
|
|
|
set(u"ctrl+pgup"_q, Command::ChatPrevious);
|
|
|
|
set(u"alt+up"_q, Command::ChatPrevious);
|
|
|
|
|
|
|
|
set(u"%1+tab"_q.arg(ctrl), Command::ChatNext);
|
|
|
|
set(u"%1+shift+tab"_q.arg(ctrl), Command::ChatPrevious);
|
|
|
|
set(u"%1+backtab"_q.arg(ctrl), Command::ChatPrevious);
|
|
|
|
|
|
|
|
set(u"ctrl+alt+home"_q, Command::ChatFirst);
|
|
|
|
set(u"ctrl+alt+end"_q, Command::ChatLast);
|
|
|
|
|
|
|
|
set(u"f5"_q, Command::SupportReloadTemplates);
|
|
|
|
set(u"ctrl+delete"_q, Command::SupportToggleMuted);
|
|
|
|
set(u"ctrl+insert"_q, Command::SupportScrollToCurrent);
|
|
|
|
set(u"ctrl+shift+x"_q, Command::SupportHistoryBack);
|
|
|
|
set(u"ctrl+shift+c"_q, Command::SupportHistoryForward);
|
|
|
|
|
|
|
|
set(u"ctrl+1"_q, Command::ChatPinned1);
|
|
|
|
set(u"ctrl+2"_q, Command::ChatPinned2);
|
|
|
|
set(u"ctrl+3"_q, Command::ChatPinned3);
|
|
|
|
set(u"ctrl+4"_q, Command::ChatPinned4);
|
|
|
|
set(u"ctrl+5"_q, Command::ChatPinned5);
|
2023-01-03 05:59:42 +00:00
|
|
|
set(u"ctrl+6"_q, Command::ChatPinned6);
|
|
|
|
set(u"ctrl+7"_q, Command::ChatPinned7);
|
|
|
|
set(u"ctrl+8"_q, Command::ChatPinned8);
|
2019-03-11 19:01:41 +00:00
|
|
|
|
2021-03-13 12:12:08 +00:00
|
|
|
auto &&folders = ranges::views::zip(
|
2020-03-27 17:51:09 +00:00
|
|
|
kShowFolder,
|
2021-03-13 12:12:08 +00:00
|
|
|
ranges::views::ints(1, ranges::unreachable));
|
2020-03-27 17:51:09 +00:00
|
|
|
|
|
|
|
for (const auto [command, index] : folders) {
|
2022-11-29 21:46:36 +00:00
|
|
|
set(u"%1+%2"_q.arg(ctrl).arg(index), command);
|
2020-03-27 17:51:09 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 21:46:36 +00:00
|
|
|
set(u"%1+shift+down"_q.arg(ctrl), Command::FolderNext);
|
|
|
|
set(u"%1+shift+up"_q.arg(ctrl), Command::FolderPrevious);
|
2020-03-28 23:04:20 +00:00
|
|
|
|
2022-11-29 21:46:36 +00:00
|
|
|
set(u"ctrl+0"_q, Command::ChatSelf);
|
2020-01-05 05:14:52 +00:00
|
|
|
|
2022-11-29 21:46:36 +00:00
|
|
|
set(u"ctrl+9"_q, Command::ShowArchive);
|
|
|
|
set(u"ctrl+j"_q, Command::ShowContacts);
|
2020-09-16 12:14:35 +00:00
|
|
|
|
2022-11-29 21:46:36 +00:00
|
|
|
set(u"ctrl+r"_q, Command::ReadChat);
|
2018-11-16 12:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::writeDefaultFile() {
|
|
|
|
auto file = QFile(DefaultFilePath());
|
|
|
|
if (!file.open(QIODevice::WriteOnly)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const char *defaultHeader = R"HEADER(
|
|
|
|
// This is a list of default shortcuts for Telegram Desktop
|
|
|
|
// Please don't modify it, its content is not used in any way
|
|
|
|
// You can place your own shortcuts in the 'shortcuts-custom.json' file
|
|
|
|
|
|
|
|
)HEADER";
|
|
|
|
file.write(defaultHeader);
|
|
|
|
|
|
|
|
auto shortcuts = QJsonArray();
|
|
|
|
auto version = QJsonObject();
|
2022-11-29 21:46:36 +00:00
|
|
|
version.insert(u"version"_q, QString::number(AppVersion));
|
2018-11-16 12:15:14 +00:00
|
|
|
shortcuts.push_back(version);
|
|
|
|
|
|
|
|
for (const auto &[sequence, shortcut] : _shortcuts) {
|
2023-01-18 07:58:57 +00:00
|
|
|
const auto object = shortcut.get();
|
|
|
|
auto i = _commandByObject.findFirst(object);
|
|
|
|
const auto end = _commandByObject.end();
|
|
|
|
for (; i != end && i->first == object; ++i) {
|
2018-11-16 12:15:14 +00:00
|
|
|
const auto j = CommandNames.find(i->second);
|
2020-03-30 19:34:07 +00:00
|
|
|
if (j != CommandNames.end()) {
|
2018-11-16 12:15:14 +00:00
|
|
|
QJsonObject entry;
|
2022-11-29 21:46:36 +00:00
|
|
|
entry.insert(u"keys"_q, sequence.toString().toLower());
|
|
|
|
entry.insert(u"command"_q, j->second);
|
2018-11-16 12:15:14 +00:00
|
|
|
shortcuts.append(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto document = QJsonDocument();
|
|
|
|
document.setArray(shortcuts);
|
|
|
|
file.write(document.toJson(QJsonDocument::Indented));
|
|
|
|
}
|
|
|
|
|
2020-03-30 19:34:07 +00:00
|
|
|
void Manager::set(const QString &keys, Command command, bool replace) {
|
2018-11-16 12:15:14 +00:00
|
|
|
if (keys.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto result = QKeySequence(keys, QKeySequence::PortableText);
|
|
|
|
if (result.isEmpty()) {
|
2022-11-29 21:46:36 +00:00
|
|
|
_errors.push_back(u"Could not derive key sequence '%1'!"_q.arg(keys));
|
2018-11-16 12:15:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-01-18 07:58:57 +00:00
|
|
|
auto shortcut = base::make_unique_q<QAction>();
|
|
|
|
shortcut->setShortcut(result);
|
|
|
|
shortcut->setShortcutContext(Qt::ApplicationShortcut);
|
2018-12-29 13:09:45 +00:00
|
|
|
if (!AutoRepeatCommands.contains(command)) {
|
2018-11-16 12:15:14 +00:00
|
|
|
shortcut->setAutoRepeat(false);
|
|
|
|
}
|
|
|
|
const auto isMediaShortcut = MediaCommands.contains(command);
|
2018-12-13 08:48:31 +00:00
|
|
|
const auto isSupportShortcut = SupportCommands.contains(command);
|
|
|
|
if (isMediaShortcut || isSupportShortcut) {
|
2018-11-16 12:15:14 +00:00
|
|
|
shortcut->setEnabled(false);
|
|
|
|
}
|
2023-01-18 07:58:57 +00:00
|
|
|
auto object = shortcut.get();
|
2018-11-16 12:15:14 +00:00
|
|
|
auto i = _shortcuts.find(result);
|
|
|
|
if (i == end(_shortcuts)) {
|
|
|
|
i = _shortcuts.emplace(result, std::move(shortcut)).first;
|
2020-03-30 19:34:07 +00:00
|
|
|
} else if (replace) {
|
2018-11-16 12:15:14 +00:00
|
|
|
unregister(std::exchange(i->second, std::move(shortcut)));
|
2020-03-30 19:34:07 +00:00
|
|
|
} else {
|
2023-01-18 07:58:57 +00:00
|
|
|
object = i->second.get();
|
2020-03-30 19:34:07 +00:00
|
|
|
}
|
2023-01-18 07:58:57 +00:00
|
|
|
_commandByObject.emplace(object, command);
|
2020-04-02 12:20:53 +00:00
|
|
|
if (!shortcut && isMediaShortcut) {
|
2018-11-16 12:15:14 +00:00
|
|
|
_mediaShortcuts.emplace(i->second.get());
|
|
|
|
}
|
2020-04-02 12:20:53 +00:00
|
|
|
if (!shortcut && isSupportShortcut) {
|
2018-12-13 08:48:31 +00:00
|
|
|
_supportShortcuts.emplace(i->second.get());
|
|
|
|
}
|
2018-11-16 12:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::remove(const QString &keys) {
|
|
|
|
if (keys.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto result = QKeySequence(keys, QKeySequence::PortableText);
|
|
|
|
if (result.isEmpty()) {
|
2022-11-29 21:46:36 +00:00
|
|
|
_errors.push_back(u"Could not derive key sequence '%1'!"_q.arg(keys));
|
2018-11-16 12:15:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto i = _shortcuts.find(result);
|
|
|
|
if (i != end(_shortcuts)) {
|
|
|
|
unregister(std::move(i->second));
|
|
|
|
_shortcuts.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 07:58:57 +00:00
|
|
|
void Manager::unregister(base::unique_qptr<QAction> shortcut) {
|
2018-11-16 12:15:14 +00:00
|
|
|
if (shortcut) {
|
2023-01-18 07:58:57 +00:00
|
|
|
_commandByObject.erase(shortcut.get());
|
2018-11-16 12:15:14 +00:00
|
|
|
_mediaShortcuts.erase(shortcut.get());
|
2018-12-13 08:48:31 +00:00
|
|
|
_supportShortcuts.erase(shortcut.get());
|
2018-11-16 12:15:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Manager Data;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-03-30 19:34:07 +00:00
|
|
|
Request::Request(std::vector<Command> commands)
|
|
|
|
: _commands(std::move(commands)) {
|
2018-11-16 12:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Request::check(Command command, int priority) {
|
2020-03-30 19:34:07 +00:00
|
|
|
if (ranges::contains(_commands, command)
|
|
|
|
&& priority > _handlerPriority) {
|
2018-11-16 12:15:14 +00:00
|
|
|
_handlerPriority = priority;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Request::handle(FnMut<bool()> handler) {
|
|
|
|
_handler = std::move(handler);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-30 19:34:07 +00:00
|
|
|
FnMut<bool()> RequestHandler(std::vector<Command> commands) {
|
|
|
|
auto request = Request(std::move(commands));
|
2018-11-16 12:15:14 +00:00
|
|
|
RequestsStream.fire(&request);
|
2018-11-16 15:12:39 +00:00
|
|
|
return std::move(request._handler);
|
|
|
|
}
|
|
|
|
|
2020-03-30 19:34:07 +00:00
|
|
|
FnMut<bool()> RequestHandler(Command command) {
|
|
|
|
return RequestHandler(std::vector<Command>{ command });
|
|
|
|
}
|
|
|
|
|
2018-11-16 15:12:39 +00:00
|
|
|
bool Launch(Command command) {
|
|
|
|
if (auto handler = RequestHandler(command)) {
|
|
|
|
return handler();
|
|
|
|
}
|
|
|
|
return false;
|
2018-11-16 12:15:14 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 19:34:07 +00:00
|
|
|
bool Launch(std::vector<Command> commands) {
|
|
|
|
if (auto handler = RequestHandler(std::move(commands))) {
|
|
|
|
return handler();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:15:14 +00:00
|
|
|
rpl::producer<not_null<Request*>> Requests() {
|
|
|
|
return RequestsStream.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Start() {
|
|
|
|
Data.fill();
|
|
|
|
}
|
|
|
|
|
|
|
|
const QStringList &Errors() {
|
|
|
|
return Data.errors();
|
|
|
|
}
|
|
|
|
|
2023-01-18 07:58:57 +00:00
|
|
|
bool HandleEvent(
|
|
|
|
not_null<QObject*> object,
|
|
|
|
not_null<QShortcutEvent*> event) {
|
|
|
|
return Launch(Data.lookup(object));
|
2018-11-16 12:15:14 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 08:48:31 +00:00
|
|
|
void ToggleMediaShortcuts(bool toggled) {
|
|
|
|
Data.toggleMedia(toggled);
|
2018-11-16 12:15:14 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 08:48:31 +00:00
|
|
|
void ToggleSupportShortcuts(bool toggled) {
|
|
|
|
Data.toggleSupport(toggled);
|
2018-11-16 12:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Finish() {
|
|
|
|
Data.clear();
|
|
|
|
}
|
|
|
|
|
2023-01-18 07:58:57 +00:00
|
|
|
void Listen(not_null<QWidget*> widget) {
|
|
|
|
Data.listen(widget);
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:15:14 +00:00
|
|
|
} // namespace Shortcuts
|