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;
|
2024-07-17 18:47:58 +02:00
|
|
|
use crate::text;
|
|
|
|
|
use crate::text::paragraph::{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
|
2024-03-24 05:03:09 +01:00
|
|
|
Theme: Catalog,
|
2022-06-29 10:51:01 +02:00
|
|
|
Renderer: text::Renderer,
|
|
|
|
|
{
|
2024-04-01 21:47:55 +02:00
|
|
|
fragment: Fragment<'a>,
|
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-24 05:03:09 +01:00
|
|
|
class: Theme::Class<'a>,
|
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
|
2024-03-24 05:03:09 +01:00
|
|
|
Theme: Catalog,
|
2022-06-29 10:51:01 +02:00
|
|
|
Renderer: text::Renderer,
|
|
|
|
|
{
|
2019-11-21 13:47:20 +01:00
|
|
|
/// Create a new fragment of [`Text`] with the given contents.
|
2024-04-01 21:47:55 +02:00
|
|
|
pub fn new(fragment: impl IntoFragment<'a>) -> Self {
|
2019-11-21 13:47:20 +01:00
|
|
|
Text {
|
2024-04-01 21:47:55 +02:00
|
|
|
fragment: fragment.into_fragment(),
|
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-24 05:03:09 +01:00
|
|
|
class: Theme::default(),
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-12 15:11:30 +02:00
|
|
|
/// Centers the [`Text`], both horizontally and vertically.
|
|
|
|
|
pub fn center(self) -> Self {
|
2024-07-12 18:12:34 +02:00
|
|
|
self.align_x(alignment::Horizontal::Center)
|
|
|
|
|
.align_y(alignment::Vertical::Center)
|
2024-07-12 15:11:30 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-30 14:20:52 +02:00
|
|
|
/// Sets the [`alignment::Horizontal`] of the [`Text`].
|
2024-07-12 15:11:30 +02:00
|
|
|
pub fn align_x(
|
2019-11-21 13:47:20 +01:00
|
|
|
mut self,
|
2024-07-12 15:11:30 +02:00
|
|
|
alignment: impl Into<alignment::Horizontal>,
|
2019-11-21 13:47:20 +01:00
|
|
|
) -> Self {
|
2024-07-12 15:11:30 +02:00
|
|
|
self.horizontal_alignment = alignment.into();
|
2019-11-21 13:47:20 +01:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-30 14:20:52 +02:00
|
|
|
/// Sets the [`alignment::Vertical`] of the [`Text`].
|
2024-07-12 15:11:30 +02:00
|
|
|
pub fn align_y(
|
2021-09-20 15:09:55 +07:00
|
|
|
mut self,
|
2024-07-12 15:11:30 +02:00
|
|
|
alignment: impl Into<alignment::Vertical>,
|
2021-09-20 15:09:55 +07:00
|
|
|
) -> Self {
|
2024-07-12 15:11:30 +02:00
|
|
|
self.vertical_alignment = alignment.into();
|
2019-11-21 13:47:20 +01:00
|
|
|
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
|
|
|
|
|
}
|
2024-03-24 05:03:09 +01:00
|
|
|
|
|
|
|
|
/// Sets the style of the [`Text`].
|
|
|
|
|
#[must_use]
|
|
|
|
|
pub fn style(mut self, style: impl Fn(&Theme) -> Style + 'a) -> Self
|
|
|
|
|
where
|
|
|
|
|
Theme::Class<'a>: From<StyleFn<'a, Theme>>,
|
|
|
|
|
{
|
|
|
|
|
self.class = (Box::new(style) as StyleFn<'a, Theme>).into();
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets the [`Color`] of the [`Text`].
|
|
|
|
|
pub fn color(self, color: impl Into<Color>) -> Self
|
|
|
|
|
where
|
|
|
|
|
Theme::Class<'a>: From<StyleFn<'a, Theme>>,
|
|
|
|
|
{
|
|
|
|
|
self.color_maybe(Some(color))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets the [`Color`] of the [`Text`], if `Some`.
|
|
|
|
|
pub fn color_maybe(self, color: Option<impl Into<Color>>) -> Self
|
|
|
|
|
where
|
|
|
|
|
Theme::Class<'a>: From<StyleFn<'a, Theme>>,
|
|
|
|
|
{
|
|
|
|
|
let color = color.map(Into::into);
|
|
|
|
|
|
|
|
|
|
self.style(move |_theme| Style { color })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets the style class of the [`Text`].
|
|
|
|
|
#[cfg(feature = "advanced")]
|
|
|
|
|
#[must_use]
|
|
|
|
|
pub fn class(mut self, class: impl Into<Theme::Class<'a>>) -> Self {
|
|
|
|
|
self.class = class.into();
|
|
|
|
|
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)]
|
2024-07-17 18:47:58 +02:00
|
|
|
pub struct State<P: Paragraph>(paragraph::Plain<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
|
2024-03-24 05:03:09 +01:00
|
|
|
Theme: Catalog,
|
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 {
|
2024-07-17 18:47:58 +02:00
|
|
|
tree::State::new(State::<Renderer::Paragraph>(
|
|
|
|
|
paragraph::Plain::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,
|
2024-04-01 21:47:55 +02:00
|
|
|
&self.fragment,
|
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,
|
2024-03-24 05:03:09 +01:00
|
|
|
defaults: &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-24 05:03:09 +01:00
|
|
|
let style = theme.style(&self.class);
|
2024-03-04 19:31:26 +01:00
|
|
|
|
2024-03-24 05:03:09 +01:00
|
|
|
draw(renderer, defaults, layout, state, style, 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>,
|
2024-03-24 05:03:09 +01:00
|
|
|
appearance: Style,
|
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(
|
2024-07-17 18:47:58 +02:00
|
|
|
paragraph.raw(),
|
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-24 05:03:09 +01:00
|
|
|
Theme: Catalog + '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-24 05:03:09 +01:00
|
|
|
Theme: Catalog + '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-24 05:03:09 +01:00
|
|
|
Theme: Catalog + '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)]
|
2024-03-24 05:03:09 +01:00
|
|
|
pub struct Style {
|
2023-03-04 05:37:11 +01:00
|
|
|
/// 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-24 05:03:09 +01:00
|
|
|
/// The theme catalog of a [`Text`].
|
|
|
|
|
pub trait Catalog: Sized {
|
|
|
|
|
/// The item class of this [`Catalog`].
|
|
|
|
|
type Class<'a>;
|
2024-03-04 19:31:26 +01:00
|
|
|
|
2024-03-24 05:03:09 +01:00
|
|
|
/// The default class produced by this [`Catalog`].
|
|
|
|
|
fn default<'a>() -> Self::Class<'a>;
|
|
|
|
|
|
|
|
|
|
/// The [`Style`] of a class with the given status.
|
|
|
|
|
fn style(&self, item: &Self::Class<'_>) -> Style;
|
2024-03-04 19:31:26 +01:00
|
|
|
}
|
|
|
|
|
|
2024-03-24 05:03:09 +01:00
|
|
|
/// A styling function for a [`Text`].
|
|
|
|
|
///
|
|
|
|
|
/// This is just a boxed closure: `Fn(&Theme, Status) -> Style`.
|
|
|
|
|
pub type StyleFn<'a, Theme> = Box<dyn Fn(&Theme) -> Style + 'a>;
|
|
|
|
|
|
|
|
|
|
impl Catalog for Theme {
|
|
|
|
|
type Class<'a> = StyleFn<'a, Self>;
|
|
|
|
|
|
|
|
|
|
fn default<'a>() -> Self::Class<'a> {
|
|
|
|
|
Box::new(|_theme| Style::default())
|
2024-03-06 20:30:58 +01:00
|
|
|
}
|
|
|
|
|
|
2024-03-24 05:03:09 +01:00
|
|
|
fn style(&self, class: &Self::Class<'_>) -> Style {
|
|
|
|
|
class(self)
|
2024-03-06 20:30:58 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-04-01 21:36:08 +02:00
|
|
|
|
2024-07-13 13:26:22 +02:00
|
|
|
/// The default text styling; color is inherited.
|
|
|
|
|
pub fn default(_theme: &Theme) -> Style {
|
|
|
|
|
Style { color: None }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Text with the default base color.
|
|
|
|
|
pub fn base(theme: &Theme) -> Style {
|
|
|
|
|
Style {
|
|
|
|
|
color: Some(theme.palette().text),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-11 04:57:40 +02:00
|
|
|
/// Text conveying some important information, like an action.
|
|
|
|
|
pub fn primary(theme: &Theme) -> Style {
|
|
|
|
|
Style {
|
|
|
|
|
color: Some(theme.palette().primary),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Text conveying some secondary information, like a footnote.
|
|
|
|
|
pub fn secondary(theme: &Theme) -> Style {
|
|
|
|
|
Style {
|
|
|
|
|
color: Some(theme.extended_palette().secondary.strong.color),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Text conveying some positive information, like a successful event.
|
|
|
|
|
pub fn success(theme: &Theme) -> Style {
|
|
|
|
|
Style {
|
|
|
|
|
color: Some(theme.palette().success),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Text conveying some negative information, like an error.
|
|
|
|
|
pub fn danger(theme: &Theme) -> Style {
|
|
|
|
|
Style {
|
|
|
|
|
color: Some(theme.palette().danger),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 21:47:55 +02:00
|
|
|
/// A fragment of [`Text`].
|
2024-04-01 21:36:08 +02:00
|
|
|
///
|
|
|
|
|
/// This is just an alias to a string that may be either
|
|
|
|
|
/// borrowed or owned.
|
2024-04-01 21:47:55 +02:00
|
|
|
pub type Fragment<'a> = Cow<'a, str>;
|
2024-04-01 21:36:08 +02:00
|
|
|
|
2024-04-01 21:47:55 +02:00
|
|
|
/// A trait for converting a value to some text [`Fragment`].
|
|
|
|
|
pub trait IntoFragment<'a> {
|
|
|
|
|
/// Converts the value to some text [`Fragment`].
|
|
|
|
|
fn into_fragment(self) -> Fragment<'a>;
|
2024-04-01 21:36:08 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-02 10:08:10 +02:00
|
|
|
impl<'a> IntoFragment<'a> for Fragment<'a> {
|
|
|
|
|
fn into_fragment(self) -> Fragment<'a> {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, 'b> IntoFragment<'a> for &'a Fragment<'b> {
|
|
|
|
|
fn into_fragment(self) -> Fragment<'a> {
|
|
|
|
|
Fragment::Borrowed(self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 21:47:55 +02:00
|
|
|
impl<'a> IntoFragment<'a> for &'a str {
|
|
|
|
|
fn into_fragment(self) -> Fragment<'a> {
|
|
|
|
|
Fragment::Borrowed(self)
|
2024-04-01 21:36:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 21:47:55 +02:00
|
|
|
impl<'a> IntoFragment<'a> for &'a String {
|
|
|
|
|
fn into_fragment(self) -> Fragment<'a> {
|
|
|
|
|
Fragment::Borrowed(self.as_str())
|
2024-04-01 21:36:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 21:47:55 +02:00
|
|
|
impl<'a> IntoFragment<'a> for String {
|
|
|
|
|
fn into_fragment(self) -> Fragment<'a> {
|
|
|
|
|
Fragment::Owned(self)
|
2024-04-01 21:36:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 21:47:55 +02:00
|
|
|
macro_rules! into_fragment {
|
2024-04-01 21:36:08 +02:00
|
|
|
($type:ty) => {
|
2024-04-01 21:47:55 +02:00
|
|
|
impl<'a> IntoFragment<'a> for $type {
|
|
|
|
|
fn into_fragment(self) -> Fragment<'a> {
|
|
|
|
|
Fragment::Owned(self.to_string())
|
2024-04-01 21:36:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 21:47:55 +02:00
|
|
|
impl<'a> IntoFragment<'a> for &$type {
|
|
|
|
|
fn into_fragment(self) -> Fragment<'a> {
|
|
|
|
|
Fragment::Owned(self.to_string())
|
2024-04-01 21:36:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 21:47:55 +02:00
|
|
|
into_fragment!(char);
|
|
|
|
|
into_fragment!(bool);
|
2024-04-01 21:36:08 +02:00
|
|
|
|
2024-04-01 21:47:55 +02:00
|
|
|
into_fragment!(u8);
|
|
|
|
|
into_fragment!(u16);
|
|
|
|
|
into_fragment!(u32);
|
|
|
|
|
into_fragment!(u64);
|
|
|
|
|
into_fragment!(u128);
|
2024-04-02 09:24:22 +02:00
|
|
|
into_fragment!(usize);
|
2024-04-01 21:36:08 +02:00
|
|
|
|
2024-04-01 21:47:55 +02:00
|
|
|
into_fragment!(i8);
|
|
|
|
|
into_fragment!(i16);
|
|
|
|
|
into_fragment!(i32);
|
|
|
|
|
into_fragment!(i64);
|
|
|
|
|
into_fragment!(i128);
|
2024-04-02 09:24:22 +02:00
|
|
|
into_fragment!(isize);
|
2024-04-01 21:36:08 +02:00
|
|
|
|
2024-04-01 21:47:55 +02:00
|
|
|
into_fragment!(f32);
|
|
|
|
|
into_fragment!(f64);
|