Add context menu entry to toggle offline tabs (#5318)

Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
nerix 2024-04-12 23:05:47 +02:00 committed by GitHub
parent e6bf503594
commit 1ca77a1e84
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 88 additions and 22 deletions

View file

@ -2,6 +2,7 @@
## Unversioned ## Unversioned
- Minor: Added context menu action to toggle visibility of offline tabs. (#5318)
- Minor: Report sub duration for more multi-month gift cases. (#5319) - Minor: Report sub duration for more multi-month gift cases. (#5319)
- Bugfix: Fixed split tooltip getting stuck in some cases. (#5309) - 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) - Bugfix: Fixed the version string not showing up as expected in Finder on macOS. (#5311)

View file

@ -33,7 +33,6 @@ namespace chatterino {
Notebook::Notebook(QWidget *parent) Notebook::Notebook(QWidget *parent)
: BaseWidget(parent) : BaseWidget(parent)
, menu_(this)
, addButton_(new NotebookButton(this)) , addButton_(new NotebookButton(this))
{ {
this->addButton_->setIcon(NotebookButton::Icon::Plus); this->addButton_->setIcon(NotebookButton::Icon::Plus);
@ -81,8 +80,6 @@ Notebook::Notebook(QWidget *parent)
<< "Notebook must be created within a BaseWindow"; << "Notebook must be created within a BaseWindow";
} }
this->addNotebookActionsToMenu(&this->menu_);
// Manually resize the add button so the initial paint uses the correct // Manually resize the add button so the initial paint uses the correct
// width when computing the maximum width occupied per column in vertical // width when computing the maximum width occupied per column in vertical
// tab rendering. // tab rendering.
@ -1125,7 +1122,14 @@ void Notebook::mousePressEvent(QMouseEvent *event)
switch (event->button()) switch (event->button())
{ {
case Qt::RightButton: { 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; break;
default:; default:;
@ -1294,6 +1298,10 @@ SplitNotebook::SplitNotebook(Window *parent)
this->addCustomButtons(); this->addCustomButtons();
} }
this->toggleOfflineTabsAction_ = new QAction({}, this);
QObject::connect(this->toggleOfflineTabsAction_, &QAction::triggered, this,
&SplitNotebook::toggleOfflineTabs);
getSettings()->tabVisibility.connect( getSettings()->tabVisibility.connect(
[this](int val, auto) { [this](int val, auto) {
auto visibility = NotebookTabVisibility(val); auto visibility = NotebookTabVisibility(val);
@ -1307,12 +1315,17 @@ SplitNotebook::SplitNotebook(Window *parent)
this->setTabVisibilityFilter([](const NotebookTab *tab) { this->setTabVisibilityFilter([](const NotebookTab *tab) {
return tab->isLive(); return tab->isLive();
}); });
this->toggleOfflineTabsAction_->setText("Show all tabs");
break; break;
case NotebookTabVisibility::AllTabs: case NotebookTabVisibility::AllTabs:
default: default:
this->setTabVisibilityFilter(nullptr); this->setTabVisibilityFilter(nullptr);
this->toggleOfflineTabsAction_->setText(
"Show live tabs only");
break; break;
} }
this->updateToggleOfflineTabsHotkey(visibility);
}, },
this->signalHolder_, true); 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*/) void SplitNotebook::showEvent(QShowEvent * /*event*/)
{ {
if (auto *page = this->getSelectedPage()) if (auto *page = this->getSelectedPage())
@ -1442,6 +1480,42 @@ void SplitNotebook::addCustomButtons()
this->updateStreamerModeIcon(); 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() void SplitNotebook::updateStreamerModeIcon()
{ {
if (this->streamerModeIcon_ == nullptr) if (this->streamerModeIcon_ == nullptr)

View file

@ -118,7 +118,7 @@ public:
bool isNotebookLayoutLocked() const; bool isNotebookLayoutLocked() const;
void setLockNotebookLayout(bool value); void setLockNotebookLayout(bool value);
void addNotebookActionsToMenu(QMenu *menu); virtual void addNotebookActionsToMenu(QMenu *menu);
// Update layout and tab visibility // Update layout and tab visibility
void refresh(); void refresh();
@ -182,7 +182,7 @@ private:
size_t visibleButtonCount() const; size_t visibleButtonCount() const;
QList<Item> items_; QList<Item> items_;
QMenu menu_; QMenu *menu_ = nullptr;
QWidget *selectedPage_ = nullptr; QWidget *selectedPage_ = nullptr;
NotebookButton *addButton_; NotebookButton *addButton_;
@ -215,6 +215,9 @@ public:
void select(QWidget *page, bool focusPage = true) override; void select(QWidget *page, bool focusPage = true) override;
void themeChangedEvent() override; void themeChangedEvent() override;
void addNotebookActionsToMenu(QMenu *menu) override;
void toggleOfflineTabs();
protected: protected:
void showEvent(QShowEvent *event) override; void showEvent(QShowEvent *event) override;
@ -223,6 +226,9 @@ private:
pajlada::Signals::SignalHolder signalHolder_; pajlada::Signals::SignalHolder signalHolder_;
QAction *toggleOfflineTabsAction_;
void updateToggleOfflineTabsHotkey(NotebookTabVisibility newTabVisibility);
// Main window on Windows has basically a duplicate of this in Window // Main window on Windows has basically a duplicate of this in Window
NotebookButton *streamerModeIcon_{}; NotebookButton *streamerModeIcon_{};
void updateStreamerModeIcon(); void updateStreamerModeIcon();

View file

@ -659,22 +659,7 @@ void Window::addShortcuts()
} }
else if (arg == "toggleLiveOnly") else if (arg == "toggleLiveOnly")
{ {
if (!this->notebook_->getShowTabs()) this->notebook_->toggleOfflineTabs();
{
// 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);
}
} }
else else
{ {