From 33b5a900197e2798a393d6d9a0834039666eddbb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 19 Apr 2023 01:19:56 +0200 Subject: Make basic text shaping the default shaping strategy --- widget/src/checkbox.rs | 2 ++ widget/src/overlay/menu.rs | 1 + widget/src/pick_list.rs | 3 +++ widget/src/radio.rs | 1 + widget/src/text_input.rs | 7 ++++++- widget/src/toggler.rs | 1 + 6 files changed, 14 insertions(+), 1 deletion(-) (limited to 'widget/src') diff --git a/widget/src/checkbox.rs b/widget/src/checkbox.rs index 6505cfdd..e28f76af 100644 --- a/widget/src/checkbox.rs +++ b/widget/src/checkbox.rs @@ -273,6 +273,7 @@ where color: custom_style.icon_color, horizontal_alignment: alignment::Horizontal::Center, vertical_alignment: alignment::Vertical::Center, + advanced_shape: true, }); } } @@ -292,6 +293,7 @@ where }, alignment::Horizontal::Left, alignment::Vertical::Center, + false, ); } } diff --git a/widget/src/overlay/menu.rs b/widget/src/overlay/menu.rs index c322c8ba..c904730d 100644 --- a/widget/src/overlay/menu.rs +++ b/widget/src/overlay/menu.rs @@ -500,6 +500,7 @@ where }, horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Center, + advanced_shape: false, }); } } diff --git a/widget/src/pick_list.rs b/widget/src/pick_list.rs index cd23cdd2..d44f4cae 100644 --- a/widget/src/pick_list.rs +++ b/widget/src/pick_list.rs @@ -366,6 +366,7 @@ where text_size, font.unwrap_or_else(|| renderer.default_font()), Size::new(f32::INFINITY, f32::INFINITY), + false, ); width.round() @@ -628,6 +629,7 @@ pub fn draw<'a, T, Renderer>( }, horizontal_alignment: alignment::Horizontal::Right, vertical_alignment: alignment::Vertical::Center, + advanced_shape: false, }); } @@ -653,6 +655,7 @@ pub fn draw<'a, T, Renderer>( }, horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Center, + advanced_shape: false, }); } } diff --git a/widget/src/radio.rs b/widget/src/radio.rs index c3229aed..b685c1a1 100644 --- a/widget/src/radio.rs +++ b/widget/src/radio.rs @@ -309,6 +309,7 @@ where }, alignment::Horizontal::Left, alignment::Vertical::Center, + false, ); } } diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index 9db382f7..abf858ca 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -463,6 +463,7 @@ where &icon.code_point.to_string(), icon.size.unwrap_or_else(|| renderer.default_size()), icon.font, + true, ); let mut text_node = layout::Node::new( @@ -975,6 +976,7 @@ pub fn draw( bounds: icon_layout.bounds(), horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Top, + advanced_shape: true, }); } @@ -1079,6 +1081,7 @@ pub fn draw( if text.is_empty() { placeholder } else { &text }, size, font, + true, ); let render = |renderer: &mut Renderer| { @@ -1106,6 +1109,7 @@ pub fn draw( size, horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Center, + advanced_shape: true, }); }; @@ -1311,7 +1315,7 @@ where let text_before_cursor = value.until(cursor_index).to_string(); let text_value_width = - renderer.measure_width(&text_before_cursor, size, font); + renderer.measure_width(&text_before_cursor, size, font, true); let offset = ((text_value_width + 5.0) - text_bounds.width).max(0.0); @@ -1346,6 +1350,7 @@ where Size::INFINITY, Point::new(x + offset, text_bounds.height / 2.0), true, + true, ) .map(text::Hit::cursor)?; diff --git a/widget/src/toggler.rs b/widget/src/toggler.rs index 713a9c30..d3033ddb 100644 --- a/widget/src/toggler.rs +++ b/widget/src/toggler.rs @@ -249,6 +249,7 @@ where Default::default(), self.text_alignment, alignment::Vertical::Center, + false, ); } -- cgit From 4bd290afe7d81d9aaf7467b3ce91491f6600261a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 19 Apr 2023 02:00:45 +0200 Subject: Introduce `text::Shaping` enum and replace magic boolean --- widget/src/checkbox.rs | 19 ++++++++++++++++--- widget/src/overlay/menu.rs | 13 ++++++++++++- widget/src/pick_list.rs | 46 ++++++++++++++++++++++++++++++++++++---------- widget/src/radio.rs | 22 ++++++++++++++++++---- widget/src/text.rs | 1 + widget/src/text_input.rs | 18 +++++++++++------- widget/src/toggler.rs | 13 +++++++++++-- 7 files changed, 105 insertions(+), 27 deletions(-) (limited to 'widget/src') diff --git a/widget/src/checkbox.rs b/widget/src/checkbox.rs index e28f76af..2a09b8fd 100644 --- a/widget/src/checkbox.rs +++ b/widget/src/checkbox.rs @@ -46,6 +46,7 @@ where size: f32, spacing: f32, text_size: Option, + text_shaping: text::Shaping, font: Option, icon: Icon, style: ::Style, @@ -82,11 +83,13 @@ where size: Self::DEFAULT_SIZE, spacing: Self::DEFAULT_SPACING, text_size: None, + text_shaping: text::Shaping::Basic, font: None, icon: Icon { font: Renderer::ICON_FONT, code_point: Renderer::CHECKMARK_ICON, size: None, + shaping: text::Shaping::Basic, }, style: Default::default(), } @@ -116,6 +119,12 @@ where self } + /// Sets the [`text::Shaping`] strategy of the [`Checkbox`]. + pub fn text_shaping(mut self, shaping: text::Shaping) -> Self { + self.text_shaping = shaping; + self + } + /// Sets the [`Font`] of the text of the [`Checkbox`]. /// /// [`Font`]: crate::text::Renderer::Font @@ -171,7 +180,8 @@ where .size( self.text_size .unwrap_or_else(|| renderer.default_size()), - ), + ) + .shaping(self.text_shaping), ) .layout(renderer, limits) } @@ -257,6 +267,7 @@ where font, code_point, size, + shaping, } = &self.icon; let size = size.unwrap_or(bounds.height * 0.7); @@ -273,7 +284,7 @@ where color: custom_style.icon_color, horizontal_alignment: alignment::Horizontal::Center, vertical_alignment: alignment::Vertical::Center, - advanced_shape: true, + shaping: *shaping, }); } } @@ -293,7 +304,7 @@ where }, alignment::Horizontal::Left, alignment::Vertical::Center, - false, + self.text_shaping, ); } } @@ -322,4 +333,6 @@ pub struct Icon { pub code_point: char, /// Font size of the content. pub size: Option, + /// The shaping strategy of the icon. + pub shaping: text::Shaping, } diff --git a/widget/src/overlay/menu.rs b/widget/src/overlay/menu.rs index c904730d..7de3cbae 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_shaping: text::Shaping, font: Option, style: ::Style, } @@ -58,6 +59,7 @@ where width: 0.0, padding: Padding::ZERO, text_size: None, + text_shaping: text::Shaping::Basic, font: None, style: Default::default(), } @@ -81,6 +83,12 @@ where self } + /// Sets the [`text::Shaping`] strategy of the [`Menu`]. + pub fn text_shaping(mut self, shaping: text::Shaping) -> Self { + self.text_shaping = shaping; + self + } + /// Sets the font of the [`Menu`]. pub fn font(mut self, font: impl Into) -> Self { self.font = Some(font.into()); @@ -168,6 +176,7 @@ where padding, font, text_size, + text_shaping, style, } = menu; @@ -177,6 +186,7 @@ where last_selection, font, text_size, + text_shaping, padding, style: style.clone(), })); @@ -311,6 +321,7 @@ where last_selection: &'a mut Option, padding: Padding, text_size: Option, + text_shaping: text::Shaping, font: Option, style: ::Style, } @@ -500,7 +511,7 @@ where }, horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Center, - advanced_shape: false, + shaping: self.text_shaping, }); } } diff --git a/widget/src/pick_list.rs b/widget/src/pick_list.rs index d44f4cae..c0cb2946 100644 --- a/widget/src/pick_list.rs +++ b/widget/src/pick_list.rs @@ -36,6 +36,7 @@ where width: Length, padding: Padding, text_size: Option, + text_shaping: text::Shaping, font: Option, handle: Handle, style: ::Style, @@ -71,6 +72,7 @@ where width: Length::Shrink, padding: Self::DEFAULT_PADDING, text_size: None, + text_shaping: text::Shaping::Basic, font: None, handle: Default::default(), style: Default::default(), @@ -101,6 +103,12 @@ where self } + /// Sets the [`text::Shaping`] strategy of the [`PickList`]. + pub fn text_shaping(mut self, shaping: text::Shaping) -> Self { + self.text_shaping = shaping; + self + } + /// Sets the font of the [`PickList`]. pub fn font(mut self, font: impl Into) -> Self { self.font = Some(font.into()); @@ -164,6 +172,7 @@ where self.width, self.padding, self.text_size, + self.text_shaping, self.font, self.placeholder.as_deref(), &self.options, @@ -221,6 +230,7 @@ where cursor_position, self.padding, self.text_size, + self.text_shaping, font, self.placeholder.as_deref(), self.selected.as_ref(), @@ -243,6 +253,7 @@ where state, self.padding, self.text_size, + self.text_shaping, self.font.unwrap_or_else(|| renderer.default_font()), &self.options, self.style.clone(), @@ -336,6 +347,8 @@ pub struct Icon { pub code_point: char, /// Font size of the content. pub size: Option, + /// The shaping strategy of the icon. + pub shaping: text::Shaping, } /// Computes the layout of a [`PickList`]. @@ -345,6 +358,7 @@ pub fn layout( width: Length, padding: Padding, text_size: Option, + text_shaping: text::Shaping, font: Option, placeholder: Option<&str>, options: &[T], @@ -366,7 +380,7 @@ where text_size, font.unwrap_or_else(|| renderer.default_font()), Size::new(f32::INFINITY, f32::INFINITY), - false, + text_shaping, ); width.round() @@ -516,6 +530,7 @@ pub fn overlay<'a, T, Message, Renderer>( state: &'a mut State, padding: Padding, text_size: Option, + text_shaping: text::Shaping, font: Renderer::Font, options: &'a [T], style: ::Style, @@ -543,6 +558,7 @@ where .width(bounds.width) .padding(padding) .font(font) + .text_shaping(text_shaping) .style(style); if let Some(text_size) = text_size { @@ -563,6 +579,7 @@ pub fn draw<'a, T, Renderer>( cursor_position: Point, padding: Padding, text_size: Option, + text_shaping: text::Shaping, font: Renderer::Font, placeholder: Option<&str>, selected: Option<&T>, @@ -595,25 +612,34 @@ pub fn draw<'a, T, Renderer>( ); let handle = match handle { - Handle::Arrow { size } => { - Some((Renderer::ICON_FONT, Renderer::ARROW_DOWN_ICON, *size)) - } + Handle::Arrow { size } => Some(( + Renderer::ICON_FONT, + Renderer::ARROW_DOWN_ICON, + *size, + text::Shaping::Basic, + )), Handle::Static(Icon { font, code_point, size, - }) => Some((*font, *code_point, *size)), + shaping, + }) => Some((*font, *code_point, *size, *shaping)), Handle::Dynamic { open, closed } => { if state().is_open { - Some((open.font, open.code_point, open.size)) + Some((open.font, open.code_point, open.size, open.shaping)) } else { - Some((closed.font, closed.code_point, closed.size)) + Some(( + closed.font, + closed.code_point, + closed.size, + closed.shaping, + )) } } Handle::None => None, }; - if let Some((font, code_point, size)) = handle { + if let Some((font, code_point, size, shaping)) = handle { let size = size.unwrap_or_else(|| renderer.default_size()); renderer.fill_text(Text { @@ -629,7 +655,7 @@ pub fn draw<'a, T, Renderer>( }, horizontal_alignment: alignment::Horizontal::Right, vertical_alignment: alignment::Vertical::Center, - advanced_shape: false, + shaping, }); } @@ -655,7 +681,7 @@ pub fn draw<'a, T, Renderer>( }, horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Center, - advanced_shape: false, + shaping: text_shaping, }); } } diff --git a/widget/src/radio.rs b/widget/src/radio.rs index b685c1a1..f62f4703 100644 --- a/widget/src/radio.rs +++ b/widget/src/radio.rs @@ -81,6 +81,7 @@ where size: f32, spacing: f32, text_size: Option, + text_shaping: text::Shaping, font: Option, style: ::Style, } @@ -123,6 +124,7 @@ where size: Self::DEFAULT_SIZE, spacing: Self::DEFAULT_SPACING, //15 text_size: None, + text_shaping: text::Shaping::Basic, font: None, style: Default::default(), } @@ -152,6 +154,12 @@ where self } + /// Sets the [`text::Shaping`] strategy of the [`Radio`] button. + pub fn text_shaping(mut self, shaping: text::Shaping) -> Self { + self.text_shaping = shaping; + self + } + /// Sets the text font of the [`Radio`] button. pub fn font(mut self, font: impl Into) -> Self { self.font = Some(font.into()); @@ -192,9 +200,15 @@ where .spacing(self.spacing) .align_items(Alignment::Center) .push(Row::new().width(self.size).height(self.size)) - .push(Text::new(&self.label).width(self.width).size( - self.text_size.unwrap_or_else(|| renderer.default_size()), - )) + .push( + Text::new(&self.label) + .width(self.width) + .size( + self.text_size + .unwrap_or_else(|| renderer.default_size()), + ) + .shaping(self.text_shaping), + ) .layout(renderer, limits) } @@ -309,7 +323,7 @@ where }, alignment::Horizontal::Left, alignment::Vertical::Center, - false, + self.text_shaping, ); } } diff --git a/widget/src/text.rs b/widget/src/text.rs index 04c31edc..50aa1370 100644 --- a/widget/src/text.rs +++ b/widget/src/text.rs @@ -1,3 +1,4 @@ +pub use crate::core::text::Shaping; pub use crate::core::widget::text::*; pub type Text<'a, Renderer = crate::Renderer> = diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index abf858ca..32d0b1f8 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -463,7 +463,7 @@ where &icon.code_point.to_string(), icon.size.unwrap_or_else(|| renderer.default_size()), icon.font, - true, + text::Shaping::Advanced, ); let mut text_node = layout::Node::new( @@ -976,7 +976,7 @@ pub fn draw( bounds: icon_layout.bounds(), horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Top, - advanced_shape: true, + shaping: text::Shaping::Advanced, }); } @@ -1081,7 +1081,7 @@ pub fn draw( if text.is_empty() { placeholder } else { &text }, size, font, - true, + text::Shaping::Advanced, ); let render = |renderer: &mut Renderer| { @@ -1109,7 +1109,7 @@ pub fn draw( size, horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Center, - advanced_shape: true, + shaping: text::Shaping::Advanced, }); }; @@ -1314,8 +1314,12 @@ where { let text_before_cursor = value.until(cursor_index).to_string(); - let text_value_width = - renderer.measure_width(&text_before_cursor, size, font, true); + let text_value_width = renderer.measure_width( + &text_before_cursor, + size, + font, + text::Shaping::Advanced, + ); let offset = ((text_value_width + 5.0) - text_bounds.width).max(0.0); @@ -1348,9 +1352,9 @@ where size, font, Size::INFINITY, + text::Shaping::Advanced, Point::new(x + offset, text_bounds.height / 2.0), true, - true, ) .map(text::Hit::cursor)?; diff --git a/widget/src/toggler.rs b/widget/src/toggler.rs index d3033ddb..639bbb3b 100644 --- a/widget/src/toggler.rs +++ b/widget/src/toggler.rs @@ -43,6 +43,7 @@ where size: f32, text_size: Option, text_alignment: alignment::Horizontal, + text_shaping: text::Shaping, spacing: f32, font: Option, style: ::Style, @@ -80,6 +81,7 @@ where size: Self::DEFAULT_SIZE, text_size: None, text_alignment: alignment::Horizontal::Left, + text_shaping: text::Shaping::Basic, spacing: 0.0, font: None, style: Default::default(), @@ -110,6 +112,12 @@ where self } + /// Sets the [`text::Shaping`] strategy of the [`Toggler`]. + pub fn text_shaping(mut self, shaping: text::Shaping) -> Self { + self.text_shaping = shaping; + self + } + /// Sets the spacing between the [`Toggler`] and the text. pub fn spacing(mut self, spacing: impl Into) -> Self { self.spacing = spacing.into().0; @@ -167,7 +175,8 @@ where .size( self.text_size .unwrap_or_else(|| renderer.default_size()), - ), + ) + .shaping(self.text_shaping), ); } @@ -249,7 +258,7 @@ where Default::default(), self.text_alignment, alignment::Vertical::Center, - false, + self.text_shaping, ); } -- cgit