forked from qt-creator/qt-creator
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>
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
#include <QHBoxLayout>
|
#include <QHBoxLayout>
|
||||||
#include <QScrollBar>
|
#include <QScrollBar>
|
||||||
#include <QTextBrowser>
|
#include <QTextBrowser>
|
||||||
|
#include <QTimer>
|
||||||
#include <QToolButton>
|
#include <QToolButton>
|
||||||
|
|
||||||
namespace TextEditor::Internal {
|
namespace TextEditor::Internal {
|
||||||
@@ -100,8 +101,25 @@ public:
|
|||||||
}
|
}
|
||||||
toolbarLayout->addWidget(swapViews);
|
toolbarLayout->addWidget(swapViews);
|
||||||
|
|
||||||
connect(m_document.data(), &TextDocument::mimeTypeChanged,
|
connect(m_document.data(),
|
||||||
m_document.data(), &TextDocument::changed);
|
&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 =
|
const auto viewToggled =
|
||||||
[swapViews](QWidget *view, bool visible, QWidget *otherView, QToolButton *otherButton) {
|
[swapViews](QWidget *view, bool visible, QWidget *otherView, QToolButton *otherButton) {
|
||||||
@@ -128,8 +146,12 @@ public:
|
|||||||
connect(togglePreviewVisible,
|
connect(togglePreviewVisible,
|
||||||
&QToolButton::toggled,
|
&QToolButton::toggled,
|
||||||
this,
|
this,
|
||||||
[this, browser, toggleEditorVisible, viewToggled](bool visible) {
|
[this, browser, toggleEditorVisible, viewToggled, updatePreview](bool visible) {
|
||||||
viewToggled(browser, visible, m_textEditorWidget, toggleEditorVisible);
|
viewToggled(browser, visible, m_textEditorWidget, toggleEditorVisible);
|
||||||
|
if (visible && m_performDelayedUpdate) {
|
||||||
|
m_performDelayedUpdate = false;
|
||||||
|
updatePreview();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
connect(swapViews, &QToolButton::clicked, m_textEditorWidget, [this, toolbarLayout] {
|
connect(swapViews, &QToolButton::clicked, m_textEditorWidget, [this, toolbarLayout] {
|
||||||
@@ -152,19 +174,18 @@ public:
|
|||||||
kTextEditorRightDefault);
|
kTextEditorRightDefault);
|
||||||
});
|
});
|
||||||
|
|
||||||
connect(m_document->document(), &QTextDocument::contentsChanged, this, [this, browser] {
|
// TODO directly update when we build with Qt 6.5.2
|
||||||
QHash<QScrollBar *, int> positions;
|
m_previewTimer.setInterval(500);
|
||||||
const auto scrollBars = browser->findChildren<QScrollBar *>();
|
m_previewTimer.setSingleShot(true);
|
||||||
|
connect(&m_previewTimer, &QTimer::timeout, this, [this, togglePreviewVisible, updatePreview] {
|
||||||
|
if (togglePreviewVisible->isChecked())
|
||||||
|
updatePreview();
|
||||||
|
else
|
||||||
|
m_performDelayedUpdate = true;
|
||||||
|
});
|
||||||
|
|
||||||
// save scroll positions
|
connect(m_document->document(), &QTextDocument::contentsChanged, &m_previewTimer, [this] {
|
||||||
for (QScrollBar *scrollBar : scrollBars)
|
m_previewTimer.start();
|
||||||
positions.insert(scrollBar, scrollBar->value());
|
|
||||||
|
|
||||||
browser->setMarkdown(m_document->plainText());
|
|
||||||
|
|
||||||
// restore scroll positions
|
|
||||||
for (QScrollBar *scrollBar : scrollBars)
|
|
||||||
scrollBar->setValue(positions.value(scrollBar));
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,6 +207,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
QTimer m_previewTimer;
|
||||||
|
bool m_performDelayedUpdate = false;
|
||||||
Core::MiniSplitter *m_splitter;
|
Core::MiniSplitter *m_splitter;
|
||||||
TextEditorWidget *m_textEditorWidget;
|
TextEditorWidget *m_textEditorWidget;
|
||||||
TextDocumentPtr m_document;
|
TextDocumentPtr m_document;
|
||||||
|
|||||||
Reference in New Issue
Block a user