Skip to content

desktop: Pause frame clock during scroll indicator hold - #4057

Merged
madcodelife merged 1 commit into
longbridge:mainfrom
M4n5ter:pr/desktop-scroll-indicator-hold-timer
Dec 3, 2026
Merged

madcodelife merged 1 commit into
longbridge:mainfrom
M4n5ter:pr/desktop-scroll-indicator-hold-timer

Conversation

@M4n5ter

@M4n5ter M4n5ter commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Description

Patch upstreamed from https://github.com/apache/maka (Apache-2.0).

On desktop backends (macOS, Linux, Windows, and embedded GPUI), showing the overlay scroll indicator keeps it at alpha = 1.0 for INDICATOR_HOLD (800 ms) before fading out over INDICATOR_FADE (250 ms). Previously, the scroll frame callback kept running on every display frame throughout the entire 800 ms hold period even though nothing changed on screen—so idle post-scroll holds and streaming content that periodically scrolls to the bottom kept the display link / frame clock running continuously.

This change pauses the frame callback once only the indicator hold remains and arms a single one-shot timer for the remaining hold duration (measured from the current monotonic clock rather than the previous frame timestamp):

  • nsh-core: Tracks Live::hold_timer, adds indicator_hold_left(since), hold_ended(host), and ScrollHost::wake_after(&self, ms: u64) -> bool (default returns false for source compatibility with existing ScrollHost implementations). When wake_after returns false (e.g., Win32 SetTimer failure), frame() clears hold_timer and stays on the existing frame-callback path instead of wedging future fades. If the indicator was shown again before the timer fires, hold_ended re-arms directly for the remaining hold without waking an empty frame; if detached or indicators were turned off, it avoids waking the frame clock.
  • nsh-platform-macos: Mirrors the same hold-pause and re-arm behavior on macOS (Tick::Hold / wake_after_hold), keeping cosmetic_frame balanced and adding a #[cfg(feature = "dev")] AppView::display_link_running() hook with a native regression check (tests/native/scroll_indicator_hold.rs).
  • nsh-platform-linux / nsh-platform-windows / crates/gpui/nsh-platform-gpui: Implements ScrollHost::wake_after using glib::timeout_add_local_once, input::after_ms (now returning bool), and rt::after_ms (now returning bool). The GPUI change is within docs/dev/gpui.md#scope ("Input inside the surface: pointer, scrolling").

Public API

  • nsh_core::scroll::ScrollHost: added #[doc(hidden)] provided method fn wake_after(&self, ms: u64) -> bool (default returns false) — schedules hold_ended on the view's thread after ms milliseconds; returns false if scheduling fails so core falls back to the frame callback path.
  • nsh_core::scroll::indicator_hold_left(since: f64) -> Option<u64> (#[doc(hidden)]) — returns remaining indicator hold milliseconds, or None once fading.
  • nsh_core::scroll::hold_ended(host: &impl ScrollHost) (#[doc(hidden)]) — called when the hold timer fires to start fading or re-arm for remaining hold.
  • nsh_platform_macos::AppView::display_link_running(&self) -> bool (#[cfg(feature = "dev")]) — test hook reporting whether the display link is currently unpaused.

Breaking Changes

None. ScrollHost::wake_after defaults to returning false, so existing external ScrollHost implementations continue compiling and fall back to the frame callback path (covered by crates/nsh-core/tests/scroll_compat.rs).

How to Test

rustfmt --edition 2024 --check \
  crates/nsh-core/src/scroll.rs \
  crates/nsh-platform-macos/src/scroll.rs \
  crates/nsh-platform-macos/src/surface.rs \
  crates/nsh-platform-macos/src/app.rs \
  crates/nsh-platform-macos/tests/native/main.rs \
  crates/nsh-platform-macos/tests/native/scroll_indicator_hold.rs \
  crates/nsh-platform-linux/src/scroll.rs \
  crates/nsh-platform-windows/src/input.rs \
  crates/nsh-platform-windows/src/scroll.rs \
  crates/gpui/nsh-platform-gpui/src/rt.rs \
  crates/gpui/nsh-platform-gpui/src/input.rs \
  crates/gpui/nsh-platform-gpui/src/scroll.rs
cargo clippy -p nsh-core -p nsh-platform-macos --all-targets --features nsh-platform-macos/dev -- -D warnings
cargo test -p nsh-core scroll::
cargo test -p nsh-platform-macos --lib scroll::
NSH_SCROLL_INDICATOR_HOLD_CHECK_ONLY=1 cargo test -p nsh-platform-macos --features dev --test native -- --ignored --nocapture
NSH_SCROLL_INDICATOR_CHECK_ONLY=1 cargo test -p nsh-platform-macos --test native -- --ignored --nocapture
cargo check --manifest-path crates/gpui/Cargo.toml -p nsh-platform-gpui --features runtime-shaders

Verification Results

  • macOS (Apple Silicon, 120 Hz):
    • nsh-core scroll::: 42 passed (including hold pause, re-arm without extra frame start, wake_after failure fallback, detached/hidden indicator cleanup, and lagging frame timestamp).
    • nsh-platform-macos --lib scroll::: 22 passed.
    • NSH_SCROLL_INDICATOR_HOLD_CHECK_ONLY=1: fails on main (display link pauses during indicator hold never happened), passes on HEAD (scroll_indicator_hold: display link paused during hold and faded indicator out).
    • NSH_SCROLL_INDICATOR_CHECK_ONLY=1: passed.
    • examples/bench/jank/jank.py (--target macos --runs 3 --seconds 4, debug nsh + nsh-macos-host):
      • Before (main 96838c858): startup 619 ms; animation 120 Hz frame p95 0.61 ms / max 0.87 ms, gap 8.33/8.33/8.34 ms, over 0%; scroll 120 Hz frame p95 2.26 ms / max 2.72 ms, gap 8.33/8.34/8.34 ms, over 0% (OK: within budget).
      • After (0325d4317): startup 617 ms; animation 120 Hz frame p95 0.65 ms / max 0.84 ms, gap 8.33/8.33/8.35 ms, over 0%; scroll 120 Hz frame p95 2.64 ms / max 3.06 ms, gap 8.33/8.33/16.67 ms, over 0% (OK: within budget).
  • Linux (x86_64 Arch Linux):
    • cargo clippy -p nsh-core -p nsh-platform-linux --all-targets -- -D warnings: 0 warnings.
    • cargo test -p nsh-core --lib scroll::: 42 passed.
    • cargo test -p nsh-platform-linux --lib scroll::: passed.
  • Windows (Windows 11 VM):
    • cargo clippy -p nsh-platform-windows --all-targets -- -D warnings: 0 warnings.
    • cargo test -p nsh-core --lib scroll::: 42 passed.
    • cargo test -p nsh-platform-windows --lib scroll::: 3 passed.
  • GPUI:
    • cargo check --manifest-path crates/gpui/Cargo.toml -p nsh-platform-gpui --features runtime-shaders: passed.

Checklist

  • I have read the CONTRIBUTING document and followed the guidelines.
  • If this changes crates/gpui, it is one of the changes the GPUI backend's scope accepts, and the description says which.
  • Reviewed the changes in this PR and confirmed AI generated code (if any) is accurate.
  • Verified on macOS (including jank.py before/after and AppKit hold regression check), Linux, Windows, and GPUI.

@huacnlee
huacnlee marked this pull request as ready for review December 2, 2026 13:10
@huacnlee

huacnlee commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

❌ Preserve existing ScrollHost implementations before accepting this scheduling optimization.

At crates/nsh-core/src/scroll.rs:327, the new required wake_after method breaks every external ScrollHost implementation that compiled against released v0.8.0 (the public trait is present in that tag). The existing timer-failure fallback already provides a compatible solution: give this method a default implementation returning false, and let the built-in backends override it. Existing hosts then keep their working frame callback path, while updated hosts gain the idle-hold optimization. Please cover an implementation that omits this new method in the compatibility checks. This is a source-compatibility defect, not a disagreement with the optimization's goal.

Scope/API review: stopping unchanged post-scroll frames is a useful bounded optimization, including scrolling inside an embedded GPUI surface. The public additions are ScrollHost::wake_after, indicator_hold_left, hold_ended, and the dev-only macOS AppView::display_link_running hook; the rest is backend plumbing. No new TSX/app configuration is necessary. Making the timer hook optional is the smaller compatible contract.

Verification: reviewed the full diff and timer re-arm/failure/detach paths; git diff origin/main...HEAD --check passed. The PR reports a main-fails/head-passes AppKit hold regression, core/platform tests, and macOS jank measurements; those are author-provided results, not my own device verification. Linux/Windows before/after jank.py results are still absent, and CI is queued. I have not independently run native rendering or recorded the interaction on macOS/Windows.

Reviewed head: 0325d4317253073996f4e379ac64a1ed95a98179.

Independent root-session verification at 0325d43: cargo test -p nsh-core --lib scroll:: passed all 42 tests. This does not cover external trait implementations or native platform timing.

🤖 Noop (reviewer) · AI agent acting for @huacnlee

@huacnlee huacnlee left a comment

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.

.

@M4n5ter
M4n5ter force-pushed the pr/desktop-scroll-indicator-hold-timer branch from 0325d43 to c1a5bb5 Compare December 2, 2026 15:36
@M4n5ter

M4n5ter commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the source-compatibility feedback in c1a5bb5be:

  • Added a default implementation returning false to ScrollHost::wake_after in crates/nsh-core/src/scroll.rs so external v0.8.0 ScrollHost implementations continue compiling and fall back to the frame callback path.
  • Added crates/nsh-core/tests/scroll_compat.rs (existing_scroll_host_without_wake_after_falls_back_to_frame_callbacks) implementing ScrollHost without overriding wake_after and verifying the hold + fade frame-callback fallback.
  • Verified with rustfmt --edition 2024 --check, cargo clippy -p nsh-core --all-targets -- -D warnings, cargo test -p nsh-core --test scroll_compat (1 passed), and cargo test -p nsh-core --lib scroll:: (42 passed).

@huacnlee

huacnlee commented Dec 2, 2026

Copy link
Copy Markdown
Member

💡 The compatibility defect is fixed; the new public scroll API and a small scroll regression in the posted benchmark still need the owner's decision.

  • Compat resolved: crates/nsh-core/src/scroll.rs (~L324-331) gives ScrollHost::wake_after a default returning false, so v0.8.0 hosts keep the frame-callback path. crates/nsh-core/tests/scroll_compat.rs covers a host that omits it. This addresses my earlier ❌.
  • Logic: frame() (~L1235-1268) arms one timer per hold via hold_timer, measures the remainder from host.now(), and falls back to ticking when the timer can't be armed or the hold has already elapsed. hold_ended() (~L1287-1318) re-arms for a re-shown indicator, skips detached views or views already ticking or dragging the thumb, and otherwise starts the fade. I found no path that leaves the indicator stuck.
  • Public surface (owner decision): ScrollHost::wake_after, indicator_hold_left, hold_ended, and the dev-only macOS AppView::display_link_running. Only wake_after and hold_ended are needed by backends. indicator_hold_left could stay pub(crate) unless an out-of-tree host needs it.
  • Perf: the posted macOS jank numbers show scroll frame p95 2.26 -> 2.64 ms and max gap 8.34 -> 16.67 ms (one dropped frame). Still within budget, but please rerun to confirm it is noise, since the goal is fewer idle frames without hurting scrolling. Linux and Windows have no before/after numbers.
  • I updated the PR description: the Public API section said "required method", which no longer matches the diff.

Recommendation: accept once the scroll gap is shown to be noise and indicator_hold_left is made crate-private (or justified). CI is pending.

Reviewed at c1a5bb5

🤖 Noop (reviewer) · Opus 5.5 acting for @huacnlee

@M4n5ter

M4n5ter commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for re-reviewing! Addressing the two remaining points:

  1. Why indicator_hold_left is pub rather than pub(crate): nsh-platform-macos is a separate crate from nsh-core and implements AppKit scrolling directly rather than through ScrollHost. It calls nsh_core::scroll::indicator_hold_left at crates/nsh-platform-macos/src/scroll.rs:64,1659,1723,1984 right alongside its existing pub sibling nsh_core::scroll::indicator_alpha (because INDICATOR_HOLD is private to nsh-core::scroll). Keeping indicator_hold_left as a pub helper avoids duplicating the constant across crates and stays consistent with indicator_alpha / indicator_span_within.

  2. Re-ran paired macOS jank.py --runs 3 (main 96838c858 vs head c1a5bb5be) back-to-back on the same Apple Silicon 120 Hz desktop:

    • Before (main 96838c858): startup 624 ms ([2508, 624, 617]); animation 120 Hz (9 s): frame p95 0.53 ms / max 0.69 ms, gap 8.33/8.33/8.34 ms, over 0%; scroll 120 Hz (12 s): frame p95 2.42 ms / max 2.98 ms, gap 8.33/8.33/16.67 ms, over 0% (OK: within budget).
    • After (c1a5bb5be): startup 638 ms ([2442, 638, 616]); animation 120 Hz (9 s): frame p95 0.54 ms / max 0.70 ms, gap 8.33/8.33/16.67 ms, over 0%; scroll 120 Hz (12 s): frame p95 2.42 ms / max 2.69 ms, gap 8.33/8.33/16.67 ms, over 0% (OK: within budget).

    Scroll frame p95 is identical (2.42 ms vs 2.42 ms), frame max is 2.69 ms vs 2.98 ms, and main hits the same occasional single-frame 16.67 ms gap (over 0%), confirming the earlier run difference was run-to-run noise.

@M4n5ter
M4n5ter force-pushed the pr/desktop-scroll-indicator-hold-timer branch from c1a5bb5 to 31be181 Compare December 3, 2026 00:49
@huacnlee

huacnlee commented Dec 3, 2026

Copy link
Copy Markdown
Member

💡 Earlier compat issue is fixed and the perf rerun shows no regression; the new public scroll API needs a maintainer call.

  • ScrollHost::wake_after now has a default, and crates/nsh-core/tests/scroll_compat.rs covers a v0.8.0-style host. Thanks.
  • Paired macOS jank runs: p95 2.42 ms on both main and head; no Linux/Windows numbers.
  • Public additions: ScrollHost::wake_after, nsh_core::scroll::indicator_hold_left, hold_ended. They exist only for nsh-platform-macos (crates/nsh-platform-macos/src/scroll.rs:64); consider #[doc(hidden)] to keep them out of the stable surface before the 0.9 freeze.
  • Code provenance (apache/maka) still needs a maintainer decision.

Reviewed at 31be181 (read-only diff review; CI still pending).

🤖 Noop (reviewer) · Opus 5.5 acting for @huacnlee

@M4n5ter
M4n5ter force-pushed the pr/desktop-scroll-indicator-hold-timer branch from 31be181 to 0d5945e Compare December 3, 2026 02:10
@M4n5ter

M4n5ter commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Rebased onto latest main (resolving the conflict in crates/nsh-core/src/scroll.rs around cancel_input and unifying TestHost on ticks_started) and marked ScrollHost::wake_after, nsh_core::scroll::indicator_hold_left, and nsh_core::scroll::hold_ended as #[doc(hidden)] as suggested so they stay out of the 0.9 documented public API surface. Re-verified public_api_is_typed, scroll_compat, nsh-core/nsh-platform-macos clippy + tests, and GPUI check at 0d5945ead.

@huacnlee huacnlee left a comment

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.

✅ All earlier review points are addressed; the scheduling hooks are now kept out of the documented API.

  • Compat: ScrollHost::wake_after has a default returning false (crates/nsh-core/src/scroll.rs); crates/nsh-core/tests/scroll_compat.rs covers a v0.8.0-style host without it.
  • Public surface: ScrollHost::wake_after, scroll::indicator_hold_left and scroll::hold_ended are #[doc(hidden)]; indicator_hold_left stays pub only because nsh-platform-macos calls it (crates/nsh-platform-macos/src/scroll.rs:64). No addition to the documented 0.9 surface.
  • Perf: paired macOS jank.py --runs 3 shows scroll frame p95 2.42 ms on both main and head; the single 16.67 ms gap also appears on main. No Linux/Windows numbers, non-blocking since the change only removes idle frames after scrolling stops.
  • Logic: one timer per hold, re-armed by hold_ended, frame-callback fallback when no timer can be armed; no path leaves the indicator stuck.
  • CI was queued at review time; merge only once green.

Reviewed head: 0d5945e

🤖 Noop (reviewer) · AI agent acting for @huacnlee

@madcodelife
madcodelife merged commit 4fc89aa into longbridge:main Dec 3, 2026
31 checks passed
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.

3 participants