diff --git a/crates/base/src/input/base/element.rs b/crates/base/src/input/base/element.rs index 698bd7b31d..7a89ae9024 100644 --- a/crates/base/src/input/base/element.rs +++ b/crates/base/src/input/base/element.rs @@ -3141,6 +3141,7 @@ pub(super) struct PrepaintState { cursor_scroll_offset: Point, /// row index (zero based), no wrap, same line as the cursor. current_row: Option, + active_row: Option, selection_paths: Vec>, hover_highlight_path: Option>, search_match_paths: Vec<(Path, bool)>, @@ -3813,6 +3814,13 @@ impl Element for TextElement Element for TextElement Element for TextElement Element for TextElement Element for TextElement Element for TextElement>>); + + impl EditorLineNumberRenderer for ActiveLineNumbers { + fn measure_line_numbers( + &self, + digit_count: usize, + body_style: &TextStyle, + window: &Window, + ) -> (Pixels, Pixels) { + ().measure_line_numbers(digit_count, body_style, window) + } + + fn shape_line_number( + &self, + number: usize, + is_active: bool, + body_style: &TextStyle, + window: &Window, + cx: &App, + ) -> ShapedLine { + if is_active { + self.0.borrow_mut().push(number); + } + ().shape_line_number(number, is_active, body_style, window, cx) + } + } + + struct ActiveLineHarness(Entity, ActiveLineNumbers); + + impl Render for ActiveLineHarness { + fn render(&mut self, _: &mut Window, _: &mut Context) -> impl IntoElement { + div() + .size_full() + .child(Editor::new(&self.0).with_line_number_renderer(self.1.clone())) + } + } + + #[gpui::test] + fn active_line_follows_the_input_and_its_menu_focus(cx: &mut TestAppContext) { + cx.update(crate::init); + for only_when_focused in [false, true] { + let numbers = ActiveLineNumbers::default(); + let mut editor = None; + let window = cx.open_window(size(px(240.), px(140.)), |window, cx| { + let state = cx.new(|cx| { + EditorState::new(window, cx) + .active_line_only_when_focused(only_when_focused) + .default_value("one\ntwo\nthree") + }); + editor = Some(state.clone()); + ActiveLineHarness(state, numbers.clone()) + }); + let editor = editor.unwrap(); + let mut cx = VisualTestContext::from_window(window.into(), cx); + cx.update(|window, cx| { + let menu_focus = cx.focus_handle(); + let other_focus = cx.focus_handle(); + let active = |window: &mut Window, cx: &mut App| { + window.draw(cx).clear(cx); + numbers.0.borrow_mut().clear(); + window.draw(cx).clear(cx); + numbers.0.borrow().clone() + }; + + editor.update(cx, |state, cx| { + state.set_cursor_position(Position::new(1, 0), window, cx); + state.focus(window, cx); + }); + assert_eq!(active(window, cx), vec![2]); + + other_focus.focus(window, cx); + let unfocused: Vec = if only_when_focused { vec![] } else { vec![2] }; + assert_eq!(active(window, cx), unfocused, "{only_when_focused}"); + + editor.update(cx, |state, cx| { + state.set_selection_focus(Some(menu_focus.clone()), cx); + }); + menu_focus.focus(window, cx); + assert_eq!(active(window, cx), vec![2]); + + other_focus.focus(window, cx); + editor.update(cx, |state, cx| { + state.set_active_line_only_when_focused(false, window, cx); + }); + assert_eq!(active(window, cx), vec![2]); + }); + } + } + /// Paints an input moved by its parent, then counts the views a draw with /// nothing else to do builds: a notify during a draw only marks a view /// dirty, for the next draw. Only GPUI Fast reuses a clean view. diff --git a/crates/base/src/input/base/mode.rs b/crates/base/src/input/base/mode.rs index 021fb89878..e132194849 100644 --- a/crates/base/src/input/base/mode.rs +++ b/crates/base/src/input/base/mode.rs @@ -47,6 +47,7 @@ pub(crate) enum LayoutMode { language: Box, indent_guides: bool, folding: bool, + active_line_only_when_focused: bool, highlighter: Rc>>>, highlighter_factory: Option, diagnostics: DiagnosticSet, @@ -86,6 +87,7 @@ impl LayoutMode { line_number: true, indent_guides: true, folding: true, + active_line_only_when_focused: false, diagnostics: DiagnosticSet::new(&Rope::new()), auto_closed_pairs: Default::default(), auto_close: true, @@ -150,6 +152,16 @@ impl LayoutMode { ) } + pub(super) fn is_active_line_only_when_focused(&self) -> bool { + matches!( + self, + Self::CodeEditor { + active_line_only_when_focused: true, + .. + } + ) + } + pub(super) fn set_auto_close(&mut self, enabled: bool) { if let Self::CodeEditor { auto_close, .. } = self { *auto_close = enabled; @@ -450,11 +462,13 @@ mod tests { assert_eq!(mode.max_rows(), usize::MAX); assert_eq!(mode.min_rows(), 1); assert_eq!(mode.is_folding(), true); + assert_eq!(mode.is_active_line_only_when_focused(), false); let mode = LayoutMode::CodeEditor { line_number: false, indent_guides: false, folding: false, + active_line_only_when_focused: true, rows: 0, tab: Default::default(), language: Box::default(), @@ -469,6 +483,7 @@ mod tests { assert_eq!(mode.has_indent_guides(), false); assert_eq!(mode.min_rows(), 1); assert_eq!(mode.is_folding(), false); + assert_eq!(mode.is_active_line_only_when_focused(), true); assert_eq!(mode.is_auto_close(), false); assert_eq!(mode.is_smart_indent(), false); } diff --git a/crates/base/src/input/base/state.rs b/crates/base/src/input/base/state.rs index 63815f8c57..05a227b2d8 100644 --- a/crates/base/src/input/base/state.rs +++ b/crates/base/src/input/base/state.rs @@ -13997,6 +13997,41 @@ impl InputBaseState { cx.notify(); } + /// Set enable/disable showing the active line only while focused. + /// + /// Default: false + #[doc(hidden)] + pub fn active_line_only_when_focused(mut self, active_line_only_when_focused: bool) -> Self { + if let LayoutMode::CodeEditor { + active_line_only_when_focused: a, + .. + } = &mut self.mode + { + *a = active_line_only_when_focused; + } + self + } + + /// Set showing the active line only while focused. + pub fn set_active_line_only_when_focused( + &mut self, + active_line_only_when_focused: bool, + _: &mut Window, + cx: &mut Context, + ) { + if let LayoutMode::CodeEditor { + active_line_only_when_focused: a, + .. + } = &mut self.mode + { + if *a == active_line_only_when_focused { + return; + } + *a = active_line_only_when_focused; + } + cx.notify(); + } + /// Set enable/disable automatic closing brackets and quotes. /// /// When enabled, typing an opener from [`LanguageConfig::auto_closing_pairs`](crate::input::language_config::LanguageConfig::auto_closing_pairs) inserts the diff --git a/website/component/editor.md b/website/component/editor.md index 25c13997ca..b60b713020 100644 --- a/website/component/editor.md +++ b/website/component/editor.md @@ -132,6 +132,7 @@ let editor = cx.new(|cx| { .folding(true) .show_whitespaces(true) .scroll_bounce(true) + .active_line_only_when_focused(true) .default_value(source) }); ``` @@ -141,6 +142,11 @@ bottom and spring back with precise trackpad or Magic Mouse scrolling, as in the [Textarea](./textarea.md#scroll-bounce). The gutter moves with the code; the frame and scrollbar stay put. Ordinary mouse-wheel line scrolling does not bounce. +`active_line_only_when_focused(true)` shows the active line number, the +active-line background and the caret's fold control only while the editor or +its context menu has focus, so that of several editors on screen only the +focused one shows an active line. + ## Manual completions `ShowCompletions` requests suggestions without editing text. It has **no default diff --git a/website/zh-CN/component/editor.md b/website/zh-CN/component/editor.md index 663e5094bd..7bd1cdc1b9 100644 --- a/website/zh-CN/component/editor.md +++ b/website/zh-CN/component/editor.md @@ -112,12 +112,15 @@ let editor = cx.new(|cx| { .folding(true) .show_whitespaces(true) .scroll_bounce(true) + .active_line_only_when_focused(true) .default_value(source) }); ``` `scroll_bounce(true)` 让超出编辑器高度的代码在触控板或 Magic Mouse 的精细像素滚动到达顶部或底部时越过边缘并弹回,与 [Textarea](./textarea.md#滚动回弹) 相同。行号栏随代码移动,外框和滚动条保持不动;普通鼠标滚轮的按行滚动不触发回弹。 +`active_line_only_when_focused(true)` 让当前行的行号、当前行背景和光标所在行的折叠按钮只在编辑器或其右键菜单获得焦点时显示,这样界面上有多个编辑器时,只有获得焦点的那个显示当前行。 + ## 主动补全 `ShowCompletions` 可以在不修改文本的情况下请求补全,**没有默认快捷键**。应用应结合平台习惯选择绑定,避开 Ctrl+Space 等输入法快捷键。Editor 的绑定可以使用 `Input && mode == editor` 条件;现有 `Input` 绑定仍然有效。