Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 70 additions & 2 deletions ReText/syncscroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ class SyncScroll:

def __init__(self, previewFrame,
editorPositionToSourceLineFunc,
sourceLineToEditorPositionFunc):
sourceLineToEditorPositionFunc,
setEditorScrollValueFunc=None):
self.posmap = {}
self.frame = previewFrame
self.editorPositionToSourceLine = editorPositionToSourceLineFunc
self.sourceLineToEditorPosition = sourceLineToEditorPositionFunc
# Optional callback to set the editor vertical scroll value (in pixels)
self._setEditorScrollValue = setEditorScrollValueFunc

self.previewPositionBeforeLoad = QPoint()
self.contentIsLoading = False
Expand All @@ -34,6 +37,10 @@ def __init__(self, previewFrame,
self.editorViewportOffset = 0
self.editorCursorPosition = 0

# Guards to prevent recursive scroll feedback loops
self._updating_preview = False
self._updating_editor = False

self.frame.contentsSizeChanged.connect(self._handlePreviewResized)
self.frame.loadStarted.connect(self._handleLoadStarted)
self.frame.loadFinished.connect(self._handleLoadFinished)
Expand All @@ -46,6 +53,10 @@ def handleEditorResized(self, editorViewportHeight):
self._updatePreviewScrollPosition()

def handleEditorScrolled(self, editorViewportOffset):
# If we are programmatically updating the editor due to preview scroll,
# ignore this event to avoid feedback loops.
if self._updating_editor:
return
self.editorViewportOffset = editorViewportOffset
return self._updatePreviewScrollPosition()

Expand Down Expand Up @@ -137,9 +148,66 @@ def _updatePreviewScrollPosition(self):

pos = self.frame.scrollPosition()
pos.setY(preview_scroll_offset)
self.frame.setScrollPosition(pos)
# Prevent preview→editor feedback while we adjust preview scroll
self._updating_preview = True
try:
self.frame.setScrollPosition(pos)
finally:
self._updating_preview = False

def _setPositionMap(self, posmap):
self.posmap = posmap
if posmap:
self.posmap[0] = 0

def handlePreviewScrolled(self, previewScrollPosition):
"""
Update editor scroll position based on preview scroll position.

previewScrollPosition can be either a QPointF/QPoint or a numeric Y value.
"""
if not self._setEditorScrollValue:
return
# Avoid reacting to our own preview updates
if self._updating_preview:
return
if not self.posmap:
return

# Extract Y coordinate
try:
preview_y = previewScrollPosition.y()
except AttributeError:
preview_y = float(previewScrollPosition)

# Binary search using line numbers to find nearest posmap values
posmap_lines = [0] + sorted(self.posmap.keys())
min_index = 0
max_index = len(posmap_lines) - 1
while max_index - min_index > 1:
current_index = int((min_index + max_index) / 2)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use the // operator here, I guess?

Suggested change
current_index = int((min_index + max_index) / 2)
current_index = (min_index + max_index) // 2

current_line = posmap_lines[current_index]
if self.posmap[current_line] > preview_y:
max_index = current_index
else:
min_index = current_index

min_line = posmap_lines[min_index]
max_line = posmap_lines[max_index]

min_preview_pos = self.posmap[min_line]
max_preview_pos = self.posmap[max_line]

min_textedit_pos = self.sourceLineToEditorPosition(min_line)
max_textedit_pos = self.sourceLineToEditorPosition(max_line)

editor_scroll_to = self._linearScale(preview_y,
min_preview_pos, max_preview_pos,
min_textedit_pos, max_textedit_pos)

# Apply editor scroll with guard to avoid triggering editor→preview update
self._updating_editor = True
try:
self._setEditorScrollValue(int(editor_scroll_to))
finally:
self._updating_editor = False
16 changes: 13 additions & 3 deletions ReText/webenginepreview.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,13 @@ def __init__(self, tab,

self.setPage(webPage)

self.syncscroll = SyncScroll(webPage,
editorPositionToSourceLineFunc,
sourceLineToEditorPositionFunc)
self.editBox = tab.editBox
self.syncscroll = SyncScroll(
webPage,
editorPositionToSourceLineFunc,
sourceLineToEditorPositionFunc,
setEditorScrollValueFunc=self.editBox.verticalScrollBar().setValue,
)

settings = self.settings()
settings.setDefaultTextEncoding('utf-8')
Expand All @@ -172,6 +175,10 @@ def __init__(self, tab,
self.editBox.verticalScrollBar().valueChanged.connect(self.syncscroll.handleEditorScrolled)
self.editBox.resized.connect(self._handleEditorResized)

# When preview is scrolled, update the editor scroll accordingly
if hasattr(webPage, 'scrollPositionChanged'):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This signal was introduced in Qt 5.7, and we require Qt 6. Maybe the if is not needed?

And the same below on line 236.

webPage.scrollPositionChanged.connect(self.syncscroll.handlePreviewScrolled)

# Scroll the preview when the mouse wheel is used to scroll
# beyond the beginning/end of the editor
self.editBox.scrollLimitReached.connect(self._handleWheelEvent)
Expand Down Expand Up @@ -225,6 +232,9 @@ def disconnectExternalSignals(self):
self.editBox.resized.disconnect(self._handleEditorResized)

self.editBox.scrollLimitReached.disconnect(self._handleWheelEvent)
# Disconnect preview scroll synchronization
if hasattr(self.page(), 'scrollPositionChanged'):
self.page().scrollPositionChanged.disconnect(self.syncscroll.handlePreviewScrolled)

def _handleCursorPositionChanged(self):
editorCursorPosition = self.editBox.verticalScrollBar().value() + \
Expand Down