Let a Theme control the background color of an application

... and remove `Application::background_color`
This commit is contained in:
Héctor Ramón Jiménez 2022-05-25 05:01:18 +02:00
parent 2cfb307f8c
commit 03eda9b162
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
9 changed files with 33 additions and 79 deletions

View file

@ -12,7 +12,7 @@ use iced::pure::{Application, Element};
use iced::theme::{self, Theme}; use iced::theme::{self, Theme};
use iced::time; use iced::time;
use iced::window; use iced::window;
use iced::{Alignment, Color, Command, Length, Settings, Subscription}; use iced::{Alignment, Command, Length, Settings, Subscription};
use preset::Preset; use preset::Preset;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
@ -71,10 +71,6 @@ impl Application for GameOfLife {
String::from("Game of Life - Iced") String::from("Game of Life - Iced")
} }
fn background_color(&self) -> Color {
style::BACKGROUND
}
fn update(&mut self, message: Message) -> Command<Message> { fn update(&mut self, message: Message) -> Command<Message> {
match message { match message {
Message::Grid(message, version) => { Message::Grid(message, version) => {

View file

@ -9,6 +9,7 @@ use iced_winit::application;
use iced_winit::conversion; use iced_winit::conversion;
use iced_winit::futures; use iced_winit::futures;
use iced_winit::futures::channel::mpsc; use iced_winit::futures::channel::mpsc;
use iced_winit::theme::{self, Definition as _};
use iced_winit::user_interface; use iced_winit::user_interface;
use iced_winit::{Clipboard, Debug, Proxy, Settings}; use iced_winit::{Clipboard, Debug, Proxy, Settings};
@ -25,6 +26,7 @@ where
A: Application + 'static, A: Application + 'static,
E: Executor + 'static, E: Executor + 'static,
C: window::GLCompositor<Renderer = A::Renderer> + 'static, C: window::GLCompositor<Renderer = A::Renderer> + 'static,
<A::Renderer as iced_native::Renderer>::Theme: theme::Definition,
{ {
use futures::task; use futures::task;
use futures::Future; use futures::Future;
@ -203,6 +205,7 @@ async fn run_instance<A, E, C>(
A: Application + 'static, A: Application + 'static,
E: Executor + 'static, E: Executor + 'static,
C: window::GLCompositor<Renderer = A::Renderer> + 'static, C: window::GLCompositor<Renderer = A::Renderer> + 'static,
<A::Renderer as iced_native::Renderer>::Theme: theme::Definition,
{ {
use glutin::event; use glutin::event;
use iced_winit::futures::stream::StreamExt; use iced_winit::futures::stream::StreamExt;
@ -376,7 +379,7 @@ async fn run_instance<A, E, C>(
compositor.present( compositor.present(
&mut renderer, &mut renderer,
state.viewport(), state.viewport(),
state.background_color(), theme.background_color(),
&debug.overlay(), &debug.overlay(),
); );

View file

@ -1,5 +1,6 @@
use crate::theme;
use crate::window; use crate::window;
use crate::{Color, Command, Element, Executor, Settings, Subscription}; use crate::{Command, Element, Executor, Settings, Subscription};
/// An interactive cross-platform application. /// An interactive cross-platform application.
/// ///
@ -101,7 +102,7 @@ pub trait Application: Sized {
type Message: std::fmt::Debug + Send; type Message: std::fmt::Debug + Send;
/// The theme of your [`Application`]. /// The theme of your [`Application`].
type Theme: Default; type Theme: Default + theme::Definition;
/// The data needed to initialize your [`Application`]. /// The data needed to initialize your [`Application`].
type Flags; type Flags;
@ -167,13 +168,6 @@ pub trait Application: Sized {
window::Mode::Windowed 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`]. /// Returns the scale factor of the [`Application`].
/// ///
/// It can be used to dynamically control the size of the UI at runtime /// It can be used to dynamically control the size of the UI at runtime
@ -277,10 +271,6 @@ where
self.0.subscription() self.0.subscription()
} }
fn background_color(&self) -> Color {
self.0.background_color()
}
fn scale_factor(&self) -> f64 { fn scale_factor(&self) -> f64 {
self.0.scale_factor() self.0.scale_factor()
} }

View file

@ -1,6 +1,7 @@
use crate::pure::{self, Pure}; use crate::pure::{self, Pure};
use crate::theme;
use crate::window; use crate::window;
use crate::{Color, Command, Executor, Settings, Subscription}; use crate::{Command, Executor, Settings, Subscription};
/// A pure version of [`Application`]. /// A pure version of [`Application`].
/// ///
@ -22,7 +23,7 @@ pub trait Application: Sized {
type Message: std::fmt::Debug + Send; type Message: std::fmt::Debug + Send;
/// The theme of your [`Application`]. /// The theme of your [`Application`].
type Theme: Default; type Theme: Default + theme::Definition;
/// The data needed to initialize your [`Application`]. /// The data needed to initialize your [`Application`].
type Flags; type Flags;
@ -88,13 +89,6 @@ pub trait Application: Sized {
window::Mode::Windowed 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`]. /// Returns the scale factor of the [`Application`].
/// ///
/// It can be used to dynamically control the size of the UI at runtime /// It can be used to dynamically control the size of the UI at runtime
@ -185,10 +179,6 @@ where
A::mode(&self.application) A::mode(&self.application)
} }
fn background_color(&self) -> Color {
A::background_color(&self.application)
}
fn scale_factor(&self) -> f64 { fn scale_factor(&self) -> f64 {
A::scale_factor(&self.application) A::scale_factor(&self.application)
} }

View file

@ -1,5 +1,5 @@
use crate::pure; use crate::pure;
use crate::{Color, Command, Error, Settings, Subscription, Theme}; use crate::{Command, Error, Settings, Subscription, Theme};
/// A pure version of [`Sandbox`]. /// A pure version of [`Sandbox`].
/// ///
@ -44,13 +44,6 @@ pub trait Sandbox {
Theme::default() 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`]. /// Returns the scale factor of the [`Sandbox`].
/// ///
/// It can be used to dynamically control the size of the UI at runtime /// It can be used to dynamically control the size of the UI at runtime
@ -120,10 +113,6 @@ where
Subscription::none() Subscription::none()
} }
fn background_color(&self) -> Color {
T::background_color(self)
}
fn scale_factor(&self) -> f64 { fn scale_factor(&self) -> f64 {
T::scale_factor(self) T::scale_factor(self)
} }

View file

@ -1,5 +1,5 @@
use crate::{ use crate::{
Application, Color, Command, Element, Error, Settings, Subscription, Theme, Application, Command, Element, Error, Settings, Subscription, Theme,
}; };
/// A sandboxed [`Application`]. /// A sandboxed [`Application`].
@ -121,13 +121,6 @@ pub trait Sandbox {
Theme::default() 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`]. /// Returns the scale factor of the [`Sandbox`].
/// ///
/// It can be used to dynamically control the size of the UI at runtime /// It can be used to dynamically control the size of the UI at runtime
@ -197,10 +190,6 @@ where
Subscription::none() Subscription::none()
} }
fn background_color(&self) -> Color {
T::background_color(self)
}
fn scale_factor(&self) -> f64 { fn scale_factor(&self) -> f64 {
T::scale_factor(self) T::scale_factor(self)
} }

View file

@ -4,7 +4,7 @@ pub use self::palette::Palette;
use crate::button; use crate::button;
use iced_core::Background; use iced_core::{Background, Color};
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Theme { 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)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Button { pub enum Button {
Primary, Primary,

View file

@ -6,9 +6,10 @@ pub use state::State;
use crate::clipboard::{self, Clipboard}; use crate::clipboard::{self, Clipboard};
use crate::conversion; use crate::conversion;
use crate::mouse; use crate::mouse;
use crate::theme::{self, Definition as _};
use crate::{ use crate::{
Color, Command, Debug, Error, Executor, Mode, Proxy, Runtime, Settings, Command, Debug, Error, Executor, Mode, Proxy, Runtime, Settings, Size,
Size, Subscription, Subscription,
}; };
use iced_futures::futures; use iced_futures::futures;
@ -77,13 +78,6 @@ pub trait Application: Program {
Mode::Windowed 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`]. /// Returns the scale factor of the [`Application`].
/// ///
/// It can be used to dynamically control the size of the UI at runtime /// It can be used to dynamically control the size of the UI at runtime
@ -115,6 +109,7 @@ where
A: Application + 'static, A: Application + 'static,
E: Executor + 'static, E: Executor + 'static,
C: window::Compositor<Renderer = A::Renderer> + 'static, C: window::Compositor<Renderer = A::Renderer> + 'static,
<A::Renderer as iced_native::Renderer>::Theme: theme::Definition,
{ {
use futures::task; use futures::task;
use futures::Future; use futures::Future;
@ -250,6 +245,7 @@ async fn run_instance<A, E, C>(
A: Application + 'static, A: Application + 'static,
E: Executor + 'static, E: Executor + 'static,
C: window::Compositor<Renderer = A::Renderer> + 'static, C: window::Compositor<Renderer = A::Renderer> + 'static,
<A::Renderer as iced_native::Renderer>::Theme: theme::Definition,
{ {
use iced_futures::futures::stream::StreamExt; use iced_futures::futures::stream::StreamExt;
use winit::event; use winit::event;
@ -425,7 +421,7 @@ async fn run_instance<A, E, C>(
&mut renderer, &mut renderer,
&mut surface, &mut surface,
state.viewport(), state.viewport(),
state.background_color(), theme.background_color(),
&debug.overlay(), &debug.overlay(),
) { ) {
Ok(()) => { Ok(()) => {

View file

@ -1,5 +1,5 @@
use crate::conversion; 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 std::marker::PhantomData;
use winit::event::{Touch, WindowEvent}; use winit::event::{Touch, WindowEvent};
@ -10,7 +10,6 @@ use winit::window::Window;
pub struct State<A: Application> { pub struct State<A: Application> {
title: String, title: String,
mode: Mode, mode: Mode,
background_color: Color,
scale_factor: f64, scale_factor: f64,
viewport: Viewport, viewport: Viewport,
viewport_version: usize, viewport_version: usize,
@ -24,7 +23,6 @@ impl<A: Application> State<A> {
pub fn new(application: &A, window: &Window) -> Self { pub fn new(application: &A, window: &Window) -> Self {
let title = application.title(); let title = application.title();
let mode = application.mode(); let mode = application.mode();
let background_color = application.background_color();
let scale_factor = application.scale_factor(); let scale_factor = application.scale_factor();
let viewport = { let viewport = {
@ -39,7 +37,6 @@ impl<A: Application> State<A> {
Self { Self {
title, title,
mode, mode,
background_color,
scale_factor, scale_factor,
viewport, viewport,
viewport_version: 0, 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`]. /// Returns the current [`Viewport`] of the [`State`].
pub fn viewport(&self) -> &Viewport { pub fn viewport(&self) -> &Viewport {
&self.viewport &self.viewport
@ -187,9 +179,6 @@ impl<A: Application> State<A> {
self.mode = new_mode; self.mode = new_mode;
} }
// Update background color
self.background_color = application.background_color();
// Update scale factor // Update scale factor
let new_scale_factor = application.scale_factor(); let new_scale_factor = application.scale_factor();