Edit book: Make cursor movement smooth by not highlighting matching tags while the cursor is moving. Only match highlighting tags if the cursor stays still for a time.

This commit is contained in:
Kovid Goyal 2014-04-30 15:43:55 +05:30
parent 16bee93353
commit 7c67e314d9

View File

@ -14,7 +14,7 @@ import regex
from PyQt4.Qt import ( from PyQt4.Qt import (
QPlainTextEdit, QFontDatabase, QToolTip, QPalette, QFont, QKeySequence, QPlainTextEdit, QFontDatabase, QToolTip, QPalette, QFont, QKeySequence,
QTextEdit, QTextFormat, QWidget, QSize, QPainter, Qt, QRect, pyqtSlot, QTextEdit, QTextFormat, QWidget, QSize, QPainter, Qt, QRect, pyqtSlot,
QApplication, QMimeData, QColor, QColorDialog) QApplication, QMimeData, QColor, QColorDialog, QTimer)
from calibre import prepare_string_for_xml, xml_entity_to_unicode from calibre import prepare_string_for_xml, xml_entity_to_unicode
from calibre.gui2.tweak_book import tprefs, TOP from calibre.gui2.tweak_book import tprefs, TOP
@ -135,6 +135,8 @@ class TextEdit(PlainTextEdit):
self.smarts = NullSmarts(self) self.smarts = NullSmarts(self)
self.current_cursor_line = None self.current_cursor_line = None
self.current_search_mark = None self.current_search_mark = None
self.smarts_highlight_timer = t = QTimer()
t.setInterval(750), t.setSingleShot(True), t.timeout.connect(self.update_extra_selections)
self.highlighter = SyntaxHighlighter() self.highlighter = SyntaxHighlighter()
self.line_number_area = LineNumbers(self) self.line_number_area = LineNumbers(self)
self.apply_settings() self.apply_settings()
@ -252,13 +254,16 @@ class TextEdit(PlainTextEdit):
self.setTextCursor(c) self.setTextCursor(c)
self.ensureCursorVisible() self.ensureCursorVisible()
def update_extra_selections(self): def update_extra_selections(self, instant=True):
sel = [] sel = []
if self.current_cursor_line is not None: if self.current_cursor_line is not None:
sel.append(self.current_cursor_line) sel.append(self.current_cursor_line)
if self.current_search_mark is not None: if self.current_search_mark is not None:
sel.append(self.current_search_mark) sel.append(self.current_search_mark)
sel.extend(self.smarts.get_extra_selections(self)) if instant:
sel.extend(self.smarts.get_extra_selections(self))
else:
self.smarts_highlight_timer.start()
self.setExtraSelections(sel) self.setExtraSelections(sel)
# Search and replace {{{ # Search and replace {{{
@ -456,7 +461,7 @@ class TextEdit(PlainTextEdit):
sel.cursor = self.textCursor() sel.cursor = self.textCursor()
sel.cursor.clearSelection() sel.cursor.clearSelection()
self.current_cursor_line = sel self.current_cursor_line = sel
self.update_extra_selections() self.update_extra_selections(instant=False)
# Update the cursor line's line number in the line number area # Update the cursor line's line number in the line number area
try: try:
self.line_number_area.update(0, self.last_current_lnum[0], self.line_number_area.width(), self.last_current_lnum[1]) self.line_number_area.update(0, self.last_current_lnum[0], self.line_number_area.width(), self.last_current_lnum[1])