diff options
author | 2022-05-25 05:01:18 +0200 | |
---|---|---|
committer | 2022-05-25 05:01:18 +0200 | |
commit | 03eda9b162012c503ead649e5ccb95b7ef1d10ed (patch) | |
tree | a134dbab97011c0a465f4527d2ae0bb771f51554 | |
parent | 2cfb307f8c3927a0876c6b754a5d7d673b9edfee (diff) | |
download | iced-03eda9b162012c503ead649e5ccb95b7ef1d10ed.tar.gz iced-03eda9b162012c503ead649e5ccb95b7ef1d10ed.tar.bz2 iced-03eda9b162012c503ead649e5ccb95b7ef1d10ed.zip |
Let a `Theme` control the background color of an application
... and remove `Application::background_color`
Diffstat (limited to '')
-rw-r--r-- | examples/pure/game_of_life/src/main.rs | 6 | ||||
-rw-r--r-- | glutin/src/application.rs | 5 | ||||
-rw-r--r-- | src/application.rs | 16 | ||||
-rw-r--r-- | src/pure/application.rs | 16 | ||||
-rw-r--r-- | src/pure/sandbox.rs | 13 | ||||
-rw-r--r-- | src/sandbox.rs | 13 | ||||
-rw-r--r-- | style/src/theme.rs | 14 | ||||
-rw-r--r-- | winit/src/application.rs | 16 | ||||
-rw-r--r-- | winit/src/application/state.rs | 13 |
9 files changed, 33 insertions, 79 deletions
diff --git a/examples/pure/game_of_life/src/main.rs b/examples/pure/game_of_life/src/main.rs index a3e46888..0eded2ce 100644 --- a/examples/pure/game_of_life/src/main.rs +++ b/examples/pure/game_of_life/src/main.rs @@ -12,7 +12,7 @@ use iced::pure::{Application, Element}; use iced::theme::{self, Theme}; use iced::time; use iced::window; -use iced::{Alignment, Color, Command, Length, Settings, Subscription}; +use iced::{Alignment, Command, Length, Settings, Subscription}; use preset::Preset; use std::time::{Duration, Instant}; @@ -71,10 +71,6 @@ impl Application for GameOfLife { String::from("Game of Life - Iced") } - fn background_color(&self) -> Color { - style::BACKGROUND - } - fn update(&mut self, message: Message) -> Command<Message> { match message { Message::Grid(message, version) => { diff --git a/glutin/src/application.rs b/glutin/src/application.rs index d93059e5..7a5d78ea 100644 --- a/glutin/src/application.rs +++ b/glutin/src/application.rs @@ -9,6 +9,7 @@ use iced_winit::application; use iced_winit::conversion; use iced_winit::futures; use iced_winit::futures::channel::mpsc; +use iced_winit::theme::{self, Definition as _}; use iced_winit::user_interface; use iced_winit::{Clipboard, Debug, Proxy, Settings}; @@ -25,6 +26,7 @@ where A: Application + 'static, E: Executor + 'static, C: window::GLCompositor<Renderer = A::Renderer> + 'static, + <A::Renderer as iced_native::Renderer>::Theme: theme::Definition, { use futures::task; use futures::Future; @@ -203,6 +205,7 @@ async fn run_instance<A, E, C>( A: Application + 'static, E: Executor + 'static, C: window::GLCompositor<Renderer = A::Renderer> + 'static, + <A::Renderer as iced_native::Renderer>::Theme: theme::Definition, { use glutin::event; use iced_winit::futures::stream::StreamExt; @@ -376,7 +379,7 @@ async fn run_instance<A, E, C>( compositor.present( &mut renderer, state.viewport(), - state.background_color(), + theme.background_color(), &debug.overlay(), ); diff --git a/src/application.rs b/src/application.rs index f05d2c4b..01571b56 100644 --- a/src/application.rs +++ b/src/application.rs @@ -1,5 +1,6 @@ +use crate::theme; use crate::window; -use crate::{Color, Command, Element, Executor, Settings, Subscription}; +use crate::{Command, Element, Executor, Settings, Subscription}; /// An interactive cross-platform application. /// @@ -101,7 +102,7 @@ pub trait Application: Sized { type Message: std::fmt::Debug + Send; /// The theme of your [`Application`]. - type Theme: Default; + type Theme: Default + theme::Definition; /// The data needed to initialize your [`Application`]. type Flags; @@ -167,13 +168,6 @@ pub trait Application: Sized { window::Mode::Windowed } - /// Returns the background color of the [`Application`]. - /// - /// By default, it returns [`Color::WHITE`]. - fn background_color(&self) -> Color { - Color::WHITE - } - /// Returns the scale factor of the [`Application`]. /// /// It can be used to dynamically control the size of the UI at runtime @@ -277,10 +271,6 @@ where self.0.subscription() } - fn background_color(&self) -> Color { - self.0.background_color() - } - fn scale_factor(&self) -> f64 { self.0.scale_factor() } diff --git a/src/pure/application.rs b/src/pure/application.rs index 77f68c9e..1306ab6c 100644 --- a/src/pure/application.rs +++ b/src/pure/application.rs @@ -1,6 +1,7 @@ use crate::pure::{self, Pure}; +use crate::theme; use crate::window; -use crate::{Color, Command, Executor, Settings, Subscription}; +use crate::{Command, Executor, Settings, Subscription}; /// A pure version of [`Application`]. /// @@ -22,7 +23,7 @@ pub trait Application: Sized { type Message: std::fmt::Debug + Send; /// The theme of your [`Application`]. - type Theme: Default; + type Theme: Default + theme::Definition; /// The data needed to initialize your [`Application`]. type Flags; @@ -88,13 +89,6 @@ pub trait Application: Sized { window::Mode::Windowed } - /// Returns the background color of the [`Application`]. - /// - /// By default, it returns [`Color::WHITE`]. - fn background_color(&self) -> Color { - Color::WHITE - } - /// Returns the scale factor of the [`Application`]. /// /// It can be used to dynamically control the size of the UI at runtime @@ -185,10 +179,6 @@ where A::mode(&self.application) } - fn background_color(&self) -> Color { - A::background_color(&self.application) - } - fn scale_factor(&self) -> f64 { A::scale_factor(&self.application) } diff --git a/src/pure/sandbox.rs b/src/pure/sandbox.rs index 207a32bd..a58cace7 100644 --- a/src/pure/sandbox.rs +++ b/src/pure/sandbox.rs @@ -1,5 +1,5 @@ use crate::pure; -use crate::{Color, Command, Error, Settings, Subscription, Theme}; +use crate::{Command, Error, Settings, Subscription, Theme}; /// A pure version of [`Sandbox`]. /// @@ -44,13 +44,6 @@ pub trait Sandbox { Theme::default() } - /// Returns the background color of the [`Sandbox`]. - /// - /// By default, it returns [`Color::WHITE`]. - fn background_color(&self) -> Color { - Color::WHITE - } - /// Returns the scale factor of the [`Sandbox`]. /// /// It can be used to dynamically control the size of the UI at runtime @@ -120,10 +113,6 @@ where Subscription::none() } - fn background_color(&self) -> Color { - T::background_color(self) - } - fn scale_factor(&self) -> f64 { T::scale_factor(self) } diff --git a/src/sandbox.rs b/src/sandbox.rs index 16819569..f03562fb 100644 --- a/src/sandbox.rs +++ b/src/sandbox.rs @@ -1,5 +1,5 @@ use crate::{ - Application, Color, Command, Element, Error, Settings, Subscription, Theme, + Application, Command, Element, Error, Settings, Subscription, Theme, }; /// A sandboxed [`Application`]. @@ -121,13 +121,6 @@ pub trait Sandbox { Theme::default() } - /// Returns the background color of the [`Sandbox`]. - /// - /// By default, it returns [`Color::WHITE`]. - fn background_color(&self) -> Color { - Color::WHITE - } - /// Returns the scale factor of the [`Sandbox`]. /// /// It can be used to dynamically control the size of the UI at runtime @@ -197,10 +190,6 @@ where Subscription::none() } - fn background_color(&self) -> Color { - T::background_color(self) - } - fn scale_factor(&self) -> f64 { T::scale_factor(self) } diff --git a/style/src/theme.rs b/style/src/theme.rs index 91a9e921..16f24923 100644 --- a/style/src/theme.rs +++ b/style/src/theme.rs @@ -4,7 +4,7 @@ pub use self::palette::Palette; use crate::button; -use iced_core::Background; +use iced_core::{Background, Color}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Theme { @@ -34,6 +34,18 @@ impl Default for Theme { } } +pub trait Definition { + fn background_color(&self) -> Color; +} + +impl Definition for Theme { + fn background_color(&self) -> Color { + let palette = self.extended_palette(); + + palette.background.base + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Button { Primary, diff --git a/winit/src/application.rs b/winit/src/application.rs index abe6b8a9..55fd9e73 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -6,9 +6,10 @@ pub use state::State; use crate::clipboard::{self, Clipboard}; use crate::conversion; use crate::mouse; +use crate::theme::{self, Definition as _}; use crate::{ - Color, Command, Debug, Error, Executor, Mode, Proxy, Runtime, Settings, - Size, Subscription, + Command, Debug, Error, Executor, Mode, Proxy, Runtime, Settings, Size, + Subscription, }; use iced_futures::futures; @@ -77,13 +78,6 @@ pub trait Application: Program { Mode::Windowed } - /// Returns the background [`Color`] of the [`Application`]. - /// - /// By default, it returns [`Color::WHITE`]. - fn background_color(&self) -> Color { - Color::WHITE - } - /// Returns the scale factor of the [`Application`]. /// /// It can be used to dynamically control the size of the UI at runtime @@ -115,6 +109,7 @@ where A: Application + 'static, E: Executor + 'static, C: window::Compositor<Renderer = A::Renderer> + 'static, + <A::Renderer as iced_native::Renderer>::Theme: theme::Definition, { use futures::task; use futures::Future; @@ -250,6 +245,7 @@ async fn run_instance<A, E, C>( A: Application + 'static, E: Executor + 'static, C: window::Compositor<Renderer = A::Renderer> + 'static, + <A::Renderer as iced_native::Renderer>::Theme: theme::Definition, { use iced_futures::futures::stream::StreamExt; use winit::event; @@ -425,7 +421,7 @@ async fn run_instance<A, E, C>( &mut renderer, &mut surface, state.viewport(), - state.background_color(), + theme.background_color(), &debug.overlay(), ) { Ok(()) => { diff --git a/winit/src/application/state.rs b/winit/src/application/state.rs index b54d3aed..34a9b10e 100644 --- a/winit/src/application/state.rs +++ b/winit/src/application/state.rs @@ -1,5 +1,5 @@ use crate::conversion; -use crate::{Application, Color, Debug, Mode, Point, Size, Viewport}; +use crate::{Application, Debug, Mode, Point, Size, Viewport}; use std::marker::PhantomData; use winit::event::{Touch, WindowEvent}; @@ -10,7 +10,6 @@ use winit::window::Window; pub struct State<A: Application> { title: String, mode: Mode, - background_color: Color, scale_factor: f64, viewport: Viewport, viewport_version: usize, @@ -24,7 +23,6 @@ impl<A: Application> State<A> { pub fn new(application: &A, window: &Window) -> Self { let title = application.title(); let mode = application.mode(); - let background_color = application.background_color(); let scale_factor = application.scale_factor(); let viewport = { @@ -39,7 +37,6 @@ impl<A: Application> State<A> { Self { title, mode, - background_color, scale_factor, viewport, viewport_version: 0, @@ -50,11 +47,6 @@ impl<A: Application> State<A> { } } - /// Returns the current background [`Color`] of the [`State`]. - pub fn background_color(&self) -> Color { - self.background_color - } - /// Returns the current [`Viewport`] of the [`State`]. pub fn viewport(&self) -> &Viewport { &self.viewport @@ -187,9 +179,6 @@ impl<A: Application> State<A> { self.mode = new_mode; } - // Update background color - self.background_color = application.background_color(); - // Update scale factor let new_scale_factor = application.scale_factor(); |