Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keyboard shortcuts to change accounts #279

Merged
merged 1 commit into from
Dec 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions Telegram/SourceFiles/core/shortcuts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ const auto CommandByName = base::flat_map<QString, Command>{
{ qsl("pinned_4") , Command::ChatPinned4 },
{ qsl("pinned_5") , Command::ChatPinned5 },

{ qsl("account1") , Command::ShowAccount1 },
{ qsl("account2") , Command::ShowAccount2 },
{ qsl("account3") , Command::ShowAccount3 },
{ qsl("account4") , Command::ShowAccount4 },
{ qsl("account5") , Command::ShowAccount5 },
{ qsl("account6") , Command::ShowAccount6 },
{ qsl("account7") , Command::ShowAccount7 },
{ qsl("account8") , Command::ShowAccount8 },
{ qsl("account9") , Command::ShowAccount9 },
{ qsl("last_account") , Command::ShowAccountLast },

// Kotatogram: legacy keys
{ qsl("folder_all") , Command::ShowAllChats },
{ qsl("folder_1") , Command::ShowFolder1 },
Expand Down Expand Up @@ -171,6 +182,17 @@ const auto CommandNames = base::flat_map<Command, QString>{
{ Command::ChatPinned3 , qsl("pinned_3") },
{ Command::ChatPinned4 , qsl("pinned_4") },
{ Command::ChatPinned5 , qsl("pinned_5") },

{ Command::ShowAccount1 , qsl("account1") },
{ Command::ShowAccount2 , qsl("account2") },
{ Command::ShowAccount3 , qsl("account3") },
{ Command::ShowAccount4 , qsl("account4") },
{ Command::ShowAccount5 , qsl("account5") },
{ Command::ShowAccount6 , qsl("account6") },
{ Command::ShowAccount7 , qsl("account7") },
{ Command::ShowAccount8 , qsl("account8") },
{ Command::ShowAccount9 , qsl("account9") },
{ Command::ShowAccountLast, qsl("last_account") },
};

class Manager {
Expand Down Expand Up @@ -414,6 +436,16 @@ void Manager::fillDefaults() {
set(qsl("%1+%2").arg(ctrl).arg(index), command);
}

auto &&accounts = ranges::views::zip(
kShowAccount,
ranges::views::ints(1, ranges::unreachable));

for (const auto [command, index] : accounts) {
set(qsl("alt+%1").arg(index), command);
}

set(qsl("alt+0"), Command::ShowAccountLast);

set(qsl("%1+shift+down").arg(ctrl), Command::FolderNext);
set(qsl("%1+shift+up").arg(ctrl), Command::FolderPrevious);

Expand Down
24 changes: 24 additions & 0 deletions Telegram/SourceFiles/core/shortcuts.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ enum class Command {
JumpToDate,
ReloadLang,
Restart,

ShowAccount1,
ShowAccount2,
ShowAccount3,
ShowAccount4,
ShowAccount5,
ShowAccount6,
ShowAccount7,
ShowAccount8,
ShowAccount9,
ShowAccountLast,
};

[[maybe_unused]] constexpr auto kShowFolder = {
Expand All @@ -79,6 +90,19 @@ enum class Command {
Command::ShowFolderLast,
};

[[maybe_unused]] constexpr auto kShowAccount = {
Command::ShowAccount1,
Command::ShowAccount2,
Command::ShowAccount3,
Command::ShowAccount4,
Command::ShowAccount5,
Command::ShowAccount6,
Command::ShowAccount7,
Command::ShowAccount8,
Command::ShowAccount9,
Command::ShowAccountLast,
};

[[nodiscard]] FnMut<bool()> RequestHandler(Command command);

class Request {
Expand Down
24 changes: 24 additions & 0 deletions Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ For license and copyright information please follow this link:
#include "apiwrap.h"
#include "main/main_session.h"
#include "main/main_session_settings.h"
#include "main/main_account.h"
#include "main/main_domain.h"
#include "window/notifications_manager.h"
#include "window/window_controller.h"
#include "window/window_session_controller.h"
Expand Down Expand Up @@ -3126,6 +3128,28 @@ void InnerWidget::setupShortcuts() {
}
}

const auto accounts = &Core::App().domain().accounts();
if (const auto accountsCount = int(accounts->size())) {
auto &&accountShortcuts = ranges::views::zip(
Shortcuts::kShowAccount,
ranges::views::ints(0, ranges::unreachable));

for (const auto [command, index] : accountShortcuts) {
const auto select = (command == Command::ShowAccountLast)
? accountsCount - 1
: std::clamp(index, 0, accountsCount - 1);
request->check(command) && request->handle([=] {
if (select <= accountsCount) {
const auto account = (*accounts)[select].account.get();
if (account != &Core::App().domain().active()) {
Core::App().domain().maybeActivate(account);
}
}
return true;
});
}
}

static const auto kPinned = {
Command::ChatPinned1,
Command::ChatPinned2,
Expand Down