From c4b4207f4768c7e254ff0a6bf95c4d76ea08ce48 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 14:41:34 +0100 Subject: Support custom themes in `Program` API --- src/application/program.rs | 32 +++++++++++++++++--------------- src/lib.rs | 5 +++-- 2 files changed, 20 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/application/program.rs b/src/application/program.rs index 24e00efe..d70f39cf 100644 --- a/src/application/program.rs +++ b/src/application/program.rs @@ -64,37 +64,38 @@ use std::borrow::Cow; /// ] /// } /// ``` -pub fn program( +pub fn program( title: impl Title, update: impl Update, - view: impl for<'a> self::View<'a, State, Message>, -) -> Program< - impl Definition, -> + view: impl for<'a> self::View<'a, State, Message, Theme>, +) -> Program> where State: Default + 'static, Message: Send + std::fmt::Debug, + Theme: Default + application::DefaultStyle, { use std::marker::PhantomData; - struct Application { + struct Application { update: Update, view: View, _state: PhantomData, _message: PhantomData, + _theme: PhantomData, } - impl Definition - for Application + impl Definition + for Application where State: Default, Message: Send + std::fmt::Debug, + Theme: Default + application::DefaultStyle, Update: self::Update, - View: for<'a> self::View<'a, State, Message>, + View: for<'a> self::View<'a, State, Message, Theme>, { type State = State; type Message = Message; - type Theme = crate::Theme; + type Theme = Theme; type Executor = executor::Default; fn build(&self) -> (Self::State, Command) { @@ -123,6 +124,7 @@ where view, _state: PhantomData, _message: PhantomData, + _theme: PhantomData, }, settings: Settings::default(), } @@ -793,18 +795,18 @@ where /// /// This trait allows the [`program`] builder to take any closure that /// returns any `Into>`. -pub trait View<'a, State, Message> { +pub trait View<'a, State, Message, Theme> { /// Produces the widget of the [`Program`]. - fn view(&self, state: &'a State) -> impl Into>; + fn view(&self, state: &'a State) -> impl Into>; } -impl<'a, T, State, Message, Widget> View<'a, State, Message> for T +impl<'a, T, State, Message, Theme, Widget> View<'a, State, Message, Theme> for T where T: Fn(&'a State) -> Widget, State: 'static, - Widget: Into>, + Widget: Into>, { - fn view(&self, state: &'a State) -> impl Into> { + fn view(&self, state: &'a State) -> impl Into> { self(state) } } diff --git a/src/lib.rs b/src/lib.rs index d238e78a..bc87b1a3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -373,14 +373,15 @@ pub type Result = std::result::Result<(), Error>; /// ] /// } /// ``` -pub fn run( +pub fn run( title: impl application::Title + 'static, update: impl application::Update + 'static, - view: impl for<'a> application::View<'a, State, Message> + 'static, + view: impl for<'a> application::View<'a, State, Message, Theme> + 'static, ) -> Result where State: Default + 'static, Message: std::fmt::Debug + Send + 'static, + Theme: Default + application::DefaultStyle + 'static, { program(title, update, view).run() } -- cgit