From 48e57533601748ee2bfe68621b80471420f9c2fb Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Fri, 17 May 2024 16:34:38 +0200 Subject: [PATCH 1/6] chore: get rid of duplicate scale event --- src/widgets/BaseWindow.cpp | 1 - src/widgets/Notebook.cpp | 15 +++++++++++- src/widgets/Notebook.hpp | 5 ++++ src/widgets/dialogs/SettingsDialog.cpp | 6 +---- src/widgets/helper/NotebookTab.cpp | 34 ++++++++++++++------------ src/widgets/helper/NotebookTab.hpp | 10 +++++--- 6 files changed, 44 insertions(+), 27 deletions(-) diff --git a/src/widgets/BaseWindow.cpp b/src/widgets/BaseWindow.cpp index bc8767e4999..f3a2afa151a 100644 --- a/src/widgets/BaseWindow.cpp +++ b/src/widgets/BaseWindow.cpp @@ -227,7 +227,6 @@ BaseWindow::BaseWindow(FlagsEnum _flags, QWidget *parent) [this]() { postToThread([this] { this->updateScale(); - this->updateScale(); }); }, this->connections_, false); diff --git a/src/widgets/Notebook.cpp b/src/widgets/Notebook.cpp index 7fdff5495cf..e924329d2a4 100644 --- a/src/widgets/Notebook.cpp +++ b/src/widgets/Notebook.cpp @@ -593,6 +593,12 @@ void Notebook::showTabVisibilityInfoPopup() void Notebook::refresh() { + if (this->refreshPaused_) + { + this->refreshRequested_ = true; + return; + } + this->performLayout(); this->updateTabVisibility(); } @@ -652,13 +658,20 @@ void Notebook::resizeAddButton() this->addButton_->setFixedSize(h, h); } -void Notebook::scaleChangedEvent(float) +void Notebook::scaleChangedEvent(float /*scale*/) { this->resizeAddButton(); + this->refreshPaused_ = true; + this->refreshRequested_ = false; for (auto &i : this->items_) { i.tab->updateSize(); } + this->refreshPaused_ = false; + if (this->refreshRequested_) + { + this->refresh(); + } } void Notebook::resizeEvent(QResizeEvent *) diff --git a/src/widgets/Notebook.hpp b/src/widgets/Notebook.hpp index ac0162c4283..2829bf4fd3c 100644 --- a/src/widgets/Notebook.hpp +++ b/src/widgets/Notebook.hpp @@ -193,7 +193,12 @@ class Notebook : public BaseWidget bool showAddButton_ = false; int lineOffset_ = 20; bool lockNotebookLayout_ = false; + + bool refreshPaused_ = false; + bool refreshRequested_ = false; + NotebookTabLocation tabLocation_ = NotebookTabLocation::Top; + QAction *lockNotebookLayoutAction_; QAction *showTabsAction_; QAction *toggleTopMostAction_; diff --git a/src/widgets/dialogs/SettingsDialog.cpp b/src/widgets/dialogs/SettingsDialog.cpp index 2a71593eccf..e8f594438ab 100644 --- a/src/widgets/dialogs/SettingsDialog.cpp +++ b/src/widgets/dialogs/SettingsDialog.cpp @@ -397,12 +397,8 @@ void SettingsDialog::refresh() } } -void SettingsDialog::scaleChangedEvent(float newDpi) +void SettingsDialog::scaleChangedEvent(float /*newScale*/) { - assert(newDpi == 1.F && - "Scaling is disabled for the settings dialog - its scale should " - "always be 1"); - for (SettingsDialogTab *tab : this->tabs_) { tab->setFixedHeight(30); diff --git a/src/widgets/helper/NotebookTab.cpp b/src/widgets/helper/NotebookTab.cpp index f7cb2b3a0f3..b86415f4ce2 100644 --- a/src/widgets/helper/NotebookTab.cpp +++ b/src/widgets/helper/NotebookTab.cpp @@ -182,10 +182,15 @@ void NotebookTab::growWidth(int width) } } -int NotebookTab::normalTabWidth() +int NotebookTab::normalTabWidth() const +{ + return this->normalTabWidthForHeight(this->height()); +} + +int NotebookTab::normalTabWidthForHeight(int height) const { float scale = this->scale(); - int width; + int width = 0; QFontMetrics metrics = getIApp()->getFonts()->getFontMetrics(FontStyle::UiTabs, scale); @@ -199,13 +204,13 @@ int NotebookTab::normalTabWidth() width = (metrics.horizontalAdvance(this->getTitle()) + int(16 * scale)); } - if (this->height() > 150 * scale) + if (static_cast(height) > 150 * scale) { - width = this->height(); + width = height; } else { - width = clamp(width, this->height(), int(150 * scale)); + width = std::clamp(width, height, static_cast(150 * scale)); } return width; @@ -214,8 +219,8 @@ int NotebookTab::normalTabWidth() void NotebookTab::updateSize() { float scale = this->scale(); - int width = this->normalTabWidth(); - auto height = int(NOTEBOOK_TAB_HEIGHT * scale); + auto height = static_cast(NOTEBOOK_TAB_HEIGHT * scale); + int width = this->normalTabWidthForHeight(height); if (width < this->growWidth_) { @@ -628,13 +633,13 @@ void NotebookTab::paintEvent(QPaintEvent *) } } -bool NotebookTab::hasXButton() +bool NotebookTab::hasXButton() const { return getSettings()->showTabCloseButton && this->notebook_->getAllowUserTabManagement(); } -bool NotebookTab::shouldDrawXButton() +bool NotebookTab::shouldDrawXButton() const { return this->hasXButton() && (this->mouseOver_ || this->selected_); } @@ -820,18 +825,15 @@ void NotebookTab::update() Button::update(); } -QRect NotebookTab::getXRect() +QRect NotebookTab::getXRect() const { QRect rect = this->rect(); float s = this->scale(); int size = static_cast(16 * s); - int centerAdjustment = - this->tabLocation_ == - (NotebookTabLocation::Top || - this->tabLocation_ == NotebookTabLocation::Bottom) - ? (size / 3) // slightly off true center - : (size / 2); // true center + int centerAdjustment = this->tabLocation_ == NotebookTabLocation::Top + ? (size / 3) // slightly off true center + : (size / 2); // true center QRect xRect(rect.right() - static_cast(20 * s), rect.center().y() - centerAdjustment, size, size); diff --git a/src/widgets/helper/NotebookTab.hpp b/src/widgets/helper/NotebookTab.hpp index 65b1f46ed31..6dc7d9464cf 100644 --- a/src/widgets/helper/NotebookTab.hpp +++ b/src/widgets/helper/NotebookTab.hpp @@ -71,7 +71,7 @@ class NotebookTab : public Button void hideTabXChanged(); void growWidth(int width); - int normalTabWidth(); + int normalTabWidth() const; protected: void themeChangedEvent() override; @@ -100,11 +100,13 @@ class NotebookTab : public Button private: void showRenameDialog(); - bool hasXButton(); - bool shouldDrawXButton(); - QRect getXRect(); + bool hasXButton() const; + bool shouldDrawXButton() const; + QRect getXRect() const; void titleUpdated(); + int normalTabWidthForHeight(int height) const; + QPropertyAnimation positionChangedAnimation_; bool positionChangedAnimationRunning_ = false; QPoint positionAnimationDesiredPoint_; From a1e08f2964bc5208853953369920f4432a3501f5 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Sat, 18 May 2024 22:47:14 +0200 Subject: [PATCH 2/6] experiment: check for duplicate setScale --- src/widgets/BaseWidget.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/widgets/BaseWidget.cpp b/src/widgets/BaseWidget.cpp index 5e2c932fd14..564ba5963c6 100644 --- a/src/widgets/BaseWidget.cpp +++ b/src/widgets/BaseWidget.cpp @@ -54,7 +54,11 @@ float BaseWidget::scale() const void BaseWidget::setScale(float value) { - // update scale value + if (this->scale_ == value) + { + return; + } + this->scale_ = value; this->scaleChangedEvent(this->scale()); From cb6b2997a52b9a8a0b20b300f53673111edef5ff Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Sat, 18 May 2024 22:59:50 +0200 Subject: [PATCH 3/6] fix: initial scale event --- src/widgets/splits/SplitHeader.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/widgets/splits/SplitHeader.cpp b/src/widgets/splits/SplitHeader.cpp index 6b7021798ea..35fb1dbde40 100644 --- a/src/widgets/splits/SplitHeader.cpp +++ b/src/widgets/splits/SplitHeader.cpp @@ -271,6 +271,8 @@ SplitHeader::SplitHeader(Split *split) } }); } + + this->scaleChangedEvent(this->scale()); } void SplitHeader::initializeLayout() From 4b505f22fa604dea43a4f28c6a7638559824a7b5 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Sat, 18 May 2024 23:08:24 +0200 Subject: [PATCH 4/6] chore: add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index edba34f08a5..4cc9646e93f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - Dev: Add doxygen build target. (#5377) - Dev: Make printing of strings in tests easier. (#5379) - Dev: Refactor and document `Scrollbar`. (#5334, #5393) +- Dev: Reduced the amount of scale events. (#5404) ## 2.5.1 From b1cd7a46b860cd748d7df235577b116ff32621ac Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sat, 18 May 2024 23:26:02 +0200 Subject: [PATCH 5/6] nit: fix includes --- src/widgets/helper/NotebookTab.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/widgets/helper/NotebookTab.cpp b/src/widgets/helper/NotebookTab.cpp index b86415f4ce2..cf9141fb700 100644 --- a/src/widgets/helper/NotebookTab.cpp +++ b/src/widgets/helper/NotebookTab.cpp @@ -8,7 +8,6 @@ #include "singletons/Settings.hpp" #include "singletons/Theme.hpp" #include "singletons/WindowManager.hpp" -#include "util/Clamp.hpp" #include "util/Helpers.hpp" #include "widgets/dialogs/SettingsDialog.hpp" #include "widgets/Notebook.hpp" @@ -25,6 +24,8 @@ #include #include +#include + namespace chatterino { namespace { // Translates the given rectangle by an amount in the direction to appear like the tab is selected. From d01ea7be848d633425d6cd4a21d1ad83f4407432 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Sun, 19 May 2024 10:45:10 +0200 Subject: [PATCH 6/6] fix: restore settings check --- src/widgets/dialogs/SettingsDialog.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/widgets/dialogs/SettingsDialog.cpp b/src/widgets/dialogs/SettingsDialog.cpp index e8f594438ab..d9ad48fff78 100644 --- a/src/widgets/dialogs/SettingsDialog.cpp +++ b/src/widgets/dialogs/SettingsDialog.cpp @@ -397,8 +397,12 @@ void SettingsDialog::refresh() } } -void SettingsDialog::scaleChangedEvent(float /*newScale*/) +void SettingsDialog::scaleChangedEvent(float newScale) { + assert(newScale == 1.F && + "Scaling is disabled for the settings dialog - its scale should " + "always be 1"); + for (SettingsDialogTab *tab : this->tabs_) { tab->setFixedHeight(30);