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` --- winit/src/application.rs | 72 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 10 deletions(-) (limited to 'winit/src/application.rs') diff --git a/winit/src/application.rs b/winit/src/application.rs index 05a4f070..fbca6ee4 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -10,7 +10,7 @@ use crate::core::renderer; use crate::core::time::Instant; use crate::core::widget::operation; use crate::core::window; -use crate::core::{Event, Point, Size}; +use crate::core::{Color, Event, Point, Size}; use crate::futures::futures; use crate::futures::{Executor, Runtime, Subscription}; use crate::graphics::compositor::{self, Compositor}; @@ -18,7 +18,7 @@ use crate::runtime::clipboard; use crate::runtime::program::Program; use crate::runtime::user_interface::{self, UserInterface}; use crate::runtime::{Command, Debug}; -use crate::style::application::{Appearance, StyleSheet}; +use crate::style::Theme; use crate::{Clipboard, Error, Proxy, Settings}; use futures::channel::mpsc; @@ -39,7 +39,7 @@ use std::sync::Arc; /// can be toggled by pressing `F12`. pub trait Application: Program where - Self::Theme: StyleSheet, + Style: Default, { /// The data needed to initialize your [`Application`]. type Flags; @@ -64,8 +64,8 @@ where fn theme(&self) -> Self::Theme; /// Returns the `Style` variation of the `Theme`. - fn style(&self) -> ::Style { - Default::default() + fn style(&self, theme: &Self::Theme) -> Appearance { + Style::default().resolve(theme) } /// Returns the event `Subscription` for the current state of the @@ -95,6 +95,58 @@ where } } +/// The appearance of an application. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Appearance { + /// The background [`Color`] of the application. + pub background_color: Color, + + /// The default text [`Color`] of the application. + pub text_color: Color, +} + +/// The style of an [`Application`]. +#[derive(Debug, PartialEq, Eq)] +pub struct Style(pub fn(&Theme) -> Appearance); + +impl Style { + /// Resolves the [`Style`] with the given `Theme` to produce + /// an [`Appearance`]. + pub fn resolve(self, theme: &Theme) -> Appearance { + (self.0)(theme) + } +} + +impl Clone for Style { + fn clone(&self) -> Self { + *self + } +} + +impl Copy for Style {} + +impl From Appearance> for Style { + fn from(f: fn(&Theme) -> Appearance) -> Self { + Style(f) + } +} + +impl Default for Style { + fn default() -> Self { + Style(default) + } +} + +/// The default style of an [`Application`]. +pub fn default(theme: &Theme) -> Appearance { + let palette = theme.extended_palette(); + + Appearance { + background_color: palette.background.base.color, + text_color: palette.background.base.text, + } +} + /// Runs an [`Application`] with an executor, compositor, and the provided /// settings. pub fn run( @@ -105,7 +157,7 @@ where A: Application + 'static, E: Executor + 'static, C: Compositor + 'static, - A::Theme: StyleSheet, + Style: Default, { use futures::task; use futures::Future; @@ -289,7 +341,7 @@ async fn run_instance( A: Application + 'static, E: Executor + 'static, C: Compositor + 'static, - A::Theme: StyleSheet, + Style: Default, { use futures::stream::StreamExt; use winit::event; @@ -612,7 +664,7 @@ pub fn build_user_interface<'a, A: Application>( debug: &mut Debug, ) -> UserInterface<'a, A::Message, A::Theme, A::Renderer> where - A::Theme: StyleSheet, + Style: Default, { debug.view_started(); let view = application.view(); @@ -643,7 +695,7 @@ pub fn update( window: &winit::window::Window, ) where C: Compositor + 'static, - A::Theme: StyleSheet, + Style: Default, { for message in messages.drain(..) { debug.log_message(&message); @@ -694,7 +746,7 @@ pub fn run_command( A: Application, E: Executor, C: Compositor + 'static, - A::Theme: StyleSheet, + Style: Default, { use crate::runtime::command; use crate::runtime::system; -- cgit From 905f2160e6eb7504f52d9bd62c7bfa42c8ec2902 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 00:14:41 +0100 Subject: Move `Theme` type to `iced_core` --- winit/src/application.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'winit/src/application.rs') diff --git a/winit/src/application.rs b/winit/src/application.rs index fbca6ee4..d2a61580 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -10,7 +10,7 @@ use crate::core::renderer; use crate::core::time::Instant; use crate::core::widget::operation; use crate::core::window; -use crate::core::{Color, Event, Point, Size}; +use crate::core::{Color, Event, Point, Size, Theme}; use crate::futures::futures; use crate::futures::{Executor, Runtime, Subscription}; use crate::graphics::compositor::{self, Compositor}; @@ -18,7 +18,6 @@ use crate::runtime::clipboard; use crate::runtime::program::Program; use crate::runtime::user_interface::{self, UserInterface}; use crate::runtime::{Command, Debug}; -use crate::style::Theme; use crate::{Clipboard, Error, Proxy, Settings}; use futures::channel::mpsc; -- 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` --- winit/src/application.rs | 50 +++++++++++++++--------------------------------- 1 file changed, 15 insertions(+), 35 deletions(-) (limited to 'winit/src/application.rs') diff --git a/winit/src/application.rs b/winit/src/application.rs index d2a61580..1f97d911 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -38,7 +38,7 @@ use std::sync::Arc; /// can be toggled by pressing `F12`. pub trait Application: Program where - Style: Default, + Self::Theme: DefaultStyle, { /// The data needed to initialize your [`Application`]. type Flags; @@ -64,7 +64,7 @@ where /// Returns the `Style` variation of the `Theme`. fn style(&self, theme: &Self::Theme) -> Appearance { - Style::default().resolve(theme) + theme.default_style() } /// Returns the event `Subscription` for the current state of the @@ -104,39 +104,19 @@ pub struct Appearance { pub text_color: Color, } -/// The style of an [`Application`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style(pub fn(&Theme) -> Appearance); - -impl Style { - /// Resolves the [`Style`] with the given `Theme` to produce - /// an [`Appearance`]. - pub fn resolve(self, theme: &Theme) -> Appearance { - (self.0)(theme) - } -} - -impl Clone for Style { - fn clone(&self) -> Self { - *self - } -} - -impl Copy for Style {} - -impl From Appearance> for Style { - fn from(f: fn(&Theme) -> Appearance) -> Self { - Style(f) - } +/// The default style of an [`Application`]. +pub trait DefaultStyle { + /// Returns the default style of an [`Application`]. + fn default_style(&self) -> Appearance; } -impl Default for Style { - fn default() -> Self { - Style(default) +impl DefaultStyle for Theme { + fn default_style(&self) -> Appearance { + default(self) } } -/// The default style of an [`Application`]. +/// The default [`Appearance`] of an [`Application`] with the built-in [`Theme`]. pub fn default(theme: &Theme) -> Appearance { let palette = theme.extended_palette(); @@ -156,7 +136,7 @@ where A: Application + 'static, E: Executor + 'static, C: Compositor + 'static, - Style: Default, + A::Theme: DefaultStyle, { use futures::task; use futures::Future; @@ -340,7 +320,7 @@ async fn run_instance( A: Application + 'static, E: Executor + 'static, C: Compositor + 'static, - Style: Default, + A::Theme: DefaultStyle, { use futures::stream::StreamExt; use winit::event; @@ -663,7 +643,7 @@ pub fn build_user_interface<'a, A: Application>( debug: &mut Debug, ) -> UserInterface<'a, A::Message, A::Theme, A::Renderer> where - Style: Default, + A::Theme: DefaultStyle, { debug.view_started(); let view = application.view(); @@ -694,7 +674,7 @@ pub fn update( window: &winit::window::Window, ) where C: Compositor + 'static, - Style: Default, + A::Theme: DefaultStyle, { for message in messages.drain(..) { debug.log_message(&message); @@ -745,7 +725,7 @@ pub fn run_command( A: Application, E: Executor, C: Compositor + 'static, - Style: Default, + A::Theme: DefaultStyle, { use crate::runtime::command; use crate::runtime::system; -- cgit