diff options
Diffstat (limited to '')
-rw-r--r-- | native/src/renderer.rs | 16 | ||||
-rw-r--r-- | native/src/renderer/null.rs | 145 | ||||
-rw-r--r-- | native/src/renderer/windowed.rs | 19 |
3 files changed, 180 insertions, 0 deletions
diff --git a/native/src/renderer.rs b/native/src/renderer.rs index 833de571..7a68ada4 100644 --- a/native/src/renderer.rs +++ b/native/src/renderer.rs @@ -21,16 +21,32 @@ //! [`checkbox::Renderer`]: ../widget/checkbox/trait.Renderer.html mod debugger; +#[cfg(debug_assertions)] +mod null; mod windowed; pub use debugger::Debugger; +#[cfg(debug_assertions)] +pub use null::Null; pub use windowed::{Target, Windowed}; use crate::{layout, Element}; +/// 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. + /// + /// [`Renderer`]: trait.Renderer.html type Output; + /// Lays out the elements of a user interface. + /// + /// You should override this if you need to perform any operations before or + /// after layouting. For instance, trimming the measurements cache. fn layout<'a, Message>( &mut self, element: &Element<'a, Message, Self>, diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs new file mode 100644 index 00000000..182f033a --- /dev/null +++ b/native/src/renderer/null.rs @@ -0,0 +1,145 @@ +use crate::{ + button, checkbox, column, radio, row, scrollable, text, text_input, + Background, Color, Element, Font, HorizontalAlignment, Layout, Point, + Rectangle, Renderer, Size, VerticalAlignment, +}; + +/// A renderer that does nothing. +#[derive(Debug, Clone, Copy)] +pub struct Null; + +impl Renderer for Null { + type Output = (); +} + +impl column::Renderer for Null { + fn draw<Message>( + &mut self, + _content: &[Element<'_, Message, Self>], + _layout: Layout<'_>, + _cursor_position: Point, + ) { + } +} + +impl row::Renderer for Null { + fn draw<Message>( + &mut self, + _content: &[Element<'_, Message, Self>], + _layout: Layout<'_>, + _cursor_position: Point, + ) { + } +} + +impl text::Renderer for Null { + fn default_size(&self) -> u16 { + 20 + } + + fn measure( + &self, + _content: &str, + _size: u16, + _font: Font, + _bounds: Size, + ) -> (f32, f32) { + (0.0, 20.0) + } + + fn draw( + &mut self, + _bounds: Rectangle, + _content: &str, + _size: u16, + _font: Font, + _color: Option<Color>, + _horizontal_alignment: HorizontalAlignment, + _vertical_alignment: VerticalAlignment, + ) { + } +} + +impl scrollable::Renderer for Null { + fn is_mouse_over_scrollbar( + &self, + _bounds: Rectangle, + _content_bounds: Rectangle, + _cursor_position: Point, + ) -> bool { + false + } + + fn draw( + &mut self, + _scrollable: &scrollable::State, + _bounds: Rectangle, + _content_bounds: Rectangle, + _is_mouse_over: bool, + _is_mouse_over_scrollbar: bool, + _offset: u32, + _content: Self::Output, + ) { + } +} + +impl text_input::Renderer for Null { + fn default_size(&self) -> u16 { + 20 + } + + fn draw( + &mut self, + _bounds: Rectangle, + _text_bounds: Rectangle, + _cursor_position: Point, + _size: u16, + _placeholder: &str, + _value: &text_input::Value, + _state: &text_input::State, + ) -> Self::Output { + } +} + +impl button::Renderer for Null { + fn draw( + &mut self, + _bounds: Rectangle, + _cursor_position: Point, + _is_pressed: bool, + _background: Option<Background>, + _border_radius: u16, + _content: Self::Output, + ) -> Self::Output { + } +} + +impl radio::Renderer for Null { + fn default_size(&self) -> u32 { + 20 + } + + fn draw( + &mut self, + _bounds: Rectangle, + _is_selected: bool, + _is_mouse_over: bool, + _label: Self::Output, + ) { + } +} + +impl checkbox::Renderer for Null { + fn default_size(&self) -> u32 { + 20 + } + + fn draw( + &mut self, + _bounds: Rectangle, + _is_checked: bool, + _is_mouse_over: bool, + _label: Self::Output, + ) { + } +} diff --git a/native/src/renderer/windowed.rs b/native/src/renderer/windowed.rs index 6d0419d2..813a03f2 100644 --- a/native/src/renderer/windowed.rs +++ b/native/src/renderer/windowed.rs @@ -2,11 +2,21 @@ use crate::MouseCursor; use raw_window_handle::HasRawWindowHandle; +/// A renderer that can target windows. pub trait Windowed: super::Renderer + Sized { + /// The type of target. type Target: Target<Renderer = Self>; + /// Creates a new [`Windowed`] renderer. + /// + /// [`Windowed`]: trait.Windowed.html fn new() -> Self; + /// Performs the drawing operations described in the output on the given + /// target. + /// + /// The overlay can be a bunch of debug text logs. It should be rendered on + /// top of the GUI on most scenarios. fn draw<T: AsRef<str>>( &mut self, output: &Self::Output, @@ -15,9 +25,15 @@ pub trait Windowed: super::Renderer + Sized { ) -> MouseCursor; } +/// A rendering target. pub trait Target { + /// The renderer of this target. type Renderer; + /// Creates a new rendering [`Target`] from the given window handle, width, + /// height and dpi factor. + /// + /// [`Target`]: trait.Target.html fn new<W: HasRawWindowHandle>( window: &W, width: u16, @@ -26,6 +42,9 @@ pub trait Target { renderer: &Self::Renderer, ) -> Self; + /// Resizes the current [`Target`]. + /// + /// [`Target`]: trait.Target.html fn resize( &mut self, width: u16, |