Skip to content

Commit

Permalink
Dynamic speedup of view scrolling during select-drag
Browse files Browse the repository at this point in the history
When dragging to select text and crossing the view's edge, scroll slowly at first, and increasingly faster the further from the edge the mouse is moved. Impose a min/max on this speed to not get out of control (and not scroll too slowly either).
  • Loading branch information
tangledhelix committed Jan 4, 2025
1 parent 8774ea8 commit efbabcf
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/guiguts/maintext.py
Original file line number Diff line number Diff line change
Expand Up @@ -3276,7 +3276,36 @@ def _autoscroll_callback(self, event: tk.Event) -> None:
if 0 <= widget_x <= width and 0 <= widget_y <= height:
return

scroll_delay = 100
out_of_bounds_distance = 0
out_of_bounds_direction = ""

if widget_x < 0:
out_of_bounds_distance = abs(widget_x)
out_of_bounds_direction = "left"
elif widget_x > width:
out_of_bounds_distance = abs(widget_x - width)
out_of_bounds_direction = "right"
elif widget_y < 0:
out_of_bounds_distance = abs(widget_y)
out_of_bounds_direction = "up"
elif widget_y > height:
out_of_bounds_distance = abs(widget_y - height)
out_of_bounds_direction = "down"

# Attempt to "smooth scroll" by basing the delay on math rather than
# choosing some arbitrary boundaries. Enforce a minimum and maximum
# value, otherwise we start at about 5 seconds and get to ridiculous
# minimums.
scroll_delay_fastest = 100
scroll_delay_slowest = 500
scroll_delay = max(
scroll_delay_slowest,
min(scroll_delay_fastest, (5000 / out_of_bounds_distance)),
)

# Increase speed for horizontal scrolling
if out_of_bounds_direction in ("left", "right"):
scroll_delay = scroll_delay / 1.5

# Scroll in the appropriate direction (x or y).
if widget_y < 0:
Expand All @@ -3289,7 +3318,7 @@ def _autoscroll_callback(self, event: tk.Event) -> None:
event.widget.xview_scroll(1, "units")

self._on_change()
self.after(scroll_delay, lambda: self._autoscroll_callback(event))
self.after(int(scroll_delay), lambda: self._autoscroll_callback(event))


def img_from_page_mark(mark: str) -> str:
Expand Down

0 comments on commit efbabcf

Please sign in to comment.