use std::cell::RefCell;
use cranpose_ui_graphics::{Rect, Size};
use cranpose_ui_layout::Placement;
use super::{LayoutState, NodeCache};
use crate::layout::LayoutRuntimeState;
pub(crate) struct LayoutParts {
cache: NodeCache,
layout_state: RefCell<LayoutState>,
runtime_state: RefCell<LayoutRuntimeState>,
}
impl LayoutParts {
pub(crate) fn new(runtime_state: LayoutRuntimeState) -> Self {
Self {
cache: NodeCache::default(),
layout_state: RefCell::default(),
runtime_state: RefCell::new(runtime_state),
}
}
pub(crate) fn empty() -> Self {
Self::new(LayoutRuntimeState::new(
super::layout_node::empty_measure_policy(),
))
}
pub(crate) fn cache(&self) -> &NodeCache {
&self.cache
}
pub(crate) fn layout_state(&self) -> &RefCell<LayoutState> {
&self.layout_state
}
pub(crate) fn note_child_placements(&self, placements: &[Placement]) {
let with_z = placements.iter().any(|placement| placement.z_index != 0.0);
self.layout_state
.borrow_mut()
.set_places_children_with_z_index(with_z);
}
pub(crate) fn runtime_state(&self) -> &RefCell<LayoutRuntimeState> {
&self.runtime_state
}
pub(crate) fn placed_coordinator_rect(&self, ordinal: usize, node_size: Size) -> Option<Rect> {
self.runtime_state
.borrow()
.placed_coordinator_rect(ordinal, node_size)
}
}
impl Drop for LayoutParts {
fn drop(&mut self) {
if self.layout_state.get_mut().places_children_with_z_index() {
super::layout_state::count_z_index_placer(false);
}
}
}