From 1ca77a1e8417ce8c81ff36543ed7e99f42376b60 Mon Sep 17 00:00:00 2001 From: nerix Date: Fri, 12 Apr 2024 23:05:47 +0200 Subject: [PATCH] Add context menu entry to toggle offline tabs (#5318) Co-authored-by: pajlada --- CHANGELOG.md | 1 + src/widgets/Notebook.cpp | 82 ++++++++++++++++++++++++++++++++++++++-- src/widgets/Notebook.hpp | 10 ++++- src/widgets/Window.cpp | 17 +-------- 4 files changed, 88 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0edadcdb7..cafe3e3e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unversioned +- Minor: Added context menu action to toggle visibility of offline tabs. (#5318) - Minor: Report sub duration for more multi-month gift cases. (#5319) - Bugfix: Fixed split tooltip getting stuck in some cases. (#5309) - Bugfix: Fixed the version string not showing up as expected in Finder on macOS. (#5311) diff --git a/src/widgets/Notebook.cpp b/src/widgets/Notebook.cpp index c16181f80..7fdff5495 100644 --- a/src/widgets/Notebook.cpp +++ b/src/widgets/Notebook.cpp @@ -33,7 +33,6 @@ namespace chatterino { Notebook::Notebook(QWidget *parent) : BaseWidget(parent) - , menu_(this) , addButton_(new NotebookButton(this)) { this->addButton_->setIcon(NotebookButton::Icon::Plus); @@ -81,8 +80,6 @@ Notebook::Notebook(QWidget *parent) << "Notebook must be created within a BaseWindow"; } - this->addNotebookActionsToMenu(&this->menu_); - // Manually resize the add button so the initial paint uses the correct // width when computing the maximum width occupied per column in vertical // tab rendering. @@ -1125,7 +1122,14 @@ void Notebook::mousePressEvent(QMouseEvent *event) switch (event->button()) { case Qt::RightButton: { - this->menu_.popup(event->globalPos() + QPoint(0, 8)); + event->accept(); + + if (!this->menu_) + { + this->menu_ = new QMenu(this); + this->addNotebookActionsToMenu(this->menu_); + } + this->menu_->popup(event->globalPos() + QPoint(0, 8)); } break; default:; @@ -1294,6 +1298,10 @@ SplitNotebook::SplitNotebook(Window *parent) this->addCustomButtons(); } + this->toggleOfflineTabsAction_ = new QAction({}, this); + QObject::connect(this->toggleOfflineTabsAction_, &QAction::triggered, this, + &SplitNotebook::toggleOfflineTabs); + getSettings()->tabVisibility.connect( [this](int val, auto) { auto visibility = NotebookTabVisibility(val); @@ -1307,12 +1315,17 @@ SplitNotebook::SplitNotebook(Window *parent) this->setTabVisibilityFilter([](const NotebookTab *tab) { return tab->isLive(); }); + this->toggleOfflineTabsAction_->setText("Show all tabs"); break; case NotebookTabVisibility::AllTabs: default: this->setTabVisibilityFilter(nullptr); + this->toggleOfflineTabsAction_->setText( + "Show live tabs only"); break; } + + this->updateToggleOfflineTabsHotkey(visibility); }, this->signalHolder_, true); @@ -1365,6 +1378,31 @@ SplitNotebook::SplitNotebook(Window *parent) }); } +void SplitNotebook::toggleOfflineTabs() +{ + if (!this->getShowTabs()) + { + // Tabs are currently hidden, so the intention is to show + // tabs again before enabling the live only setting + this->setShowTabs(true); + getSettings()->tabVisibility.setValue(NotebookTabVisibility::LiveOnly); + } + else + { + getSettings()->tabVisibility.setValue( + getSettings()->tabVisibility.getEnum() == + NotebookTabVisibility::LiveOnly + ? NotebookTabVisibility::AllTabs + : NotebookTabVisibility::LiveOnly); + } +} + +void SplitNotebook::addNotebookActionsToMenu(QMenu *menu) +{ + Notebook::addNotebookActionsToMenu(menu); + menu->addAction(this->toggleOfflineTabsAction_); +} + void SplitNotebook::showEvent(QShowEvent * /*event*/) { if (auto *page = this->getSelectedPage()) @@ -1442,6 +1480,42 @@ void SplitNotebook::addCustomButtons() this->updateStreamerModeIcon(); } +void SplitNotebook::updateToggleOfflineTabsHotkey( + NotebookTabVisibility newTabVisibility) +{ + auto *hotkeys = getIApp()->getHotkeys(); + auto getKeySequence = [&](auto argument) { + return hotkeys->getDisplaySequence(HotkeyCategory::Window, + "setTabVisibility", {{argument}}); + }; + + auto toggleSeq = getKeySequence("toggleLiveOnly"); + + switch (newTabVisibility) + { + case NotebookTabVisibility::AllTabs: + if (toggleSeq.isEmpty()) + { + toggleSeq = getKeySequence("liveOnly"); + } + break; + + case NotebookTabVisibility::LiveOnly: + if (toggleSeq.isEmpty()) + { + toggleSeq = getKeySequence("toggle"); + + if (toggleSeq.isEmpty()) + { + toggleSeq = getKeySequence("on"); + } + } + break; + } + + this->toggleOfflineTabsAction_->setShortcut(toggleSeq); +} + void SplitNotebook::updateStreamerModeIcon() { if (this->streamerModeIcon_ == nullptr) diff --git a/src/widgets/Notebook.hpp b/src/widgets/Notebook.hpp index 9aa694c66..ac0162c42 100644 --- a/src/widgets/Notebook.hpp +++ b/src/widgets/Notebook.hpp @@ -118,7 +118,7 @@ public: bool isNotebookLayoutLocked() const; void setLockNotebookLayout(bool value); - void addNotebookActionsToMenu(QMenu *menu); + virtual void addNotebookActionsToMenu(QMenu *menu); // Update layout and tab visibility void refresh(); @@ -182,7 +182,7 @@ private: size_t visibleButtonCount() const; QList items_; - QMenu menu_; + QMenu *menu_ = nullptr; QWidget *selectedPage_ = nullptr; NotebookButton *addButton_; @@ -215,6 +215,9 @@ public: void select(QWidget *page, bool focusPage = true) override; void themeChangedEvent() override; + void addNotebookActionsToMenu(QMenu *menu) override; + void toggleOfflineTabs(); + protected: void showEvent(QShowEvent *event) override; @@ -223,6 +226,9 @@ private: pajlada::Signals::SignalHolder signalHolder_; + QAction *toggleOfflineTabsAction_; + void updateToggleOfflineTabsHotkey(NotebookTabVisibility newTabVisibility); + // Main window on Windows has basically a duplicate of this in Window NotebookButton *streamerModeIcon_{}; void updateStreamerModeIcon(); diff --git a/src/widgets/Window.cpp b/src/widgets/Window.cpp index 7106f872f..a9b0995dd 100644 --- a/src/widgets/Window.cpp +++ b/src/widgets/Window.cpp @@ -659,22 +659,7 @@ void Window::addShortcuts() } else if (arg == "toggleLiveOnly") { - if (!this->notebook_->getShowTabs()) - { - // Tabs are currently hidden, so the intention is to show - // tabs again before enabling the live only setting - this->notebook_->setShowTabs(true); - getSettings()->tabVisibility.setValue( - NotebookTabVisibility::LiveOnly); - } - else - { - getSettings()->tabVisibility.setValue( - getSettings()->tabVisibility.getEnum() == - NotebookTabVisibility::LiveOnly - ? NotebookTabVisibility::AllTabs - : NotebookTabVisibility::LiveOnly); - } + this->notebook_->toggleOfflineTabs(); } else {