Skip to content

fix(macos): auto-scroll when dragging selection beyond viewport#28

Open
yyanezh wants to merge 3 commits into
manaflow-ai:mainfrom
yyanezh:fix/autoscroll-drag-selection
Open

fix(macos): auto-scroll when dragging selection beyond viewport#28
yyanezh wants to merge 3 commits into
manaflow-ai:mainfrom
yyanezh:fix/autoscroll-drag-selection

Conversation

@yyanezh
Copy link
Copy Markdown

@yyanezh yyanezh commented Apr 7, 2026

Summary

  • When selecting text by dragging, moving the cursor above or below the terminal viewport now auto-scrolls so users can select text beyond the visible area
  • Previously mouseDragged only forwarded the position to Ghostty core with no scroll behavior — selecting was limited to the currently visible content

Implementation

  • Added a repeating timer (50ms) in mouseDragged that sends scroll events + updates selection position while the cursor is outside the view bounds
  • Timer direction changes correctly when dragging switches from up to down (or vice versa)
  • Timer stops on mouseUp or when cursor returns to the viewport

Test plan

  • Select text by clicking and dragging down past the bottom edge — terminal should scroll down while extending selection
  • Same for dragging up past the top edge — should scroll up
  • Drag down past edge, then move cursor back up past top edge without releasing — scroll direction should reverse
  • Release mouse button — scrolling should stop immediately
  • Normal drag selection within viewport should work as before

Summary by cubic

Enables auto-scroll when dragging a text selection past the top or bottom of the terminal on macOS, letting you select beyond the visible area. Fixes the previous limit to on-screen content.

  • Bug Fixes

    • Start auto-scroll during drag when the cursor exits the viewport; extend selection as it scrolls.
    • Reverse direction when crossing edges; stop on mouse up, re-entering the view, or lifecycle cleanup.
    • Use a 50ms timer to send scroll and position updates; don't restart if direction is unchanged; in-viewport drags are unchanged.
  • Refactors

    • Added docstrings to auto-scroll helpers for clarity and future maintenance.

Written for commit f0a063a. Summary will update on new commits.

Summary by CodeRabbit

  • New Features
    • Enhanced macOS text selection with auto-scrolling when dragging beyond the visible top or bottom, allowing seamless selection across the document. Auto-scroll smoothly continues selection while scrolling and adapts when scrolling direction changes.
  • Bug Fixes
    • Auto-scroll reliably stops when the drag ends or the view is closed, preventing unintended scrolling.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 7, 2026

Warning

Rate limit exceeded

@yyanezh has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 4 minutes and 23 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 4 minutes and 23 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5f3223b3-1d1b-49d7-92f0-a76a3232c841

📥 Commits

Reviewing files that changed from the base of the PR and between 2782cf5 and f0a063a.

📒 Files selected for processing (1)
  • macos/Sources/Ghostty/Surface View/SurfaceView_AppKit.swift
📝 Walkthrough

Walkthrough

Adds drag-selection auto-scrolling to the macOS SurfaceView by tracking per-view state (autoScrollTimer, autoScrollUp, lastDragEvent), starting a repeating timer when the cursor is dragged outside vertical bounds, and emitting synthetic scroll and mouse-position events until dragging stops.

Changes

Cohort / File(s) Summary
Drag-Selection Auto-Scroll
macos/Sources/Ghostty/Surface View/SurfaceView_AppKit.swift
Added per-view state (autoScrollTimer, autoScrollUp, lastDragEvent). Updated mouseDragged to detect out-of-bounds cursor and start/stop auto-scroll. Implemented startAutoScroll(scrollUp:) to run a repeating Timer that emits synthetic MouseScrollEvent and re-sends last MousePosEvent. Added stopAutoScroll() and ensured it is called on mouseUp and deinit.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰
A hop, a drag beyond the line,
Timers hum and markers shine,
Scrolls and positions hand in paw,
Selection grows, the view withdraws—
A tiny hare applauds the draw.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 11.11% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: adding auto-scroll functionality when dragging selection beyond the viewport bounds on macOS.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented Apr 7, 2026

Greptile Summary

This PR adds auto-scroll during drag selection on macOS by starting a 50 ms repeating timer when the cursor exits the viewport. There is a P1 ordering bug: lastDragEvent is assigned before startAutoScroll is called, but startAutoScroll unconditionally invokes stopAutoScroll(), which immediately clears lastDragEvent to nil — so whenever the cursor is held still outside the viewport the timer fires indefinitely without ever calling sendMousePos, meaning the selection never extends.

  • The terminal scrolls correctly, but the selection endpoint is not updated when the cursor is stationary outside the viewport.
  • Moving lastDragEvent = event to after the startAutoScroll call fixes the ordering."

