From 9499a8f9e6f9971dedfae563cb133232aa3cebc2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 4 May 2023 13:00:16 +0200 Subject: Support configurable `LineHeight` in text widgets --- widget/src/overlay/menu.rs | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) (limited to 'widget/src/overlay') diff --git a/widget/src/overlay/menu.rs b/widget/src/overlay/menu.rs index 7de3cbae..dfb6a22a 100644 --- a/widget/src/overlay/menu.rs +++ b/widget/src/overlay/menu.rs @@ -31,6 +31,7 @@ where width: f32, padding: Padding, text_size: Option, + text_line_height: text::LineHeight, text_shaping: text::Shaping, font: Option, style: ::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, + ) -> 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, padding: Padding, text_size: Option, + text_line_height: text::LineHeight, text_shaping: text::Shaping, font: Option, style: ::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 -- cgit