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 +++++++++++++++++++++++++++----- winit/src/application/state.rs | 10 ++--- winit/src/multi_window.rs | 21 +++++----- winit/src/multi_window/state.rs | 16 ++++--- winit/src/multi_window/window_manager.rs | 13 +++--- 5 files changed, 91 insertions(+), 41 deletions(-) (limited to 'winit/src') 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; diff --git a/winit/src/application/state.rs b/winit/src/application/state.rs index c17a3bcc..b3bdb3a3 100644 --- a/winit/src/application/state.rs +++ b/winit/src/application/state.rs @@ -1,4 +1,4 @@ -use crate::application::{self, StyleSheet as _}; +use crate::application; use crate::conversion; use crate::core::mouse; use crate::core::{Color, Size}; @@ -14,7 +14,7 @@ use winit::window::Window; #[allow(missing_debug_implementations)] pub struct State where - A::Theme: application::StyleSheet, + application::Style: Default, { title: String, scale_factor: f64, @@ -29,14 +29,14 @@ where impl State where - A::Theme: application::StyleSheet, + application::Style: Default, { /// Creates a new [`State`] for the provided [`Application`] and window. pub fn new(application: &A, window: &Window) -> Self { let title = application.title(); let scale_factor = application.scale_factor(); let theme = application.theme(); - let appearance = theme.appearance(&application.style()); + let appearance = application.style(&theme); let viewport = { let physical_size = window.inner_size(); @@ -216,6 +216,6 @@ where // Update theme and appearance self.theme = application.theme(); - self.appearance = self.theme.appearance(&application.style()); + self.appearance = application.style(&self.theme); } } diff --git a/winit/src/multi_window.rs b/winit/src/multi_window.rs index 03066d6c..19951ed3 100644 --- a/winit/src/multi_window.rs +++ b/winit/src/multi_window.rs @@ -20,9 +20,10 @@ use crate::runtime::command::{self, Command}; use crate::runtime::multi_window::Program; use crate::runtime::user_interface::{self, UserInterface}; use crate::runtime::Debug; -use crate::style::application::StyleSheet; use crate::{Clipboard, Error, Proxy, Settings}; +pub use crate::application::{default, Appearance, Style}; + use std::collections::HashMap; use std::mem::ManuallyDrop; use std::sync::Arc; @@ -41,7 +42,7 @@ use std::time::Instant; /// 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; @@ -66,8 +67,8 @@ where fn theme(&self, window: window::Id) -> 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 @@ -108,7 +109,7 @@ where A: Application + 'static, E: Executor + 'static, C: Compositor + 'static, - A::Theme: StyleSheet, + Style: Default, { use winit::event_loop::EventLoopBuilder; @@ -349,7 +350,7 @@ async fn run_instance( A: Application + 'static, E: Executor + 'static, C: Compositor + 'static, - A::Theme: StyleSheet, + Style: Default, { use winit::event; use winit::event_loop::ControlFlow; @@ -819,7 +820,7 @@ fn build_user_interface<'a, A: Application>( id: window::Id, ) -> UserInterface<'a, A::Message, A::Theme, A::Renderer> where - A::Theme: StyleSheet, + Style: Default, { debug.view_started(); let view = application.view(id); @@ -847,7 +848,7 @@ fn update( ui_caches: &mut HashMap, ) where C: Compositor + 'static, - A::Theme: StyleSheet, + Style: Default, { for message in messages.drain(..) { debug.log_message(&message); @@ -890,7 +891,7 @@ fn run_command( A: Application, E: Executor, C: Compositor + 'static, - A::Theme: StyleSheet, + Style: Default, { use crate::runtime::clipboard; use crate::runtime::system; @@ -1216,8 +1217,8 @@ pub fn build_user_interfaces<'a, A: Application, C: Compositor>( mut cached_user_interfaces: HashMap, ) -> HashMap> where - A::Theme: StyleSheet, C: Compositor, + Style: Default, { cached_user_interfaces .drain() diff --git a/winit/src/multi_window/state.rs b/winit/src/multi_window/state.rs index 2e97a13d..8a332176 100644 --- a/winit/src/multi_window/state.rs +++ b/winit/src/multi_window/state.rs @@ -2,18 +2,16 @@ use crate::conversion; use crate::core::{mouse, window}; use crate::core::{Color, Size}; use crate::graphics::Viewport; -use crate::multi_window::Application; -use crate::style::application; +use crate::multi_window::{self, Application}; use std::fmt::{Debug, Formatter}; -use iced_style::application::StyleSheet; use winit::event::{Touch, WindowEvent}; use winit::window::Window; /// The state of a multi-windowed [`Application`]. pub struct State where - A::Theme: application::StyleSheet, + multi_window::Style: Default, { title: String, scale_factor: f64, @@ -22,12 +20,12 @@ where cursor_position: Option>, modifiers: winit::keyboard::ModifiersState, theme: A::Theme, - appearance: application::Appearance, + appearance: multi_window::Appearance, } impl Debug for State where - A::Theme: application::StyleSheet, + multi_window::Style: Default, { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.debug_struct("multi_window::State") @@ -43,7 +41,7 @@ where impl State where - A::Theme: application::StyleSheet, + multi_window::Style: Default, { /// Creates a new [`State`] for the provided [`Application`]'s `window`. pub fn new( @@ -54,7 +52,7 @@ where let title = application.title(window_id); let scale_factor = application.scale_factor(window_id); let theme = application.theme(window_id); - let appearance = theme.appearance(&application.style()); + let appearance = application.style(&theme); let viewport = { let physical_size = window.inner_size(); @@ -236,6 +234,6 @@ where // Update theme and appearance self.theme = application.theme(window_id); - self.appearance = self.theme.appearance(&application.style()); + self.appearance = application.style(&self.theme); } } diff --git a/winit/src/multi_window/window_manager.rs b/winit/src/multi_window/window_manager.rs index 23f3c0ba..2800e923 100644 --- a/winit/src/multi_window/window_manager.rs +++ b/winit/src/multi_window/window_manager.rs @@ -2,8 +2,7 @@ use crate::core::mouse; use crate::core::window::Id; use crate::core::{Point, Size}; use crate::graphics::Compositor; -use crate::multi_window::{Application, State}; -use crate::style::application::StyleSheet; +use crate::multi_window::{Application, State, Style}; use std::collections::BTreeMap; use std::sync::Arc; @@ -12,8 +11,8 @@ use winit::monitor::MonitorHandle; #[allow(missing_debug_implementations)] pub struct WindowManager where - A::Theme: StyleSheet, C: Compositor, + Style: Default, { aliases: BTreeMap, entries: BTreeMap>, @@ -23,7 +22,7 @@ impl WindowManager where A: Application, C: Compositor, - A::Theme: StyleSheet, + Style: Default, { pub fn new() -> Self { Self { @@ -109,7 +108,7 @@ impl Default for WindowManager where A: Application, C: Compositor, - A::Theme: StyleSheet, + Style: Default, { fn default() -> Self { Self::new() @@ -121,7 +120,7 @@ pub struct Window where A: Application, C: Compositor, - A::Theme: StyleSheet, + Style: Default, { pub raw: Arc, pub state: State, @@ -136,7 +135,7 @@ impl Window where A: Application, C: Compositor, - A::Theme: StyleSheet, + Style: Default, { pub fn position(&self) -> Option { self.raw -- 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 +-- winit/src/lib.rs | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) (limited to 'winit/src') 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; diff --git a/winit/src/lib.rs b/winit/src/lib.rs index 3b1b0d3a..64912b3f 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -30,7 +30,6 @@ pub use iced_graphics as graphics; pub use iced_runtime as runtime; pub use iced_runtime::core; pub use iced_runtime::futures; -pub use iced_style as style; pub use winit; #[cfg(feature = "multi-window")] -- 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 ++++++++++---------------------- winit/src/application/state.rs | 4 +-- winit/src/multi_window.rs | 18 ++++++------ winit/src/multi_window/state.rs | 6 ++-- winit/src/multi_window/window_manager.rs | 12 ++++---- 5 files changed, 35 insertions(+), 55 deletions(-) (limited to 'winit/src') 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; diff --git a/winit/src/application/state.rs b/winit/src/application/state.rs index b3bdb3a3..a0a06933 100644 --- a/winit/src/application/state.rs +++ b/winit/src/application/state.rs @@ -14,7 +14,7 @@ use winit::window::Window; #[allow(missing_debug_implementations)] pub struct State where - application::Style: Default, + A::Theme: application::DefaultStyle, { title: String, scale_factor: f64, @@ -29,7 +29,7 @@ where impl State where - application::Style: Default, + A::Theme: application::DefaultStyle, { /// Creates a new [`State`] for the provided [`Application`] and window. pub fn new(application: &A, window: &Window) -> Self { diff --git a/winit/src/multi_window.rs b/winit/src/multi_window.rs index 19951ed3..ec2f86ea 100644 --- a/winit/src/multi_window.rs +++ b/winit/src/multi_window.rs @@ -22,7 +22,7 @@ use crate::runtime::user_interface::{self, UserInterface}; use crate::runtime::Debug; use crate::{Clipboard, Error, Proxy, Settings}; -pub use crate::application::{default, Appearance, Style}; +pub use crate::application::{default, Appearance, DefaultStyle}; use std::collections::HashMap; use std::mem::ManuallyDrop; @@ -42,7 +42,7 @@ use std::time::Instant; /// 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; @@ -68,7 +68,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 @@ -109,7 +109,7 @@ where A: Application + 'static, E: Executor + 'static, C: Compositor + 'static, - Style: Default, + A::Theme: DefaultStyle, { use winit::event_loop::EventLoopBuilder; @@ -350,7 +350,7 @@ async fn run_instance( A: Application + 'static, E: Executor + 'static, C: Compositor + 'static, - Style: Default, + A::Theme: DefaultStyle, { use winit::event; use winit::event_loop::ControlFlow; @@ -820,7 +820,7 @@ fn build_user_interface<'a, A: Application>( id: window::Id, ) -> UserInterface<'a, A::Message, A::Theme, A::Renderer> where - Style: Default, + A::Theme: DefaultStyle, { debug.view_started(); let view = application.view(id); @@ -848,7 +848,7 @@ fn update( ui_caches: &mut HashMap, ) where C: Compositor + 'static, - Style: Default, + A::Theme: DefaultStyle, { for message in messages.drain(..) { debug.log_message(&message); @@ -891,7 +891,7 @@ fn run_command( A: Application, E: Executor, C: Compositor + 'static, - Style: Default, + A::Theme: DefaultStyle, { use crate::runtime::clipboard; use crate::runtime::system; @@ -1218,7 +1218,7 @@ pub fn build_user_interfaces<'a, A: Application, C: Compositor>( ) -> HashMap> where C: Compositor, - Style: Default, + A::Theme: DefaultStyle, { cached_user_interfaces .drain() diff --git a/winit/src/multi_window/state.rs b/winit/src/multi_window/state.rs index 8a332176..dfd8e696 100644 --- a/winit/src/multi_window/state.rs +++ b/winit/src/multi_window/state.rs @@ -11,7 +11,7 @@ use winit::window::Window; /// The state of a multi-windowed [`Application`]. pub struct State where - multi_window::Style: Default, + A::Theme: multi_window::DefaultStyle, { title: String, scale_factor: f64, @@ -25,7 +25,7 @@ where impl Debug for State where - multi_window::Style: Default, + A::Theme: multi_window::DefaultStyle, { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.debug_struct("multi_window::State") @@ -41,7 +41,7 @@ where impl State where - multi_window::Style: Default, + A::Theme: multi_window::DefaultStyle, { /// Creates a new [`State`] for the provided [`Application`]'s `window`. pub fn new( diff --git a/winit/src/multi_window/window_manager.rs b/winit/src/multi_window/window_manager.rs index 2800e923..71c1688b 100644 --- a/winit/src/multi_window/window_manager.rs +++ b/winit/src/multi_window/window_manager.rs @@ -2,7 +2,7 @@ use crate::core::mouse; use crate::core::window::Id; use crate::core::{Point, Size}; use crate::graphics::Compositor; -use crate::multi_window::{Application, State, Style}; +use crate::multi_window::{Application, DefaultStyle, State}; use std::collections::BTreeMap; use std::sync::Arc; @@ -12,7 +12,7 @@ use winit::monitor::MonitorHandle; pub struct WindowManager where C: Compositor, - Style: Default, + A::Theme: DefaultStyle, { aliases: BTreeMap, entries: BTreeMap>, @@ -22,7 +22,7 @@ impl WindowManager where A: Application, C: Compositor, - Style: Default, + A::Theme: DefaultStyle, { pub fn new() -> Self { Self { @@ -108,7 +108,7 @@ impl Default for WindowManager where A: Application, C: Compositor, - Style: Default, + A::Theme: DefaultStyle, { fn default() -> Self { Self::new() @@ -120,7 +120,7 @@ pub struct Window where A: Application, C: Compositor, - Style: Default, + A::Theme: DefaultStyle, { pub raw: Arc, pub state: State, @@ -135,7 +135,7 @@ impl Window where A: Application, C: Compositor, - Style: Default, + A::Theme: DefaultStyle, { pub fn position(&self) -> Option { self.raw -- cgit