use std::any::TypeId;
use accesskit::{Node, Role};
use kurbo::{Axis, Point, Size};
use tracing::{Span, trace_span};
use crate::core::{
AccessCtx, ChildrenIds, LayoutCtx, MeasureCtx, NewWidget, NoAction, PaintCtx, PropertiesRef,
RegisterCtx, SubwindowId, UpdateCtx, Widget, WidgetId, WidgetMut, WidgetPod,
};
use crate::imaging::Painter;
use crate::layout::{LenReq, Length, SizeDef};
pub(crate) struct SubwindowStack {
subwindows: Vec<SubwindowEntry>,
}
struct SubwindowEntry {
id: SubwindowId,
root: WidgetPod<dyn Widget>,
position: Point,
}
impl SubwindowStack {
pub(crate) fn new(root: NewWidget<impl Widget + ?Sized>) -> Self {
let main_subwindow_id = SubwindowId::next();
let subwindow = SubwindowEntry {
id: main_subwindow_id,
root: root.erased().to_pod(),
position: Point::ZERO,
};
Self {
subwindows: vec![subwindow],
}
}
pub(crate) fn subwindow_count(&self) -> usize {
self.subwindows.len()
}
pub(crate) fn subwindow_root_id(&self, idx: usize) -> WidgetId {
self.subwindows[idx].root.id()
}
}
impl SubwindowStack {
pub(crate) fn add_subwindow(
this: &mut WidgetMut<'_, Self>,
id: SubwindowId,
root: NewWidget<impl Widget + ?Sized>,
position: Point,
) -> SubwindowId {
let root = NewWidget {
subwindow_id: Some(id),
..root
};
let subwindow = SubwindowEntry {
id,
root: root.erased().to_pod(),
position,
};
this.widget.subwindows.push(subwindow);
this.ctx.children_changed();
id
}
#[expect(dead_code, reason = "Might be useful later")]
pub(crate) fn subwindow_root_mut<'t>(
this: &'t mut WidgetMut<'_, Self>,
idx: usize,
) -> WidgetMut<'t, dyn Widget> {
let subwindow = &mut this.widget.subwindows[idx].root;
this.ctx.get_mut(subwindow)
}
pub(crate) fn remove_subwindow(this: &mut WidgetMut<'_, Self>, id: SubwindowId) {
let idx = this.widget.subwindows.iter().position(|subw| subw.id == id);
match idx {
Some(0) => debug_panic!("Cannot remove main subwindow"),
None => debug_panic!("subwindow with root widget {id:?} not found"),
Some(idx) => {
let child = this.widget.subwindows.remove(idx).root;
this.ctx.remove_child(child);
}
}
}
pub(crate) fn reposition_subwindow(
this: &mut WidgetMut<'_, Self>,
id: SubwindowId,
new_origin: Point,
) {
let idx = this.widget.subwindows.iter().position(|subw| subw.id == id);
match idx {
Some(0) => debug_panic!("Cannot reposition main subwindow"),
None => debug_panic!("subwindow with root widget {id:?} not found"),
Some(idx) => {
this.widget.subwindows[idx].position = new_origin;
this.ctx.request_layout();
}
}
}
}
impl Widget for SubwindowStack {
type Action = NoAction;
fn register_children(&mut self, ctx: &mut RegisterCtx<'_>) {
for subwindow in self.subwindows.iter_mut() {
ctx.register_child(&mut subwindow.root);
}
}
fn property_changed(&mut self, _ctx: &mut UpdateCtx<'_>, _property_type: TypeId) {}
fn measure(
&mut self,
ctx: &mut MeasureCtx<'_>,
_props: &PropertiesRef<'_>,
axis: Axis,
_len_req: LenReq,
cross_length: Option<Length>,
) -> Length {
let Some(main_subwindow) = self.subwindows.first_mut() else {
debug_panic!("Missing first subwindow");
return Length::ZERO;
};
ctx.redirect_measurement(&mut main_subwindow.root, axis, cross_length)
}
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, _props: &PropertiesRef<'_>, size: Size) {
let Some(main_subwindow) = self.subwindows.first_mut() else {
debug_panic!("Missing first subwindow");
return;
};
ctx.run_layout(&mut main_subwindow.root, size);
ctx.place_child(&mut main_subwindow.root, Point::ORIGIN);
ctx.derive_baselines(&main_subwindow.root);
for subwindow in &mut self.subwindows[1..] {
let subwindow_size = ctx.compute_size(&mut subwindow.root, SizeDef::MAX, size.into());
ctx.run_layout(&mut subwindow.root, subwindow_size);
ctx.place_child(&mut subwindow.root, subwindow.position);
}
}
fn paint(
&mut self,
_ctx: &mut PaintCtx<'_>,
_props: &PropertiesRef<'_>,
_painter: &mut Painter<'_>,
) {
}
fn accessibility_role(&self) -> Role {
Role::GenericContainer
}
fn accessibility(
&mut self,
_ctx: &mut AccessCtx<'_>,
_props: &PropertiesRef<'_>,
_node: &mut Node,
) {
}
fn children_ids(&self) -> ChildrenIds {
self.subwindows
.iter()
.map(|child| child.root.id())
.collect()
}
fn make_trace_span(&self, id: WidgetId) -> Span {
trace_span!("SubwindowStack", id = id.trace())
}
}