Skip to content

Commit

Permalink
Improve perceived performance (#100)
Browse files Browse the repository at this point in the history
Calculate the delay of the HTML processing based on the
number of characters in the markdown document instead of
a hardcode value of 100 ms.
  • Loading branch information
cloose committed Jan 13, 2014
1 parent 2af71ab commit fa3e167
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
16 changes: 14 additions & 2 deletions app/htmlpreviewgenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ void HtmlPreviewGenerator::run()
return;
}

// delay processing by 100 ms to see if more tasks are coming
// delay processing to see if more tasks are coming
// (e.g. because the user is typing fast)
this->msleep(100);
this->msleep(calculateDelay(text));

// no more new tasks?
if (tasks.isEmpty()) {
Expand Down Expand Up @@ -243,3 +243,15 @@ MarkdownConverter::ConverterOptions HtmlPreviewGenerator::converterOptions() con

return parserOptionFlags;
}

int HtmlPreviewGenerator::calculateDelay(const QString &text)
{
const int MINIMUM_DELAY = 50;
const int MAXIMUM_DELAY = 2000;

// calculate the processing delay based on amount of characters in the
// markdown text. The delay is restricted to the interval [50, 2000] milliseconds;
int delay = qMin(qMax(text.size() / 100, MINIMUM_DELAY), MAXIMUM_DELAY);

return delay;
}
1 change: 1 addition & 0 deletions app/htmlpreviewgenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public slots:
QString renderTemplate(const QString &header, const QString &body);
QString buildHtmlHeader() const;
MarkdownConverter::ConverterOptions converterOptions() const;
int calculateDelay(const QString &text);

private:
Options *options;
Expand Down

0 comments on commit fa3e167

Please sign in to comment.