Commit f2016388 authored by aa bb's avatar aa bb Committed by Christoph Cullmann

Preview addon: Better update delay logic.

While typing, update very 1000ms (slow), but after stopping typing,
update quickly after 150ms (fast).
parent 2ce0f42e
...@@ -37,17 +37,14 @@ ...@@ -37,17 +37,14 @@
using namespace KTextEditorPreview; using namespace KTextEditorPreview;
// 300 ms as initial proposal, was found to be delay which worked for the // There are two timers that run on update. One timer is fast, but is
// author with the use-case of quickly peeking over to the preview while // cancelled each time a new updated comes in. Another timer is slow, but is
// typing to see if things are working out as intended, without getting a // not cancelled if another update comes in. With this, "while typing", the
// having-to-wait feeling. // preview is updated every 1000ms, thus one sees that something is happening
// Surely could get some more serious research what is a proper (default) value. // from the corner of one's eyes. After stopping typing, the preview is
// Perhaps the whole update logic cpuld also be overhauled, to only do an // updated quickly after 150ms so that the preview has the newest version.
// update once there was at least xxx ms idle time to meet the use-case of static const int updateDelayFast = 150; // ms
// quickly-peeking-over. And otherwise update in bigger intervals of static const int updateDelaySlow = 1000; // ms
// 500-2000(?) ms, to cover the use-case of seeing from the corner of one's
// eye that something is changing while one is editing the sources.
static const int updateDelay = 300; // ms
KPartView::KPartView(const KService::Ptr &service, QObject *parent) KPartView::KPartView(const KService::Ptr &service, QObject *parent)
: QObject(parent) : QObject(parent)
...@@ -62,9 +59,13 @@ KPartView::KPartView(const KService::Ptr &service, QObject *parent) ...@@ -62,9 +59,13 @@ KPartView::KPartView(const KService::Ptr &service, QObject *parent)
delete m_part; delete m_part;
m_errorLabel = new QLabel(QStringLiteral("KPart provides no widget.")); m_errorLabel = new QLabel(QStringLiteral("KPart provides no widget."));
} else { } else {
m_updateSquashingTimer.setSingleShot(true); m_updateSquashingTimerFast.setSingleShot(true);
m_updateSquashingTimer.setInterval(updateDelay); m_updateSquashingTimerFast.setInterval(updateDelayFast);
connect(&m_updateSquashingTimer, &QTimer::timeout, this, &KPartView::updatePreview); connect(&m_updateSquashingTimerFast, &QTimer::timeout, this, &KPartView::updatePreview);
m_updateSquashingTimerSlow.setSingleShot(true);
m_updateSquashingTimerSlow.setInterval(updateDelaySlow);
connect(&m_updateSquashingTimerSlow, &QTimer::timeout, this, &KPartView::updatePreview);
auto browserExtension = m_part->browserExtension(); auto browserExtension = m_part->browserExtension();
if (browserExtension) { if (browserExtension) {
...@@ -110,7 +111,8 @@ void KPartView::setDocument(KTextEditor::Document *document) ...@@ -110,7 +111,8 @@ void KPartView::setDocument(KTextEditor::Document *document)
if (m_document) { if (m_document) {
disconnect(m_document, &KTextEditor::Document::textChanged, this, &KPartView::triggerUpdatePreview); disconnect(m_document, &KTextEditor::Document::textChanged, this, &KPartView::triggerUpdatePreview);
m_updateSquashingTimer.stop(); m_updateSquashingTimerFast.stop();
m_updateSquashingTimerSlow.stop();
} }
m_document = document; m_document = document;
...@@ -143,21 +145,28 @@ void KPartView::setAutoUpdating(bool autoUpdating) ...@@ -143,21 +145,28 @@ void KPartView::setAutoUpdating(bool autoUpdating)
updatePreview(); updatePreview();
} }
} else { } else {
m_updateSquashingTimer.stop(); m_updateSquashingTimerSlow.stop();
m_updateSquashingTimerFast.stop();
} }
} }
void KPartView::triggerUpdatePreview() void KPartView::triggerUpdatePreview()
{ {
m_previewDirty = true; m_previewDirty = true;
if (m_part->widget()->isVisible() && m_autoUpdating && !m_updateSquashingTimer.isActive()) { if (m_part->widget()->isVisible() && m_autoUpdating) {
m_updateSquashingTimer.start(); // Reset fast timer each time
m_updateSquashingTimerFast.start();
// Start slow timer, if not already running (don't reset!)
if(!m_updateSquashingTimerSlow.isActive())
m_updateSquashingTimerSlow.start();
} }
} }
void KPartView::updatePreview() void KPartView::updatePreview()
{ {
m_updateSquashingTimerSlow.stop();
m_updateSquashingTimerFast.stop();
if (!m_part->widget()->isVisible()) { if (!m_part->widget()->isVisible()) {
return; return;
} }
......
...@@ -118,7 +118,8 @@ private: ...@@ -118,7 +118,8 @@ private:
bool m_autoUpdating = true; bool m_autoUpdating = true;
bool m_previewDirty = true; bool m_previewDirty = true;
QTimer m_updateSquashingTimer; QTimer m_updateSquashingTimerFast;
QTimer m_updateSquashingTimerSlow;
QTemporaryFile *m_bufferFile = nullptr; QTemporaryFile *m_bufferFile = nullptr;
}; };
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment