2019-09-20 19:15:31 +02:00
|
|
|
//! Write some text for your users to read.
|
2021-09-20 15:09:55 +07:00
|
|
|
use crate::alignment;
|
|
|
|
|
use crate::layout;
|
2023-06-08 20:11:59 +02:00
|
|
|
use crate::mouse;
|
2021-10-14 16:59:19 +07:00
|
|
|
use crate::renderer;
|
2023-08-30 06:36:24 +02:00
|
|
|
use crate::text::{self, Paragraph};
|
2023-08-30 04:31:21 +02:00
|
|
|
use crate::widget::tree::{self, Tree};
|
2024-01-05 17:46:33 +01:00
|
|
|
use crate::{
|
2024-03-12 14:35:55 +01:00
|
|
|
Color, Element, Layout, Length, Pixels, Point, Rectangle, Size, Theme,
|
|
|
|
|
Widget,
|
2024-01-05 17:46:33 +01:00
|
|
|
};
|
2022-06-29 10:51:01 +02:00
|
|
|
|
2021-11-01 23:59:04 +09:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
|
2023-05-08 15:37:29 +02:00
|
|
|
pub use text::{LineHeight, Shaping};
|
|
|
|
|
|
2019-11-21 13:47:20 +01:00
|
|
|
/// A paragraph of text.
|
2022-06-29 10:51:01 +02:00
|
|
|
#[allow(missing_debug_implementations)]
|
2024-01-21 17:56:01 +01:00
|
|
|
pub struct Text<'a, Theme, Renderer>
|
2022-06-29 10:51:01 +02:00
|
|
|
where
|
|
|
|
|
Renderer: text::Renderer,
|
|
|
|
|
{
|
2021-11-01 23:59:04 +09:00
|
|
|
content: Cow<'a, str>,
|
2023-08-30 04:31:21 +02:00
|
|
|
size: Option<Pixels>,
|
2023-05-08 15:37:29 +02:00
|
|
|
line_height: LineHeight,
|
2019-11-21 13:47:20 +01:00
|
|
|
width: Length,
|
|
|
|
|
height: Length,
|
2021-09-20 15:09:55 +07:00
|
|
|
horizontal_alignment: alignment::Horizontal,
|
|
|
|
|
vertical_alignment: alignment::Vertical,
|
2023-02-04 07:33:33 +01:00
|
|
|
font: Option<Renderer::Font>,
|
2023-05-08 15:37:29 +02:00
|
|
|
shaping: Shaping,
|
2024-03-12 14:35:55 +01:00
|
|
|
style: Style<'a, Theme>,
|
2019-11-21 13:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-21 17:56:01 +01:00
|
|
|
impl<'a, Theme, Renderer> Text<'a, Theme, Renderer>
|
2022-06-29 10:51:01 +02:00
|
|
|
where
|
|
|
|
|
Renderer: text::Renderer,
|
|
|
|
|
{
|
2019-11-21 13:47:20 +01:00
|
|
|
/// Create a new fragment of [`Text`] with the given contents.
|
2024-03-12 14:35:55 +01:00
|
|
|
pub fn new(content: impl Into<Cow<'a, str>>) -> Self
|
|
|
|
|
where
|
|
|
|
|
Theme: DefaultStyle + 'a,
|
|
|
|
|
{
|
2019-11-21 13:47:20 +01:00
|
|
|
Text {
|
2021-11-01 23:59:04 +09:00
|
|
|
content: content.into(),
|
2019-11-21 13:47:20 +01:00
|
|
|
size: None,
|
2023-05-08 15:37:29 +02:00
|
|
|
line_height: LineHeight::default(),
|
2023-02-04 07:33:33 +01:00
|
|
|
font: None,
|
2019-12-30 20:54:04 +01:00
|
|
|
width: Length::Shrink,
|
2019-11-21 13:47:20 +01:00
|
|
|
height: Length::Shrink,
|
2021-09-20 15:09:55 +07:00
|
|
|
horizontal_alignment: alignment::Horizontal::Left,
|
|
|
|
|
vertical_alignment: alignment::Vertical::Top,
|
2023-05-08 15:37:29 +02:00
|
|
|
shaping: Shaping::Basic,
|
2024-03-12 14:35:55 +01:00
|
|
|
style: Box::new(Theme::default_style),
|
2019-11-21 13:47:20 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets the size of the [`Text`].
|
2023-02-04 16:41:18 +01:00
|
|
|
pub fn size(mut self, size: impl Into<Pixels>) -> Self {
|
2023-08-30 04:31:21 +02:00
|
|
|
self.size = Some(size.into());
|
2019-11-21 13:47:20 +01:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-04 13:00:16 +02:00
|
|
|
/// Sets the [`LineHeight`] of the [`Text`].
|
2023-05-08 15:37:29 +02:00
|
|
|
pub fn line_height(mut self, line_height: impl Into<LineHeight>) -> Self {
|
2023-05-04 13:00:16 +02:00
|
|
|
self.line_height = line_height.into();
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-21 13:47:20 +01:00
|
|
|
/// Sets the [`Font`] of the [`Text`].
|
|
|
|
|
///
|
2022-04-30 14:20:52 +02:00
|
|
|
/// [`Font`]: crate::text::Renderer::Font
|
2020-04-23 22:17:11 +02:00
|
|
|
pub fn font(mut self, font: impl Into<Renderer::Font>) -> Self {
|
2023-02-04 07:33:33 +01:00
|
|
|
self.font = Some(font.into());
|
2019-11-21 13:47:20 +01:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-10 00:10:53 +01:00
|
|
|
/// Sets the style of the [`Text`].
|
2024-03-12 14:35:55 +01:00
|
|
|
pub fn style(mut self, style: impl Fn(&Theme) -> Appearance + 'a) -> Self {
|
|
|
|
|
self.style = Box::new(style);
|
2024-03-04 19:31:26 +01:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets the [`Color`] of the [`Text`].
|
2024-03-12 14:35:55 +01:00
|
|
|
pub fn color(self, color: impl Into<Color>) -> Self {
|
|
|
|
|
self.color_maybe(Some(color))
|
2024-03-04 19:31:26 +01:00
|
|
|
}
|
|
|
|
|
|
2024-03-08 13:51:34 +01:00
|
|
|
/// Sets the [`Color`] of the [`Text`], if `Some`.
|
2024-03-04 19:31:26 +01:00
|
|
|
pub fn color_maybe(mut self, color: Option<impl Into<Color>>) -> Self {
|
2024-03-12 14:35:55 +01:00
|
|
|
let color = color.map(Into::into);
|
|
|
|
|
|
|
|
|
|
self.style = Box::new(move |_theme| Appearance { color });
|
2022-06-29 10:51:01 +02:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-21 13:47:20 +01:00
|
|
|
/// Sets the width of the [`Text`] boundaries.
|
2023-02-04 12:24:13 +01:00
|
|
|
pub fn width(mut self, width: impl Into<Length>) -> Self {
|
|
|
|
|
self.width = width.into();
|
2019-11-21 13:47:20 +01:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets the height of the [`Text`] boundaries.
|
2023-02-04 12:24:13 +01:00
|
|
|
pub fn height(mut self, height: impl Into<Length>) -> Self {
|
|
|
|
|
self.height = height.into();
|
2019-11-21 13:47:20 +01:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-30 14:20:52 +02:00
|
|
|
/// Sets the [`alignment::Horizontal`] of the [`Text`].
|
2019-11-21 13:47:20 +01:00
|
|
|
pub fn horizontal_alignment(
|
|
|
|
|
mut self,
|
2021-09-20 15:09:55 +07:00
|
|
|
alignment: alignment::Horizontal,
|
2019-11-21 13:47:20 +01:00
|
|
|
) -> Self {
|
|
|
|
|
self.horizontal_alignment = alignment;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-30 14:20:52 +02:00
|
|
|
/// Sets the [`alignment::Vertical`] of the [`Text`].
|
2021-09-20 15:09:55 +07:00
|
|
|
pub fn vertical_alignment(
|
|
|
|
|
mut self,
|
|
|
|
|
alignment: alignment::Vertical,
|
|
|
|
|
) -> Self {
|
2019-11-21 13:47:20 +01:00
|
|
|
self.vertical_alignment = alignment;
|
|
|
|
|
self
|
|
|
|
|
}
|
2023-04-19 01:19:56 +02:00
|
|
|
|
2023-05-08 15:37:29 +02:00
|
|
|
/// Sets the [`Shaping`] strategy of the [`Text`].
|
|
|
|
|
pub fn shaping(mut self, shaping: Shaping) -> Self {
|
2023-04-19 02:00:45 +02:00
|
|
|
self.shaping = shaping;
|
2023-04-19 01:19:56 +02:00
|
|
|
self
|
|
|
|
|
}
|
2019-11-21 13:47:20 +01:00
|
|
|
}
|
2019-09-20 19:15:31 +02:00
|
|
|
|
2023-08-30 04:31:21 +02:00
|
|
|
/// The internal state of a [`Text`] widget.
|
|
|
|
|
#[derive(Debug, Default)]
|
2023-08-30 06:36:24 +02:00
|
|
|
pub struct State<P: Paragraph>(P);
|
2023-08-30 04:31:21 +02:00
|
|
|
|
2024-01-21 17:56:01 +01:00
|
|
|
impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
|
|
|
|
|
for Text<'a, Theme, Renderer>
|
2019-09-20 19:15:31 +02:00
|
|
|
where
|
2021-10-31 16:13:03 +07:00
|
|
|
Renderer: text::Renderer,
|
2019-09-20 19:15:31 +02:00
|
|
|
{
|
2023-08-30 04:31:21 +02:00
|
|
|
fn tag(&self) -> tree::Tag {
|
|
|
|
|
tree::Tag::of::<State<Renderer::Paragraph>>()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn state(&self) -> tree::State {
|
2023-08-30 06:36:24 +02:00
|
|
|
tree::State::new(State(Renderer::Paragraph::default()))
|
2023-08-30 04:31:21 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-05 17:46:33 +01:00
|
|
|
fn size(&self) -> Size<Length> {
|
|
|
|
|
Size {
|
|
|
|
|
width: self.width,
|
|
|
|
|
height: self.height,
|
|
|
|
|
}
|
2019-11-16 22:08:49 +01:00
|
|
|
}
|
|
|
|
|
|
2019-11-10 06:05:20 +01:00
|
|
|
fn layout(
|
|
|
|
|
&self,
|
2023-08-30 06:36:24 +02:00
|
|
|
tree: &mut Tree,
|
2019-11-10 06:05:20 +01:00
|
|
|
renderer: &Renderer,
|
|
|
|
|
limits: &layout::Limits,
|
|
|
|
|
) -> layout::Node {
|
2023-08-30 04:31:21 +02:00
|
|
|
layout(
|
2023-08-30 06:36:24 +02:00
|
|
|
tree.state.downcast_mut::<State<Renderer::Paragraph>>(),
|
2023-08-30 04:31:21 +02:00
|
|
|
renderer,
|
|
|
|
|
limits,
|
|
|
|
|
self.width,
|
|
|
|
|
self.height,
|
2023-02-04 07:33:33 +01:00
|
|
|
&self.content,
|
2023-05-04 13:00:16 +02:00
|
|
|
self.line_height,
|
2023-08-30 04:31:21 +02:00
|
|
|
self.size,
|
|
|
|
|
self.font,
|
|
|
|
|
self.horizontal_alignment,
|
|
|
|
|
self.vertical_alignment,
|
2023-04-19 02:00:45 +02:00
|
|
|
self.shaping,
|
2023-08-30 04:31:21 +02:00
|
|
|
)
|
2019-09-20 19:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn draw(
|
|
|
|
|
&self,
|
2023-08-30 04:31:21 +02:00
|
|
|
tree: &Tree,
|
2021-10-14 16:59:19 +07:00
|
|
|
renderer: &mut Renderer,
|
2024-01-21 17:56:01 +01:00
|
|
|
theme: &Theme,
|
2021-10-18 15:19:04 +07:00
|
|
|
style: &renderer::Style,
|
2021-10-14 16:59:19 +07:00
|
|
|
layout: Layout<'_>,
|
2023-06-08 20:11:59 +02:00
|
|
|
_cursor_position: mouse::Cursor,
|
2023-12-01 16:04:27 +01:00
|
|
|
viewport: &Rectangle,
|
2021-10-14 16:07:22 +07:00
|
|
|
) {
|
2023-08-30 04:31:21 +02:00
|
|
|
let state = tree.state.downcast_ref::<State<Renderer::Paragraph>>();
|
2024-03-12 14:35:55 +01:00
|
|
|
let appearance = (self.style)(theme);
|
2024-03-04 19:31:26 +01:00
|
|
|
|
|
|
|
|
draw(renderer, style, layout, state, appearance, viewport);
|
2019-09-20 19:15:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 04:31:21 +02:00
|
|
|
/// Produces the [`layout::Node`] of a [`Text`] widget.
|
|
|
|
|
pub fn layout<Renderer>(
|
2023-08-30 06:36:24 +02:00
|
|
|
state: &mut State<Renderer::Paragraph>,
|
2023-08-30 04:31:21 +02:00
|
|
|
renderer: &Renderer,
|
|
|
|
|
limits: &layout::Limits,
|
|
|
|
|
width: Length,
|
|
|
|
|
height: Length,
|
|
|
|
|
content: &str,
|
|
|
|
|
line_height: LineHeight,
|
|
|
|
|
size: Option<Pixels>,
|
|
|
|
|
font: Option<Renderer::Font>,
|
|
|
|
|
horizontal_alignment: alignment::Horizontal,
|
|
|
|
|
vertical_alignment: alignment::Vertical,
|
|
|
|
|
shaping: Shaping,
|
|
|
|
|
) -> layout::Node
|
|
|
|
|
where
|
|
|
|
|
Renderer: text::Renderer,
|
|
|
|
|
{
|
2024-01-09 06:35:33 +01:00
|
|
|
layout::sized(limits, width, height, |limits| {
|
|
|
|
|
let bounds = limits.max();
|
|
|
|
|
|
|
|
|
|
let size = size.unwrap_or_else(|| renderer.default_size());
|
|
|
|
|
let font = font.unwrap_or_else(|| renderer.default_font());
|
|
|
|
|
|
|
|
|
|
let State(ref mut paragraph) = state;
|
|
|
|
|
|
|
|
|
|
paragraph.update(text::Text {
|
|
|
|
|
content,
|
|
|
|
|
bounds,
|
|
|
|
|
size,
|
|
|
|
|
line_height,
|
|
|
|
|
font,
|
|
|
|
|
horizontal_alignment,
|
|
|
|
|
vertical_alignment,
|
|
|
|
|
shaping,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
paragraph.min_bounds()
|
|
|
|
|
})
|
2023-08-30 04:31:21 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-04 19:22:29 +07:00
|
|
|
/// Draws text using the same logic as the [`Text`] widget.
|
|
|
|
|
///
|
|
|
|
|
/// Specifically:
|
|
|
|
|
///
|
|
|
|
|
/// * If no `size` is provided, the default text size of the `Renderer` will be
|
|
|
|
|
/// used.
|
|
|
|
|
/// * If no `color` is provided, the [`renderer::Style::text_color`] will be
|
|
|
|
|
/// used.
|
|
|
|
|
/// * The alignment attributes do not affect the position of the bounds of the
|
|
|
|
|
/// [`Layout`].
|
2021-10-20 19:06:53 +07:00
|
|
|
pub fn draw<Renderer>(
|
|
|
|
|
renderer: &mut Renderer,
|
|
|
|
|
style: &renderer::Style,
|
|
|
|
|
layout: Layout<'_>,
|
2023-08-30 04:31:21 +02:00
|
|
|
state: &State<Renderer::Paragraph>,
|
2022-06-29 10:51:01 +02:00
|
|
|
appearance: Appearance,
|
2023-12-01 16:04:27 +01:00
|
|
|
viewport: &Rectangle,
|
2021-10-20 19:06:53 +07:00
|
|
|
) where
|
2021-10-31 16:13:03 +07:00
|
|
|
Renderer: text::Renderer,
|
2021-10-20 19:06:53 +07:00
|
|
|
{
|
2023-08-30 06:36:24 +02:00
|
|
|
let State(ref paragraph) = state;
|
2021-10-20 19:06:53 +07:00
|
|
|
let bounds = layout.bounds();
|
|
|
|
|
|
2023-08-30 04:31:21 +02:00
|
|
|
let x = match paragraph.horizontal_alignment() {
|
2021-10-20 19:06:53 +07:00
|
|
|
alignment::Horizontal::Left => bounds.x,
|
|
|
|
|
alignment::Horizontal::Center => bounds.center_x(),
|
|
|
|
|
alignment::Horizontal::Right => bounds.x + bounds.width,
|
|
|
|
|
};
|
|
|
|
|
|
2023-08-30 04:31:21 +02:00
|
|
|
let y = match paragraph.vertical_alignment() {
|
2021-10-20 19:06:53 +07:00
|
|
|
alignment::Vertical::Top => bounds.y,
|
|
|
|
|
alignment::Vertical::Center => bounds.center_y(),
|
|
|
|
|
alignment::Vertical::Bottom => bounds.y + bounds.height,
|
|
|
|
|
};
|
|
|
|
|
|
2023-08-30 04:31:21 +02:00
|
|
|
renderer.fill_paragraph(
|
2023-08-30 06:44:40 +02:00
|
|
|
paragraph,
|
2023-08-30 04:31:21 +02:00
|
|
|
Point::new(x, y),
|
|
|
|
|
appearance.color.unwrap_or(style.text_color),
|
2023-12-01 16:04:27 +01:00
|
|
|
*viewport,
|
2023-08-30 04:31:21 +02:00
|
|
|
);
|
2021-10-20 19:06:53 +07:00
|
|
|
}
|
|
|
|
|
|
2024-01-21 17:56:01 +01:00
|
|
|
impl<'a, Message, Theme, Renderer> From<Text<'a, Theme, Renderer>>
|
|
|
|
|
for Element<'a, Message, Theme, Renderer>
|
2019-09-20 19:15:31 +02:00
|
|
|
where
|
2024-03-06 20:30:58 +01:00
|
|
|
Theme: 'a,
|
2021-10-31 16:13:03 +07:00
|
|
|
Renderer: text::Renderer + 'a,
|
2019-09-20 19:15:31 +02:00
|
|
|
{
|
2024-01-21 17:56:01 +01:00
|
|
|
fn from(
|
|
|
|
|
text: Text<'a, Theme, Renderer>,
|
|
|
|
|
) -> Element<'a, Message, Theme, Renderer> {
|
2019-09-20 19:15:31 +02:00
|
|
|
Element::new(text)
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-27 01:33:16 -04:00
|
|
|
|
2024-01-21 17:56:01 +01:00
|
|
|
impl<'a, Theme, Renderer> From<&'a str> for Text<'a, Theme, Renderer>
|
2023-03-04 05:37:11 +01:00
|
|
|
where
|
2024-03-12 14:35:55 +01:00
|
|
|
Theme: DefaultStyle + 'a,
|
2023-03-04 05:37:11 +01:00
|
|
|
Renderer: text::Renderer,
|
|
|
|
|
{
|
|
|
|
|
fn from(content: &'a str) -> Self {
|
|
|
|
|
Self::new(content)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-21 17:56:01 +01:00
|
|
|
impl<'a, Message, Theme, Renderer> From<&'a str>
|
|
|
|
|
for Element<'a, Message, Theme, Renderer>
|
2022-07-27 06:49:20 +02:00
|
|
|
where
|
2024-03-12 14:35:55 +01:00
|
|
|
Theme: DefaultStyle + 'a,
|
2022-07-27 06:49:20 +02:00
|
|
|
Renderer: text::Renderer + 'a,
|
|
|
|
|
{
|
2023-03-04 05:37:11 +01:00
|
|
|
fn from(content: &'a str) -> Self {
|
|
|
|
|
Text::from(content).into()
|
2022-07-27 06:49:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
2023-03-04 05:37:11 +01:00
|
|
|
|
2024-03-09 15:26:37 +08:00
|
|
|
/// The appearance of some text.
|
2023-03-04 05:37:11 +01:00
|
|
|
#[derive(Debug, Clone, Copy, Default)]
|
|
|
|
|
pub struct Appearance {
|
|
|
|
|
/// The [`Color`] of the text.
|
|
|
|
|
///
|
|
|
|
|
/// The default, `None`, means using the inherited color.
|
|
|
|
|
pub color: Option<Color>,
|
|
|
|
|
}
|
2024-03-04 19:31:26 +01:00
|
|
|
|
2024-03-12 14:35:55 +01:00
|
|
|
/// The style of some [`Text`].
|
|
|
|
|
pub type Style<'a, Theme> = Box<dyn Fn(&Theme) -> Appearance + 'a>;
|
2024-03-04 19:31:26 +01:00
|
|
|
|
2024-03-12 14:35:55 +01:00
|
|
|
/// The default style of some [`Text`].
|
|
|
|
|
pub trait DefaultStyle {
|
|
|
|
|
/// Returns the default style of some [`Text`].
|
|
|
|
|
fn default_style(&self) -> Appearance;
|
2024-03-04 19:31:26 +01:00
|
|
|
}
|
|
|
|
|
|
2024-03-12 14:35:55 +01:00
|
|
|
impl DefaultStyle for Theme {
|
|
|
|
|
fn default_style(&self) -> Appearance {
|
|
|
|
|
Appearance::default()
|
2024-03-06 20:30:58 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-12 14:35:55 +01:00
|
|
|
impl DefaultStyle for Color {
|
|
|
|
|
fn default_style(&self) -> Appearance {
|
|
|
|
|
Appearance { color: Some(*self) }
|
2024-03-06 20:30:58 +01:00
|
|
|
}
|
|
|
|
|
}
|