From 7c4bf70023a8092faad9630c2c87fbf41bd6ab76 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 6 Mar 2024 21:27:03 +0100 Subject: Simplify theming for `Application` --- src/application.rs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'src/application.rs') diff --git a/src/application.rs b/src/application.rs index 3d89c758..3247a97d 100644 --- a/src/application.rs +++ b/src/application.rs @@ -1,7 +1,9 @@ //! Build interactive cross-platform applications. use crate::{Command, Element, Executor, Settings, Subscription}; -pub use crate::style::application::{Appearance, StyleSheet}; +use crate::shell::application; + +pub use application::{default, Appearance, Style}; /// An interactive cross-platform application. /// @@ -91,7 +93,10 @@ pub use crate::style::application::{Appearance, StyleSheet}; /// } /// } /// ``` -pub trait Application: Sized { +pub trait Application: Sized +where + Style: Default, +{ /// The [`Executor`] that will run commands and subscriptions. /// /// The [default executor] can be a good starting point! @@ -104,7 +109,7 @@ pub trait Application: Sized { type Message: std::fmt::Debug + Send; /// The theme of your [`Application`]. - type Theme: Default + StyleSheet; + type Theme: Default; /// The data needed to initialize your [`Application`]. type Flags; @@ -151,8 +156,8 @@ pub trait Application: Sized { /// Returns the current `Style` of the [`Theme`]. /// /// [`Theme`]: Self::Theme - fn style(&self) -> ::Style { - ::Style::default() + fn style(&self, theme: &Self::Theme) -> Appearance { + Style::default().resolve(theme) } /// Returns the event [`Subscription`] for the current state of the @@ -213,11 +218,15 @@ pub trait Application: Sized { } } -struct Instance(A); +struct Instance(A) +where + A: Application, + application::Style: Default; impl crate::runtime::Program for Instance where A: Application, + application::Style: Default, { type Message = A::Message; type Theme = A::Theme; @@ -232,9 +241,10 @@ where } } -impl crate::shell::Application for Instance +impl application::Application for Instance where A: Application, + application::Style: Default, { type Flags = A::Flags; @@ -252,8 +262,8 @@ where self.0.theme() } - fn style(&self) -> ::Style { - self.0.style() + fn style(&self, theme: &A::Theme) -> Appearance { + self.0.style(theme) } fn subscription(&self) -> Subscription { -- cgit From 833538ee7f3a60a839304762dfc29b0881d19094 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 20:11:32 +0100 Subject: Leverage `DefaultStyle` traits instead of `Default` --- src/application.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'src/application.rs') diff --git a/src/application.rs b/src/application.rs index 3247a97d..a7e4d8da 100644 --- a/src/application.rs +++ b/src/application.rs @@ -3,7 +3,7 @@ use crate::{Command, Element, Executor, Settings, Subscription}; use crate::shell::application; -pub use application::{default, Appearance, Style}; +pub use application::{default, Appearance, DefaultStyle}; /// An interactive cross-platform application. /// @@ -95,7 +95,7 @@ pub use application::{default, Appearance, Style}; /// ``` pub trait Application: Sized where - Style: Default, + Self::Theme: DefaultStyle, { /// The [`Executor`] that will run commands and subscriptions. /// @@ -153,11 +153,9 @@ where Self::Theme::default() } - /// Returns the current `Style` of the [`Theme`]. - /// - /// [`Theme`]: Self::Theme + /// Returns the current [`Appearance`] of the [`Application`]. fn style(&self, theme: &Self::Theme) -> Appearance { - Style::default().resolve(theme) + theme.default_style() } /// Returns the event [`Subscription`] for the current state of the @@ -221,12 +219,12 @@ where struct Instance(A) where A: Application, - application::Style: Default; + A::Theme: DefaultStyle; impl crate::runtime::Program for Instance where A: Application, - application::Style: Default, + A::Theme: DefaultStyle, { type Message = A::Message; type Theme = A::Theme; @@ -244,7 +242,7 @@ where impl application::Application for Instance where A: Application, - application::Style: Default, + A::Theme: DefaultStyle, { type Flags = A::Flags; -- cgit