use std::cell::RefCell;
use std::rc::Rc;
use geometry_core::Rect;
use layout_core::{LayoutError, LayoutStyle, NodeId};
use platform_core::Event;
use reactive_core::RwSignal;
use ui_tree::{Component, EventResult, RenderNode, Segment};
use crate::context::{new_container, track_layout};
use crate::layout_leaf::LayoutLeaf;
#[derive(Clone)]
pub(crate) struct Child {
pub(crate) item: Rc<RefCell<Box<dyn LayoutItem>>>,
pub(crate) rect: Option<RwSignal<Rect>>,
pub(crate) segment: Rc<Segment>,
}
impl Child {
pub(crate) fn node(&self) -> layout_core::NodeId {
self.item.borrow().layout_node()
}
}
pub(crate) fn make_child(widget: Box<dyn LayoutItem>) -> Child {
let rect = track_layout(widget.layout_node());
let item = Rc::new(RefCell::new(widget));
let segment = mount_item_segment(Rc::clone(&item));
Child {
item,
rect,
segment,
}
}
pub(crate) type TrackedChildren = Vec<Child>;
pub(crate) fn mount_item_segment(item: Rc<RefCell<Box<dyn LayoutItem>>>) -> Rc<Segment> {
let name = item
.try_borrow()
.map(|i| i.debug_name())
.unwrap_or("Component");
Segment::mount_fn_named(name, move || item.try_borrow().ok().map(|i| i.view()))
}
pub(crate) trait LeafWidget {
fn layout_leaf(&self) -> &LayoutLeaf;
}
pub trait LayoutItem: Component {
fn layout_node(&self) -> NodeId;
}
pub struct ClippedItem {
inner: Box<dyn LayoutItem>,
rect: RwSignal<Rect>,
}
impl ClippedItem {
pub fn new(inner: Box<dyn LayoutItem>) -> Self {
let rect = track_layout(inner.layout_node()).expect("clipped item's node not registered");
Self { inner, rect }
}
}
impl LayoutItem for ClippedItem {
fn layout_node(&self) -> NodeId {
self.inner.layout_node()
}
}
impl Component for ClippedItem {
fn view(&self) -> RenderNode {
RenderNode::Clip {
rect: self.rect.get(),
radius: renderer_core::BorderRadius::zero(),
children: ui_tree::NodeVec::collect([self.inner.view()]),
}
}
fn on_event(&mut self, event: &Event) -> EventResult {
self.inner.on_event(event)
}
fn debug_name(&self) -> &'static str {
"Clipped"
}
}
impl<T: LeafWidget + Component> LayoutItem for T {
fn layout_node(&self) -> NodeId {
self.layout_leaf().node
}
}
impl Component for Box<dyn LayoutItem> {
fn view(&self) -> RenderNode {
(**self).view()
}
fn on_event(&mut self, event: &Event) -> EventResult {
(**self).on_event(event)
}
fn debug_name(&self) -> &'static str {
(**self).debug_name()
}
}
impl LayoutItem for Box<dyn LayoutItem> {
fn layout_node(&self) -> NodeId {
(**self).layout_node()
}
}
pub fn box_item(item: impl LayoutItem + 'static) -> Box<dyn LayoutItem> {
Box::new(item)
}
pub(crate) fn register_container(
layout_style: LayoutStyle,
children: Vec<Box<dyn LayoutItem>>,
) -> Result<(NodeId, RwSignal<Rect>, TrackedChildren), LayoutError> {
let child_nodes = children.iter().map(|c| c.layout_node()).collect::<Vec<_>>();
let node = new_container(layout_style, &child_nodes)?;
let rect = track_layout(node).expect("new_container always registers a signal");
let children = children.into_iter().map(make_child).collect();
Ok((node, rect, children))
}
#[macro_export]
macro_rules! impl_leaf_widget {
($struct:ident) => {
impl $crate::layout_item::LeafWidget for $struct {
fn layout_leaf(&self) -> &$crate::layout_leaf::LayoutLeaf {
&self.leaf
}
}
};
}