From ec76a4c0bb9131ab14fe676630a1f465bfc05ee2 Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 6 Jul 2022 10:09:10 +0400 Subject: [PATCH] Optimize custom emoji repaints in the InputField. --- ui/widgets/input_fields.cpp | 18 +++++++++++++++++- ui/widgets/input_fields.h | 3 +++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/ui/widgets/input_fields.cpp b/ui/widgets/input_fields.cpp index 75e2e34..8a87c91 100644 --- a/ui/widgets/input_fields.cpp +++ b/ui/widgets/input_fields.cpp @@ -963,6 +963,9 @@ protected: void inputMethodEvent(QInputMethodEvent *e) override { return outer()->inputMethodEventInner(e); } + void paintEvent(QPaintEvent *e) override { + return outer()->paintEventInner(e); + } bool canInsertFromMimeData(const QMimeData *source) const override { return outer()->canInsertFromMimeDataInner(source); @@ -1664,13 +1667,26 @@ void InputField::setCustomEmojiFactory( Fn paused) { _customEmojiObject = std::make_unique([=]( QStringView data) { - return factory(data, [=] { _inner->update(); }); + return factory(data, [=] { customEmojiRepaint(); }); }, std::move(paused)); _inner->document()->documentLayout()->registerHandler( kCustomEmojiFormat, _customEmojiObject.get()); } +void InputField::customEmojiRepaint() { + if (_customEmojiRepaintScheduled) { + return; + } + _customEmojiRepaintScheduled = true; + _inner->update(); +} + +void InputField::paintEventInner(QPaintEvent *e) { + _customEmojiRepaintScheduled = false; + _inner->QTextEdit::paintEvent(e); +} + void InputField::setAdditionalMargin(int margin) { _additionalMargin = margin; QResizeEvent e(size(), size()); diff --git a/ui/widgets/input_fields.h b/ui/widgets/input_fields.h index 67fffed..a80e24c 100644 --- a/ui/widgets/input_fields.h +++ b/ui/widgets/input_fields.h @@ -442,6 +442,7 @@ private: void contextMenuEventInner(QContextMenuEvent *e, QMenu *m = nullptr); void dropEventInner(QDropEvent *e); void inputMethodEventInner(QInputMethodEvent *e); + void paintEventInner(QPaintEvent *e); QMimeData *createMimeDataFromSelectionInner() const; bool canInsertFromMimeDataInner(const QMimeData *source) const; @@ -513,6 +514,7 @@ private: bool revertFormatReplace(); + void customEmojiRepaint(); void highlightMarkdown(); const style::InputField &_st; @@ -536,6 +538,7 @@ private: bool _forcePlaceholderHidden = false; bool _reverseMarkdownReplacement = false; + bool _customEmojiRepaintScheduled = false; // Tags list which we should apply while setText() call or insert from mime data. TagList _insertedTags;