From 26170e4fdb692bd5b98bed3da8d4070f8ec7e4ee Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sat, 6 Jan 2018 20:58:56 +0100 Subject: [PATCH] Add CTRL+SHIFT+T and CTRL+SHIFT+W to Open and Close tabs Fixes #21 --- src/widgets/helper/shortcut.hpp | 8 ++++++++ src/widgets/notebook.cpp | 9 +++++++++ src/widgets/notebook.hpp | 1 + src/widgets/window.cpp | 26 ++++++++++++++++---------- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/widgets/helper/shortcut.hpp b/src/widgets/helper/shortcut.hpp index 609633ab977..a45ce2276a7 100644 --- a/src/widgets/helper/shortcut.hpp +++ b/src/widgets/helper/shortcut.hpp @@ -14,5 +14,13 @@ inline void CreateShortcut(WidgetType *w, const char *key, Func func) QObject::connect(s, &QShortcut::activated, w, func); } +template +inline void CreateWindowShortcut(WidgetType *w, const char *key, Func func) +{ + auto s = new QShortcut(QKeySequence(key), w); + s->setContext(Qt::WindowShortcut); + QObject::connect(s, &QShortcut::activated, w, func); +} + } // namespace widgets } // namespace chatterino diff --git a/src/widgets/notebook.cpp b/src/widgets/notebook.cpp index a3b9d74e766..531eb96f595 100644 --- a/src/widgets/notebook.cpp +++ b/src/widgets/notebook.cpp @@ -95,6 +95,15 @@ void Notebook::removePage(SplitContainer *page) this->performLayout(); } +void Notebook::removeCurrentPage() +{ + if (this->selectedPage == nullptr) { + return; + } + + this->removePage(this->selectedPage); +} + void Notebook::select(SplitContainer *page) { if (page == this->selectedPage) { diff --git a/src/widgets/notebook.hpp b/src/widgets/notebook.hpp index c44567cc79b..cbf7ec00931 100644 --- a/src/widgets/notebook.hpp +++ b/src/widgets/notebook.hpp @@ -28,6 +28,7 @@ class Notebook : public BaseWidget SplitContainer *addPage(const std::string &uuid, bool select = false); void removePage(SplitContainer *page); + void removeCurrentPage(); void select(SplitContainer *page); void selectIndex(unsigned index); diff --git a/src/widgets/window.cpp b/src/widgets/window.cpp index 4a7b250202a..d547e38f764 100644 --- a/src/widgets/window.cpp +++ b/src/widgets/window.cpp @@ -58,20 +58,26 @@ Window::Window(const QString &windowName, singletons::ThemeManager &_themeManage /// Initialize program-wide hotkeys // CTRL+P: Open Settings Dialog - CreateShortcut(this, "CTRL+P", [] { + CreateWindowShortcut(this, "CTRL+P", [] { SettingsDialog::showDialog(); // }); // CTRL+Number: Switch to n'th tab - CreateShortcut(this, "CTRL+1", [this] { this->notebook.selectIndex(0); }); - CreateShortcut(this, "CTRL+2", [this] { this->notebook.selectIndex(1); }); - CreateShortcut(this, "CTRL+3", [this] { this->notebook.selectIndex(2); }); - CreateShortcut(this, "CTRL+4", [this] { this->notebook.selectIndex(3); }); - CreateShortcut(this, "CTRL+5", [this] { this->notebook.selectIndex(4); }); - CreateShortcut(this, "CTRL+6", [this] { this->notebook.selectIndex(5); }); - CreateShortcut(this, "CTRL+7", [this] { this->notebook.selectIndex(6); }); - CreateShortcut(this, "CTRL+8", [this] { this->notebook.selectIndex(7); }); - CreateShortcut(this, "CTRL+9", [this] { this->notebook.selectIndex(8); }); + CreateWindowShortcut(this, "CTRL+1", [this] { this->notebook.selectIndex(0); }); + CreateWindowShortcut(this, "CTRL+2", [this] { this->notebook.selectIndex(1); }); + CreateWindowShortcut(this, "CTRL+3", [this] { this->notebook.selectIndex(2); }); + CreateWindowShortcut(this, "CTRL+4", [this] { this->notebook.selectIndex(3); }); + CreateWindowShortcut(this, "CTRL+5", [this] { this->notebook.selectIndex(4); }); + CreateWindowShortcut(this, "CTRL+6", [this] { this->notebook.selectIndex(5); }); + CreateWindowShortcut(this, "CTRL+7", [this] { this->notebook.selectIndex(6); }); + CreateWindowShortcut(this, "CTRL+8", [this] { this->notebook.selectIndex(7); }); + CreateWindowShortcut(this, "CTRL+9", [this] { this->notebook.selectIndex(8); }); + + // CTRL+SHIFT+T: New tab + CreateWindowShortcut(this, "CTRL+SHIFT+T", [this] { this->notebook.addNewPage(); }); + + // CTRL+SHIFT+W: Close current tab + CreateWindowShortcut(this, "CTRL+SHIFT+W", [this] { this->notebook.removeCurrentPage(); }); } void Window::repaintVisibleChatWidgets(Channel *channel)