Implement Widget::draw for Text
This commit is contained in:
parent
03b3493138
commit
3a0c503db9
11 changed files with 128 additions and 32 deletions
|
|
@ -19,13 +19,17 @@
|
|||
//! [`text::Renderer`]: crate::widget::text::Renderer
|
||||
//! [`Checkbox`]: crate::widget::Checkbox
|
||||
//! [`checkbox::Renderer`]: crate::widget::checkbox::Renderer
|
||||
pub mod text;
|
||||
|
||||
pub use text::Text;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
mod null;
|
||||
#[cfg(debug_assertions)]
|
||||
pub use null::Null;
|
||||
|
||||
use crate::{layout, Element, Rectangle};
|
||||
use crate::layout;
|
||||
use crate::{Element, Rectangle};
|
||||
|
||||
/// A component that can take the state of a user interface and produce an
|
||||
/// output for its users.
|
||||
|
|
@ -48,4 +52,6 @@ pub trait Renderer: Sized {
|
|||
}
|
||||
|
||||
fn with_layer(&mut self, bounds: Rectangle, f: impl FnOnce(&mut Self));
|
||||
|
||||
fn clear(&mut self);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,12 +4,13 @@ use crate::container;
|
|||
use crate::pane_grid;
|
||||
use crate::progress_bar;
|
||||
use crate::radio;
|
||||
use crate::renderer::{self, Renderer};
|
||||
use crate::scrollable;
|
||||
use crate::slider;
|
||||
use crate::text;
|
||||
use crate::text_input;
|
||||
use crate::toggler;
|
||||
use crate::{Font, Padding, Point, Rectangle, Renderer, Size};
|
||||
use crate::{Font, Padding, Point, Rectangle, Size};
|
||||
|
||||
/// A renderer that does nothing.
|
||||
///
|
||||
|
|
@ -28,11 +29,17 @@ impl Renderer for Null {
|
|||
type Defaults = ();
|
||||
|
||||
fn with_layer(&mut self, _bounds: Rectangle, _f: impl FnOnce(&mut Self)) {}
|
||||
|
||||
fn clear(&mut self) {}
|
||||
}
|
||||
|
||||
impl renderer::Text for Null {
|
||||
type Font = Font;
|
||||
|
||||
fn fill_text(&mut self, _text: renderer::text::Section<'_, Self::Font>) {}
|
||||
}
|
||||
|
||||
impl text::Renderer for Null {
|
||||
type Font = Font;
|
||||
|
||||
fn default_size(&self) -> u16 {
|
||||
20
|
||||
}
|
||||
|
|
|
|||
20
native/src/renderer/text.rs
Normal file
20
native/src/renderer/text.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
use crate::alignment;
|
||||
use crate::{Color, Rectangle, Renderer};
|
||||
|
||||
pub trait Text: Renderer {
|
||||
/// The font type used.
|
||||
type Font: Default + Copy;
|
||||
|
||||
fn fill_text(&mut self, section: Section<'_, Self::Font>);
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Section<'a, Font> {
|
||||
pub content: &'a str,
|
||||
pub bounds: Rectangle,
|
||||
pub size: Option<f32>,
|
||||
pub color: Option<Color>,
|
||||
pub font: Font,
|
||||
pub horizontal_alignment: alignment::Horizontal,
|
||||
pub vertical_alignment: alignment::Vertical,
|
||||
}
|
||||
|
|
@ -330,6 +330,9 @@ where
|
|||
/// }
|
||||
/// ```
|
||||
pub fn draw(&mut self, renderer: &mut Renderer, cursor_position: Point) {
|
||||
// TODO: Move to shell level (?)
|
||||
renderer.clear();
|
||||
|
||||
let viewport = Rectangle::with_size(self.bounds);
|
||||
|
||||
let overlay = if let Some(mut overlay) =
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
//! Write some text for your users to read.
|
||||
use crate::alignment;
|
||||
use crate::layout;
|
||||
use crate::renderer;
|
||||
use crate::{
|
||||
Color, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget,
|
||||
};
|
||||
|
|
@ -133,13 +134,35 @@ where
|
|||
|
||||
fn draw(
|
||||
&self,
|
||||
_renderer: &mut Renderer,
|
||||
renderer: &mut Renderer,
|
||||
_defaults: &Renderer::Defaults,
|
||||
_layout: Layout<'_>,
|
||||
layout: Layout<'_>,
|
||||
_cursor_position: Point,
|
||||
_viewport: &Rectangle,
|
||||
) {
|
||||
// TODO
|
||||
let bounds = layout.bounds();
|
||||
|
||||
let x = match self.horizontal_alignment {
|
||||
alignment::Horizontal::Left => bounds.x,
|
||||
alignment::Horizontal::Center => bounds.center_x(),
|
||||
alignment::Horizontal::Right => bounds.x + bounds.width,
|
||||
};
|
||||
|
||||
let y = match self.vertical_alignment {
|
||||
alignment::Vertical::Top => bounds.y,
|
||||
alignment::Vertical::Center => bounds.center_y(),
|
||||
alignment::Vertical::Bottom => bounds.y + bounds.height,
|
||||
};
|
||||
|
||||
renderer.fill_text(renderer::text::Section {
|
||||
content: &self.content,
|
||||
size: self.size.map(f32::from),
|
||||
bounds: Rectangle { x, y, ..bounds },
|
||||
color: self.color,
|
||||
font: self.font,
|
||||
horizontal_alignment: self.horizontal_alignment,
|
||||
vertical_alignment: self.vertical_alignment,
|
||||
});
|
||||
}
|
||||
|
||||
fn hash_layout(&self, state: &mut Hasher) {
|
||||
|
|
@ -159,10 +182,7 @@ where
|
|||
/// able to use [`Text`] in your user interface.
|
||||
///
|
||||
/// [renderer]: crate::Renderer
|
||||
pub trait Renderer: crate::Renderer {
|
||||
/// The font type used for [`Text`].
|
||||
type Font: Default + Copy;
|
||||
|
||||
pub trait Renderer: renderer::Text {
|
||||
/// Returns the default size of [`Text`].
|
||||
fn default_size(&self) -> u16;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue