History

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.

Jason Lee committed 1 year ago

Commit 64c8c191 parent 622d6265 files · +27 −62Browse files

Unified · Split

5 files
  1. crates/editor/src/element.rs +7 −22
  2. crates/gpui/src/text_system.rs +7 −1
  3. crates/gpui/src/text_system/line_wrapper.rs +4 −13
  4. crates/repl/src/outputs/table.rs +1 −3
  5. crates/terminal_view/src/terminal_element.rs +8 −23
crates/editor/src/element.rs+7−22
@@ -1701,9 +1701,7 @@ impl EditorElement { · 1700 unmodified lines
17011701 len,
17021702 font,
17031703 color,
1704− background_color: None,
1705− strikethrough: None,
1706− underline: None,
1704+ ..Default::default()
17071705 }],
17081706 None,
17091707 )
@@ -3583,9 +3581,7 @@ impl EditorElement { · 1873 unmodified lines
35833581 len: line.len(),
35843582 font: style.text.font(),
35853583 color: placeholder_color,
3586− background_color: None,
3587− underline: None,
3588− strikethrough: None,
3584+ ..Default::default()
35893585 };
35903586 let line = window.text_system().shape_line(
35913587 line.to_string().into(),
@@ -7440,9 +7436,7 @@ impl EditorElement { · 3848 unmodified lines
74407436 len: column,
74417437 font: style.text.font(),
74427438 color: Hsla::default(),
7443− background_color: None,
7444− underline: None,
7445− strikethrough: None,
7439+ ..Default::default()
74467440 }],
74477441 None,
74487442 );
@@ -7465,9 +7459,7 @@ impl EditorElement { · 16 unmodified lines
74657459 len: text.len(),
74667460 font: self.style.text.font(),
74677461 color,
7468− background_color: None,
7469− underline: None,
7470− strikethrough: None,
7462+ ..Default::default()
74717463 };
74727464 window.text_system().shape_line(
74737465 text,
@@ -9568,9 +9560,7 @@ impl Element for EditorElement { · 2094 unmodified lines
95689560 len: tab_len,
95699561 font: self.style.text.font(),
95709562 color: cx.theme().colors().editor_invisible,
9571− background_color: None,
9572− underline: None,
9573− strikethrough: None,
9563+ ..Default::default()
95749564 }],
95759565 None,
95769566 );
@@ -9584,9 +9574,7 @@ impl Element for EditorElement { · 7 unmodified lines
95849574 len: space_len,
95859575 font: self.style.text.font(),
95869576 color: cx.theme().colors().editor_invisible,
9587− background_color: None,
9588− underline: None,
9589− strikethrough: None,
9577+ ..Default::default()
95909578 }],
95919579 None,
95929580 );
@@ -11573,11 +11561,8 @@ mod tests { · 1980 unmodified lines
1157311561 fn generate_test_run(len: usize, color: Hsla) -> TextRun {
1157411562 TextRun {
1157511563 len,
11576− font: gpui::font(".SystemUIFont"),
1157711564 color,
11578− background_color: None,
11579− underline: None,
11580− strikethrough: None,
11565+ ..Default::default()
1158111566 }
1158211567 }
1158311568
crates/gpui/src/text_system.rs+7−1
@@ -739,7 +739,7 @@ impl Display for FontStyle { · 738 unmodified lines
739739 }
740740
741741 /// A styled run of text, for use in [`crate::TextLayout`].
742−#[derive(Clone, Debug, PartialEq, Eq)]
742+#[derive(Clone, Debug, PartialEq, Eq, Default)]
743743 pub struct TextRun {
744744 /// A number of utf8 bytes
745745 pub len: usize,
@@ -813,6 +813,12 @@ pub struct Font { · 67 unmodified lines
813813 pub style: FontStyle,
814814 }
815815
816+impl Default for Font {
817+ fn default() -> Self {
818+ font(".SystemUIFont")
819+ }
820+}
821+
816822 /// Get a [`Font`] for a given name.
817823 pub fn font(family: impl Into<SharedString>) -> Font {
818824 Font {
crates/gpui/src/text_system/line_wrapper.rs+4−13
@@ -315,9 +315,7 @@ impl Boundary { · 314 unmodified lines
315315 #[cfg(test)]
316316 mod tests {
317317 use super::*;
318− use crate::{
319− Font, FontFeatures, FontStyle, FontWeight, Hsla, TestAppContext, TestDispatcher, font,
320− };
318+ use crate::{Font, FontFeatures, FontStyle, FontWeight, TestAppContext, TestDispatcher, font};
321319 #[cfg(target_os = "macos")]
322320 use crate::{TextRun, WindowTextSystem, WrapBoundary};
323321 use rand::prelude::*;
@@ -341,10 +339,7 @@ mod tests { · 17 unmodified lines
341339 weight: FontWeight::default(),
342340 style: FontStyle::Normal,
343341 },
344− color: Hsla::default(),
345− background_color: None,
346− underline: None,
347− strikethrough: None,
342+ ..Default::default()
348343 })
349344 .collect()
350345 }
@@ -691,16 +686,12 @@ mod tests { · 340 unmodified lines
691686 font: font("Helvetica"),
692687 color: Default::default(),
693688 underline: Default::default(),
694− strikethrough: None,
695− background_color: None,
689+ ..Default::default()
696690 };
697691 let bold = TextRun {
698692 len: 0,
699693 font: font("Helvetica").bold(),
700− color: Default::default(),
701− underline: Default::default(),
702− strikethrough: None,
703− background_color: None,
694+ ..Default::default()
704695 };
705696
706697 let text = "aa bbb cccc ddddd eeee".into();
crates/repl/src/outputs/table.rs+1−3
@@ -101,9 +101,7 @@ impl TableView { · 100 unmodified lines
101101 len: 0,
102102 font: text_font,
103103 color: text_style.color,
104− background_color: None,
105− underline: None,
106− strikethrough: None,
104+ ..Default::default()
107105 }];
108106
109107 for field in table.schema.fields.iter() {
crates/terminal_view/src/terminal_element.rs+8−23
@@ -1112,9 +1112,7 @@ impl Element for TerminalElement { · 1111 unmodified lines
11121112 len,
11131113 font: text_style.font(),
11141114 color: theme.colors().terminal_ansi_background,
1115− background_color: None,
1116− underline: Default::default(),
1117− strikethrough: None,
1115+ ..Default::default()
11181116 }],
11191117 None,
11201118 )
@@ -1322,9 +1320,8 @@ impl Element for TerminalElement { · 201 unmodified lines
13221320 len: text_to_mark.len(),
13231321 font: ime_style.font(),
13241322 color: ime_style.color,
1325− background_color: None,
13261323 underline: ime_style.underline,
1327− strikethrough: None,
1324+ ..Default::default()
13281325 }],
13291326 None
13301327 );
@@ -1842,27 +1839,21 @@ mod tests { · 511 unmodified lines
18421839 len: 1,
18431840 font: font("Helvetica"),
18441841 color: Hsla::red(),
1845− background_color: None,
1846− underline: None,
1847− strikethrough: None,
1842+ ..Default::default()
18481843 };
18491844
18501845 let style2 = TextRun {
18511846 len: 1,
18521847 font: font("Helvetica"),
18531848 color: Hsla::red(),
1854− background_color: None,
1855− underline: None,
1856− strikethrough: None,
1849+ ..Default::default()
18571850 };
18581851
18591852 let style3 = TextRun {
18601853 len: 1,
18611854 font: font("Helvetica"),
18621855 color: Hsla::blue(), // Different color
1863− background_color: None,
1864− underline: None,
1865− strikethrough: None,
1856+ ..Default::default()
18661857 };
18671858
18681859 let font_size = AbsoluteLength::Pixels(px(12.0));
@@ -1881,9 +1872,7 @@ mod tests { · 12 unmodified lines
18811872 len: 1,
18821873 font: font("Helvetica"),
18831874 color: Hsla::red(),
1884− background_color: None,
1885− underline: None,
1886− strikethrough: None,
1875+ ..Default::default()
18871876 };
18881877
18891878 let font_size = AbsoluteLength::Pixels(px(12.0));
@@ -1912,9 +1901,7 @@ mod tests { · 22 unmodified lines
19121901 len: 1,
19131902 font: font("Helvetica"),
19141903 color: Hsla::red(),
1915− background_color: None,
1916− underline: None,
1917− strikethrough: None,
1904+ ..Default::default()
19181905 };
19191906
19201907 let font_size = AbsoluteLength::Pixels(px(12.0));
@@ -1944,9 +1931,7 @@ mod tests { · 23 unmodified lines
19441931 len: 1,
19451932 font: font("Helvetica"),
19461933 color: Hsla::red(),
1947− background_color: None,
1948− underline: None,
1949− strikethrough: None,
1934+ ..Default::default()
19501935 };
19511936
19521937 let font_size = AbsoluteLength::Pixels(px(12.0));