Allow spoilers and custom emoji in toasts.

This commit is contained in:
John Preston 2022-10-04 09:13:57 +04:00
parent 89ae115a87
commit eafd7dc818
3 changed files with 21 additions and 15 deletions

View file

@ -37,6 +37,7 @@ struct Config {
bool dark = false; bool dark = false;
RectPart slideSide = RectPart::None; RectPart slideSide = RectPart::None;
ClickHandlerFilter filter; ClickHandlerFilter filter;
Fn<std::any(not_null<QWidget*>)> textContext;
}; };
void SetDefaultParent(not_null<QWidget*> parent); void SetDefaultParent(not_null<QWidget*> parent);

View file

@ -38,9 +38,15 @@ Widget::Widget(QWidget *parent, const Config &config)
_text.setMarkedText( _text.setMarkedText(
_st->style, _st->style,
_multiline ? config.text : TextUtilities::SingleLine(config.text), _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); setMouseTracking(true);
} else { } else {
setAttribute(Qt::WA_TransparentForMouseEvents); setAttribute(Qt::WA_TransparentForMouseEvents);
@ -51,9 +57,7 @@ Widget::Widget(QWidget *parent, const Config &config)
} }
void Widget::setInputUsed(bool used) { void Widget::setInputUsed(bool used) {
setAttribute( setAttribute(Qt::WA_TransparentForMouseEvents, !used && !_processMouse);
Qt::WA_TransparentForMouseEvents,
!used && !_text.hasLinks());
} }
void Widget::onParentResized() { void Widget::onParentResized() {
@ -147,16 +151,16 @@ void Widget::paintEvent(QPaintEvent *e) {
const auto lines = _maxTextHeight / _st->style.font->height; const auto lines = _maxTextHeight / _st->style.font->height;
p.setPen(st::toastFg); p.setPen(st::toastFg);
_text.drawElided( _text.draw(p, {
p, .position = { _st->padding.left(), _textTop },
_st->padding.left(), .availableWidth = _textWidth + 1,
_textTop, .spoiler = Ui::Text::DefaultSpoilerCache(),
_textWidth + 1, .elisionLines = lines,
lines); });
} }
void Widget::leaveEventHook(QEvent *e) { void Widget::leaveEventHook(QEvent *e) {
if (!_text.hasLinks()) { if (!_processMouse) {
return; return;
} }
if (ClickHandler::getActive()) { if (ClickHandler::getActive()) {
@ -167,7 +171,7 @@ void Widget::leaveEventHook(QEvent *e) {
} }
void Widget::mouseMoveEvent(QMouseEvent *e) { void Widget::mouseMoveEvent(QMouseEvent *e) {
if (!_text.hasLinks()) { if (!_processMouse) {
return; return;
} }
const auto point = e->pos() const auto point = e->pos()
@ -184,14 +188,14 @@ void Widget::mouseMoveEvent(QMouseEvent *e) {
} }
void Widget::mousePressEvent(QMouseEvent *e) { void Widget::mousePressEvent(QMouseEvent *e) {
if (!_text.hasLinks() || e->button() != Qt::LeftButton) { if (!_processMouse || e->button() != Qt::LeftButton) {
return; return;
} }
ClickHandler::pressed(); ClickHandler::pressed();
} }
void Widget::mouseReleaseEvent(QMouseEvent *e) { void Widget::mouseReleaseEvent(QMouseEvent *e) {
if (!_text.hasLinks() || e->button() != Qt::LeftButton) { if (!_processMouse || e->button() != Qt::LeftButton) {
return; return;
} }
if (const auto handler = ClickHandler::unpressed()) { if (const auto handler = ClickHandler::unpressed()) {

View file

@ -44,6 +44,7 @@ private:
float64 _shownLevel = 0; float64 _shownLevel = 0;
bool _multiline = false; bool _multiline = false;
bool _dark = false; bool _dark = false;
bool _processMouse = false;
int _maxTextWidth = 0; int _maxTextWidth = 0;
int _maxTextHeight = 0; int _maxTextHeight = 0;