From 64c8c19e1b36e2b9be9a87cc06bc8b5202093608 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Thu, 6 Nov 2025 09:50:23 +0800 Subject: [PATCH] gpui: Impl Default for TextRun (#41084) Release Notes: - N/A When I was implementing Input, I often used `TextRun`, but `background`, `underline` and `strikethrough` were often not used. So make change to simplify it. --- crates/editor/src/element.rs | 29 +++++------------- crates/gpui/src/text_system.rs | 8 ++++- crates/gpui/src/text_system/line_wrapper.rs | 17 +++-------- crates/repl/src/outputs/table.rs | 4 +-- crates/terminal_view/src/terminal_element.rs | 31 +++++--------------- 5 files changed, 27 insertions(+), 62 deletions(-) diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 0053afe1637309688e57625f9b7511888a0410aa..0f0d9f6e0abc25af60b2089578571360b074e6f0 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -1701,9 +1701,7 @@ impl EditorElement { len, font, color, - background_color: None, - strikethrough: None, - underline: None, + ..Default::default() }], None, ) @@ -3583,9 +3581,7 @@ impl EditorElement { len: line.len(), font: style.text.font(), color: placeholder_color, - background_color: None, - underline: None, - strikethrough: None, + ..Default::default() }; let line = window.text_system().shape_line( line.to_string().into(), @@ -7440,9 +7436,7 @@ impl EditorElement { len: column, font: style.text.font(), color: Hsla::default(), - background_color: None, - underline: None, - strikethrough: None, + ..Default::default() }], None, ); @@ -7465,9 +7459,7 @@ impl EditorElement { len: text.len(), font: self.style.text.font(), color, - background_color: None, - underline: None, - strikethrough: None, + ..Default::default() }; window.text_system().shape_line( text, @@ -9568,9 +9560,7 @@ impl Element for EditorElement { len: tab_len, font: self.style.text.font(), color: cx.theme().colors().editor_invisible, - background_color: None, - underline: None, - strikethrough: None, + ..Default::default() }], None, ); @@ -9584,9 +9574,7 @@ impl Element for EditorElement { len: space_len, font: self.style.text.font(), color: cx.theme().colors().editor_invisible, - background_color: None, - underline: None, - strikethrough: None, + ..Default::default() }], None, ); @@ -11573,11 +11561,8 @@ mod tests { fn generate_test_run(len: usize, color: Hsla) -> TextRun { TextRun { len, - font: gpui::font(".SystemUIFont"), color, - background_color: None, - underline: None, - strikethrough: None, + ..Default::default() } } diff --git a/crates/gpui/src/text_system.rs b/crates/gpui/src/text_system.rs index 0c8a32b16c5273bdcfd8b5380cf6e0698cffcbcb..39f68e3226a81633fbd82d1eab989f3a2893d9da 100644 --- a/crates/gpui/src/text_system.rs +++ b/crates/gpui/src/text_system.rs @@ -739,7 +739,7 @@ impl Display for FontStyle { } /// A styled run of text, for use in [`crate::TextLayout`]. -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, Default)] pub struct TextRun { /// A number of utf8 bytes pub len: usize, @@ -813,6 +813,12 @@ pub struct Font { pub style: FontStyle, } +impl Default for Font { + fn default() -> Self { + font(".SystemUIFont") + } +} + /// Get a [`Font`] for a given name. pub fn font(family: impl Into) -> Font { Font { diff --git a/crates/gpui/src/text_system/line_wrapper.rs b/crates/gpui/src/text_system/line_wrapper.rs index 0192a03a3238e8fad1f5f20e2b824755c0ecee4d..45159313b43c508029f2525234c80c6575d0f695 100644 --- a/crates/gpui/src/text_system/line_wrapper.rs +++ b/crates/gpui/src/text_system/line_wrapper.rs @@ -315,9 +315,7 @@ impl Boundary { #[cfg(test)] mod tests { use super::*; - use crate::{ - Font, FontFeatures, FontStyle, FontWeight, Hsla, TestAppContext, TestDispatcher, font, - }; + use crate::{Font, FontFeatures, FontStyle, FontWeight, TestAppContext, TestDispatcher, font}; #[cfg(target_os = "macos")] use crate::{TextRun, WindowTextSystem, WrapBoundary}; use rand::prelude::*; @@ -341,10 +339,7 @@ mod tests { weight: FontWeight::default(), style: FontStyle::Normal, }, - color: Hsla::default(), - background_color: None, - underline: None, - strikethrough: None, + ..Default::default() }) .collect() } @@ -691,16 +686,12 @@ mod tests { font: font("Helvetica"), color: Default::default(), underline: Default::default(), - strikethrough: None, - background_color: None, + ..Default::default() }; let bold = TextRun { len: 0, font: font("Helvetica").bold(), - color: Default::default(), - underline: Default::default(), - strikethrough: None, - background_color: None, + ..Default::default() }; let text = "aa bbb cccc ddddd eeee".into(); diff --git a/crates/repl/src/outputs/table.rs b/crates/repl/src/outputs/table.rs index 2a5864c7bca5f1bf8ac2f242e7ce694df6c00aac..f6bf30f394d2232750f7f1beb21dbbc27c0ba941 100644 --- a/crates/repl/src/outputs/table.rs +++ b/crates/repl/src/outputs/table.rs @@ -101,9 +101,7 @@ impl TableView { len: 0, font: text_font, color: text_style.color, - background_color: None, - underline: None, - strikethrough: None, + ..Default::default() }]; for field in table.schema.fields.iter() { diff --git a/crates/terminal_view/src/terminal_element.rs b/crates/terminal_view/src/terminal_element.rs index e06e8e9c63633746f336d308614ed9963740c4f8..4187f5bc3a522822d9b48d3c5ebbc4124c55c897 100644 --- a/crates/terminal_view/src/terminal_element.rs +++ b/crates/terminal_view/src/terminal_element.rs @@ -1112,9 +1112,7 @@ impl Element for TerminalElement { len, font: text_style.font(), color: theme.colors().terminal_ansi_background, - background_color: None, - underline: Default::default(), - strikethrough: None, + ..Default::default() }], None, ) @@ -1322,9 +1320,8 @@ impl Element for TerminalElement { len: text_to_mark.len(), font: ime_style.font(), color: ime_style.color, - background_color: None, underline: ime_style.underline, - strikethrough: None, + ..Default::default() }], None ); @@ -1842,27 +1839,21 @@ mod tests { len: 1, font: font("Helvetica"), color: Hsla::red(), - background_color: None, - underline: None, - strikethrough: None, + ..Default::default() }; let style2 = TextRun { len: 1, font: font("Helvetica"), color: Hsla::red(), - background_color: None, - underline: None, - strikethrough: None, + ..Default::default() }; let style3 = TextRun { len: 1, font: font("Helvetica"), color: Hsla::blue(), // Different color - background_color: None, - underline: None, - strikethrough: None, + ..Default::default() }; let font_size = AbsoluteLength::Pixels(px(12.0)); @@ -1881,9 +1872,7 @@ mod tests { len: 1, font: font("Helvetica"), color: Hsla::red(), - background_color: None, - underline: None, - strikethrough: None, + ..Default::default() }; let font_size = AbsoluteLength::Pixels(px(12.0)); @@ -1912,9 +1901,7 @@ mod tests { len: 1, font: font("Helvetica"), color: Hsla::red(), - background_color: None, - underline: None, - strikethrough: None, + ..Default::default() }; let font_size = AbsoluteLength::Pixels(px(12.0)); @@ -1944,9 +1931,7 @@ mod tests { len: 1, font: font("Helvetica"), color: Hsla::red(), - background_color: None, - underline: None, - strikethrough: None, + ..Default::default() }; let font_size = AbsoluteLength::Pixels(px(12.0));