diff options
Diffstat (limited to 'winit/src/application')
-rw-r--r-- | winit/src/application/state.rs | 40 |
1 files changed, 36 insertions, 4 deletions
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<A: Application> { +#[allow(missing_debug_implementations)] +pub struct State<A: Application> +where + <A::Renderer as crate::Renderer>::Theme: application::StyleSheet, +{ title: String, mode: Mode, scale_factor: f64, @@ -15,15 +19,22 @@ pub struct State<A: Application> { viewport_version: usize, cursor_position: winit::dpi::PhysicalPosition<f64>, modifiers: winit::event::ModifiersState, + theme: <A::Renderer as crate::Renderer>::Theme, + appearance: application::Appearance, application: PhantomData<A>, } -impl<A: Application> State<A> { +impl<A: Application> State<A> +where + <A::Renderer as crate::Renderer>::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<A: Application> State<A> { // 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<A: Application> State<A> { self.modifiers } + /// Returns the current theme of the [`State`]. + pub fn theme(&self) -> &<A::Renderer as crate::Renderer>::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<A: Application> State<A> { self.scale_factor = new_scale_factor; } + + // Update theme and appearance + self.theme = application.theme(); + self.appearance = self.theme.appearance(application.style()); } } |