diff options
author | Eike Ziller <eike.ziller@qt.io> | 2023-05-05 16:30:19 +0200 |
---|---|---|
committer | Eike Ziller <eike.ziller@qt.io> | 2023-05-08 10:36:19 +0000 |
commit | 5cb0a5629682731e42bd076de74c581065de1eb3 (patch) | |
tree | 2b93f8fbc855d492c808ff691ecb6e23516cad1f /src/plugins/texteditor | |
parent | ce00785efbcab3aa1cc4f3ae210ecc558f5652cd (diff) | |
download | qt-creator-5cb0a5629682731e42bd076de74c581065de1eb3.tar.gz |
Markdown: Delay update of preview
Delay the update: The implementation in Qt is currently very slow, and
while we don't have a fix, editing with immediate update is very
sluggish.
Also do not update the preview, if it isn't visible.
Change-Id: I35e10af232064b02a36a8be3758ea0bbc5b66f40
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: David Schulz <david.schulz@qt.io>
Diffstat (limited to 'src/plugins/texteditor')
-rw-r--r-- | src/plugins/texteditor/markdowneditor.cpp | 53 |
1 files changed, 38 insertions, 15 deletions
diff --git a/src/plugins/texteditor/markdowneditor.cpp b/src/plugins/texteditor/markdowneditor.cpp index 59a524226d..3cd1dfb789 100644 --- a/src/plugins/texteditor/markdowneditor.cpp +++ b/src/plugins/texteditor/markdowneditor.cpp @@ -16,6 +16,7 @@ #include <QHBoxLayout> #include <QScrollBar> #include <QTextBrowser> +#include <QTimer> #include <QToolButton> namespace TextEditor::Internal { @@ -100,8 +101,25 @@ public: } toolbarLayout->addWidget(swapViews); - connect(m_document.data(), &TextDocument::mimeTypeChanged, - m_document.data(), &TextDocument::changed); + connect(m_document.data(), + &TextDocument::mimeTypeChanged, + m_document.data(), + &TextDocument::changed); + + const auto updatePreview = [this, browser] { + QHash<QScrollBar *, int> positions; + const auto scrollBars = browser->findChildren<QScrollBar *>(); + + // save scroll positions + for (QScrollBar *scrollBar : scrollBars) + positions.insert(scrollBar, scrollBar->value()); + + browser->setMarkdown(m_document->plainText()); + + // restore scroll positions + for (QScrollBar *scrollBar : scrollBars) + scrollBar->setValue(positions.value(scrollBar)); + }; const auto viewToggled = [swapViews](QWidget *view, bool visible, QWidget *otherView, QToolButton *otherButton) { @@ -128,8 +146,12 @@ public: connect(togglePreviewVisible, &QToolButton::toggled, this, - [this, browser, toggleEditorVisible, viewToggled](bool visible) { + [this, browser, toggleEditorVisible, viewToggled, updatePreview](bool visible) { viewToggled(browser, visible, m_textEditorWidget, toggleEditorVisible); + if (visible && m_performDelayedUpdate) { + m_performDelayedUpdate = false; + updatePreview(); + } }); connect(swapViews, &QToolButton::clicked, m_textEditorWidget, [this, toolbarLayout] { @@ -152,19 +174,18 @@ public: kTextEditorRightDefault); }); - connect(m_document->document(), &QTextDocument::contentsChanged, this, [this, browser] { - QHash<QScrollBar *, int> positions; - const auto scrollBars = browser->findChildren<QScrollBar *>(); - - // save scroll positions - for (QScrollBar *scrollBar : scrollBars) - positions.insert(scrollBar, scrollBar->value()); - - browser->setMarkdown(m_document->plainText()); + // TODO directly update when we build with Qt 6.5.2 + m_previewTimer.setInterval(500); + m_previewTimer.setSingleShot(true); + connect(&m_previewTimer, &QTimer::timeout, this, [this, togglePreviewVisible, updatePreview] { + if (togglePreviewVisible->isChecked()) + updatePreview(); + else + m_performDelayedUpdate = true; + }); - // restore scroll positions - for (QScrollBar *scrollBar : scrollBars) - scrollBar->setValue(positions.value(scrollBar)); + connect(m_document->document(), &QTextDocument::contentsChanged, &m_previewTimer, [this] { + m_previewTimer.start(); }); } @@ -186,6 +207,8 @@ public: } private: + QTimer m_previewTimer; + bool m_performDelayedUpdate = false; Core::MiniSplitter *m_splitter; TextEditorWidget *m_textEditorWidget; TextDocumentPtr m_document; |