From 1b9b1739649ddf942937a6b9a1a22933070e5ecc Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 7 Feb 2023 20:05:34 +0400 Subject: [PATCH] Support spoilers in FlatLabel. --- ui/widgets/labels.cpp | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/ui/widgets/labels.cpp b/ui/widgets/labels.cpp index 0cdadf5..291539a 100644 --- a/ui/widgets/labels.cpp +++ b/ui/widgets/labels.cpp @@ -262,6 +262,12 @@ void FlatLabel::init() { void FlatLabel::textUpdated() { refreshSize(); setMouseTracking(_selectable || _text.hasLinks()); + if (_text.hasSpoilers()) { + _text.setSpoilerLinkFilter([weak = Ui::MakeWeak(this)]( + const ClickContext &context) { + return (context.button == Qt::LeftButton) && weak; + }); + } update(); } @@ -942,16 +948,32 @@ void FlatLabel::paintEvent(QPaintEvent *e) { ? ((width() - _textWidth) / 2) : (width() - _st.margin.right() - _textWidth)) : _st.margin.left(); - auto selection = _selection.empty() ? (_contextMenu ? _savedSelection : _selection) : _selection; - bool heightExceeded = _st.maxHeight && (_st.maxHeight < _fullTextHeight || textWidth < _text.maxWidth()); - bool renderElided = _breakEverywhere || heightExceeded; - if (renderElided) { - auto lineHeight = qMax(_st.style.lineHeight, _st.style.font->height); - auto lines = _st.maxHeight ? qMax(_st.maxHeight / lineHeight, 1) : ((height() / lineHeight) + 2); - _text.drawElided(p, textLeft, _st.margin.top(), textWidth, lines, _st.align, e->rect().y(), e->rect().bottom(), 0, _breakEverywhere, selection); - } else { - _text.draw(p, textLeft, _st.margin.top(), textWidth, _st.align, e->rect().y(), e->rect().bottom(), selection); - } + const auto selection = !_selection.empty() + ? _selection + : _contextMenu + ? _savedSelection + : _selection; + const auto heightExceeded = _st.maxHeight + && (_st.maxHeight < _fullTextHeight || textWidth < _text.maxWidth()); + const auto renderElided = _breakEverywhere || heightExceeded; + const auto lineHeight = qMax(_st.style.lineHeight, _st.style.font->height); + const auto lines = !renderElided + ? 0 + : _st.maxHeight + ? qMax(_st.maxHeight / lineHeight, 1) + : ((height() / lineHeight) + 2); + _text.draw(p, { + .position = { textLeft, _st.margin.top() }, + .availableWidth = textWidth, + .align = _st.align, + .clip = e->rect(), + .palette = &_st.palette, + .spoiler = Text::DefaultSpoilerCache(), + .now = crl::now(), + .selection = selection, + .elisionLines = lines, + .elisionBreakEverywhere = renderElided && _breakEverywhere, + }); } DividerLabel::DividerLabel(