Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
gpui: Fix text ellipsis appearing even the flex element has space (#17149)
Jason Lee0835d456dc76
diff --git a/crates/gpui/examples/text_wrapper.rs b/crates/gpui/examples/text_wrapper.rsindex 37902f2dd8b..063d60d1983 100644--- a/crates/gpui/examples/text_wrapper.rs+++ b/crates/gpui/examples/text_wrapper.rs@@ -13,6 +13,36 @@ impl Render for HelloWorld {.p_2().gap_2().bg(gpui::white())+ .child(+ div()+ .flex()+ .flex_row()+ .gap_2()+ .child(+ div()+ .flex()+ .border_1()+ .border_color(gpui::red())+ .text_ellipsis()+ .child("longer text in flex 1"),+ )+ .child(+ div()+ .flex()+ .border_1()+ .border_color(gpui::red())+ .text_ellipsis()+ .child("short flex"),+ )+ .child(+ div()+ .overflow_hidden()+ .border_1()+ .border_color(gpui::red())+ .text_ellipsis()+ .child("A short text in normal div"),+ ),+ ).child(div().text_xl()diff --git a/crates/gpui/src/text_system/line_wrapper.rs b/crates/gpui/src/text_system/line_wrapper.rsindex 229dc3fc02f..31e852afdf5 100644--- a/crates/gpui/src/text_system/line_wrapper.rs+++ b/crates/gpui/src/text_system/line_wrapper.rs@@ -106,18 +106,29 @@ impl LineWrapper {ellipsis: Option<&str>,) -> SharedString {let mut width = px(0.);+ let mut ellipsis_width = px(0.);if let Some(ellipsis) = ellipsis {for c in ellipsis.chars() {- width += self.width_for_char(c);+ ellipsis_width += self.width_for_char(c);}}let mut char_indices = line.char_indices();+ let mut truncate_ix = 0;for (ix, c) in char_indices {+ if width + ellipsis_width <= truncate_width {+ truncate_ix = ix;+ }+let char_width = self.width_for_char(c);width += char_width;- if width > truncate_width {- return SharedString::from(format!("{}{}", &line[..ix], ellipsis.unwrap_or("")));++ if width.floor() > truncate_width {+ return SharedString::from(format!(+ "{}{}",+ &line[..truncate_ix],+ ellipsis.unwrap_or("")+ ));}}