diff options
Diffstat (limited to 'glutin')
-rw-r--r-- | glutin/src/multi_window.rs | 14 | ||||
-rw-r--r-- | glutin/src/multi_window/state.rs | 223 |
2 files changed, 5 insertions, 232 deletions
diff --git a/glutin/src/multi_window.rs b/glutin/src/multi_window.rs index e79ec77d..2b456543 100644 --- a/glutin/src/multi_window.rs +++ b/glutin/src/multi_window.rs @@ -1,8 +1,4 @@ //! Create interactive, native cross-platform applications. -mod state; - -pub use state::State; - use crate::mouse; use crate::{Error, Executor, Runtime}; @@ -382,7 +378,7 @@ async fn run_instance<A, E, C>( .expect("Create surface."); let current_context = context.make_current(&surface).expect("Make current."); - let state = State::new(&application, id, &window); + let state = multi_window::State::new(&application, id, &window); let physical_size = state.physical_size(); surface.resize( @@ -577,7 +573,7 @@ async fn run_instance<A, E, C>( event::Event::UserEvent(event) => match event { Event::Application(message) => messages.push(message), Event::WindowCreated(id, window) => { - let state = State::new(&application, id, &window); + let state = multi_window::State::new(&application, id, &window); let user_interface = multi_window::build_user_interface( &application, user_interface::Cache::default(), @@ -771,7 +767,7 @@ async fn run_instance<A, E, C>( pub fn update<A: Application, E: Executor>( application: &mut A, cache: &mut user_interface::Cache, - state: &State<A>, + state: &multi_window::State<A>, renderer: &mut A::Renderer, runtime: &mut Runtime<E, Proxy<Event<A::Message>>, Event<A::Message>>, clipboard: &mut Clipboard, @@ -813,7 +809,7 @@ pub fn update<A: Application, E: Executor>( pub fn run_command<A, E>( application: &A, cache: &mut user_interface::Cache, - state: &State<A>, + state: &multi_window::State<A>, renderer: &mut A::Renderer, command: Command<A::Message>, runtime: &mut Runtime<E, Proxy<Event<A::Message>>, Event<A::Message>>, @@ -993,7 +989,7 @@ pub fn build_user_interfaces<'a, A>( application: &'a A, renderer: &mut A::Renderer, debug: &mut Debug, - states: &HashMap<window::Id, State<A>>, + states: &HashMap<window::Id, multi_window::State<A>>, mut pure_states: HashMap<window::Id, user_interface::Cache>, ) -> HashMap< window::Id, diff --git a/glutin/src/multi_window/state.rs b/glutin/src/multi_window/state.rs deleted file mode 100644 index 8ed134b2..00000000 --- a/glutin/src/multi_window/state.rs +++ /dev/null @@ -1,223 +0,0 @@ -use crate::application::{self, StyleSheet as _}; -use crate::conversion; -use crate::multi_window::Application; -use crate::window; -use crate::{Color, Debug, Point, Size, Viewport}; - -use iced_winit::winit; -use winit::event::{Touch, WindowEvent}; -use winit::window::Window; - -use std::marker::PhantomData; - -/// The state of a windowed [`Application`]. -#[allow(missing_debug_implementations)] -pub struct State<A: Application> -where - <A::Renderer as crate::Renderer>::Theme: application::StyleSheet, -{ - title: String, - scale_factor: f64, - viewport: Viewport, - viewport_changed: bool, - cursor_position: winit::dpi::PhysicalPosition<f64>, - modifiers: winit::event::ModifiersState, - theme: <A::Renderer as crate::Renderer>::Theme, - appearance: iced_winit::application::Appearance, - application: PhantomData<A>, -} - -impl<A: Application> State<A> -where - <A::Renderer as crate::Renderer>::Theme: application::StyleSheet, -{ - /// Creates a new [`State`] for the provided [`Application`]'s window. - pub fn new( - application: &A, - window_id: window::Id, - window: &Window, - ) -> Self { - let title = application.title(window_id); - let scale_factor = application.scale_factor(); - let theme = application.theme(); - let appearance = theme.appearance(&application.style()); - - let viewport = { - let physical_size = window.inner_size(); - - Viewport::with_physical_size( - Size::new(physical_size.width, physical_size.height), - window.scale_factor() * scale_factor, - ) - }; - - Self { - title, - scale_factor, - viewport, - viewport_changed: false, - // 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, - } - } - - /// Returns the current [`Viewport`] of the [`State`]. - pub fn viewport(&self) -> &Viewport { - &self.viewport - } - - /// Returns whether or not the current [`Viewport`] has changed. - pub fn viewport_changed(&self) -> bool { - self.viewport_changed - } - - /// Returns the physical [`Size`] of the [`Viewport`] of the [`State`]. - pub fn physical_size(&self) -> Size<u32> { - self.viewport.physical_size() - } - - /// Returns the logical [`Size`] of the [`Viewport`] of the [`State`]. - pub fn logical_size(&self) -> Size<f32> { - self.viewport.logical_size() - } - - /// Returns the current scale factor of the [`Viewport`] of the [`State`]. - pub fn scale_factor(&self) -> f64 { - self.viewport.scale_factor() - } - - /// Returns the current cursor position of the [`State`]. - pub fn cursor_position(&self) -> Point { - conversion::cursor_position( - self.cursor_position, - self.viewport.scale_factor(), - ) - } - - /// Returns the current keyboard modifiers of the [`State`]. - pub fn modifiers(&self) -> winit::event::ModifiersState { - 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( - &mut self, - window: &Window, - event: &WindowEvent<'_>, - _debug: &mut Debug, - ) { - match event { - WindowEvent::Resized(new_size) => { - let size = Size::new(new_size.width, new_size.height); - - self.viewport = Viewport::with_physical_size( - size, - window.scale_factor() * self.scale_factor, - ); - - self.viewport_changed = true; - } - WindowEvent::ScaleFactorChanged { - scale_factor: new_scale_factor, - new_inner_size, - } => { - let size = - Size::new(new_inner_size.width, new_inner_size.height); - - self.viewport = Viewport::with_physical_size( - size, - new_scale_factor * self.scale_factor, - ); - - self.viewport_changed = true; - } - WindowEvent::CursorMoved { position, .. } - | WindowEvent::Touch(Touch { - location: position, .. - }) => { - self.cursor_position = *position; - } - WindowEvent::CursorLeft { .. } => { - // TODO: Encode cursor availability in the type-system - self.cursor_position = - winit::dpi::PhysicalPosition::new(-1.0, -1.0); - } - WindowEvent::ModifiersChanged(new_modifiers) => { - self.modifiers = *new_modifiers; - } - #[cfg(feature = "debug")] - WindowEvent::KeyboardInput { - input: - glutin::event::KeyboardInput { - virtual_keycode: - Some(glutin::event::VirtualKeyCode::F12), - state: glutin::event::ElementState::Pressed, - .. - }, - .. - } => _debug.toggle(), - _ => {} - } - } - - /// Synchronizes the [`State`] with its [`Application`] and its respective - /// window. - /// - /// Normally an [`Application`] should be synchronized with its [`State`] - /// and window after calling [`Application::update`]. - /// - /// [`Application::update`]: crate::Program::update - pub fn synchronize( - &mut self, - application: &A, - window_id: window::Id, - window: &Window, - ) { - // Update window title - let new_title = application.title(window_id); - - if self.title != new_title { - window.set_title(&new_title); - - self.title = new_title; - } - - // Update scale factor - let new_scale_factor = application.scale_factor(); - - if self.scale_factor != new_scale_factor { - let size = window.inner_size(); - - self.viewport = Viewport::with_physical_size( - Size::new(size.width, size.height), - window.scale_factor() * new_scale_factor, - ); - - self.scale_factor = new_scale_factor; - } - - // Update theme and appearance - self.theme = application.theme(); - self.appearance = self.theme.appearance(&application.style()); - } -} |