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 ++++++++++++++++++++++++++++----------- winit/src/application/state.rs | 40 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 64 insertions(+), 15 deletions(-) (limited to 'winit') 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); diff --git a/winit/src/application/state.rs b/winit/src/application/state.rs index 34a9b10e..6b843919 100644 --- a/winit/src/application/state.rs +++ b/winit/src/application/state.rs @@ -1,13 +1,17 @@ +use crate::application::{self, StyleSheet as _}; use crate::conversion; -use crate::{Application, Debug, Mode, Point, Size, Viewport}; +use crate::{Application, Color, Debug, Mode, Point, Size, Viewport}; use std::marker::PhantomData; use winit::event::{Touch, WindowEvent}; use winit::window::Window; /// The state of a windowed [`Application`]. -#[derive(Debug, Clone)] -pub struct State { +#[allow(missing_debug_implementations)] +pub struct State +where + ::Theme: application::StyleSheet, +{ title: String, mode: Mode, scale_factor: f64, @@ -15,15 +19,22 @@ pub struct State { viewport_version: usize, cursor_position: winit::dpi::PhysicalPosition, modifiers: winit::event::ModifiersState, + theme: ::Theme, + appearance: application::Appearance, application: PhantomData, } -impl State { +impl State +where + ::Theme: application::StyleSheet, +{ /// Creates a new [`State`] for the provided [`Application`] and window. pub fn new(application: &A, window: &Window) -> Self { let title = application.title(); let mode = application.mode(); let scale_factor = application.scale_factor(); + let theme = application.theme(); + let appearance = theme.appearance(application.style()); let viewport = { let physical_size = window.inner_size(); @@ -43,6 +54,8 @@ impl State { // TODO: Encode cursor availability in the type-system cursor_position: winit::dpi::PhysicalPosition::new(-1.0, -1.0), modifiers: winit::event::ModifiersState::default(), + theme, + appearance, application: PhantomData, } } @@ -87,6 +100,21 @@ impl State { self.modifiers } + /// Returns the current theme of the [`State`]. + pub fn theme(&self) -> &::Theme { + &self.theme + } + + /// Returns the current background [`Color`] of the [`State`]. + pub fn background_color(&self) -> Color { + self.appearance.background_color + } + + /// Returns the current text [`Color`] of the [`State`]. + pub fn text_color(&self) -> Color { + self.appearance.text_color + } + /// Processes the provided window event and updates the [`State`] /// accordingly. pub fn update( @@ -192,5 +220,9 @@ impl State { self.scale_factor = new_scale_factor; } + + // Update theme and appearance + self.theme = application.theme(); + self.appearance = self.theme.appearance(application.style()); } } -- cgit