From 4ef7f0dd246e79f352fdd01f6957ab5ee4f84c06 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sat, 20 Sep 2025 18:50:42 +0300 Subject: [PATCH] Added ability to display non-negative numbers in CharactersLimitLabel. --- .../history_view_characters_limit.cpp | 53 +++++++++++++------ .../controls/history_view_characters_limit.h | 5 ++ 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/Telegram/SourceFiles/history/view/controls/history_view_characters_limit.cpp b/Telegram/SourceFiles/history/view/controls/history_view_characters_limit.cpp index c3f273674f..275acb3604 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_characters_limit.cpp +++ b/Telegram/SourceFiles/history/view/controls/history_view_characters_limit.cpp @@ -10,36 +10,57 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/rect.h" #include "styles/style_chat_helpers.h" +namespace { + +constexpr auto kMinus = QChar(0x2212); +constexpr auto kLimit = int(999); + +[[nodiscard]] int CountDigits(int n) { + return n == 0 ? 1 : static_cast(std::log10(std::abs(n))) + 1; +} + +} // namespace + namespace HistoryView::Controls { CharactersLimitLabel::CharactersLimitLabel( not_null parent, not_null widgetToAlign, style::align align) -: Ui::FlatLabel(parent, st::historyCharsLimitationLabel) { +: Ui::FlatLabel(parent, st::historyCharsLimitationLabel) +, _widgetToAlign(widgetToAlign) +, _position((align == style::al_top) + ? Fn([=](int height, const QRect &g) { + const auto w = textMaxWidth(); + move(g.x() + (g.width() - w) / 2, rect::bottom(g)); + }) + : Fn([=](int height, const QRect &g) { + const auto w = textMaxWidth(); + move(g.x() + (g.width() - w) / 2, g.y() - height); + })) { Expects((align == style::al_top) || align == style::al_bottom); - const auto w = st::historyCharsLimitationLabel.minWidth; - using F = Fn; - const auto position = (align == style::al_top) - ? F([=](int height, const QRect &g) { - move(g.x() + (g.width() - w) / 2, rect::bottom(g)); - }) - : F([=](int height, const QRect &g) { - move(g.x() + (g.width() - w) / 2, g.y() - height); - }); rpl::combine( Ui::RpWidget::heightValue(), widgetToAlign->geometryValue() - ) | rpl::start_with_next(position, lifetime()); + ) | rpl::start_with_next(_position, lifetime()); } void CharactersLimitLabel::setLeft(int value) { - if (value <= 0) { - return; + const auto orderChanged = (CountDigits(value) != CountDigits(_lastValue)); + _lastValue = value; + + if (value > 0) { + setTextColorOverride(st::historyCharsLimitationLabel.textFg->c); + Ui::FlatLabel::setText(kMinus + + QString::number(std::min(value, kLimit))); + } else { + setTextColorOverride(st::windowSubTextFg->c); + Ui::FlatLabel::setText(QString::number(-value)); + } + + if (orderChanged) { + _position(height(), _widgetToAlign->geometry()); } - constexpr auto kMinus = QChar(0x2212); - constexpr auto kLimit = int(999); - Ui::FlatLabel::setText(kMinus + QString::number(std::min(value, kLimit))); } } // namespace HistoryView::Controls diff --git a/Telegram/SourceFiles/history/view/controls/history_view_characters_limit.h b/Telegram/SourceFiles/history/view/controls/history_view_characters_limit.h index 3b6054e191..e0e1b854ac 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_characters_limit.h +++ b/Telegram/SourceFiles/history/view/controls/history_view_characters_limit.h @@ -20,6 +20,11 @@ public: void setLeft(int value); +private: + int _lastValue = 0; + not_null _widgetToAlign; + Fn _position; + }; } // namespace HistoryView::Controls