|
1 | | -use gpui::{App, Div, Entity, InteractiveElement as _, IntoElement, RenderOnce, Stateful, Window}; |
| 1 | +use gpui::{ |
| 2 | + App, Div, Entity, EntityId, InteractiveElement as _, IntoElement, Pixels, ShapedLine, |
| 3 | + SharedString, Stateful, TextRun, TextStyle, View, ViewElement, Window, px, |
| 4 | +}; |
2 | 5 |
|
3 | | -use super::{EditorMode, InputBaseState, InputModeKind}; |
| 6 | +use super::{EditorMode, InputBaseState, InputModeKind, element::LINE_NUMBER_RIGHT_MARGIN}; |
| 7 | + |
| 8 | +/// Supplies computed line-number geometry and glyphs for a single rendered frame. |
| 9 | +/// |
| 10 | +/// The element owns this renderer, never the editing state: application styling |
| 11 | +/// must not become retained Base data. Measurement runs before wrapping; shaping |
| 12 | +/// runs only for visible buffer lines after wrapping and folding are resolved. |
| 13 | +#[doc(hidden)] |
| 14 | +pub trait EditorLineNumberRenderer: 'static { |
| 15 | + /// Returns the number lane width (excluding the gap) and resolved gap. |
| 16 | + /// A zero digit count requests only the gap when line numbers are hidden. |
| 17 | + fn measure_line_numbers( |
| 18 | + &self, |
| 19 | + digit_count: usize, |
| 20 | + body_style: &TextStyle, |
| 21 | + window: &Window, |
| 22 | + ) -> (Pixels, Pixels); |
| 23 | + |
| 24 | + /// Resolves a shared vertical offset from the configured fonts before shaping rows. |
| 25 | + /// Renderers using body typography need no vertical correction. |
| 26 | + fn resolve_line_number_baseline_offset( |
| 27 | + &self, |
| 28 | + _body_style: &TextStyle, |
| 29 | + _window: &Window, |
| 30 | + ) -> Pixels { |
| 31 | + px(0.) |
| 32 | + } |
| 33 | + |
| 34 | + /// Shapes one supported, one-based line number, without padding characters. |
| 35 | + fn shape_line_number( |
| 36 | + &self, |
| 37 | + number: usize, |
| 38 | + is_active: bool, |
| 39 | + body_style: &TextStyle, |
| 40 | + window: &Window, |
| 41 | + cx: &App, |
| 42 | + ) -> ShapedLine; |
| 43 | +} |
| 44 | + |
| 45 | +impl EditorLineNumberRenderer for () { |
| 46 | + fn measure_line_numbers( |
| 47 | + &self, |
| 48 | + digit_count: usize, |
| 49 | + body_style: &TextStyle, |
| 50 | + window: &Window, |
| 51 | + ) -> (Pixels, Pixels) { |
| 52 | + if digit_count == 0 { |
| 53 | + return (px(0.), LINE_NUMBER_RIGHT_MARGIN); |
| 54 | + } |
| 55 | + let font_size = body_style.font_size.to_pixels(window.rem_size()); |
| 56 | + let font_id = window.text_system().resolve_font(&body_style.font()); |
| 57 | + // Proportional digits can differ in width; every supported digit must fit. |
| 58 | + let width = "0123456789" |
| 59 | + .chars() |
| 60 | + .map(|digit| window.text_system().layout_width(font_id, font_size, digit)) |
| 61 | + .fold(px(0.), Pixels::max) |
| 62 | + * digit_count; |
| 63 | + (width, LINE_NUMBER_RIGHT_MARGIN) |
| 64 | + } |
| 65 | + |
| 66 | + fn resolve_line_number_baseline_offset(&self, _: &TextStyle, _: &Window) -> Pixels { |
| 67 | + // The default renderer uses the body's own font and size. |
| 68 | + px(0.) |
| 69 | + } |
| 70 | + |
| 71 | + fn shape_line_number( |
| 72 | + &self, |
| 73 | + number: usize, |
| 74 | + _: bool, |
| 75 | + body_style: &TextStyle, |
| 76 | + window: &Window, |
| 77 | + _: &App, |
| 78 | + ) -> ShapedLine { |
| 79 | + let label: SharedString = number.to_string().into(); |
| 80 | + // Right alignment uses the shaped advance, so padding characters would |
| 81 | + // change the label's measured width rather than provide layout spacing. |
| 82 | + let run = TextRun { |
| 83 | + len: label.len(), |
| 84 | + font: body_style.font(), |
| 85 | + color: body_style.color, |
| 86 | + background_color: None, |
| 87 | + underline: None, |
| 88 | + strikethrough: None, |
| 89 | + }; |
| 90 | + window.text_system().shape_line( |
| 91 | + label, |
| 92 | + body_style.font_size.to_pixels(window.rem_size()), |
| 93 | + &[run], |
| 94 | + None, |
| 95 | + ) |
| 96 | + } |
| 97 | +} |
4 | 98 |
|
5 | 99 | /// State for source-code editing. |
6 | 100 | /// |
@@ -224,22 +318,51 @@ impl EditorState { |
224 | 318 | } |
225 | 319 |
|
226 | 320 | /// An unstyled source-code editor. |
227 | | -#[derive(IntoElement)] |
228 | | -pub struct Editor { |
| 321 | +pub struct Editor<R = ()> { |
229 | 322 | state: Entity<EditorState>, |
| 323 | + line_number_renderer: R, |
230 | 324 | } |
231 | 325 |
|
232 | 326 | impl Editor { |
233 | 327 | pub fn new(state: &Entity<EditorState>) -> Self { |
234 | 328 | Self { |
235 | 329 | state: state.clone(), |
| 330 | + line_number_renderer: (), |
236 | 331 | } |
237 | 332 | } |
238 | 333 | } |
239 | 334 |
|
240 | | -impl RenderOnce for Editor { |
241 | | - fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement { |
242 | | - self.state |
| 335 | +impl<R: EditorLineNumberRenderer> Editor<R> { |
| 336 | + /// Supplies presentation for this element's frame without changing editor state. |
| 337 | + #[doc(hidden)] |
| 338 | + pub fn with_line_number_renderer<T: EditorLineNumberRenderer>(self, renderer: T) -> Editor<T> { |
| 339 | + Editor { |
| 340 | + state: self.state, |
| 341 | + line_number_renderer: renderer, |
| 342 | + } |
| 343 | + } |
| 344 | +} |
| 345 | + |
| 346 | +impl<R: EditorLineNumberRenderer> IntoElement for Editor<R> { |
| 347 | + type Element = ViewElement<Self>; |
| 348 | + |
| 349 | + fn into_element(self) -> Self::Element { |
| 350 | + ViewElement::new(self) |
| 351 | + } |
| 352 | +} |
| 353 | + |
| 354 | +impl<R: EditorLineNumberRenderer> View for Editor<R> { |
| 355 | + fn entity_id(&self) -> Option<EntityId> { |
| 356 | + Some(self.state.entity_id()) |
| 357 | + } |
| 358 | + |
| 359 | + fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { |
| 360 | + // Preserve the entity's identity for invalidation and element-state scoping. |
| 361 | + self.state.update(cx, |state, cx| { |
| 362 | + state |
| 363 | + .render_with_line_numbers(self.line_number_renderer, window, cx) |
| 364 | + .into_any_element() |
| 365 | + }) |
243 | 366 | } |
244 | 367 | } |
245 | 368 |
|
|
0 commit comments