Confidence Score: 4/5

Not safe to merge as-is — P1 ordering bug means the selection-extension half of the feature is broken for any cursor held still outside the viewport.

One clear P1 defect: lastDragEvent is cleared by stopAutoScroll() inside startAutoScroll before the timer starts, so sendMousePos is never called for a stationary cursor. Scrolling works, but the core selection-extension behavior does not. Score is 4 rather than lower because the fix is a single-line reordering and the rest of the implementation is sound.

macos/Sources/Ghostty/Surface View/SurfaceView_AppKit.swift — specifically the ordering of lastDragEvent = event relative to the startAutoScroll call in mouseDragged.

Important Files Changed

Filename Overview
macos/Sources/Ghostty/Surface View/SurfaceView_AppKit.swift Adds auto-scroll timer for drag selection; P1 ordering bug causes lastDragEvent to always be nil on timer fire when cursor is stationary outside the viewport, breaking selection extension.

Sequence Diagram

sequenceDiagram
    participant User
    participant mouseDragged
    participant startAutoScroll
    participant stopAutoScroll
    participant Timer
    participant Core as Ghostty Core

    User->>mouseDragged: drag cursor below viewport
    mouseDragged->>mouseDragged: lastDragEvent = event (line 990)
    mouseDragged->>startAutoScroll: startAutoScroll(scrollUp: false)
    startAutoScroll->>stopAutoScroll: stopAutoScroll() (unconditional)
    stopAutoScroll->>stopAutoScroll: lastDragEvent = nil ❌
    startAutoScroll->>Timer: scheduledTimer(0.05s, repeating)
    Note over Timer: 50 ms passes
    Timer->>Core: sendMouseScroll(y: -1.0) ✅
    Timer->>Timer: if let lastEvent = lastDragEvent → nil, skip
    Note over Core: sendMousePos never called ❌
    Note over Core: Selection does not extend

    Note over mouseDragged,Timer: Fix: move lastDragEvent = event AFTER startAutoScroll
    mouseDragged->>startAutoScroll: startAutoScroll(scrollUp: false)
    startAutoScroll->>stopAutoScroll: stopAutoScroll()
    stopAutoScroll->>stopAutoScroll: lastDragEvent = nil
    startAutoScroll->>Timer: scheduledTimer(0.05s, repeating)
    mouseDragged->>mouseDragged: lastDragEvent = event ✅
    Note over Timer: 50 ms passes
    Timer->>Core: sendMouseScroll(y: -1.0) ✅
    Timer->>Core: sendMousePos(x, y) ✅
    Note over Core: Selection extends correctly
Loading

Reviews (1): Last reviewed commit: "fix(macos): add auto-scroll when draggin..." | Re-trigger Greptile

Comment thread macos/Sources/Ghostty/Surface View/SurfaceView_AppKit.swift
Comment thread macos/Sources/Ghostty/Surface View/SurfaceView_AppKit.swift Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@macos/Sources/Ghostty/Surface` View/SurfaceView_AppKit.swift:
- Around line 220-223: The view's repeating auto-scroll timer (autoScrollTimer)
may continue firing after the view is deallocated because deinit does not call
stopAutoScroll(); update the SurfaceView_AppKit deinit to call stopAutoScroll()
to invalidate and nil out autoScrollTimer (the same cleanup performed by the
existing stopAutoScroll() method), ensuring the timer is removed from the run
loop and auto-scroll state (autoScrollUp/lastDragEvent) is cleared when the view
is torn down.
- Around line 1018-1039: The auto-scroll timer created in the autoScrollTimer
block uses Timer.scheduledTimer which attaches to RunLoop.Mode.default and won't
fire during drag tracking; change the creation to use an unscheduled Timer
(still using the same closure and keeping references to autoScrollTimer,
lastDragEvent, surfaceModel.sendMouseScroll, surfaceModel.sendMousePos and
convert(_:from:)) and then explicitly add it to RunLoop.main in
RunLoop.Mode.common so the timer fires during event-tracking drag mode; ensure
you preserve the existing weak self capture and the logic that re-sends mouse
position when lastDragEvent exists.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5a6be798-1e06-47bd-a11a-4c8701eae8f7

📥 Commits

Reviewing files that changed from the base of the PR and between 5c781d7 and fd28478.

📒 Files selected for processing (1)
  • macos/Sources/Ghostty/Surface View/SurfaceView_AppKit.swift

Comment thread macos/Sources/Ghostty/Surface View/SurfaceView_AppKit.swift
Comment thread macos/Sources/Ghostty/Surface View/SurfaceView_AppKit.swift Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant