From 5c5e7653bed248ba63faa6563e4d673e4441415e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 2 Dec 2023 22:26:01 +0100 Subject: Refactor `Windows` abstraction into `WindowManager` --- winit/src/multi_window/window_manager.rs | 156 +++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 winit/src/multi_window/window_manager.rs (limited to 'winit/src/multi_window/window_manager.rs') diff --git a/winit/src/multi_window/window_manager.rs b/winit/src/multi_window/window_manager.rs new file mode 100644 index 00000000..d54156e7 --- /dev/null +++ b/winit/src/multi_window/window_manager.rs @@ -0,0 +1,156 @@ +use crate::core::mouse; +use crate::core::window::Id; +use crate::core::{Point, Size}; +use crate::graphics::Compositor; +use crate::multi_window::{Application, State}; +use crate::style::application::StyleSheet; + +use std::collections::BTreeMap; +use winit::monitor::MonitorHandle; + +#[allow(missing_debug_implementations)] +pub struct WindowManager +where + ::Theme: StyleSheet, + C: Compositor, +{ + aliases: BTreeMap, + entries: BTreeMap>, +} + +impl WindowManager +where + A: Application, + C: Compositor, + ::Theme: StyleSheet, +{ + pub fn new() -> Self { + Self { + aliases: BTreeMap::new(), + entries: BTreeMap::new(), + } + } + + pub fn insert( + &mut self, + id: Id, + window: winit::window::Window, + application: &A, + compositor: &mut C, + exit_on_close_request: bool, + ) -> &mut Window { + let state = State::new(application, id, &window); + let viewport_version = state.viewport_version(); + let physical_size = state.physical_size(); + let surface = compositor.create_surface( + &window, + physical_size.width, + physical_size.height, + ); + let renderer = compositor.create_renderer(); + + let _ = self.aliases.insert(window.id(), id); + + let _ = self.entries.insert( + id, + Window { + raw: window, + state, + viewport_version, + exit_on_close_request, + surface, + renderer, + mouse_interaction: mouse::Interaction::Idle, + }, + ); + + self.entries + .get_mut(&id) + .expect("Get window that was just inserted") + } + + pub fn is_empty(&self) -> bool { + self.entries.is_empty() + } + + pub fn iter_mut( + &mut self, + ) -> impl Iterator)> { + self.entries.iter_mut().map(|(k, v)| (*k, v)) + } + + pub fn get_mut(&mut self, id: Id) -> Option<&mut Window> { + self.entries.get_mut(&id) + } + + pub fn get_mut_alias( + &mut self, + id: winit::window::WindowId, + ) -> Option<(Id, &mut Window)> { + let id = self.aliases.get(&id).copied()?; + + Some((id, self.get_mut(id)?)) + } + + pub fn last_monitor(&self) -> Option { + self.entries.values().last()?.raw.current_monitor() + } + + pub fn remove(&mut self, id: Id) -> Option> { + let window = self.entries.remove(&id)?; + let _ = self.aliases.remove(&window.raw.id()); + + Some(window) + } +} + +impl Default for WindowManager +where + A: Application, + C: Compositor, + ::Theme: StyleSheet, +{ + fn default() -> Self { + Self::new() + } +} + +#[allow(missing_debug_implementations)] +pub struct Window +where + A: Application, + C: Compositor, + ::Theme: StyleSheet, +{ + pub raw: winit::window::Window, + pub state: State, + pub viewport_version: u64, + pub exit_on_close_request: bool, + pub mouse_interaction: mouse::Interaction, + pub surface: C::Surface, + pub renderer: A::Renderer, +} + +impl Window +where + A: Application, + C: Compositor, + ::Theme: StyleSheet, +{ + pub fn position(&self) -> Option { + self.raw + .inner_position() + .ok() + .map(|position| position.to_logical(self.raw.scale_factor())) + .map(|position| Point { + x: position.x, + y: position.y, + }) + } + + pub fn size(&self) -> Size { + let size = self.raw.inner_size().to_logical(self.raw.scale_factor()); + + Size::new(size.width, size.height) + } +} -- cgit From 8bf238697226e827dc983f9d89afbd0e252c5254 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 18 Jan 2024 09:55:27 +0100 Subject: Remove `Compositor` window generic And update `glyphon` and `window_clipboard` --- winit/src/multi_window/window_manager.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'winit/src/multi_window/window_manager.rs') diff --git a/winit/src/multi_window/window_manager.rs b/winit/src/multi_window/window_manager.rs index d54156e7..9e15f9ea 100644 --- a/winit/src/multi_window/window_manager.rs +++ b/winit/src/multi_window/window_manager.rs @@ -6,6 +6,7 @@ use crate::multi_window::{Application, State}; use crate::style::application::StyleSheet; use std::collections::BTreeMap; +use std::sync::Arc; use winit::monitor::MonitorHandle; #[allow(missing_debug_implementations)] @@ -34,7 +35,7 @@ where pub fn insert( &mut self, id: Id, - window: winit::window::Window, + window: Arc, application: &A, compositor: &mut C, exit_on_close_request: bool, @@ -43,7 +44,7 @@ where let viewport_version = state.viewport_version(); let physical_size = state.physical_size(); let surface = compositor.create_surface( - &window, + window.clone(), physical_size.width, physical_size.height, ); @@ -122,7 +123,7 @@ where C: Compositor, ::Theme: StyleSheet, { - pub raw: winit::window::Window, + pub raw: Arc, pub state: State, pub viewport_version: u64, pub exit_on_close_request: bool, -- cgit