use oxiui_core::window::{WindowChannel, WindowConfig, WindowId, WindowManager};
#[derive(Clone, Debug)]
pub struct SecondaryWindow {
pub id: WindowId,
pub config: WindowConfig,
pub content_key: Option<usize>,
}
pub struct WindowRegistry {
manager: WindowManager,
secondary: Vec<SecondaryWindow>,
}
impl WindowRegistry {
pub fn new() -> Self {
Self {
manager: WindowManager::default(),
secondary: Vec::new(),
}
}
pub fn open_window(&mut self, config: WindowConfig) -> WindowId {
let id = self.manager.create_window(config.clone());
self.secondary.push(SecondaryWindow {
id,
config,
content_key: None,
});
id
}
pub fn close_window(&mut self, id: WindowId) -> Option<SecondaryWindow> {
if id == WindowId::PRIMARY {
return None;
}
let _ = self.manager.destroy_window(id); let pos = self.secondary.iter().position(|w| w.id == id)?;
Some(self.secondary.remove(pos))
}
pub fn channel(&self) -> &WindowChannel {
self.manager.channel()
}
pub fn secondary_windows(&self) -> &[SecondaryWindow] {
&self.secondary
}
pub fn secondary_count(&self) -> usize {
self.secondary.len()
}
}
impl Default for WindowRegistry {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn open_window_returns_non_primary_id() {
let mut reg = WindowRegistry::new();
let id = reg.open_window(WindowConfig::new("secondary"));
assert_ne!(id, WindowId::PRIMARY);
}
#[test]
fn open_window_increments_secondary_count() {
let mut reg = WindowRegistry::new();
assert_eq!(reg.secondary_count(), 0);
reg.open_window(WindowConfig::new("w1"));
assert_eq!(reg.secondary_count(), 1);
reg.open_window(WindowConfig::new("w2"));
assert_eq!(reg.secondary_count(), 2);
}
#[test]
fn close_window_removes_secondary() {
let mut reg = WindowRegistry::new();
let id = reg.open_window(WindowConfig::new("w"));
assert_eq!(reg.secondary_count(), 1);
let removed = reg.close_window(id);
assert!(removed.is_some());
assert_eq!(reg.secondary_count(), 0);
}
#[test]
fn close_primary_window_is_noop() {
let mut reg = WindowRegistry::new();
let result = reg.close_window(WindowId::PRIMARY);
assert!(result.is_none());
}
#[test]
fn channel_send_and_drain() {
let reg = WindowRegistry::new();
let ch = reg.channel().clone();
let wid = WindowId(42);
ch.send(wid, "hello").unwrap();
let msgs = ch.drain_messages(wid).unwrap();
assert_eq!(msgs, vec!["hello"]);
}
#[test]
fn secondary_windows_slice_matches_registration_order() {
let mut reg = WindowRegistry::new();
let id1 = reg.open_window(WindowConfig::new("first"));
let id2 = reg.open_window(WindowConfig::new("second"));
let windows = reg.secondary_windows();
assert_eq!(windows.len(), 2);
assert_eq!(windows[0].id, id1);
assert_eq!(windows[1].id, id2);
}
#[test]
fn close_nonexistent_window_returns_none() {
let mut reg = WindowRegistry::new();
let result = reg.close_window(WindowId(999));
assert!(result.is_none());
}
}