Support custom themes in Program API

This commit is contained in:
Héctor Ramón Jiménez 2024-03-17 14:41:34 +01:00
parent a034e40f7c
commit c4b4207f47
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 20 additions and 17 deletions

View file

@ -64,37 +64,38 @@ use std::borrow::Cow;
/// ] /// ]
/// } /// }
/// ``` /// ```
pub fn program<State, Message>( pub fn program<State, Message, Theme>(
title: impl Title<State>, title: impl Title<State>,
update: impl Update<State, Message>, update: impl Update<State, Message>,
view: impl for<'a> self::View<'a, State, Message>, view: impl for<'a> self::View<'a, State, Message, Theme>,
) -> Program< ) -> Program<impl Definition<State = State, Message = Message, Theme = Theme>>
impl Definition<State = State, Message = Message, Theme = crate::Theme>,
>
where where
State: Default + 'static, State: Default + 'static,
Message: Send + std::fmt::Debug, Message: Send + std::fmt::Debug,
Theme: Default + application::DefaultStyle,
{ {
use std::marker::PhantomData; use std::marker::PhantomData;
struct Application<State, Message, Update, View> { struct Application<State, Message, Theme, Update, View> {
update: Update, update: Update,
view: View, view: View,
_state: PhantomData<State>, _state: PhantomData<State>,
_message: PhantomData<Message>, _message: PhantomData<Message>,
_theme: PhantomData<Theme>,
} }
impl<State, Message, Update, View> Definition impl<State, Message, Theme, Update, View> Definition
for Application<State, Message, Update, View> for Application<State, Message, Theme, Update, View>
where where
State: Default, State: Default,
Message: Send + std::fmt::Debug, Message: Send + std::fmt::Debug,
Theme: Default + application::DefaultStyle,
Update: self::Update<State, Message>, Update: self::Update<State, Message>,
View: for<'a> self::View<'a, State, Message>, View: for<'a> self::View<'a, State, Message, Theme>,
{ {
type State = State; type State = State;
type Message = Message; type Message = Message;
type Theme = crate::Theme; type Theme = Theme;
type Executor = executor::Default; type Executor = executor::Default;
fn build(&self) -> (Self::State, Command<Self::Message>) { fn build(&self) -> (Self::State, Command<Self::Message>) {
@ -123,6 +124,7 @@ where
view, view,
_state: PhantomData, _state: PhantomData,
_message: PhantomData, _message: PhantomData,
_theme: PhantomData,
}, },
settings: Settings::default(), settings: Settings::default(),
} }
@ -793,18 +795,18 @@ where
/// ///
/// This trait allows the [`program`] builder to take any closure that /// This trait allows the [`program`] builder to take any closure that
/// returns any `Into<Element<'_, Message>>`. /// returns any `Into<Element<'_, Message>>`.
pub trait View<'a, State, Message> { pub trait View<'a, State, Message, Theme> {
/// Produces the widget of the [`Program`]. /// Produces the widget of the [`Program`].
fn view(&self, state: &'a State) -> impl Into<Element<'a, Message>>; fn view(&self, state: &'a State) -> impl Into<Element<'a, Message, Theme>>;
} }
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 where
T: Fn(&'a State) -> Widget, T: Fn(&'a State) -> Widget,
State: 'static, State: 'static,
Widget: Into<Element<'a, Message>>, Widget: Into<Element<'a, Message, Theme>>,
{ {
fn view(&self, state: &'a State) -> impl Into<Element<'a, Message>> { fn view(&self, state: &'a State) -> impl Into<Element<'a, Message, Theme>> {
self(state) self(state)
} }
} }

View file

@ -373,14 +373,15 @@ pub type Result = std::result::Result<(), Error>;
/// ] /// ]
/// } /// }
/// ``` /// ```
pub fn run<State, Message>( pub fn run<State, Message, Theme>(
title: impl application::Title<State> + 'static, title: impl application::Title<State> + 'static,
update: impl application::Update<State, Message> + 'static, update: impl application::Update<State, Message> + 'static,
view: impl for<'a> application::View<'a, State, Message> + 'static, view: impl for<'a> application::View<'a, State, Message, Theme> + 'static,
) -> Result ) -> Result
where where
State: Default + 'static, State: Default + 'static,
Message: std::fmt::Debug + Send + 'static, Message: std::fmt::Debug + Send + 'static,
Theme: Default + application::DefaultStyle + 'static,
{ {
program(title, update, view).run() program(title, update, view).run()
} }