Introduce text::Shaping enum and replace magic boolean

This commit is contained in:
Héctor Ramón Jiménez 2023-04-19 02:00:45 +02:00
parent 33b5a90019
commit 4bd290afe7
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
25 changed files with 203 additions and 132 deletions

View file

@ -46,6 +46,7 @@ where
size: f32,
spacing: f32,
text_size: Option<f32>,
text_shaping: text::Shaping,
font: Option<Renderer::Font>,
icon: Icon<Renderer::Font>,
style: <Renderer::Theme as StyleSheet>::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<Font> {
pub code_point: char,
/// Font size of the content.
pub size: Option<f32>,
/// The shaping strategy of the icon.
pub shaping: text::Shaping,
}

View file

@ -31,6 +31,7 @@ where
width: f32,
padding: Padding,
text_size: Option<f32>,
text_shaping: text::Shaping,
font: Option<Renderer::Font>,
style: <Renderer::Theme as StyleSheet>::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<Renderer::Font>) -> 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<T>,
padding: Padding,
text_size: Option<f32>,
text_shaping: text::Shaping,
font: Option<Renderer::Font>,
style: <Renderer::Theme as StyleSheet>::Style,
}
@ -500,7 +511,7 @@ where
},
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Center,
advanced_shape: false,
shaping: self.text_shaping,
});
}
}

View file

@ -36,6 +36,7 @@ where
width: Length,
padding: Padding,
text_size: Option<f32>,
text_shaping: text::Shaping,
font: Option<Renderer::Font>,
handle: Handle<Renderer::Font>,
style: <Renderer::Theme as StyleSheet>::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<Renderer::Font>) -> 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<Font> {
pub code_point: char,
/// Font size of the content.
pub size: Option<f32>,
/// The shaping strategy of the icon.
pub shaping: text::Shaping,
}
/// Computes the layout of a [`PickList`].
@ -345,6 +358,7 @@ pub fn layout<Renderer, T>(
width: Length,
padding: Padding,
text_size: Option<f32>,
text_shaping: text::Shaping,
font: Option<Renderer::Font>,
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<T>,
padding: Padding,
text_size: Option<f32>,
text_shaping: text::Shaping,
font: Renderer::Font,
options: &'a [T],
style: <Renderer::Theme as StyleSheet>::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<f32>,
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,
});
}
}

View file

@ -81,6 +81,7 @@ where
size: f32,
spacing: f32,
text_size: Option<f32>,
text_shaping: text::Shaping,
font: Option<Renderer::Font>,
style: <Renderer::Theme as StyleSheet>::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<Renderer::Font>) -> 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,
);
}
}

View file

@ -1,3 +1,4 @@
pub use crate::core::text::Shaping;
pub use crate::core::widget::text::*;
pub type Text<'a, Renderer = crate::Renderer> =

View file

@ -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<Renderer>(
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<Renderer>(
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<Renderer>(
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)?;

View file

@ -43,6 +43,7 @@ where
size: f32,
text_size: Option<f32>,
text_alignment: alignment::Horizontal,
text_shaping: text::Shaping,
spacing: f32,
font: Option<Renderer::Font>,
style: <Renderer::Theme as StyleSheet>::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<Pixels>) -> 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,
);
}