From 79af7c5523ae59d4dd13cc2bde86bb8611bde11c Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Tue, 24 May 2022 08:50:24 +0300 Subject: [PATCH] Added ability to set disable monospace in numbers animation. --- ui/effects/numbers_animation.cpp | 30 +++++++++++++++++++++++------- ui/effects/numbers_animation.h | 3 +++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/ui/effects/numbers_animation.cpp b/ui/effects/numbers_animation.cpp index 2a44ac3..cbe0230 100644 --- a/ui/effects/numbers_animation.cpp +++ b/ui/effects/numbers_animation.cpp @@ -28,6 +28,10 @@ void NumbersAnimation::setDuration(int duration) { _duration = duration; } +void NumbersAnimation::setDisabledMonospace(bool value) { + _disabledMonospace = value; +} + void NumbersAnimation::setText(const QString &text, int value) { if (_a_ready.animating()) { _delayedText = text; @@ -80,8 +84,13 @@ void NumbersAnimation::realSetText(QString text, int value) { --oldSize; } } - _fromWidth = oldSize * _digitWidth; - _toWidth = newSize * _digitWidth; + if (_disabledMonospace) { + _fromWidth = _toWidth; + _toWidth = _font->m.horizontalAdvance(text); + } else { + _fromWidth = oldSize * _digitWidth; + _toWidth = newSize * _digitWidth; + } if (animating) { _a_ready.start( [this] { animationCallback(); }, @@ -122,30 +131,37 @@ void NumbersAnimation::paint(QPainter &p, int x, int y, int outerWidth) { QString singleChar('0'); if (style::RightToLeft()) x = outerWidth - x - width; - x += width - _digits.size() * _digitWidth; + x += width + - (_disabledMonospace ? _toWidth : _digits.size() * _digitWidth); auto fromTop = anim::interpolate(0, _font->height, progress) * (_growing ? 1 : -1); auto toTop = anim::interpolate(_font->height, 0, progress) * (_growing ? -1 : 1); for (auto i = 0; i != digitsCount; ++i) { auto &digit = _digits[i]; auto from = digit.from; auto to = digit.to; + const auto toCharWidth = (!_disabledMonospace || to.isDigit()) + ? _digitWidth + : _font->m.horizontalAdvance(to); + const auto fromCharWidth = (!_disabledMonospace || from.isDigit()) + ? _digitWidth + : _font->m.horizontalAdvance(from); if (from == to) { p.setOpacity(1.); singleChar[0] = from; - p.drawText(x + (_digitWidth - digit.fromWidth) / 2, y + _font->ascent, singleChar); + p.drawText(x + (toCharWidth - digit.fromWidth) / 2, y + _font->ascent, singleChar); } else { if (from.unicode()) { p.setOpacity(1. - progress); singleChar[0] = from; - p.drawText(x + (_digitWidth - digit.fromWidth) / 2, y + fromTop + _font->ascent, singleChar); + p.drawText(x + (fromCharWidth - digit.fromWidth) / 2, y + fromTop + _font->ascent, singleChar); } if (to.unicode()) { p.setOpacity(progress); singleChar[0] = to; - p.drawText(x + (_digitWidth - digit.toWidth) / 2, y + toTop + _font->ascent, singleChar); + p.drawText(x + (toCharWidth - digit.toWidth) / 2, y + toTop + _font->ascent, singleChar); } } - x += _digitWidth; + x += std::max(toCharWidth, fromCharWidth); } p.setOpacity(1.); } diff --git a/ui/effects/numbers_animation.h b/ui/effects/numbers_animation.h index c2f3fd3..b18d217 100644 --- a/ui/effects/numbers_animation.h +++ b/ui/effects/numbers_animation.h @@ -26,6 +26,7 @@ public: } void setText(const QString &text, int value); void setDuration(int duration); + void setDisabledMonospace(bool value); void finishAnimating(); void paint(QPainter &p, int x, int y, int outerWidth); @@ -60,6 +61,8 @@ private: int _value = 0; bool _growing = false; + bool _disabledMonospace = false; + Fn _animationCallback; Fn _widthChangedCallback;