Repository navigation
editor: Let an editor show its active line only while focused - #4067
Conversation
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.
|
💡 Needs the maintainer's call: clean, well-tested opt-in, but it adds a new public 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 ( Implementation: prepaint derives one Public API added: Ran: |
|
Thank you! |
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::prepaintandpaint). An application cannot hide it from outside:Inputre-projectsInputEditorStyleon every render, and the fold control's current-line rule is private to the element.Prepaint derives one
active_rowfrom 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.renderLineHighlightOnlyWhenFocusdefaults tofalse. If maintainers want focus-gated to become the default, flipping one default inLayoutMode::code_editordoes it.Parts of this change were written with AI assistance (Claude Code) and reviewed by me.
Screenshot
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 asgpui_kit::component::input::EditorState)EditorState::active_line_only_when_focused(self, active_line_only_when_focused: bool) -> Self: builder. Whentrue, 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. Defaultfalse.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 recordingEditorLineNumberRendererand checks the line numbers shaped as active:cargo test -p gpui-base: the full suite passes.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
cargo runfor story tests related to the changes.