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.
This commit is contained in:
Jason Lee
2025-11-06 01:50:23 +00:00
committed by GitHub
parent 622d626a29
commit 64c8c19e1b
5 changed files with 27 additions and 62 deletions
+7 -22
View File
@@ -1701,9 +1701,7 @@ impl EditorElement {
len, len,
font, font,
color, color,
background_color: None, ..Default::default()
strikethrough: None,
underline: None,
}], }],
None, None,
) )
@@ -3583,9 +3581,7 @@ impl EditorElement {
len: line.len(), len: line.len(),
font: style.text.font(), font: style.text.font(),
color: placeholder_color, color: placeholder_color,
background_color: None, ..Default::default()
underline: None,
strikethrough: None,
}; };
let line = window.text_system().shape_line( let line = window.text_system().shape_line(
line.to_string().into(), line.to_string().into(),
@@ -7440,9 +7436,7 @@ impl EditorElement {
len: column, len: column,
font: style.text.font(), font: style.text.font(),
color: Hsla::default(), color: Hsla::default(),
background_color: None, ..Default::default()
underline: None,
strikethrough: None,
}], }],
None, None,
); );
@@ -7465,9 +7459,7 @@ impl EditorElement {
len: text.len(), len: text.len(),
font: self.style.text.font(), font: self.style.text.font(),
color, color,
background_color: None, ..Default::default()
underline: None,
strikethrough: None,
}; };
window.text_system().shape_line( window.text_system().shape_line(
text, text,
@@ -9568,9 +9560,7 @@ impl Element for EditorElement {
len: tab_len, len: tab_len,
font: self.style.text.font(), font: self.style.text.font(),
color: cx.theme().colors().editor_invisible, color: cx.theme().colors().editor_invisible,
background_color: None, ..Default::default()
underline: None,
strikethrough: None,
}], }],
None, None,
); );
@@ -9584,9 +9574,7 @@ impl Element for EditorElement {
len: space_len, len: space_len,
font: self.style.text.font(), font: self.style.text.font(),
color: cx.theme().colors().editor_invisible, color: cx.theme().colors().editor_invisible,
background_color: None, ..Default::default()
underline: None,
strikethrough: None,
}], }],
None, None,
); );
@@ -11573,11 +11561,8 @@ mod tests {
fn generate_test_run(len: usize, color: Hsla) -> TextRun { fn generate_test_run(len: usize, color: Hsla) -> TextRun {
TextRun { TextRun {
len, len,
font: gpui::font(".SystemUIFont"),
color, color,
background_color: None, ..Default::default()
underline: None,
strikethrough: None,
} }
} }
+7 -1
View File
@@ -739,7 +739,7 @@ impl Display for FontStyle {
} }
/// A styled run of text, for use in [`crate::TextLayout`]. /// A styled run of text, for use in [`crate::TextLayout`].
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq, Default)]
pub struct TextRun { pub struct TextRun {
/// A number of utf8 bytes /// A number of utf8 bytes
pub len: usize, pub len: usize,
@@ -813,6 +813,12 @@ pub struct Font {
pub style: FontStyle, pub style: FontStyle,
} }
impl Default for Font {
fn default() -> Self {
font(".SystemUIFont")
}
}
/// Get a [`Font`] for a given name. /// Get a [`Font`] for a given name.
pub fn font(family: impl Into<SharedString>) -> Font { pub fn font(family: impl Into<SharedString>) -> Font {
Font { Font {
+4 -13
View File
@@ -315,9 +315,7 @@ impl Boundary {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::{ use crate::{Font, FontFeatures, FontStyle, FontWeight, TestAppContext, TestDispatcher, font};
Font, FontFeatures, FontStyle, FontWeight, Hsla, TestAppContext, TestDispatcher, font,
};
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
use crate::{TextRun, WindowTextSystem, WrapBoundary}; use crate::{TextRun, WindowTextSystem, WrapBoundary};
use rand::prelude::*; use rand::prelude::*;
@@ -341,10 +339,7 @@ mod tests {
weight: FontWeight::default(), weight: FontWeight::default(),
style: FontStyle::Normal, style: FontStyle::Normal,
}, },
color: Hsla::default(), ..Default::default()
background_color: None,
underline: None,
strikethrough: None,
}) })
.collect() .collect()
} }
@@ -691,16 +686,12 @@ mod tests {
font: font("Helvetica"), font: font("Helvetica"),
color: Default::default(), color: Default::default(),
underline: Default::default(), underline: Default::default(),
strikethrough: None, ..Default::default()
background_color: None,
}; };
let bold = TextRun { let bold = TextRun {
len: 0, len: 0,
font: font("Helvetica").bold(), font: font("Helvetica").bold(),
color: Default::default(), ..Default::default()
underline: Default::default(),
strikethrough: None,
background_color: None,
}; };
let text = "aa bbb cccc ddddd eeee".into(); let text = "aa bbb cccc ddddd eeee".into();
+1 -3
View File
@@ -101,9 +101,7 @@ impl TableView {
len: 0, len: 0,
font: text_font, font: text_font,
color: text_style.color, color: text_style.color,
background_color: None, ..Default::default()
underline: None,
strikethrough: None,
}]; }];
for field in table.schema.fields.iter() { for field in table.schema.fields.iter() {
+8 -23
View File
@@ -1112,9 +1112,7 @@ impl Element for TerminalElement {
len, len,
font: text_style.font(), font: text_style.font(),
color: theme.colors().terminal_ansi_background, color: theme.colors().terminal_ansi_background,
background_color: None, ..Default::default()
underline: Default::default(),
strikethrough: None,
}], }],
None, None,
) )
@@ -1322,9 +1320,8 @@ impl Element for TerminalElement {
len: text_to_mark.len(), len: text_to_mark.len(),
font: ime_style.font(), font: ime_style.font(),
color: ime_style.color, color: ime_style.color,
background_color: None,
underline: ime_style.underline, underline: ime_style.underline,
strikethrough: None, ..Default::default()
}], }],
None None
); );
@@ -1842,27 +1839,21 @@ mod tests {
len: 1, len: 1,
font: font("Helvetica"), font: font("Helvetica"),
color: Hsla::red(), color: Hsla::red(),
background_color: None, ..Default::default()
underline: None,
strikethrough: None,
}; };
let style2 = TextRun { let style2 = TextRun {
len: 1, len: 1,
font: font("Helvetica"), font: font("Helvetica"),
color: Hsla::red(), color: Hsla::red(),
background_color: None, ..Default::default()
underline: None,
strikethrough: None,
}; };
let style3 = TextRun { let style3 = TextRun {
len: 1, len: 1,
font: font("Helvetica"), font: font("Helvetica"),
color: Hsla::blue(), // Different color color: Hsla::blue(), // Different color
background_color: None, ..Default::default()
underline: None,
strikethrough: None,
}; };
let font_size = AbsoluteLength::Pixels(px(12.0)); let font_size = AbsoluteLength::Pixels(px(12.0));
@@ -1881,9 +1872,7 @@ mod tests {
len: 1, len: 1,
font: font("Helvetica"), font: font("Helvetica"),
color: Hsla::red(), color: Hsla::red(),
background_color: None, ..Default::default()
underline: None,
strikethrough: None,
}; };
let font_size = AbsoluteLength::Pixels(px(12.0)); let font_size = AbsoluteLength::Pixels(px(12.0));
@@ -1912,9 +1901,7 @@ mod tests {
len: 1, len: 1,
font: font("Helvetica"), font: font("Helvetica"),
color: Hsla::red(), color: Hsla::red(),
background_color: None, ..Default::default()
underline: None,
strikethrough: None,
}; };
let font_size = AbsoluteLength::Pixels(px(12.0)); let font_size = AbsoluteLength::Pixels(px(12.0));
@@ -1944,9 +1931,7 @@ mod tests {
len: 1, len: 1,
font: font("Helvetica"), font: font("Helvetica"),
color: Hsla::red(), color: Hsla::red(),
background_color: None, ..Default::default()
underline: None,
strikethrough: None,
}; };
let font_size = AbsoluteLength::Pixels(px(12.0)); let font_size = AbsoluteLength::Pixels(px(12.0));