use crate::events::PlatformEvent;
use crate::window::{LivePlatformWindow, PlatformWindow, WindowConfig};
use std::collections::HashMap;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WindowId(pub u64);
impl WindowId {
pub const MAIN: Self = Self(1);
}
struct ManagedWindowEntry {
#[allow(dead_code)]
id: WindowId,
window: Box<dyn PlatformWindow>,
is_modal: bool,
parent_id: Option<WindowId>,
children: Vec<WindowId>,
}
pub struct WindowManager {
windows: HashMap<u64, ManagedWindowEntry>,
active_window: Option<WindowId>,
next_id: u64,
}
impl Default for WindowManager {
fn default() -> Self {
Self::new()
}
}
impl WindowManager {
pub fn new() -> Self {
Self {
windows: HashMap::new(),
active_window: None,
next_id: 1,
}
}
pub fn create_window(&mut self, config: WindowConfig) -> WindowId {
let win = Box::new(LivePlatformWindow::new(config));
self.register_window(win)
}
pub fn register_window(&mut self, window: Box<dyn PlatformWindow>) -> WindowId {
let id = WindowId(self.next_id);
self.next_id += 1;
let entry = ManagedWindowEntry {
id,
window,
is_modal: false,
parent_id: None,
children: Vec::new(),
};
self.windows.insert(id.0, entry);
if self.active_window.is_none() {
self.active_window = Some(id);
}
id
}
pub fn create_modal(
&mut self,
parent: WindowId,
config: WindowConfig,
) -> Result<WindowId, String> {
let win = Box::new(LivePlatformWindow::new(config));
self.register_modal(parent, win)
}
pub fn register_modal(
&mut self,
parent: WindowId,
window: Box<dyn PlatformWindow>,
) -> Result<WindowId, String> {
if !self.windows.contains_key(&parent.0) {
return Err(format!("Parent window {:?} does not exist", parent));
}
let id = WindowId(self.next_id);
self.next_id += 1;
let entry = ManagedWindowEntry {
id,
window,
is_modal: true,
parent_id: Some(parent),
children: Vec::new(),
};
self.windows.insert(id.0, entry);
if let Some(parent_entry) = self.windows.get_mut(&parent.0) {
parent_entry.children.push(id);
}
self.active_window = Some(id);
Ok(id)
}
pub fn close_window(&mut self, id: WindowId) -> Result<(), String> {
let entry = self
.windows
.remove(&id.0)
.ok_or_else(|| format!("Window {:?} not found", id))?;
if let Some(parent_id) = entry.parent_id {
if let Some(parent) = self.windows.get_mut(&parent_id.0) {
parent.children.retain(|&c| c != id);
}
}
for child_id in entry.children {
let _ = self.close_window(child_id);
}
if self.active_window == Some(id) {
self.active_window = entry
.parent_id
.filter(|p| self.windows.contains_key(&p.0))
.or_else(|| self.windows.keys().next().map(|&k| WindowId(k)));
}
Ok(())
}
pub fn get_window(&self, id: WindowId) -> Option<&dyn PlatformWindow> {
self.windows.get(&id.0).map(|e| e.window.as_ref())
}
pub fn get_window_mut(&mut self, id: WindowId) -> Option<&mut (dyn PlatformWindow + 'static)> {
self.windows.get_mut(&id.0).map(|e| e.window.as_mut())
}
pub fn active_window(&self) -> Option<WindowId> {
self.active_window
}
pub fn set_active_window(&mut self, id: WindowId) -> Result<(), String> {
if !self.windows.contains_key(&id.0) {
return Err(format!("Window {:?} not found", id));
}
if self.is_modal_blocked(id) {
return Err(format!(
"Window {:?} cannot receive focus while blocked by a modal dialog",
id
));
}
for (wid, entry) in self.windows.iter_mut() {
entry.window.set_focused(*wid == id.0);
}
self.active_window = Some(id);
Ok(())
}
pub fn is_modal_blocked(&self, id: WindowId) -> bool {
if let Some(entry) = self.windows.get(&id.0) {
entry.children.iter().any(|&child_id| {
self.windows
.get(&child_id.0)
.is_some_and(|child| child.is_modal)
})
} else {
false
}
}
pub fn window_count(&self) -> usize {
self.windows.len()
}
pub fn broadcast_event(&mut self, event: PlatformEvent) {
for entry in self.windows.values_mut() {
entry.window.push_event(event.clone());
}
}
pub fn poll_all_events(&mut self) -> Vec<(WindowId, PlatformEvent)> {
let mut all_events = Vec::new();
for (&id, entry) in self.windows.iter_mut() {
let events = entry.window.poll_events();
for ev in events {
all_events.push((WindowId(id), ev));
}
}
all_events
}
}