Explorar o código

delay to preview after code/math blocks update to avoid blink

Le Tan %!s(int64=4) %!d(string=hai) anos
pai
achega
c50f76c40a
Modificáronse 2 ficheiros con 46 adicións e 4 borrados
  1. 34 4
      src/widgets/editors/previewhelper.cpp
  2. 12 0
      src/widgets/editors/previewhelper.h

+ 34 - 4
src/widgets/editors/previewhelper.cpp

@@ -3,6 +3,7 @@
 #include <QDebug>
 #include <QTextDocument>
 #include <QTextBlock>
+#include <QTimer>
 
 #include <vtextedit/pegmarkdownhighlighterdata.h>
 #include <vtextedit/texteditorconfig.h>
@@ -123,6 +124,19 @@ PreviewHelper::PreviewHelper(MarkdownEditor *p_editor, QObject *p_parent)
       m_mathBlockCache(100, nullptr)
 {
     setMarkdownEditor(p_editor);
+
+    const int interval = 1000;
+    m_codeBlockTimer = new QTimer(this);
+    m_codeBlockTimer->setSingleShot(true);
+    m_codeBlockTimer->setInterval(interval);
+    connect(m_codeBlockTimer, &QTimer::timeout,
+            this, &PreviewHelper::handleCodeBlocksUpdate);
+
+    m_mathBlockTimer = new QTimer(this);
+    m_mathBlockTimer->setSingleShot(true);
+    m_mathBlockTimer->setInterval(interval);
+    connect(m_mathBlockTimer, &QTimer::timeout,
+            this, &PreviewHelper::handleMathBlocksUpdate);
 }
 
 void PreviewHelper::codeBlocksUpdated(vte::TimeStamp p_timeStamp,
@@ -133,13 +147,19 @@ void PreviewHelper::codeBlocksUpdated(vte::TimeStamp p_timeStamp,
         return;
     }
 
+    m_pendingCodeBlocks = p_codeBlocks;
+    m_codeBlockTimer->start();
+}
+
+void PreviewHelper::handleCodeBlocksUpdate()
+{
     ++m_codeBlockTimeStamp;
     m_codeBlocksData.clear();
 
     QVector<int> needPreviewBlocks;
 
-    for (int i = 0; i < p_codeBlocks.size(); ++i) {
-        const auto &cb = p_codeBlocks[i];
+    for (int i = 0; i < m_pendingCodeBlocks.size(); ++i) {
+        const auto &cb = m_pendingCodeBlocks[i];
 
         const auto needPreview = isLangNeedPreview(cb.m_lang);
         if (!needPreview.first && !needPreview.second) {
@@ -172,6 +192,8 @@ void PreviewHelper::codeBlocksUpdated(vte::TimeStamp p_timeStamp,
     }
 
     updateEditorInplacePreviewCodeBlock();
+
+    m_pendingCodeBlocks.clear();
 }
 
 bool PreviewHelper::checkPreviewSourceLang(SourceFlag p_flag, const QString &p_lang) const
@@ -337,13 +359,19 @@ void PreviewHelper::mathBlocksUpdated(const QVector<vte::peg::MathBlock> &p_math
         return;
     }
 
+    m_pendingMathBlocks = p_mathBlocks;
+    m_mathBlockTimer->start();
+}
+
+void PreviewHelper::handleMathBlocksUpdate()
+{
     ++m_mathBlockTimeStamp;
     m_mathBlocksData.clear();
-    m_mathBlocksData.reserve(p_mathBlocks.size());
+    m_mathBlocksData.reserve(m_pendingMathBlocks.size());
 
     bool needUpdateEditorInplacePreview = true;
 
-    for (const auto &mb : p_mathBlocks) {
+    for (const auto &mb : m_pendingMathBlocks) {
         m_mathBlocksData.append(MathBlockPreviewData(mb));
         const int blockPreviewIdx = m_mathBlocksData.size() - 1;
 
@@ -368,6 +396,8 @@ void PreviewHelper::mathBlocksUpdated(const QVector<vte::peg::MathBlock> &p_math
     if (needUpdateEditorInplacePreview) {
         updateEditorInplacePreviewMathBlock();
     }
+
+    m_pendingMathBlocks.clear();
 }
 
 void PreviewHelper::inplacePreviewMathBlock(int p_blockPreviewIdx)

+ 12 - 0
src/widgets/editors/previewhelper.h

@@ -194,6 +194,10 @@ namespace vnotex
 
         bool needForcedBackground(const QString &p_lang) const;
 
+        void handleCodeBlocksUpdate();
+
+        void handleMathBlocksUpdate();
+
         MarkdownEditor *m_editor = nullptr;
 
         QTextDocument *m_document = nullptr;
@@ -229,6 +233,14 @@ namespace vnotex
         bool m_webPlantUmlEnabled = true;
 
         bool m_webGraphvizEnabled = true;
+
+        QVector<vte::peg::FencedCodeBlock> m_pendingCodeBlocks;
+
+        QTimer *m_codeBlockTimer = nullptr;
+
+        QVector<vte::peg::MathBlock> m_pendingMathBlocks;
+
+        QTimer *m_mathBlockTimer = nullptr;
     };
 }