2019-09-20 19:15:31 +02:00
|
|
|
//! Write some text for your users to read.
|
2019-11-10 01:55:32 +01:00
|
|
|
use crate::{layout, Element, Hasher, Layout, Point, Widget};
|
2019-09-20 19:15:31 +02:00
|
|
|
|
|
|
|
|
use std::hash::Hash;
|
|
|
|
|
|
|
|
|
|
pub use iced_core::text::*;
|
|
|
|
|
|
|
|
|
|
impl<Message, Renderer> Widget<Message, Renderer> for Text
|
|
|
|
|
where
|
|
|
|
|
Renderer: self::Renderer,
|
|
|
|
|
{
|
2019-11-10 01:55:32 +01:00
|
|
|
fn layout(&self, renderer: &Renderer, limits: &layout::Limits) -> Layout {
|
|
|
|
|
renderer.layout(&self, limits)
|
2019-09-20 19:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn draw(
|
|
|
|
|
&self,
|
|
|
|
|
renderer: &mut Renderer,
|
2019-11-10 01:55:32 +01:00
|
|
|
layout: &Layout,
|
2019-09-20 19:15:31 +02:00
|
|
|
_cursor_position: Point,
|
2019-10-11 22:15:39 +02:00
|
|
|
) -> Renderer::Output {
|
2019-10-05 03:56:18 +02:00
|
|
|
renderer.draw(&self, layout)
|
2019-09-20 19:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn hash_layout(&self, state: &mut Hasher) {
|
|
|
|
|
self.content.hash(state);
|
|
|
|
|
self.size.hash(state);
|
2019-09-20 19:31:49 +02:00
|
|
|
self.width.hash(state);
|
|
|
|
|
self.height.hash(state);
|
2019-09-20 19:15:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The renderer of a [`Text`] fragment.
|
|
|
|
|
///
|
|
|
|
|
/// Your [renderer] will need to implement this trait before being
|
|
|
|
|
/// able to use [`Text`] in your [`UserInterface`].
|
|
|
|
|
///
|
|
|
|
|
/// [`Text`]: struct.Text.html
|
|
|
|
|
/// [renderer]: ../../renderer/index.html
|
|
|
|
|
/// [`UserInterface`]: ../../struct.UserInterface.html
|
2019-10-05 03:56:18 +02:00
|
|
|
pub trait Renderer: crate::Renderer {
|
2019-09-20 19:15:31 +02:00
|
|
|
/// Creates a [`Node`] with the given [`Style`] for the provided [`Text`]
|
|
|
|
|
/// contents and size.
|
|
|
|
|
///
|
|
|
|
|
/// You should probably use [`Node::with_measure`] to allow [`Text`] to
|
|
|
|
|
/// adapt to the dimensions of its container.
|
|
|
|
|
///
|
|
|
|
|
/// [`Node`]: ../../struct.Node.html
|
|
|
|
|
/// [`Style`]: ../../struct.Style.html
|
|
|
|
|
/// [`Text`]: struct.Text.html
|
|
|
|
|
/// [`Node::with_measure`]: ../../struct.Node.html#method.with_measure
|
2019-11-10 01:55:32 +01:00
|
|
|
fn layout(&self, text: &Text, limits: &layout::Limits) -> Layout;
|
2019-09-20 19:15:31 +02:00
|
|
|
|
|
|
|
|
/// Draws a [`Text`] fragment.
|
|
|
|
|
///
|
|
|
|
|
/// It receives:
|
|
|
|
|
/// * the bounds of the [`Text`]
|
|
|
|
|
/// * the contents of the [`Text`]
|
|
|
|
|
/// * the size of the [`Text`]
|
|
|
|
|
/// * the color of the [`Text`]
|
|
|
|
|
/// * the [`HorizontalAlignment`] of the [`Text`]
|
|
|
|
|
/// * the [`VerticalAlignment`] of the [`Text`]
|
|
|
|
|
///
|
|
|
|
|
/// [`Text`]: struct.Text.html
|
|
|
|
|
/// [`HorizontalAlignment`]: enum.HorizontalAlignment.html
|
|
|
|
|
/// [`VerticalAlignment`]: enum.VerticalAlignment.html
|
2019-11-10 01:55:32 +01:00
|
|
|
fn draw(&mut self, text: &Text, layout: &Layout) -> Self::Output;
|
2019-09-20 19:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, Message, Renderer> From<Text> for Element<'a, Message, Renderer>
|
|
|
|
|
where
|
|
|
|
|
Renderer: self::Renderer,
|
|
|
|
|
{
|
|
|
|
|
fn from(text: Text) -> Element<'a, Message, Renderer> {
|
|
|
|
|
Element::new(text)
|
|
|
|
|
}
|
|
|
|
|
}
|