From 84c0b39fde0eaf2f2b8eaaa3c4ca33a1d9e50d60 Mon Sep 17 00:00:00 2001 From: nerix Date: Sun, 25 May 2025 11:17:06 +0200 Subject: [PATCH] refactor: buttons and friends (#6102) --- CHANGELOG.md | 1 + src/CMakeLists.txt | 35 +- src/util/InitUpdateButton.hpp | 19 - src/widgets/BaseWindow.cpp | 32 +- src/widgets/BaseWindow.hpp | 24 +- src/widgets/DraggablePopup.cpp | 4 +- src/widgets/DraggablePopup.hpp | 4 +- src/widgets/Notebook.cpp | 32 +- src/widgets/Notebook.hpp | 18 +- src/widgets/Window.cpp | 23 +- src/widgets/Window.hpp | 6 +- src/widgets/{helper => buttons}/Button.cpp | 354 +++++++----------- src/widgets/buttons/Button.hpp | 211 +++++++++++ src/widgets/buttons/DimButton.cpp | 36 ++ src/widgets/buttons/DimButton.hpp | 40 ++ .../buttons}/InitUpdateButton.cpp | 6 +- src/widgets/buttons/InitUpdateButton.hpp | 15 + src/widgets/buttons/LabelButton.cpp | 56 +++ src/widgets/buttons/LabelButton.hpp | 55 +++ .../{helper => buttons}/NotebookButton.cpp | 28 +- .../{helper => buttons}/NotebookButton.hpp | 9 +- src/widgets/buttons/PixmapButton.cpp | 98 +++++ src/widgets/buttons/PixmapButton.hpp | 46 +++ .../{helper => buttons}/SignalLabel.cpp | 2 +- .../{helper => buttons}/SignalLabel.hpp | 0 src/widgets/buttons/SvgButton.cpp | 80 ++++ src/widgets/buttons/SvgButton.hpp | 70 ++++ .../{helper => buttons}/TitlebarButton.cpp | 51 +-- .../{helper => buttons}/TitlebarButton.hpp | 28 +- .../{helper => buttons}/TitlebarButtons.cpp | 4 +- .../{helper => buttons}/TitlebarButtons.hpp | 0 src/widgets/dialogs/ReplyThreadPopup.cpp | 2 +- src/widgets/dialogs/SettingsDialog.cpp | 1 - src/widgets/dialogs/UserInfoPopup.cpp | 43 ++- src/widgets/dialogs/UserInfoPopup.hpp | 11 +- src/widgets/helper/Button.hpp | 108 ------ src/widgets/helper/ChannelView.cpp | 18 +- src/widgets/helper/ChannelView.hpp | 4 +- src/widgets/helper/EffectLabel.cpp | 43 --- src/widgets/helper/EffectLabel.hpp | 40 -- src/widgets/helper/NotebookTab.cpp | 1 + src/widgets/helper/NotebookTab.hpp | 5 +- src/widgets/helper/OverlayInteraction.hpp | 2 +- src/widgets/settingspages/AboutPage.cpp | 1 + src/widgets/settingspages/GeneralPageView.hpp | 2 +- src/widgets/splits/SplitHeader.cpp | 23 +- src/widgets/splits/SplitHeader.hpp | 15 +- src/widgets/splits/SplitInput.cpp | 58 ++- src/widgets/splits/SplitInput.hpp | 10 +- 49 files changed, 1083 insertions(+), 691 deletions(-) delete mode 100644 src/util/InitUpdateButton.hpp rename src/widgets/{helper => buttons}/Button.cpp (60%) create mode 100644 src/widgets/buttons/Button.hpp create mode 100644 src/widgets/buttons/DimButton.cpp create mode 100644 src/widgets/buttons/DimButton.hpp rename src/{util => widgets/buttons}/InitUpdateButton.cpp (94%) create mode 100644 src/widgets/buttons/InitUpdateButton.hpp create mode 100644 src/widgets/buttons/LabelButton.cpp create mode 100644 src/widgets/buttons/LabelButton.hpp rename src/widgets/{helper => buttons}/NotebookButton.cpp (90%) rename src/widgets/{helper => buttons}/NotebookButton.hpp (80%) create mode 100644 src/widgets/buttons/PixmapButton.cpp create mode 100644 src/widgets/buttons/PixmapButton.hpp rename src/widgets/{helper => buttons}/SignalLabel.cpp (93%) rename src/widgets/{helper => buttons}/SignalLabel.hpp (100%) create mode 100644 src/widgets/buttons/SvgButton.cpp create mode 100644 src/widgets/buttons/SvgButton.hpp rename src/widgets/{helper => buttons}/TitlebarButton.cpp (75%) rename src/widgets/{helper => buttons}/TitlebarButton.hpp (65%) rename src/widgets/{helper => buttons}/TitlebarButtons.cpp (96%) rename src/widgets/{helper => buttons}/TitlebarButtons.hpp (100%) delete mode 100644 src/widgets/helper/Button.hpp delete mode 100644 src/widgets/helper/EffectLabel.cpp delete mode 100644 src/widgets/helper/EffectLabel.hpp diff --git a/CHANGELOG.md b/CHANGELOG.md index ed727e11..ddaae5ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,7 @@ - Dev: Simplified string literals to be a re-export of Qt functions. (#6175) - Dev: Fixed incorrect lua generation of static methods for typescript plugins. (#6190, #6223) - Dev: Merged top/bottom and left/right notebook layouts. (#6215) +- Dev: Refactored `Button` and friends. (#6102) ## 2.5.3 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f32b3dec..0b5433d9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -523,8 +523,6 @@ set(SOURCE_FILES util/Helpers.hpp util/IncognitoBrowser.cpp util/IncognitoBrowser.hpp - util/InitUpdateButton.cpp - util/InitUpdateButton.hpp util/IpcQueue.cpp util/IpcQueue.hpp util/IrcHelpers.cpp @@ -602,6 +600,27 @@ set(SOURCE_FILES widgets/Window.cpp widgets/Window.hpp + widgets/buttons/Button.cpp + widgets/buttons/Button.hpp + widgets/buttons/DimButton.cpp + widgets/buttons/DimButton.hpp + widgets/buttons/InitUpdateButton.cpp + widgets/buttons/InitUpdateButton.hpp + widgets/buttons/LabelButton.cpp + widgets/buttons/LabelButton.hpp + widgets/buttons/NotebookButton.cpp + widgets/buttons/NotebookButton.hpp + widgets/buttons/PixmapButton.cpp + widgets/buttons/PixmapButton.hpp + widgets/buttons/SignalLabel.cpp + widgets/buttons/SignalLabel.hpp + widgets/buttons/SvgButton.cpp + widgets/buttons/SvgButton.hpp + widgets/buttons/TitlebarButton.cpp + widgets/buttons/TitlebarButton.hpp + widgets/buttons/TitlebarButtons.cpp + widgets/buttons/TitlebarButtons.hpp + widgets/dialogs/BadgePickerDialog.cpp widgets/dialogs/BadgePickerDialog.hpp widgets/dialogs/ChannelFilterEditorDialog.cpp @@ -660,8 +679,6 @@ set(SOURCE_FILES widgets/helper/color/SBCanvas.cpp widgets/helper/color/SBCanvas.hpp - widgets/helper/Button.cpp - widgets/helper/Button.hpp widgets/helper/ChannelView.cpp widgets/helper/ChannelView.hpp widgets/helper/ComboBoxItemDelegate.cpp @@ -670,16 +687,12 @@ set(SOURCE_FILES widgets/helper/DebugPopup.hpp widgets/helper/EditableModelView.cpp widgets/helper/EditableModelView.hpp - widgets/helper/EffectLabel.cpp - widgets/helper/EffectLabel.hpp widgets/helper/IconDelegate.cpp widgets/helper/IconDelegate.hpp widgets/helper/InvisibleSizeGrip.cpp widgets/helper/InvisibleSizeGrip.hpp widgets/helper/MessageView.cpp widgets/helper/MessageView.hpp - widgets/helper/NotebookButton.cpp - widgets/helper/NotebookButton.hpp widgets/helper/NotebookTab.cpp widgets/helper/NotebookTab.hpp widgets/helper/OverlayInteraction.cpp @@ -694,14 +707,8 @@ set(SOURCE_FILES widgets/helper/SearchPopup.hpp widgets/helper/SettingsDialogTab.cpp widgets/helper/SettingsDialogTab.hpp - widgets/helper/SignalLabel.cpp - widgets/helper/SignalLabel.hpp widgets/helper/TableStyles.cpp widgets/helper/TableStyles.hpp - widgets/helper/TitlebarButton.cpp - widgets/helper/TitlebarButton.hpp - widgets/helper/TitlebarButtons.cpp - widgets/helper/TitlebarButtons.hpp widgets/helper/TrimRegExpValidator.cpp widgets/helper/TrimRegExpValidator.hpp diff --git a/src/util/InitUpdateButton.hpp b/src/util/InitUpdateButton.hpp deleted file mode 100644 index 707ff707..00000000 --- a/src/util/InitUpdateButton.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include - -namespace pajlada { -namespace Signals { - class SignalHolder; -} -} // namespace pajlada - -namespace chatterino { - -class Button; -class UpdateDialog; - -void initUpdateButton(Button &button, - pajlada::Signals::SignalHolder &signalHolder); - -} // namespace chatterino diff --git a/src/widgets/BaseWindow.cpp b/src/widgets/BaseWindow.cpp index 6f016545..a16dd544 100644 --- a/src/widgets/BaseWindow.cpp +++ b/src/widgets/BaseWindow.cpp @@ -8,8 +8,9 @@ #include "util/DebugCount.hpp" #include "util/PostToThread.hpp" #include "util/WindowsHelper.hpp" -#include "widgets/helper/EffectLabel.hpp" -#include "widgets/helper/TitlebarButtons.hpp" +#include "widgets/buttons/LabelButton.hpp" +#include "widgets/buttons/TitlebarButton.hpp" +#include "widgets/buttons/TitlebarButtons.hpp" #include "widgets/Label.hpp" #include "widgets/Window.hpp" @@ -35,8 +36,6 @@ # include #endif -#include "widgets/helper/TitlebarButton.hpp" - namespace { using namespace chatterino; @@ -672,34 +671,21 @@ void BaseWindow::focusOutEvent(QFocusEvent *event) BaseWidget::focusOutEvent(event); } -TitleBarButton *BaseWindow::addTitleBarButton(const TitleBarButtonStyle &style, - std::function onClicked) +void BaseWindow::appendTitlebarButton(Button *button) { - TitleBarButton *button = new TitleBarButton; - button->setScaleIndependentSize(30, 30); - this->ui_.buttons.push_back(button); this->ui_.titlebarBox->insertWidget(1, button); - button->setButtonStyle(style); - - QObject::connect(button, &TitleBarButton::leftClicked, this, [onClicked] { - onClicked(); - }); - - return button; } -EffectLabel *BaseWindow::addTitleBarLabel(std::function onClicked) +LabelButton *BaseWindow::addTitleBarLabel(std::function onClicked) { - EffectLabel *button = new EffectLabel; + auto *button = new LabelButton; button->setScaleIndependentHeight(30); - this->ui_.buttons.push_back(button); - this->ui_.titlebarBox->insertWidget(1, button); + this->appendTitlebarButton(button); - QObject::connect(button, &EffectLabel::leftClicked, this, [onClicked] { - onClicked(); - }); + QObject::connect(button, &LabelButton::leftClicked, this, + std::move(onClicked)); return button; } diff --git a/src/widgets/BaseWindow.hpp b/src/widgets/BaseWindow.hpp index 6cba9d82..64dbf114 100644 --- a/src/widgets/BaseWindow.hpp +++ b/src/widgets/BaseWindow.hpp @@ -16,10 +16,11 @@ typedef struct tagMSG MSG; namespace chatterino { class Button; -class EffectLabel; +class LabelButton; +class PixmapButton; class TitleBarButton; class TitleBarButtons; -enum class TitleBarButtonStyle; +enum class TitleBarButtonStyle : std::uint8_t; class BaseWindow : public BaseWidget { @@ -52,9 +53,20 @@ public: QWidget *getLayoutContainer(); bool hasCustomWindowFrame() const; - TitleBarButton *addTitleBarButton(const TitleBarButtonStyle &style, - std::function onClicked); - EffectLabel *addTitleBarLabel(std::function onClicked); + + template + T *addTitleBarButton(std::function onClicked, auto &&...args) + { + auto *button = new T(std::forward(args)...); + button->setScaleIndependentSize(30, 30); + this->appendTitlebarButton(button); + + QObject::connect(button, &T::leftClicked, this, std::move(onClicked)); + + return button; + } + + LabelButton *addTitleBarLabel(std::function onClicked); void moveTo(QPoint point, widgets::BoundsChecking mode); @@ -169,6 +181,8 @@ private: bool handleNCHITTEST(MSG *msg, long *result); #endif + void appendTitlebarButton(Button *button); + bool enableCustomFrame_; bool frameless_; bool shown_ = false; diff --git a/src/widgets/DraggablePopup.cpp b/src/widgets/DraggablePopup.cpp index 0717515f..bc842ed3 100644 --- a/src/widgets/DraggablePopup.cpp +++ b/src/widgets/DraggablePopup.cpp @@ -2,7 +2,7 @@ #include "singletons/Resources.hpp" #include "singletons/Theme.hpp" -#include "widgets/helper/Button.hpp" +#include "widgets/buttons/PixmapButton.hpp" #include @@ -119,7 +119,7 @@ void DraggablePopup::togglePinned() } Button *DraggablePopup::createPinButton() { - this->pinButton_ = new Button(this); + this->pinButton_ = new PixmapButton(this); this->pinButton_->setPixmap(getTheme()->buttons.pin); this->pinButton_->setScaleIndependentSize(18, 18); this->pinButton_->setToolTip("Pin Window"); diff --git a/src/widgets/DraggablePopup.hpp b/src/widgets/DraggablePopup.hpp index 97217c0d..4e6e1cd4 100644 --- a/src/widgets/DraggablePopup.hpp +++ b/src/widgets/DraggablePopup.hpp @@ -9,6 +9,8 @@ namespace chatterino { +class PixmapButton; + class DraggablePopup : public BaseWindow { Q_OBJECT @@ -60,7 +62,7 @@ private: // dragTimer_ is called ~60 times per second once the user has initiated dragging QTimer dragTimer_; - Button *pinButton_ = nullptr; + PixmapButton *pinButton_ = nullptr; bool isPinned_ = false; }; diff --git a/src/widgets/Notebook.cpp b/src/widgets/Notebook.cpp index dc1083d8..f6b442c1 100644 --- a/src/widgets/Notebook.cpp +++ b/src/widgets/Notebook.cpp @@ -10,10 +10,11 @@ #include "singletons/StreamerMode.hpp" #include "singletons/Theme.hpp" #include "singletons/WindowManager.hpp" -#include "util/InitUpdateButton.hpp" +#include "widgets/buttons/InitUpdateButton.hpp" +#include "widgets/buttons/NotebookButton.hpp" +#include "widgets/buttons/PixmapButton.hpp" #include "widgets/dialogs/SettingsDialog.hpp" #include "widgets/helper/ChannelView.hpp" -#include "widgets/helper/NotebookButton.hpp" #include "widgets/helper/NotebookTab.hpp" #include "widgets/splits/Split.hpp" #include "widgets/splits/SplitContainer.hpp" @@ -1162,16 +1163,6 @@ NotebookButton *Notebook::getAddButton() return this->addButton_; } -NotebookButton *Notebook::addCustomButton() -{ - NotebookButton *btn = new NotebookButton(this); - - this->customButtons_.push_back(btn); - - this->performLayout(); - return btn; -} - NotebookTab *Notebook::getTabFromPage(QWidget *page) { for (auto &it : this->items_) @@ -1416,7 +1407,7 @@ void SplitNotebook::showEvent(QShowEvent * /*event*/) void SplitNotebook::addCustomButtons() { // settings - auto *settingsBtn = this->addCustomButton(); + auto *settingsBtn = this->addCustomButton(); // This is to ensure you can't lock yourself out of the settings if (getApp()->getArgs().safeMode) @@ -1442,7 +1433,7 @@ void SplitNotebook::addCustomButtons() }); // account - auto *userBtn = this->addCustomButton(); + auto *userBtn = this->addCustomButton(); userBtn->setVisible(!getSettings()->hideUserButton.getValue()); getSettings()->hideUserButton.connect( [userBtn](bool hide, auto) { @@ -1457,17 +1448,16 @@ void SplitNotebook::addCustomButtons() }); // updates - auto *updateBtn = this->addCustomButton(); + auto *updateBtn = this->addCustomButton(); initUpdateButton(*updateBtn, this->signalHolder_); // streamer mode - this->streamerModeIcon_ = this->addCustomButton(); - QObject::connect(this->streamerModeIcon_, &NotebookButton::leftClicked, - [this] { - getApp()->getWindows()->showSettingsDialog( - this, SettingsDialogPreference::StreamerMode); - }); + this->streamerModeIcon_ = this->addCustomButton(); + QObject::connect(this->streamerModeIcon_, &Button::leftClicked, [this] { + getApp()->getWindows()->showSettingsDialog( + this, SettingsDialogPreference::StreamerMode); + }); QObject::connect(getApp()->getStreamerMode(), &IStreamerMode::changed, this, &SplitNotebook::updateStreamerModeIcon); this->updateStreamerModeIcon(); diff --git a/src/widgets/Notebook.hpp b/src/widgets/Notebook.hpp index f5592d2e..1b4ec7b0 100644 --- a/src/widgets/Notebook.hpp +++ b/src/widgets/Notebook.hpp @@ -15,6 +15,9 @@ namespace chatterino { +class Button; +class PixmapButton; +class SvgButton; class Window; class UpdateDialog; class NotebookButton; @@ -131,7 +134,16 @@ protected: void paintEvent(QPaintEvent *) override; NotebookButton *getAddButton(); - NotebookButton *addCustomButton(); + + template + T *addCustomButton(auto &&...args) + { + auto *btn = new T(std::forward(args)..., this); + this->customButtons_.push_back(btn); + this->performLayout(); + + return btn; + } struct Item { NotebookTab *tab{}; @@ -206,7 +218,7 @@ private: QWidget *selectedPage_ = nullptr; NotebookButton *addButton_; - std::vector customButtons_; + std::vector