use std::collections::HashMap;
use std::string::String;
use std::vec::Vec;
use crate::element::View;
use crate::engine::VeloxEngine;
use crate::platform::a11y_bridge::{AccessKitBridge, AccessKitTreeUpdate};
use crate::pod::InputEvent;
use crate::solver::Region;
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct MultiWindowId(pub u32);
#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub enum WindowRole {
#[default]
Primary = 0,
Secondary = 1,
Popover = 2,
Tooltip = 3,
DockTearOff = 4,
}
#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub enum DockZone {
Left = 0,
Right = 1,
Bottom = 2,
Center = 3,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct DetachedDockPanel {
pub window_id: MultiWindowId,
pub parent_id: MultiWindowId,
pub panel_key: String,
pub title: String,
pub origin_zone: DockZone,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct CsdConfig {
pub enabled: bool,
pub titlebar_height: i16,
pub resize_border_px: i16,
pub controls_inset_right: i16,
pub double_click_maximize: bool,
}
impl Default for CsdConfig {
fn default() -> Self {
Self {
enabled: false,
titlebar_height: 36,
resize_border_px: 6,
controls_inset_right: 138,
double_click_maximize: true,
}
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum CsdHitZone {
Client,
Caption,
ResizeLeft,
ResizeRight,
ResizeTop,
ResizeBottom,
ResizeTopLeft,
ResizeTopRight,
ResizeBottomLeft,
ResizeBottomRight,
}
impl CsdConfig {
pub fn hit_test(&self, x: i16, y: i16, win_w: i16, win_h: i16) -> CsdHitZone {
if !self.enabled {
return CsdHitZone::Client;
}
let b = self.resize_border_px.max(0);
let left = x < b;
let right = x >= win_w.saturating_sub(b);
let top = y < b;
let bottom = y >= win_h.saturating_sub(b);
match (left, right, top, bottom) {
(true, _, true, _) => CsdHitZone::ResizeTopLeft,
(_, true, true, _) => CsdHitZone::ResizeTopRight,
(true, _, _, true) => CsdHitZone::ResizeBottomLeft,
(_, true, _, true) => CsdHitZone::ResizeBottomRight,
(true, _, _, _) => CsdHitZone::ResizeLeft,
(_, true, _, _) => CsdHitZone::ResizeRight,
(_, _, true, _) => CsdHitZone::ResizeTop,
(_, _, _, true) => CsdHitZone::ResizeBottom,
_ => {
if y < self.titlebar_height
&& x < win_w.saturating_sub(self.controls_inset_right)
{
CsdHitZone::Caption
} else {
CsdHitZone::Client
}
}
}
}
}
#[derive(Clone, Debug)]
pub struct MultiWindowSpec {
pub title: String,
pub width: i16,
pub height: i16,
pub role: WindowRole,
pub parent: Option<MultiWindowId>,
pub transparent: bool,
pub always_on_top: bool,
pub csd: CsdConfig,
}
impl MultiWindowSpec {
pub fn primary(title: impl Into<String>, width: i16, height: i16) -> Self {
Self {
title: title.into(),
width,
height,
role: WindowRole::Primary,
parent: None,
transparent: false,
always_on_top: false,
csd: CsdConfig::default(),
}
}
pub fn popover(parent: MultiWindowId, width: i16, height: i16) -> Self {
Self {
title: String::new(),
width,
height,
role: WindowRole::Popover,
parent: Some(parent),
transparent: true,
always_on_top: true,
csd: CsdConfig::default(),
}
}
pub fn with_csd(mut self, csd: CsdConfig) -> Self {
self.csd = csd;
self
}
}
pub struct WindowSession {
pub id: MultiWindowId,
pub spec: MultiWindowSpec,
pub engine: VeloxEngine,
pub a11y: AccessKitBridge,
pub focused: bool,
pub maximized: bool,
}
pub struct MultiWindowManager {
next_id: u32,
windows: HashMap<MultiWindowId, WindowSession>,
order: Vec<MultiWindowId>,
detached_docks: Vec<DetachedDockPanel>,
}
impl Default for MultiWindowManager {
fn default() -> Self {
Self::new()
}
}
impl MultiWindowManager {
pub fn new() -> Self {
Self {
next_id: 1,
windows: HashMap::new(),
order: Vec::new(),
detached_docks: Vec::new(),
}
}
pub fn open_window<V: View>(&mut self, spec: MultiWindowSpec, root_view: V) -> MultiWindowId {
let id = MultiWindowId(self.next_id);
self.next_id = self.next_id.wrapping_add(1).max(1);
let mut engine = VeloxEngine::new(spec.width.max(1), spec.height.max(1));
let _ = engine.mount_root(root_view);
let session = WindowSession {
id,
spec,
engine,
a11y: AccessKitBridge::new(),
focused: self.windows.is_empty(),
maximized: false,
};
self.windows.insert(id, session);
self.order.push(id);
id
}
pub fn close_window(&mut self, id: MultiWindowId) -> bool {
if self.windows.remove(&id).is_none() {
return false;
}
self.order.retain(|&w| w != id);
let orphans: Vec<MultiWindowId> = self
.windows
.values()
.filter(|w| w.spec.parent == Some(id))
.map(|w| w.id)
.collect();
for child in orphans {
self.close_window(child);
}
true
}
#[inline]
pub fn window_count(&self) -> usize {
self.windows.len()
}
#[inline]
pub fn window(&self, id: MultiWindowId) -> Option<&WindowSession> {
self.windows.get(&id)
}
#[inline]
pub fn window_mut(&mut self, id: MultiWindowId) -> Option<&mut WindowSession> {
self.windows.get_mut(&id)
}
pub fn dispatch_event(&mut self, id: MultiWindowId, event: InputEvent) -> bool {
self.windows
.get_mut(&id)
.is_some_and(|w| w.engine.dispatch_event(event))
}
pub fn broadcast_event(&mut self, event: InputEvent) -> usize {
let mut changed = 0usize;
for &id in &self.order {
if let Some(w) = self.windows.get_mut(&id) {
if w.engine.dispatch_event(event) {
changed += 1;
}
}
}
changed
}
pub fn step_all(
&mut self,
dt_ms: u16,
) -> Vec<(MultiWindowId, usize, Option<AccessKitTreeUpdate>)> {
let mut reports = Vec::with_capacity(self.order.len());
for &id in &self.order {
if let Some(w) = self.windows.get_mut(&id) {
let dirty_n = w.engine.step_frame_dt(dt_ms).len();
let a11y_update = if dirty_n > 0 {
w.a11y.sync_engine(&w.engine)
} else {
None
};
reports.push((id, dirty_n, a11y_update));
}
}
reports
}
pub fn hit_test_csd(&self, id: MultiWindowId, x: i16, y: i16) -> CsdHitZone {
let Some(w) = self.windows.get(&id) else {
return CsdHitZone::Client;
};
let vp: Region = w.engine.viewport_region();
w.spec.csd.hit_test(x, y, vp.w, vp.h)
}
pub fn tear_off_dock_panel<V: View>(
&mut self,
parent: MultiWindowId,
panel_key: impl Into<String>,
title: impl Into<String>,
origin_zone: DockZone,
width: i16,
height: i16,
view: V,
) -> MultiWindowId {
let title_str = title.into();
let key_str = panel_key.into();
let spec = MultiWindowSpec {
title: title_str.clone(),
width,
height,
role: WindowRole::DockTearOff,
parent: Some(parent),
transparent: false,
always_on_top: false,
csd: CsdConfig {
enabled: true,
..CsdConfig::default()
},
};
let win_id = self.open_window(spec, view);
self.detached_docks.push(DetachedDockPanel {
window_id: win_id,
parent_id: parent,
panel_key: key_str,
title: title_str,
origin_zone,
});
win_id
}
pub fn redock_panel(
&mut self,
win_id: MultiWindowId,
target_zone: DockZone,
) -> Option<(String, DockZone)> {
let pos = self
.detached_docks
.iter()
.position(|d| d.window_id == win_id)?;
let entry = self.detached_docks.remove(pos);
let _ = self.close_window(win_id);
Some((entry.panel_key, target_zone))
}
#[inline]
pub fn detached_dock_panels(&self) -> &[DetachedDockPanel] {
&self.detached_docks
}
}