Skip to content

Commit

Permalink
Let a Theme control the background color of an application
Browse files Browse the repository at this point in the history
... and remove `Application::background_color`
  • Loading branch information
hecrj committed May 25, 2022
1 parent 2cfb307 commit 03eda9b
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 79 deletions.
6 changes: 1 addition & 5 deletions examples/pure/game_of_life/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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) => {
Expand Down
5 changes: 4 additions & 1 deletion glutin/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
);

Expand Down
16 changes: 3 additions & 13 deletions src/application.rs
Original file line number Diff line number Diff line change
@@ -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.
///
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
16 changes: 3 additions & 13 deletions src/pure/application.rs
Original file line number Diff line number Diff line change
@@ -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`].
///
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
13 changes: 1 addition & 12 deletions src/pure/sandbox.rs
Original file line number Diff line number Diff line change
@@ -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`].
///
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
13 changes: 1 addition & 12 deletions src/sandbox.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
Application, Color, Command, Element, Error, Settings, Subscription, Theme,
Application, Command, Element, Error, Settings, Subscription, Theme,
};

/// A sandboxed [`Application`].
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
14 changes: 13 additions & 1 deletion style/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 6 additions & 10 deletions winit/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(()) => {
Expand Down
13 changes: 1 addition & 12 deletions winit/src/application/state.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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,
Expand All @@ -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 = {
Expand All @@ -39,7 +37,6 @@ impl<A: Application> State<A> {
Self {
title,
mode,
background_color,
scale_factor,
viewport,
viewport_version: 0,
Expand All @@ -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
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 03eda9b

Please sign in to comment.