Skip to content

editor: Let an editor show its active line only while focused - #4067

Merged
madcodelife merged 2 commits into
longbridge:mainfrom
wyattjoh:wyattjoh/focus-gated-current-row
Nov 21, 2026
Merged

madcodelife merged 2 commits into
longbridge:mainfrom
wyattjoh:wyattjoh/focus-gated-current-row

Conversation

@wyattjoh

@wyattjoh wyattjoh commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Description

An editor marks the caret's line as active: the line number in the active color, the active-line background, and the fold control that follows the caret. It does this whether or not the editor has focus. When two editors sit side by side, for example a document and a companion editor that continues it, both show an active line at once, and the user cannot tell which one has the keyboard.

This adds an opt-in EditorState::active_line_only_when_focused(bool) builder and its runtime setter. When it is on, an editor without focus shows no active line. A popup the editor owns (set_selection_focus, such as its context menu) keeps the active line shown, as it already keeps the selection highlighted.

The change is in gpui-base, because that is where the active row is computed and every painter that reads it lives (TextElement::prepaint and paint). An application cannot hide it from outside: Input re-projects InputEditorStyle on every render, and the fold control's current-line rule is private to the element.

Prepaint derives one active_row from the caret row and the focus test, and the four highlight sites read it: line-number shaping, the active-line background, the gutter's active fill, and the fold controls. Inline-completion ghost lines and the caret-row anchor still follow the caret row, so nothing moves.

Why opt-in rather than the new default: an always-visible active line is the current behavior, and a readonly viewer that never takes focus would lose its active line without warning. Keeping the default matches the project's backward-compatibility rule for patch releases. It also follows VS Code, where editor.renderLineHighlightOnlyWhenFocus defaults to false. If maintainers want focus-gated to become the default, flipping one default in LayoutMode::code_editor does it.

Parts of this change were written with AI assistance (Claude Code) and reviewed by me.

Screenshot

Before After
before after

Two editors side by side, with the caret on line 4 in each and focus in the left one. After uses .active_line_only_when_focused(true) on both. Before, both editors show line 4 as active. After, only the focused editor does. The demo gives the theme an active-line background so the difference is visible; the default theme has none, and there the active line shows only as a darker line number.

Public API

gpui-base (also reachable as gpui_kit::component::input::EditorState)

  • EditorState::active_line_only_when_focused(self, active_line_only_when_focused: bool) -> Self: builder. When true, the active line number, active-line background and the caret's fold control show only while the editor or a popup it owns has focus. Default false.
  • EditorState::set_active_line_only_when_focused(&mut self, active_line_only_when_focused: bool, window: &mut Window, cx: &mut Context<Self>): changes the same setting at runtime, and notifies only when the value changes.

How to Test

  • cargo test -p gpui-base active_line_follows_the_input_and_its_menu_focus: draws an editor with a recording EditorLineNumberRenderer and checks the line numbers shaped as active:
    • default, focused or blurred: line 2 stays active;
    • opted in and focused: line 2 is active;
    • opted in and blurred: no line is active;
    • opted in with focus in an owned popup: line 2 is active;
    • turned off at runtime: line 2 is active again.
  • The test fails when the focus condition is removed.
  • cargo test -p gpui-base: the full suite passes.
  • Manual check: put two Editors side by side with .active_line_only_when_focused(true) and click between them. Only the focused one shows the active line number and background.

Checklist

  • I have read the CONTRIBUTING document and followed the guidelines.
  • Reviewed the changes in this PR and confirmed AI generated code (If any) is accurate.
  • Passed cargo run for story tests related to the changes.
  • Tested macOS, Windows and Linux platforms performance (if the change is platform-specific)

Add EditorState::active_line_only_when_focused (builder) and
set_active_line_only_when_focused. When set, the active line number,
active-line background and the caret's fold control are hidden while
neither the editor nor a popup it owns has focus. Inline-completion
ghost lines keep following the caret row. Off by default.
Drop the added doc and inline comments, shorten the builder and setter
docs to the line_number/folding form, place the mode reader beside
is_auto_close/is_smart_indent, rename the test after its sibling, and
add the option to the Editor options example.
@huacnlee

Copy link
Copy Markdown
Member

💡 Needs the maintainer's call: clean, well-tested opt-in, but it adds a new public EditorState API.

Reviewed at 18e73ef.

What it does: an editor can hide its active line (number colour, active-line background, caret fold control) while it has no focus; owned popups (set_selection_focus) keep it visible. Default unchanged.

Implementation: prepaint derives one active_row from current_row + the focus check and all four highlight sites read it; ghost lines and caret anchoring still use current_row, so nothing shifts. No new dependencies.

Public API added: EditorState::active_line_only_when_focused(bool) and set_active_line_only_when_focused(bool, window, cx) (notifies only on change). Precedent: VS Code editor.renderLineHighlightOnlyWhenFocus (default false). The open question is scope only: whether this belongs in gpui-kit as a builder, or whether focus-gating should simply become the editor default.

Ran: cargo test -p gpui-base active_line_follows_the_input_and_its_menu_focus → 1 passed (Linux). CI has not run yet (needs "Approve and run").

🤖 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.

✅ LGTM: the maintainer accepted the opt-in active_line_only_when_focused API; default behaviour is unchanged and the included test passes locally.

Reviewed at 18e73ef. CI still needs a maintainer to click "Approve and run".

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

@madcodelife
madcodelife merged commit cf945af into longbridge:main Nov 21, 2026
19 checks passed
@madcodelife

Copy link
Copy Markdown
Member

Thank you!

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