Added ability to display non-negative numbers in CharactersLimitLabel.

This commit is contained in:
23rd
2025-09-20 18:50:42 +03:00
committed by John Preston
parent 4779c036f5
commit 4ef7f0dd24
2 changed files with 42 additions and 16 deletions
@@ -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<int>(std::log10(std::abs(n))) + 1;
}
} // namespace
namespace HistoryView::Controls {
CharactersLimitLabel::CharactersLimitLabel(
not_null<Ui::RpWidget*> parent,
not_null<Ui::RpWidget*> widgetToAlign,
style::align align)
: Ui::FlatLabel(parent, st::historyCharsLimitationLabel) {
: Ui::FlatLabel(parent, st::historyCharsLimitationLabel)
, _widgetToAlign(widgetToAlign)
, _position((align == style::al_top)
? Fn<void(int, const QRect &)>([=](int height, const QRect &g) {
const auto w = textMaxWidth();
move(g.x() + (g.width() - w) / 2, rect::bottom(g));
})
: Fn<void(int, const QRect &)>([=](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<void(int, const QRect &)>;
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
@@ -20,6 +20,11 @@ public:
void setLeft(int value);
private:
int _lastValue = 0;
not_null<Ui::RpWidget*> _widgetToAlign;
Fn<void(int, const QRect &)> _position;
};
} // namespace HistoryView::Controls