diff options
Diffstat (limited to '')
-rw-r--r-- | native/src/renderer.rs | 69 | ||||
-rw-r--r-- | native/src/renderer/null.rs | 291 |
2 files changed, 66 insertions, 294 deletions
diff --git a/native/src/renderer.rs b/native/src/renderer.rs index 39a6cff1..ca7ad5a2 100644 --- a/native/src/renderer.rs +++ b/native/src/renderer.rs @@ -19,28 +19,17 @@ //! [`text::Renderer`]: crate::widget::text::Renderer //! [`Checkbox`]: crate::widget::Checkbox //! [`checkbox::Renderer`]: crate::widget::checkbox::Renderer - #[cfg(debug_assertions)] mod null; #[cfg(debug_assertions)] pub use null::Null; -use crate::{layout, Element, Rectangle}; +use crate::layout; +use crate::{Background, Color, Element, Rectangle, Vector}; /// A component that can take the state of a user interface and produce an /// output for its users. pub trait Renderer: Sized { - /// The type of output of the [`Renderer`]. - /// - /// If you are implementing a graphical renderer, your output will most - /// likely be a tree of visual primitives. - type Output; - - /// The default styling attributes of the [`Renderer`]. - /// - /// This type can be leveraged to implement style inheritance. - type Defaults: Default; - /// Lays out the elements of a user interface. /// /// You should override this if you need to perform any operations before or @@ -53,12 +42,52 @@ pub trait Renderer: Sized { element.layout(self, limits) } - /// Overlays the `overlay` output with the given bounds on top of the `base` - /// output. - fn overlay( + /// Draws the primitives recorded in the given closure in a new layer. + /// + /// The layer will clip its contents to the provided `bounds`. + fn with_layer(&mut self, bounds: Rectangle, f: impl FnOnce(&mut Self)); + + /// Applies a `translation` to the primitives recorded in the given closure. + fn with_translation( &mut self, - base: Self::Output, - overlay: Self::Output, - overlay_bounds: Rectangle, - ) -> Self::Output; + translation: Vector, + f: impl FnOnce(&mut Self), + ); + + /// Clears all of the recorded primitives in the [`Renderer`]. + fn clear(&mut self); + + /// Fills a [`Quad`] with the provided [`Background`]. + fn fill_quad(&mut self, quad: Quad, background: impl Into<Background>); +} + +/// A polygon with four sides. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Quad { + /// The bounds of the [`Quad`]. + pub bounds: Rectangle, + + /// The border radius of the [`Quad`]. + pub border_radius: f32, + + /// The border width of the [`Quad`]. + pub border_width: f32, + + /// The border color of the [`Quad`]. + pub border_color: Color, +} + +/// The styling attributes of a [`Renderer`]. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Style { + /// The text color + pub text_color: Color, +} + +impl Default for Style { + fn default() -> Self { + Style { + text_color: Color::BLACK, + } + } } diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs index b5921582..a5b2f277 100644 --- a/native/src/renderer/null.rs +++ b/native/src/renderer/null.rs @@ -1,20 +1,6 @@ -use crate::alignment; -use crate::button; -use crate::checkbox; -use crate::column; -use crate::container; -use crate::pane_grid; -use crate::progress_bar; -use crate::radio; -use crate::row; -use crate::scrollable; -use crate::slider; -use crate::text; -use crate::text_input; -use crate::toggler; -use crate::{ - Color, Element, Font, Layout, Padding, Point, Rectangle, Renderer, Size, -}; +use crate::renderer::{self, Renderer}; +use crate::text::{self, Text}; +use crate::{Background, Font, Point, Rectangle, Size, Vector}; /// A renderer that does nothing. /// @@ -30,33 +16,21 @@ impl Null { } impl Renderer for Null { - type Output = (); - type Defaults = (); + fn with_layer(&mut self, _bounds: Rectangle, _f: impl FnOnce(&mut Self)) {} - fn overlay(&mut self, _base: (), _overlay: (), _overlay_bounds: Rectangle) { - } -} - -impl column::Renderer for Null { - fn draw<Message>( + fn with_translation( &mut self, - _defaults: &Self::Defaults, - _content: &[Element<'_, Message, Self>], - _layout: Layout<'_>, - _cursor_position: Point, - _viewport: &Rectangle, + _translation: Vector, + _f: impl FnOnce(&mut Self), ) { } -} -impl row::Renderer for Null { - fn draw<Message>( + fn clear(&mut self) {} + + fn fill_quad( &mut self, - _defaults: &Self::Defaults, - _content: &[Element<'_, Message, Self>], - _layout: Layout<'_>, - _cursor_position: Point, - _viewport: &Rectangle, + _quad: renderer::Quad, + _background: impl Into<Background>, ) { } } @@ -64,6 +38,10 @@ impl row::Renderer for Null { impl text::Renderer for Null { type Font = Font; + const ICON_FONT: Font = Font::Default; + const CHECKMARK_ICON: char = '0'; + const ARROW_DOWN_ICON: char = '0'; + fn default_size(&self) -> u16 { 20 } @@ -90,240 +68,5 @@ impl text::Renderer for Null { None } - fn draw( - &mut self, - _defaults: &Self::Defaults, - _bounds: Rectangle, - _content: &str, - _size: u16, - _font: Font, - _color: Option<Color>, - _horizontal_alignment: alignment::Horizontal, - _vertical_alignment: alignment::Vertical, - ) { - } -} - -impl scrollable::Renderer for Null { - type Style = (); - - fn scrollbar( - &self, - _bounds: Rectangle, - _content_bounds: Rectangle, - _offset: u32, - _scrollbar_width: u16, - _scrollbar_margin: u16, - _scroller_width: u16, - ) -> Option<scrollable::Scrollbar> { - None - } - - fn draw( - &mut self, - _scrollable: &scrollable::State, - _bounds: Rectangle, - _content_bounds: Rectangle, - _is_mouse_over: bool, - _is_mouse_over_scrollbar: bool, - _scrollbar: Option<scrollable::Scrollbar>, - _offset: u32, - _style: &Self::Style, - _content: Self::Output, - ) { - } -} - -impl text_input::Renderer for Null { - type Style = (); - - fn measure_value(&self, _value: &str, _size: u16, _font: Font) -> f32 { - 0.0 - } - - fn offset( - &self, - _text_bounds: Rectangle, - _font: Font, - _size: u16, - _value: &text_input::Value, - _state: &text_input::State, - ) -> f32 { - 0.0 - } - - fn draw( - &mut self, - _bounds: Rectangle, - _text_bounds: Rectangle, - _cursor_position: Point, - _font: Font, - _size: u16, - _placeholder: &str, - _value: &text_input::Value, - _state: &text_input::State, - _style: &Self::Style, - ) -> Self::Output { - } -} - -impl button::Renderer for Null { - const DEFAULT_PADDING: Padding = Padding::ZERO; - - type Style = (); - - fn draw<Message>( - &mut self, - _defaults: &Self::Defaults, - _bounds: Rectangle, - _cursor_position: Point, - _is_disabled: bool, - _is_pressed: bool, - _style: &Self::Style, - _content: &Element<'_, Message, Self>, - _content_layout: Layout<'_>, - ) -> Self::Output { - } -} - -impl radio::Renderer for Null { - type Style = (); - - const DEFAULT_SIZE: u16 = 20; - const DEFAULT_SPACING: u16 = 15; - - fn draw( - &mut self, - _bounds: Rectangle, - _is_selected: bool, - _is_mouse_over: bool, - _label: Self::Output, - _style: &Self::Style, - ) { - } -} - -impl checkbox::Renderer for Null { - type Style = (); - - const DEFAULT_SIZE: u16 = 20; - const DEFAULT_SPACING: u16 = 15; - - fn draw( - &mut self, - _bounds: Rectangle, - _is_checked: bool, - _is_mouse_over: bool, - _label: Self::Output, - _style: &Self::Style, - ) { - } -} - -impl slider::Renderer for Null { - type Style = (); - - const DEFAULT_HEIGHT: u16 = 30; - - fn draw( - &mut self, - _bounds: Rectangle, - _cursor_position: Point, - _range: std::ops::RangeInclusive<f32>, - _value: f32, - _is_dragging: bool, - _style_sheet: &Self::Style, - ) { - } -} - -impl progress_bar::Renderer for Null { - type Style = (); - - const DEFAULT_HEIGHT: u16 = 30; - - fn draw( - &self, - _bounds: Rectangle, - _range: std::ops::RangeInclusive<f32>, - _value: f32, - _style: &Self::Style, - ) { - } -} - -impl container::Renderer for Null { - type Style = (); - - fn draw<Message>( - &mut self, - _defaults: &Self::Defaults, - _bounds: Rectangle, - _cursor_position: Point, - _viewport: &Rectangle, - _style: &Self::Style, - _content: &Element<'_, Message, Self>, - _content_layout: Layout<'_>, - ) { - } -} - -impl pane_grid::Renderer for Null { - type Style = (); - - fn draw<Message>( - &mut self, - _defaults: &Self::Defaults, - _content: &[(pane_grid::Pane, pane_grid::Content<'_, Message, Self>)], - _dragging: Option<(pane_grid::Pane, Point)>, - _resizing: Option<(pane_grid::Axis, Rectangle, bool)>, - _layout: Layout<'_>, - _style: &<Self as pane_grid::Renderer>::Style, - _cursor_position: Point, - _viewport: &Rectangle, - ) { - } - - fn draw_pane<Message>( - &mut self, - _defaults: &Self::Defaults, - _bounds: Rectangle, - _style: &<Self as container::Renderer>::Style, - _title_bar: Option<( - &pane_grid::TitleBar<'_, Message, Self>, - Layout<'_>, - )>, - _body: (&Element<'_, Message, Self>, Layout<'_>), - _cursor_position: Point, - _viewport: &Rectangle, - ) { - } - - fn draw_title_bar<Message>( - &mut self, - _defaults: &Self::Defaults, - _bounds: Rectangle, - _style: &<Self as container::Renderer>::Style, - _content: (&Element<'_, Message, Self>, Layout<'_>), - _controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>, - _cursor_position: Point, - _viewport: &Rectangle, - ) { - } -} - -impl toggler::Renderer for Null { - type Style = (); - - const DEFAULT_SIZE: u16 = 20; - - fn draw( - &mut self, - _bounds: Rectangle, - _is_checked: bool, - _is_mouse_over: bool, - _label: Option<Self::Output>, - _style: &Self::Style, - ) { - } + fn fill_text(&mut self, _text: Text<'_, Self::Font>) {} } |