Skip to content

Focus and cursor blink: restartBlink, cursorBlinkRate, onBlur shift reset (Refs #50) - #89

Draft
DJAscendance wants to merge 8 commits into
masterfrom
feat/focus-blink
Draft

DJAscendance wants to merge 8 commits into
masterfrom
feat/focus-blink

Conversation

@DJAscendance

@DJAscendance DJAscendance commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Refs #50

Ledger rows: 615 onFocus, 616 onBlur, 679 restartBlink.

Base: 7d917785a1b65259b238f35a3fad98343d8a6d02 (master, merged in); parent PR head 910c2e085efcd7cd883b5f79dd72b1391fbf8376
Head: 941d6c6cfcf4eebc4b4d0e36a9386def958def99

Integration: Stacked on #86 (910c2e0); merge in order #81, #83, #86, #89, #90, #91, #92. Until the PR below it merges, this PR's diff against master includes that PR.

What this adds

  • EditorOptions::cursor_blink_rate (cursorBlinkRate, default 530).
  • Editor::restart_blink() (restartBlink). While the editor is focused, the blink period starts again now. It is called:
    • by set_focused(true), every time, including when the editor is already focused (onFocus ends with restartBlink);
    • after an operation that changed the selection (op.selectionChanged, operations.js:127);
    • after a handled or multi key (dispatchKeyInner, key_events.js:74-77).
  • Editor::cursor_blink_visible() follows the rate: a rate > 0 toggles every rate ms, 0 shows the cursor without blinking, and a negative rate hides it. An unfocused editor shows no cursor (CM5's CSS). Editor::next_blink_in() tells the host when to wake up.
  • set_focused(false) stops the blinker and schedules onBlur's 150 ms display.shift reset, which is skipped if the editor is focused again in time. Editor::run_timers() and next_timer_in() are the host timer hooks.
  • The native window (window.rs) draws the cursor and schedules wake-ups from these, instead of its fixed 530 ms Instant timer. It tells the editor it is focused when the app opens, and runs run_timers when idle.

Upstream refs (pinned 4ca3d05)

src/display/focus.js:21-50; src/display/selection.js:160-174; src/display/operations.js:127; src/edit/key_events.js:74-77; src/edit/options.js:156.

Tests and evidence

  • crates/cm-view/tests/focus_blink.rs runs on a host clock (cm_core::clock::set_override):
    • blink_follows_focus_and_rate: toggles at the exact rate boundaries, restarts on a selection-changing operation and on a handled key, and does not restart on an empty operation.
    • blink_rate_zero_and_negative
    • blur_resets_shift_after_150ms: the 149/150 ms boundary, refocus cancels the reset, and a repeated focus restarts the blink without a second focus event.
  • app_script.rs::winit_event_loop_under_xvfb (the native window) passes.
  • Strict, on the top of the stack (Cursor drawing: showCursorWhenSelecting, cursorHeight, fat cursor, overwriteToggle (Refs #51) #92 0b167ad, which contains this PR and every PR below it): cargo test --workspace --no-fail-fast gave 834 passed, 0 failed, 0 ignored. The same under xvfb-run -a: 834 passed, 0 failed. tools/web-test.sh: all 5 browser profiles passed 63/63.
  • cm-web is not touched. It keeps its own DOM blinker (Dom::restart_blink, setCursorBlinkRate), which matches CM5 but does not read the engine option.

Known differences

  • CM5 signals focus/blur before it changes state.focused. The port changes the state first (unchanged by this PR).
  • Not modeled: delayingBlurEvent/draggingText, readOnly "nocursor" (the port's read_only is a bool), and the hidden-input reset on focus.
  • The constructor's onBlur(cm) (which only schedules the shift reset) is not modeled.

Coverage change

  • 679: PARTIAL → PORTED
  • 615, 616: PARTIAL → PARTIAL (blink and the shift timer are done; the items above remain)

CI: pending (Actions cannot start: billing)

)

cursorCoords/charCoords/coordsChar/lineAtHeight/heightAtLine method forms
(cursor_coords_in, char_coords_in, coords_char_in, line_at_height_in,
height_at_line_in) with page and window modes over a host-supplied
window origin and page scroll, start=null/true/false, clipPos, LineHandle
arguments, includeWidgets (no widgets yet) and the end-of-document
adjustment; intoCoordSystem/fromCoordSystem ported with CM5's div
(viewOffset) semantics. The port's internal document-y frame is now
CoordSys::Doc. Checked against a CM5 recording (cm5-coord-systems.json).
… (Refs #54)

get_scroll_info() returns CM5's {left, top, height, width, clientHeight,
clientWidth}; scroll_into_view_target takes null (cursor plus
cursorScrollMargin), a line number, a position, a range, a coordinate box
or a box range (scrollToCoordsRange); scroll_rect_into_view is the
immediate, thresholded scrollIntoView(cm, rect) helper. Checked against a
CM5 recording (cm5-scroll-api.json), including scrollTo and scrollIntoView
inside one operation.
NamedListeners (one copy-on-write list per event name) and cm_core::Emitter
port on/off/getHandlers/signal/hasHandler for string-named and custom
events on any object; the editor, documents, line handles and markers get
a named table beside their typed lists. Editor::signal_dom_event and
on_dom_event port signalDOMEvent; key_down, key_press, mouse_down, paste
and maybeScrollWindow (scrollCursorIntoView) signal before handling, and a
handler that calls prevent_default or sets codemirror_ignore pre-empts
the editor.
… reset (Refs #50)

The editor keeps the blink: restart_blink on focus (also when already
focused), after an operation that changed the selection and after a
handled key; cursor_blink_visible / next_blink_in follow the
cursorBlinkRate option (> 0 blinks, 0 shows, < 0 hides). Blurring stops
the blink and schedules onBlur's 150 ms display.shift reset, run by the
host through run_timers / next_timer_in. The native window draws and
wakes from these instead of its fixed 530 ms timer.
into_coord_system honors include_widgets as CM5 does: without it the box
moves below the widgets above the line (widgetTopHeight), before the
"line" early return; master's into_coords path is the include_widgets =
false case. Exports keep both sides' items. New test
into_coord_system_include_widgets.
Keep both listener sets (gutter/line-widget events and named events).
@github-actions

Copy link
Copy Markdown

MiniMax-M3.1-Flash QA

Review summary

The PR is substantial and generally well structured: the new APIs are backed by CM5 recordings, named-event behavior has focused regression coverage, and the coordinate/scroll work includes both implementation tests and oracle-generation scripts. I found one likely functional issue and a couple of smaller robustness concerns.

Findings

  1. [P1] scroll_into_view_target performs coordinate-form scrolling immediately, bypassing CM5’s operation semantics
    File: crates/cm-view/src/editor.rs, scroll_into_view_target

    The coordinate forms return directly from the operation closure:

    ScrollTarget::Coords(r) => return cm.scroll_to_coords_range(r, r, margin),
    ScrollTarget::CoordsRange(a, b) => return cm.scroll_to_coords_range(a, b, margin),

    scroll_to_coords_range calls scroll_to_coords directly, so the scroll is applied immediately rather than being recorded as pending operation state. CM5’s scrollIntoView is a methodOp, and the coordinate form is expected to participate in the same operation ordering as scrollTo, cursor changes, and other scroll requests.

    This matters for the operation cases in scroll_api.rs: the current test only observes the final scroll position, so it can pass even though the implementation does not preserve CM5’s ordering semantics. In particular, a later scrollTo or cursor update in the same operation may incorrectly override or be overridden by the coordinate scroll.

    Suggested fix: store coordinate scroll requests in the operation state, or implement the same pending-scroll mechanism used by scrollTo, and resolve all pending scroll requests in the documented order when the operation finishes. Add a test that distinguishes immediate application from operation-final application, especially for cases such as op_coords_then_to.

  2. [P2] scroll_rect_into_view is documented as an immediate helper, but its behavior is not clearly isolated from operation state
    File: crates/cm-view/src/editor.rs, scroll_rect_into_view

    The helper wraps update_scroll_top and update_scroll_left in run_op, while scroll_into_view_target uses a different path for coordinate requests. Since both APIs manipulate the same scroll state, a future change to one path could silently alter the other.

    The distinction is intentional according to the comments—scroll_rect_into_view should be the immediate/thresholded helper—but it would be safer to centralize the underlying scroll calculation and make the operation-vs-immediate distinction explicit at the call site. This is especially important because the new tests rely on subtle differences:

    • immediate helper: changes under 2 px are ignored;
    • method form: a 1 px change is applied.

    Suggested fix: factor out the shared scroll calculation, and add explicit tests for ordering and thresholds involving both APIs in the same operation.

  3. [P2] Unknown host time causes the blur timer to fire immediately
    File: crates/cm-view/src/editor.rs, run_timers

    if now >= at || !now.is_finite() {
        self.blur_shift_at = None;
        if !self.focused {
            self.display.shift = false;
        }
    }

    The project documents that some hosts may not have a usable clock. Treating a non-finite timestamp as “the timer is due” means a host with an unknown clock immediately clears display.shift, rather than leaving the timeout pending until time becomes available.

    This differs from the cursor-blink implementation, which explicitly handles unknown/negative elapsed time conservatively.

    Suggested fix: only consider the timer due when the clock is finite and now >= at; retain the timer for unknown time, or explicitly document that unknown time is treated as due if that is the intended behavior. Add a test with NaN/unknown time.

  4. [P3] The new DomEvent API does not expose the original event’s prevent-default state
    File: crates/cm-view/src/events.rs, DomEvent

    The struct has default_prevented and codemirror_ignore as internal Cells, and the public API exposes prevent_default() and default_prevented(), but there is no way for a host or addon to initialize an event with an already-prevented state or to query whether the host had previously prevented it.

    This is not necessarily required for the current editor-owned events, but it makes the type less useful as a general signalDOMEvent payload and differs from the browser event model.

    Suggested fix: consider adding constructors or setters such as with_default_prevented, with_codemirror_ignore, or public accessors for the underlying values, depending on the intended host-facing API.

Validation recommendations

Before merging, I would run:

cargo test -p cm-core
cargo test -p cm-view
cargo test --workspace

I would also add a targeted regression test for operation ordering of coordinate-form scroll_into_view_target, and a test covering the behavior of run_timers when the host clock is unavailable.

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