Repository navigation
Refine TUI wheel scroll feel - #7179
Conversation
Co-authored-by: Orca <help@stably.ai>
📝 WalkthroughWalkthroughThe terminal mouse-wheel flow now resolves wheel events through a shared report-count module, then replays the resolved reports through a queued microtask drain. The terminal attachment code now reads cell height from the terminal DOM, uses discrete-wheel detection, and enqueues report batches instead of dispatching a fixed multiplier loop. Unit tests cover report-count resolution and replay dispatch, and end-to-end tests add timed wheel sampling plus a visible scroll fixture. ChangesRelated issues: None found Sequence Diagram(s)sequenceDiagram
participant attachTerminalMouseWheelMultiplier
participant resolveTerminalTuiMouseWheelReportCount
participant queueTerminalTuiWheelReports
participant DOM
attachTerminalMouseWheelMultiplier->>resolveTerminalTuiMouseWheelReportCount: resolve reportCount
resolveTerminalTuiMouseWheelReportCount-->>attachTerminalMouseWheelMultiplier: reportCount
attachTerminalMouseWheelMultiplier->>queueTerminalTuiWheelReports: enqueue reportCount
queueTerminalTuiWheelReports->>DOM: dispatch cloned wheel reports on drain
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (5)
src/renderer/src/lib/pane-manager/pane-terminal-mouse-wheel.ts (2)
239-266: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick winNo upper bound on
pendingReportsunder sustained fast scrolling.Each accelerated tick adds up to
normalizedMaxReportstopendingReports, while the drain only emitsTUI_WHEEL_REPORTS_PER_FRAME(4) per frame. If tick arrival outpaces the drain (fast continuous scroll), the backlog grows and reports keep firing after the gesture stops, causing scroll "momentum"/lag. Consider clampingpendingReportsto a sane ceiling.Please confirm the intended behavior for a long, fast gesture — whether accumulated reports beyond a threshold should be capped or dropped rather than fully replayed.
143-169: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueDrop the unused
streakfield from the mouse-wheel acceleration state.streakis only updated here and never read, so the acceleration logic depends entirely onelapsedMs/cadence. Remove it fromTerminalTuiMouseWheelAccelerationStateand its initializer unless it’s meant to drive the curve later.src/renderer/src/lib/pane-manager/pane-terminal-mouse-wheel.test.ts (1)
93-135: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winVerified acceleration math, but timestamp deltas are opaque.
Manually traced all four cases against
resolveTerminalTuiMouseWheelReportCount's streak/easeOutCubic logic and every expected report count is consistent. The 200ms vs 50ms tick spacing is what determines whether a tick counts as "accelerating" against the internal window constant, but that constant and its relationship to these magic numbers isn't visible from the test alone.A short inline comment noting why 200ms gaps stay unaccelerated (spacing exceeds the window) vs 50ms gaps accelerate would help future maintainers who touch
TUI_WHEEL_ACCELERATION_WINDOW_MSwithout re-deriving the math.As per coding guidelines: "When writing or modifying code driven by a design doc or non-obvious constraint, add a comment explaining why the code behaves the way it does."
Source: Coding guidelines
tests/e2e/terminal-tui-wheel-reports.spec.ts (2)
98-196: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winNear-duplicate of
probeSmallMouseWheelReports.
probeTimedSmallMouseWheelReports(lines 98-196) repeats almost the entire body of the existingprobeSmallMouseWheelReports(pane lookup, mouse-reporting activation,deltaYcomputation, event construction) with only the timing/return-shape differing. This duplicates the "Why" comment and the deltaY formula in two places, so any future tuning of the notched-wheel simulation risks drifting between the two probes.Consider making
probeTimedSmallMouseWheelReportsthe single implementation and haveprobeSmallMouseWheelReportsdelegate to it withintervalMs: 0, drainWaitMs: 0and mapsamplesback toWheelReportSample[].♻️ Proposed consolidation
-async function probeSmallMouseWheelReports( - page: Page, - ticks: number -): Promise<WheelReportSample[]> { - return page.evaluate( - async ({ tickCount, physicalMouseWheelDelta }) => { - ... - }, - { tickCount: ticks, physicalMouseWheelDelta: PHYSICAL_MOUSE_WHEEL_DELTA } - ) -} +async function probeSmallMouseWheelReports( + page: Page, + ticks: number +): Promise<WheelReportSample[]> { + const { samples } = await probeTimedSmallMouseWheelReports(page, { + ticks, + intervalMs: 0, + drainWaitMs: 0 + }) + return samples.map(({ reportCount, reportDelta, reports }) => ({ + reportCount, + reportDelta, + reports + })) +}
225-239: 🎯 Functional Correctness | 🔵 Trivial | 💤 Low valueAdd a short timing note for the slow/fast probes.
220msand40msare intentionally chosen around the140msacceleration window, so the split is stable but not obvious from the test alone.Source: Coding guidelines
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: f23e03a2-1307-46c3-b12b-c23d8fda8fa3
📒 Files selected for processing (3)
src/renderer/src/lib/pane-manager/pane-terminal-mouse-wheel.test.tssrc/renderer/src/lib/pane-manager/pane-terminal-mouse-wheel.tstests/e2e/terminal-tui-wheel-reports.spec.ts
Co-authored-by: Orca <help@stably.ai>
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (3)
src/renderer/src/lib/pane-manager/pane-terminal-mouse-wheel.ts (2)
173-203: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueRedundant multiplier normalization.
normalizeTerminalTuiMouseWheelMultiplier(options.getTuiMouseWheelMultiplier?.())is called here (line 193) and the result is passed intoresolveTerminalTuiMouseWheelReportCount, which normalizes it again internally (line 201 inpane-terminal-tui-wheel-reports.ts). Harmless since normalization is idempotent, but redundant — could drop one of the two calls.
93-104: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick winMove the terminal cell-height read off the wheel hot path.
getBoundingClientRect()here can force sync layout during bursty scrolling; cache the height and refresh it from resize/font-metric updates instead..xterm-screenis also an internal xterm DOM class, so this fallback can silently regress if xterm reshapes its markup.tests/e2e/terminal-tui-wheel-reports.spec.ts (1)
206-281: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winExtract shared active-pane resolution instead of triplicating it.
readVisibleTuiOffset,dispatchTuiWheel, and the inlinecellHeightlookup each re-derivetabId/manager/panefromwindow.__storewith the same ~10-line block.helpers/terminal.tsalready hasresolveActiveTabIdfor this (used bywaitForActivePanePtyId); reusing it here would let eachpage.evaluatetake a resolvedtabIdand just look up the pane.♻️ Proposed refactor sketch
async function readVisibleTuiOffset(page: Page): Promise<number | null> { - return page.evaluate(() => { - const state = window.__store?.getState() - const worktreeId = state?.activeWorktreeId - const tabId = - state?.activeTabType === 'terminal' - ? state.activeTabId - : worktreeId - ? (state?.activeTabIdByWorktree?.[worktreeId] ?? null) - : null - const manager = tabId ? window.__paneManagers?.get(tabId) : null - const pane = manager?.getActivePane?.() ?? manager?.getPanes?.()[0] ?? null + const tabId = await resolveActiveTabId(page) + return page.evaluate((tabId) => { + const manager = tabId ? window.__paneManagers?.get(tabId) : null + const pane = manager?.getActivePane?.() ?? manager?.getPanes?.()[0] ?? null if (!pane?.terminal) { return null } ... - }) + }, tabId) }This assumes
resolveActiveTabIdis exported from./helpers/terminal; please confirm before applying.Also applies to: 357-373
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 39088d98-fd6c-4b04-854e-69e6029861d2
📒 Files selected for processing (5)
src/renderer/src/lib/pane-manager/pane-terminal-mouse-wheel.test.tssrc/renderer/src/lib/pane-manager/pane-terminal-mouse-wheel.tssrc/renderer/src/lib/pane-manager/pane-terminal-tui-wheel-reports.tstests/e2e/fixtures/visible-tui-scroll-fixture.cjstests/e2e/terminal-tui-wheel-reports.spec.ts
| const DISCRETE_PIXEL_WHEEL_DELTA_MIN = 50 | ||
| const LEGACY_MOUSE_WHEEL_DELTA_MIN = 100 | ||
| const LEGACY_MOUSE_WHEEL_DELTA_UNIT = 120 | ||
| const DEFAULT_TERMINAL_CELL_HEIGHT = 16 | ||
| const TUI_WHEEL_ACCELERATED_DISTANCE_GAIN = 1.6 | ||
| const TUI_WHEEL_BURST_FULL_INTERVAL_MS = 16 | ||
| const TUI_WHEEL_BURST_MAX_INTERVAL_MS = 45 | ||
| const TUI_WHEEL_BURST_MAX_BONUS_ROWS = 3 | ||
| const TUI_WHEEL_BURST_RAMP_EVENTS = 4 | ||
| const TUI_WHEEL_MOMENTUM_TAIL_DECAY_RATIO = 0.85 | ||
| const TUI_WHEEL_COMPRESSED_MAX_DISTANCE_ROWS_PER_EVENT = 6 | ||
| const TUI_WHEEL_BURST_MAX_DISTANCE_ROWS_PER_EVENT = 9 |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
Tuning constants lack "why" comments.
These 15 empirically-tuned constants (burst interval thresholds, decay ratio, distance caps, gain factor) encode non-obvious design decisions (per the PR's harness-tuned cadence curve) but have no comments explaining their derivation or intended effect. A future contributor tweaking scroll feel has no context for safe bounds.
📝 Example of adding brief rationale comments
+// Below this, xterm treats a discrete pixel delta as a synthetic 1px tick rather than a real notch.
const DISCRETE_PIXEL_WHEEL_DELTA_MIN = 50
+// Legacy wheelDelta unit is 120/notch; treat >=100 as a full notch to tolerate rounding.
const LEGACY_MOUSE_WHEEL_DELTA_MIN = 100
+// Ticks faster than this are considered part of the same burst cadence (~60fps).
const TUI_WHEEL_BURST_FULL_INTERVAL_MS = 16
+// Ticks slower than this are treated as isolated, not a burst.
const TUI_WHEEL_BURST_MAX_INTERVAL_MS = 45As per coding guidelines, "When writing or modifying code driven by a design doc or non-obvious constraint, add a comment explaining why the code behaves the way it does."
Source: Coding guidelines
Co-authored-by: Orca <help@stably.ai>
Summary
Verification
Harness evidence
Made with Orca