From b1e67f32af9aa180dd501771ed1af4245ccdb64c Mon Sep 17 00:00:00 2001 From: temportalflux Date: Mon, 22 Jun 2026 14:22:02 -0400 Subject: [PATCH] change shape_text api to support non-SharedString types as input without forcing extra allocations --- crates/gpui/src/elements/div.rs | 2 +- crates/gpui/src/text_system.rs | 21 +++++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/crates/gpui/src/elements/div.rs b/crates/gpui/src/elements/div.rs index 5538d3d92a..142dc2395a 100644 --- a/crates/gpui/src/elements/div.rs +++ b/crates/gpui/src/elements/div.rs @@ -2278,7 +2278,7 @@ impl Interactivity { if let Some(text) = window .text_system() .shape_text( - element_id.into(), + &element_id, FONT_SIZE, &[window.text_style().to_run(str_len)], None, diff --git a/crates/gpui/src/text_system.rs b/crates/gpui/src/text_system.rs index 043d37f679..82adf0c7bb 100644 --- a/crates/gpui/src/text_system.rs +++ b/crates/gpui/src/text_system.rs @@ -504,11 +504,20 @@ impl WindowTextSystem { } /// Shape a multi line string of text, at the given font_size, for painting to the screen. - /// Subsets of the text can be styled independently with the `runs` parameter. + /// Subsets of the text can be styled independently with the `runs` parameter, + /// where each run dictates the length of utf8 characters in `text` that it styles. + /// The length (utf8 characters) of last item in `runs` is semantically ignored as it + /// represents the "rest" of the `text`. + /// /// If `wrap_width` is provided, the line breaks will be adjusted to fit within the given width. - pub fn shape_text( + /// + /// If the text provided is SharedString and does not contain new-lines, + /// it will be used as-is without additional allocations. + /// If the text provided is not a SharedString or contains new-lines, new SharedStrings + /// will be allocated for each substring between new-line characters (minimum of 1). + pub fn shape_text + Into>( &self, - text: SharedString, + text: S, font_size: Pixels, runs: &[TextRun], wrap_width: Option, @@ -598,7 +607,7 @@ impl WindowTextSystem { } }; - let mut split_lines = text.split('\n'); + let mut split_lines = text.as_ref().split('\n'); // Special case single lines to prevent allocating a sharedstring if let Some(first_line) = split_lines.next() @@ -625,8 +634,8 @@ impl WindowTextSystem { ); } } else { - let end = text.len(); - process_line(text, 0, end); + let end = text.as_ref().len(); + process_line(text.into(), 0, end); } self.font_runs_pool.lock().push(font_runs);