From eafd7dc818433ec435c2862bb91b7e3c7f65336b Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 4 Oct 2022 09:13:57 +0400 Subject: [PATCH] Allow spoilers and custom emoji in toasts. --- ui/toast/toast.h | 1 + ui/toast/toast_widget.cpp | 34 +++++++++++++++++++--------------- ui/toast/toast_widget.h | 1 + 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/ui/toast/toast.h b/ui/toast/toast.h index 2c0a2d5..9c2f71f 100644 --- a/ui/toast/toast.h +++ b/ui/toast/toast.h @@ -37,6 +37,7 @@ struct Config { bool dark = false; RectPart slideSide = RectPart::None; ClickHandlerFilter filter; + Fn)> textContext; }; void SetDefaultParent(not_null parent); diff --git a/ui/toast/toast_widget.cpp b/ui/toast/toast_widget.cpp index 495d7f0..9f9c861 100644 --- a/ui/toast/toast_widget.cpp +++ b/ui/toast/toast_widget.cpp @@ -38,9 +38,15 @@ Widget::Widget(QWidget *parent, const Config &config) _text.setMarkedText( _st->style, _multiline ? config.text : TextUtilities::SingleLine(config.text), - toastOptions); + toastOptions, + config.textContext ? config.textContext(this) : std::any()); + const auto weak = Ui::MakeWeak(this); + _text.setSpoilerLinkFilter([=](const ClickContext &context) { + return (weak != nullptr); + }); - if (_text.hasLinks()) { + _processMouse = _text.hasLinks() || _text.hasSpoilers(); + if (_processMouse) { setMouseTracking(true); } else { setAttribute(Qt::WA_TransparentForMouseEvents); @@ -51,9 +57,7 @@ Widget::Widget(QWidget *parent, const Config &config) } void Widget::setInputUsed(bool used) { - setAttribute( - Qt::WA_TransparentForMouseEvents, - !used && !_text.hasLinks()); + setAttribute(Qt::WA_TransparentForMouseEvents, !used && !_processMouse); } void Widget::onParentResized() { @@ -147,16 +151,16 @@ void Widget::paintEvent(QPaintEvent *e) { const auto lines = _maxTextHeight / _st->style.font->height; p.setPen(st::toastFg); - _text.drawElided( - p, - _st->padding.left(), - _textTop, - _textWidth + 1, - lines); + _text.draw(p, { + .position = { _st->padding.left(), _textTop }, + .availableWidth = _textWidth + 1, + .spoiler = Ui::Text::DefaultSpoilerCache(), + .elisionLines = lines, + }); } void Widget::leaveEventHook(QEvent *e) { - if (!_text.hasLinks()) { + if (!_processMouse) { return; } if (ClickHandler::getActive()) { @@ -167,7 +171,7 @@ void Widget::leaveEventHook(QEvent *e) { } void Widget::mouseMoveEvent(QMouseEvent *e) { - if (!_text.hasLinks()) { + if (!_processMouse) { return; } const auto point = e->pos() @@ -184,14 +188,14 @@ void Widget::mouseMoveEvent(QMouseEvent *e) { } void Widget::mousePressEvent(QMouseEvent *e) { - if (!_text.hasLinks() || e->button() != Qt::LeftButton) { + if (!_processMouse || e->button() != Qt::LeftButton) { return; } ClickHandler::pressed(); } void Widget::mouseReleaseEvent(QMouseEvent *e) { - if (!_text.hasLinks() || e->button() != Qt::LeftButton) { + if (!_processMouse || e->button() != Qt::LeftButton) { return; } if (const auto handler = ClickHandler::unpressed()) { diff --git a/ui/toast/toast_widget.h b/ui/toast/toast_widget.h index 493e5c5..1da5e19 100644 --- a/ui/toast/toast_widget.h +++ b/ui/toast/toast_widget.h @@ -44,6 +44,7 @@ private: float64 _shownLevel = 0; bool _multiline = false; bool _dark = false; + bool _processMouse = false; int _maxTextWidth = 0; int _maxTextHeight = 0;