Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get rid of duplicate scale events #5404

Merged
merged 6 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion src/widgets/BaseWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
1 change: 0 additions & 1 deletion src/widgets/BaseWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ BaseWindow::BaseWindow(FlagsEnum<Flags> _flags, QWidget *parent)
[this]() {
postToThread([this] {
this->updateScale();
this->updateScale();
});
},
this->connections_, false);
Expand Down
15 changes: 14 additions & 1 deletion src/widgets/Notebook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,12 @@ void Notebook::showTabVisibilityInfoPopup()

void Notebook::refresh()
{
if (this->refreshPaused_)
{
this->refreshRequested_ = true;
return;
}

this->performLayout();
this->updateTabVisibility();
}
Expand Down Expand Up @@ -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 *)
Expand Down
5 changes: 5 additions & 0 deletions src/widgets/Notebook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
6 changes: 1 addition & 5 deletions src/widgets/dialogs/SettingsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
pajlada marked this conversation as resolved.
Show resolved Hide resolved

for (SettingsDialogTab *tab : this->tabs_)
{
tab->setFixedHeight(30);
Expand Down
34 changes: 18 additions & 16 deletions src/widgets/helper/NotebookTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -199,13 +204,13 @@ int NotebookTab::normalTabWidth()
width = (metrics.horizontalAdvance(this->getTitle()) + int(16 * scale));
}

if (this->height() > 150 * scale)
if (static_cast<float>(height) > 150 * scale)
{
width = this->height();
width = height;
}
else
{
width = clamp(width, this->height(), int(150 * scale));
width = std::clamp(width, height, static_cast<int>(150 * scale));
pajlada marked this conversation as resolved.
Show resolved Hide resolved
}

return width;
Expand All @@ -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<int>(NOTEBOOK_TAB_HEIGHT * scale);
int width = this->normalTabWidthForHeight(height);

if (width < this->growWidth_)
{
Expand Down Expand Up @@ -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_);
}
Expand Down Expand Up @@ -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<int>(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<int>(20 * s),
rect.center().y() - centerAdjustment, size, size);
Expand Down
10 changes: 6 additions & 4 deletions src/widgets/helper/NotebookTab.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class NotebookTab : public Button
void hideTabXChanged();

void growWidth(int width);
int normalTabWidth();
int normalTabWidth() const;

protected:
void themeChangedEvent() override;
Expand Down Expand Up @@ -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_;
Expand Down
2 changes: 2 additions & 0 deletions src/widgets/splits/SplitHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ SplitHeader::SplitHeader(Split *split)
}
});
}

this->scaleChangedEvent(this->scale());
}

void SplitHeader::initializeLayout()
Expand Down
Loading