Allow setting text background color via TextStyle

This commit is contained in:
Antonio Scandurra 2023-11-23 19:22:18 +01:00
parent 1ad22231d2
commit 7eeb8078f6
4 changed files with 23 additions and 2 deletions

View file

@ -9410,6 +9410,7 @@ impl Render for Editor {
font_weight: FontWeight::NORMAL, font_weight: FontWeight::NORMAL,
font_style: FontStyle::Normal, font_style: FontStyle::Normal,
line_height: relative(1.).into(), line_height: relative(1.).into(),
background_color: None,
underline: None, underline: None,
white_space: WhiteSpace::Normal, white_space: WhiteSpace::Normal,
}, },
@ -9424,6 +9425,7 @@ impl Render for Editor {
font_weight: FontWeight::NORMAL, font_weight: FontWeight::NORMAL,
font_style: FontStyle::Normal, font_style: FontStyle::Normal,
line_height: relative(settings.buffer_line_height.value()), line_height: relative(settings.buffer_line_height.value()),
background_color: None,
underline: None, underline: None,
white_space: WhiteSpace::Normal, white_space: WhiteSpace::Normal,
}, },

View file

@ -2452,7 +2452,7 @@ impl LineWithInvisibles {
len: line_chunk.len(), len: line_chunk.len(),
font: text_style.font(), font: text_style.font(),
color: text_style.color, color: text_style.color,
background_color: None, background_color: text_style.background_color,
underline: text_style.underline, underline: text_style.underline,
}); });

View file

@ -145,6 +145,7 @@ pub struct TextStyle {
pub line_height: DefiniteLength, pub line_height: DefiniteLength,
pub font_weight: FontWeight, pub font_weight: FontWeight,
pub font_style: FontStyle, pub font_style: FontStyle,
pub background_color: Option<Hsla>,
pub underline: Option<UnderlineStyle>, pub underline: Option<UnderlineStyle>,
pub white_space: WhiteSpace, pub white_space: WhiteSpace,
} }
@ -159,6 +160,7 @@ impl Default for TextStyle {
line_height: phi(), line_height: phi(),
font_weight: FontWeight::default(), font_weight: FontWeight::default(),
font_style: FontStyle::default(), font_style: FontStyle::default(),
background_color: None,
underline: None, underline: None,
white_space: WhiteSpace::Normal, white_space: WhiteSpace::Normal,
} }
@ -182,6 +184,10 @@ impl TextStyle {
self.color.fade_out(factor); self.color.fade_out(factor);
} }
if let Some(background_color) = style.background_color {
self.background_color = Some(background_color);
}
if let Some(underline) = style.underline { if let Some(underline) = style.underline {
self.underline = Some(underline); self.underline = Some(underline);
} }
@ -212,7 +218,7 @@ impl TextStyle {
style: self.font_style, style: self.font_style,
}, },
color: self.color, color: self.color,
background_color: None, background_color: self.background_color,
underline: self.underline.clone(), underline: self.underline.clone(),
} }
} }
@ -223,6 +229,7 @@ pub struct HighlightStyle {
pub color: Option<Hsla>, pub color: Option<Hsla>,
pub font_weight: Option<FontWeight>, pub font_weight: Option<FontWeight>,
pub font_style: Option<FontStyle>, pub font_style: Option<FontStyle>,
pub background_color: Option<Hsla>,
pub underline: Option<UnderlineStyle>, pub underline: Option<UnderlineStyle>,
pub fade_out: Option<f32>, pub fade_out: Option<f32>,
} }
@ -441,6 +448,7 @@ impl From<&TextStyle> for HighlightStyle {
color: Some(other.color), color: Some(other.color),
font_weight: Some(other.font_weight), font_weight: Some(other.font_weight),
font_style: Some(other.font_style), font_style: Some(other.font_style),
background_color: other.background_color,
underline: other.underline.clone(), underline: other.underline.clone(),
fade_out: None, fade_out: None,
} }
@ -467,6 +475,10 @@ impl HighlightStyle {
self.font_style = other.font_style; self.font_style = other.font_style;
} }
if other.background_color.is_some() {
self.background_color = other.background_color;
}
if other.underline.is_some() { if other.underline.is_some() {
self.underline = other.underline; self.underline = other.underline;
} }

View file

@ -361,6 +361,13 @@ pub trait Styled: Sized {
self self
} }
fn text_bg(mut self, bg: impl Into<Hsla>) -> Self {
self.text_style()
.get_or_insert_with(Default::default)
.background_color = Some(bg.into());
self
}
fn text_size(mut self, size: impl Into<AbsoluteLength>) -> Self { fn text_size(mut self, size: impl Into<AbsoluteLength>) -> Self {
self.text_style() self.text_style()
.get_or_insert_with(Default::default) .get_or_insert_with(Default::default)