From fc13bb3d65c85cfad9f231c0776d3a45f4fbf480 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 7 Jun 2022 05:24:43 +0200 Subject: Implement theme styling for `Canvas` --- graphics/src/widget/canvas.rs | 33 +++++++++++++++++++----------- graphics/src/widget/canvas/program.rs | 26 ++++++++++++++++------- graphics/src/widget/pure/canvas.rs | 31 +++++++++++++++------------- graphics/src/widget/pure/canvas/program.rs | 10 +++++---- 4 files changed, 63 insertions(+), 37 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/canvas.rs b/graphics/src/widget/canvas.rs index 0de08b01..c3e28e8c 100644 --- a/graphics/src/widget/canvas.rs +++ b/graphics/src/widget/canvas.rs @@ -62,10 +62,10 @@ use std::marker::PhantomData; /// ```no_run /// # mod iced { /// # pub use iced_graphics::canvas; -/// # pub use iced_native::{Color, Rectangle}; +/// # pub use iced_native::{Color, Rectangle, Theme}; /// # } /// use iced::canvas::{self, Canvas, Cursor, Fill, Frame, Geometry, Path, Program}; -/// use iced::{Color, Rectangle}; +/// use iced::{Color, Rectangle, Theme}; /// /// // First, we define the data we need for drawing /// #[derive(Debug)] @@ -75,7 +75,7 @@ use std::marker::PhantomData; /// /// // Then, we implement the `Program` trait /// impl Program<()> for Circle { -/// fn draw(&self, bounds: Rectangle, _cursor: Cursor) -> Vec{ +/// fn draw(&self, _theme: &Theme, bounds: Rectangle, _cursor: Cursor) -> Vec{ /// // We prepare a new `Frame` /// let mut frame = Frame::new(bounds.size()); /// @@ -94,14 +94,21 @@ use std::marker::PhantomData; /// let canvas = Canvas::new(Circle { radius: 50.0 }); /// ``` #[derive(Debug)] -pub struct Canvas> { +pub struct Canvas +where + P: Program, +{ width: Length, height: Length, program: P, message_: PhantomData, + theme_: PhantomData, } -impl> Canvas { +impl Canvas +where + P: Program, +{ const DEFAULT_SIZE: u16 = 100; /// Creates a new [`Canvas`]. @@ -111,6 +118,7 @@ impl> Canvas { height: Length::Units(Self::DEFAULT_SIZE), program, message_: PhantomData, + theme_: PhantomData, } } @@ -127,9 +135,9 @@ impl> Canvas { } } -impl Widget> for Canvas +impl Widget> for Canvas where - P: Program, + P: Program, B: Backend, { fn width(&self) -> Length { @@ -204,7 +212,7 @@ where fn draw( &self, renderer: &mut Renderer, - _theme: &T, + theme: &T, _style: &renderer::Style, layout: Layout<'_>, cursor_position: Point, @@ -225,7 +233,7 @@ where renderer.draw_primitive(Primitive::Group { primitives: self .program - .draw(bounds, cursor) + .draw(theme, bounds, cursor) .into_iter() .map(Geometry::into_primitive) .collect(), @@ -234,15 +242,16 @@ where } } -impl<'a, Message, P, B, T> From> +impl<'a, Message, P, B, T> From> for Element<'a, Message, Renderer> where Message: 'static, - P: Program + 'a, + P: Program + 'a, B: Backend, + T: 'a, { fn from( - canvas: Canvas, + canvas: Canvas, ) -> Element<'a, Message, Renderer> { Element::new(canvas) } diff --git a/graphics/src/widget/canvas/program.rs b/graphics/src/widget/canvas/program.rs index 85a2f67b..dddc387d 100644 --- a/graphics/src/widget/canvas/program.rs +++ b/graphics/src/widget/canvas/program.rs @@ -1,6 +1,8 @@ use crate::canvas::event::{self, Event}; use crate::canvas::{Cursor, Geometry}; -use iced_native::{mouse, Rectangle}; + +use iced_native::mouse; +use iced_native::Rectangle; /// The state and logic of a [`Canvas`]. /// @@ -8,7 +10,7 @@ use iced_native::{mouse, Rectangle}; /// application. /// /// [`Canvas`]: crate::widget::Canvas -pub trait Program { +pub trait Program { /// Updates the state of the [`Program`]. /// /// When a [`Program`] is used in a [`Canvas`], the runtime will call this @@ -36,7 +38,12 @@ pub trait Program { /// /// [`Frame`]: crate::widget::canvas::Frame /// [`Cache`]: crate::widget::canvas::Cache - fn draw(&self, bounds: Rectangle, cursor: Cursor) -> Vec; + fn draw( + &self, + theme: &Theme, + bounds: Rectangle, + cursor: Cursor, + ) -> Vec; /// Returns the current mouse interaction of the [`Program`]. /// @@ -53,9 +60,9 @@ pub trait Program { } } -impl Program for &mut T +impl Program for &mut T where - T: Program, + T: Program, { fn update( &mut self, @@ -66,8 +73,13 @@ where T::update(self, event, bounds, cursor) } - fn draw(&self, bounds: Rectangle, cursor: Cursor) -> Vec { - T::draw(self, bounds, cursor) + fn draw( + &self, + theme: &Theme, + bounds: Rectangle, + cursor: Cursor, + ) -> Vec { + T::draw(self, theme, bounds, cursor) } fn mouse_interaction( diff --git a/graphics/src/widget/pure/canvas.rs b/graphics/src/widget/pure/canvas.rs index d53b20f6..d9c7e66b 100644 --- a/graphics/src/widget/pure/canvas.rs +++ b/graphics/src/widget/pure/canvas.rs @@ -30,10 +30,10 @@ use std::marker::PhantomData; /// # pub mod pure { /// # pub use iced_graphics::pure::canvas; /// # } -/// # pub use iced_native::{Color, Rectangle}; +/// # pub use iced_native::{Color, Rectangle, Theme}; /// # } /// use iced::pure::canvas::{self, Canvas, Cursor, Fill, Frame, Geometry, Path, Program}; -/// use iced::{Color, Rectangle}; +/// use iced::{Color, Rectangle, Theme}; /// /// // First, we define the data we need for drawing /// #[derive(Debug)] @@ -45,7 +45,7 @@ use std::marker::PhantomData; /// impl Program<()> for Circle { /// type State = (); /// -/// fn draw(&self, _state: &(), bounds: Rectangle, _cursor: Cursor) -> Vec{ +/// fn draw(&self, _state: &(), _theme: &Theme, bounds: Rectangle, _cursor: Cursor) -> Vec{ /// // We prepare a new `Frame` /// let mut frame = Frame::new(bounds.size()); /// @@ -64,19 +64,20 @@ use std::marker::PhantomData; /// let canvas = Canvas::new(Circle { radius: 50.0 }); /// ``` #[derive(Debug)] -pub struct Canvas +pub struct Canvas where - P: Program, + P: Program, { width: Length, height: Length, program: P, message_: PhantomData, + theme_: PhantomData, } -impl Canvas +impl Canvas where - P: Program, + P: Program, { const DEFAULT_SIZE: u16 = 100; @@ -87,6 +88,7 @@ where height: Length::Units(Self::DEFAULT_SIZE), program, message_: PhantomData, + theme_: PhantomData, } } @@ -103,9 +105,9 @@ where } } -impl Widget> for Canvas +impl Widget> for Canvas where - P: Program, + P: Program, B: Backend, { fn tag(&self) -> tree::Tag { @@ -194,7 +196,7 @@ where &self, tree: &Tree, renderer: &mut Renderer, - _theme: &T, + theme: &T, _style: &renderer::Style, layout: Layout<'_>, cursor_position: Point, @@ -216,7 +218,7 @@ where renderer.draw_primitive(Primitive::Group { primitives: self .program - .draw(state, bounds, cursor) + .draw(state, theme, bounds, cursor) .into_iter() .map(Geometry::into_primitive) .collect(), @@ -225,15 +227,16 @@ where } } -impl<'a, Message, P, B, T> From> +impl<'a, Message, P, B, T> From> for Element<'a, Message, Renderer> where Message: 'a, - P: Program + 'a, + P: Program + 'a, B: Backend, + T: 'a, { fn from( - canvas: Canvas, + canvas: Canvas, ) -> Element<'a, Message, Renderer> { Element::new(canvas) } diff --git a/graphics/src/widget/pure/canvas/program.rs b/graphics/src/widget/pure/canvas/program.rs index 058b364b..20c6406e 100644 --- a/graphics/src/widget/pure/canvas/program.rs +++ b/graphics/src/widget/pure/canvas/program.rs @@ -9,7 +9,7 @@ use crate::Rectangle; /// application. /// /// [`Canvas`]: crate::widget::Canvas -pub trait Program { +pub trait Program { /// The internal state mutated by the [`Program`]. type State: Default + 'static; @@ -44,6 +44,7 @@ pub trait Program { fn draw( &self, state: &Self::State, + theme: &Theme, bounds: Rectangle, cursor: Cursor, ) -> Vec; @@ -64,9 +65,9 @@ pub trait Program { } } -impl Program for &T +impl Program for &T where - T: Program, + T: Program, { type State = T::State; @@ -83,10 +84,11 @@ where fn draw( &self, state: &Self::State, + theme: &Theme, bounds: Rectangle, cursor: Cursor, ) -> Vec { - T::draw(self, state, bounds, cursor) + T::draw(self, state, theme, bounds, cursor) } fn mouse_interaction( -- cgit