From 664251f3f5c7b76f69a97683af1468094bba887f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 14 May 2022 01:47:55 +0200 Subject: Draft first-class `Theme` support RFC: https://github.com/iced-rs/rfcs/pull/6 --- winit/src/application.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'winit/src/application.rs') diff --git a/winit/src/application.rs b/winit/src/application.rs index 90b03d56..abe6b8a9 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -51,6 +51,9 @@ pub trait Application: Program { /// title of your application when necessary. fn title(&self) -> String; + /// Returns the current [`Theme`] of the [`Application`]. + fn theme(&self) -> ::Theme; + /// Returns the event `Subscription` for the current state of the /// application. /// @@ -255,6 +258,7 @@ async fn run_instance( let mut state = State::new(&application, &window); let mut viewport_version = state.viewport_version(); + let mut theme = application.theme(); let physical_size = state.physical_size(); @@ -327,6 +331,7 @@ async fn run_instance( let should_exit = application.should_exit(); + theme = application.theme(); user_interface = ManuallyDrop::new(build_user_interface( &mut application, cache, @@ -341,8 +346,11 @@ async fn run_instance( } debug.draw_started(); - let new_mouse_interaction = - user_interface.draw(&mut renderer, state.cursor_position()); + let new_mouse_interaction = user_interface.draw( + &mut renderer, + &theme, + state.cursor_position(), + ); debug.draw_finished(); if new_mouse_interaction != mouse_interaction { @@ -389,8 +397,11 @@ async fn run_instance( debug.layout_finished(); debug.draw_started(); - let new_mouse_interaction = user_interface - .draw(&mut renderer, state.cursor_position()); + let new_mouse_interaction = user_interface.draw( + &mut renderer, + &theme, + state.cursor_position(), + ); if new_mouse_interaction != mouse_interaction { window.set_cursor_icon(conversion::mouse_interaction( -- cgit From 03eda9b162012c503ead649e5ccb95b7ef1d10ed Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 25 May 2022 05:01:18 +0200 Subject: Let a `Theme` control the background color of an application ... and remove `Application::background_color` --- winit/src/application.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'winit/src/application.rs') 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 + 'static, + ::Theme: theme::Definition, { use futures::task; use futures::Future; @@ -250,6 +245,7 @@ async fn run_instance( A: Application + 'static, E: Executor + 'static, C: window::Compositor + 'static, + ::Theme: theme::Definition, { use iced_futures::futures::stream::StreamExt; use winit::event; @@ -425,7 +421,7 @@ async fn run_instance( &mut renderer, &mut surface, state.viewport(), - state.background_color(), + theme.background_color(), &debug.overlay(), ) { Ok(()) => { -- cgit From 822a3cd04f9edeb887d85164b0b3e556c3fde6bb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 26 May 2022 01:10:26 +0200 Subject: Let a `Theme` control the `text_color` of an application --- winit/src/application.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'winit/src/application.rs') diff --git a/winit/src/application.rs b/winit/src/application.rs index 55fd9e73..12279bbb 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -504,7 +504,10 @@ pub fn build_user_interface<'a, A: Application>( renderer: &mut A::Renderer, size: Size, debug: &mut Debug, -) -> UserInterface<'a, A::Message, A::Renderer> { +) -> UserInterface<'a, A::Message, A::Renderer> +where + ::Theme: theme::Definition, +{ debug.view_started(); let view = application.view(); debug.view_finished(); -- cgit From 7f3b7075db68a215f4331b4bfba1c8ddd1c4d7f3 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 26 May 2022 19:02:15 +0200 Subject: Rename `theme::Definition` to `application::StyleSheet` --- winit/src/application.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'winit/src/application.rs') diff --git a/winit/src/application.rs b/winit/src/application.rs index 12279bbb..c7905c60 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -6,7 +6,6 @@ pub use state::State; use crate::clipboard::{self, Clipboard}; use crate::conversion; use crate::mouse; -use crate::theme::{self, Definition as _}; use crate::{ Command, Debug, Error, Executor, Mode, Proxy, Runtime, Settings, Size, Subscription, @@ -19,6 +18,8 @@ use iced_graphics::window; use iced_native::program::Program; use iced_native::user_interface::{self, UserInterface}; +pub use iced_native::application::StyleSheet; + use std::mem::ManuallyDrop; /// An interactive, native cross-platform application. @@ -109,7 +110,7 @@ where A: Application + 'static, E: Executor + 'static, C: window::Compositor + 'static, - ::Theme: theme::Definition, + ::Theme: StyleSheet, { use futures::task; use futures::Future; @@ -245,7 +246,7 @@ async fn run_instance( A: Application + 'static, E: Executor + 'static, C: window::Compositor + 'static, - ::Theme: theme::Definition, + ::Theme: StyleSheet, { use iced_futures::futures::stream::StreamExt; use winit::event; @@ -506,7 +507,7 @@ pub fn build_user_interface<'a, A: Application>( debug: &mut Debug, ) -> UserInterface<'a, A::Message, A::Renderer> where - ::Theme: theme::Definition, + ::Theme: StyleSheet, { debug.view_started(); let view = application.view(); -- cgit From bb07d017e8c8e43ac74f66bf649643bebdc5f71d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 8 Jul 2022 20:07:33 +0200 Subject: Add `Style` variant support to `application::StyleSheet` --- winit/src/application.rs | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) (limited to 'winit/src/application.rs') diff --git a/winit/src/application.rs b/winit/src/application.rs index c7905c60..9c7dd74e 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -6,6 +6,7 @@ pub use state::State; use crate::clipboard::{self, Clipboard}; use crate::conversion; use crate::mouse; +use crate::renderer; use crate::{ Command, Debug, Error, Executor, Mode, Proxy, Runtime, Settings, Size, Subscription, @@ -18,7 +19,7 @@ use iced_graphics::window; use iced_native::program::Program; use iced_native::user_interface::{self, UserInterface}; -pub use iced_native::application::StyleSheet; +pub use iced_native::application::{Appearance, StyleSheet}; use std::mem::ManuallyDrop; @@ -33,7 +34,10 @@ use std::mem::ManuallyDrop; /// /// When using an [`Application`] with the `debug` feature enabled, a debug view /// can be toggled by pressing `F12`. -pub trait Application: Program { +pub trait Application: Program +where + ::Theme: StyleSheet, +{ /// The data needed to initialize your [`Application`]. type Flags; @@ -54,7 +58,14 @@ pub trait Application: Program { fn title(&self) -> String; /// Returns the current [`Theme`] of the [`Application`]. - fn theme(&self) -> ::Theme; + fn theme(&self) -> ::Theme; + + /// Returns the [`Style`] variation of the [`Theme`]. + fn style( + &self, + ) -> <::Theme as StyleSheet>::Style { + Default::default() + } /// Returns the event `Subscription` for the current state of the /// application. @@ -110,7 +121,7 @@ where A: Application + 'static, E: Executor + 'static, C: window::Compositor + 'static, - ::Theme: StyleSheet, + ::Theme: StyleSheet, { use futures::task; use futures::Future; @@ -246,7 +257,7 @@ async fn run_instance( A: Application + 'static, E: Executor + 'static, C: window::Compositor + 'static, - ::Theme: StyleSheet, + ::Theme: StyleSheet, { use iced_futures::futures::stream::StreamExt; use winit::event; @@ -255,7 +266,6 @@ async fn run_instance( let mut state = State::new(&application, &window); let mut viewport_version = state.viewport_version(); - let mut theme = application.theme(); let physical_size = state.physical_size(); @@ -328,7 +338,6 @@ async fn run_instance( let should_exit = application.should_exit(); - theme = application.theme(); user_interface = ManuallyDrop::new(build_user_interface( &mut application, cache, @@ -345,7 +354,10 @@ async fn run_instance( debug.draw_started(); let new_mouse_interaction = user_interface.draw( &mut renderer, - &theme, + state.theme(), + &renderer::Style { + text_color: state.text_color(), + }, state.cursor_position(), ); debug.draw_finished(); @@ -396,7 +408,10 @@ async fn run_instance( debug.draw_started(); let new_mouse_interaction = user_interface.draw( &mut renderer, - &theme, + state.theme(), + &renderer::Style { + text_color: state.text_color(), + }, state.cursor_position(), ); @@ -422,7 +437,7 @@ async fn run_instance( &mut renderer, &mut surface, state.viewport(), - theme.background_color(), + state.background_color(), &debug.overlay(), ) { Ok(()) => { @@ -531,7 +546,9 @@ pub fn update( messages: &mut Vec, window: &winit::window::Window, graphics_info: impl FnOnce() -> compositor::Information + Copy, -) { +) where + ::Theme: StyleSheet, +{ for message in messages.drain(..) { debug.log_message(&message); -- cgit