Support configurable LineHeight in text widgets

This commit is contained in:
Héctor Ramón Jiménez 2023-05-04 13:00:16 +02:00
parent 8e8808f0e1
commit 9499a8f9e6
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
24 changed files with 337 additions and 42 deletions

View file

@ -31,6 +31,7 @@ where
width: f32,
padding: Padding,
text_size: Option<f32>,
text_line_height: text::LineHeight,
text_shaping: text::Shaping,
font: Option<Renderer::Font>,
style: <Renderer::Theme as StyleSheet>::Style,
@ -59,6 +60,7 @@ where
width: 0.0,
padding: Padding::ZERO,
text_size: None,
text_line_height: text::LineHeight::default(),
text_shaping: text::Shaping::Basic,
font: None,
style: Default::default(),
@ -83,6 +85,15 @@ where
self
}
/// Sets the text [`LineHeight`] of the [`Menu`].
pub fn text_line_height(
mut self,
line_height: impl Into<text::LineHeight>,
) -> Self {
self.text_line_height = line_height.into();
self
}
/// Sets the [`text::Shaping`] strategy of the [`Menu`].
pub fn text_shaping(mut self, shaping: text::Shaping) -> Self {
self.text_shaping = shaping;
@ -176,6 +187,7 @@ where
padding,
font,
text_size,
text_line_height,
text_shaping,
style,
} = menu;
@ -186,6 +198,7 @@ where
last_selection,
font,
text_size,
text_line_height,
text_shaping,
padding,
style: style.clone(),
@ -321,6 +334,7 @@ where
last_selection: &'a mut Option<T>,
padding: Padding,
text_size: Option<f32>,
text_line_height: text::LineHeight,
text_shaping: text::Shaping,
font: Option<Renderer::Font>,
style: <Renderer::Theme as StyleSheet>::Style,
@ -352,10 +366,13 @@ where
let text_size =
self.text_size.unwrap_or_else(|| renderer.default_size());
let text_line_height =
self.text_line_height.to_absolute(Pixels(text_size));
let size = {
let intrinsic = Size::new(
0.0,
(text_size * 1.2 + self.padding.vertical())
(f32::from(text_line_height) + self.padding.vertical())
* self.options.len() as f32,
);
@ -395,9 +412,12 @@ where
.text_size
.unwrap_or_else(|| renderer.default_size());
let option_height = f32::from(
self.text_line_height.to_absolute(Pixels(text_size)),
) + self.padding.vertical();
*self.hovered_option = Some(
((cursor_position.y - bounds.y)
/ (text_size * 1.2 + self.padding.vertical()))
((cursor_position.y - bounds.y) / option_height)
as usize,
);
}
@ -410,9 +430,12 @@ where
.text_size
.unwrap_or_else(|| renderer.default_size());
let option_height = f32::from(
self.text_line_height.to_absolute(Pixels(text_size)),
) + self.padding.vertical();
*self.hovered_option = Some(
((cursor_position.y - bounds.y)
/ (text_size * 1.2 + self.padding.vertical()))
((cursor_position.y - bounds.y) / option_height)
as usize,
);
@ -462,12 +485,12 @@ where
let text_size =
self.text_size.unwrap_or_else(|| renderer.default_size());
let option_height =
(text_size * 1.2 + self.padding.vertical()) as usize;
f32::from(self.text_line_height.to_absolute(Pixels(text_size)))
+ self.padding.vertical();
let offset = viewport.y - bounds.y;
let start = (offset / option_height as f32) as usize;
let end =
((offset + viewport.height) / option_height as f32).ceil() as usize;
let start = (offset / option_height) as usize;
let end = ((offset + viewport.height) / option_height).ceil() as usize;
let visible_options = &self.options[start..end.min(self.options.len())];
@ -477,9 +500,9 @@ where
let bounds = Rectangle {
x: bounds.x,
y: bounds.y + (option_height * i) as f32,
y: bounds.y + (option_height * i as f32),
width: bounds.width,
height: text_size * 1.2 + self.padding.vertical(),
height: option_height,
};
if is_selected {
@ -503,6 +526,7 @@ where
..bounds
},
size: text_size,
line_height: self.text_line_height,
font: self.font.unwrap_or_else(|| renderer.default_font()),
color: if is_selected {
appearance.selected_text_color