use std::{
any::{Any, TypeId, type_name_of_val},
cell::{Cell, RefCell},
rc::Rc,
};
use cranpose_core::{NodeId, PackedU64};
#[expect(unused_imports, reason = "only some cfgs read it")]
use cranpose_foundation::InvalidationKind;
use cranpose_foundation::{
BasicModifierNodeContext, ModifierInvalidations, ModifierNode, ModifierNodeChain,
ModifierNodeContext, NodeCapabilities, PlainElement as _,
};
use super::{
DimensionConstraint, EdgeInsets, Elements, LayoutProperties, Modifier, ModifierInspectorRecord,
ModifierKind, ModifierLocalAncestorResolver, ModifierLocalToken, Point, ResolvedModifiers,
element_match::{self, ElementMatch},
layout_data::LayoutDataNode,
local::ModifierLocalManager,
modifier_debug_enabled,
offset::OffsetPxNode,
};
mod shared_parent_data;
pub(crate) use shared_parent_data::SharedParentData;
use crate::{
modifier_nodes::{
AlignmentElement, FillDirection, FillElement, OffsetElement, PaddingElement, SizeElement,
WeightElement,
},
text_modifier_node::TextModifierElement,
};
#[derive(Clone, Debug, PartialEq)]
pub struct ModifierChainInspectorNode {
pub depth: usize,
pub entry_index: Option<usize>,
pub type_name: &'static str,
pub capabilities: NodeCapabilities,
pub aggregate_child_capabilities: NodeCapabilities,
pub inspector: Option<ModifierInspectorRecord>,
}
pub(crate) type ModifierLocalsHandle = Rc<RefCell<ModifierLocalManager>>;
pub struct ModifierChainHandle {
chain: ModifierNodeChain,
layout_direction: crate::LayoutDirection,
pending_invalidations: Option<Box<ModifierInvalidations>>,
parent_data: SharedParentData,
modifier_locals: Option<ModifierLocalsHandle>,
debug_logging: bool,
revision: PackedU64,
changed_in_place: Cell<u64>,
#[cfg(feature = "inspection")]
inspector: Option<Rc<Vec<super::InspectorMetadata>>>,
}
#[cfg(feature = "inspection")]
fn inspector_of(modifier: &Modifier) -> Option<Rc<Vec<super::InspectorMetadata>>> {
match &modifier.kind {
ModifierKind::Empty => None,
ModifierKind::Single { inspector, .. } => inspector.clone(),
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum InPlaceReach {
Node,
Measure,
Resolved,
}
#[derive(Clone, Copy)]
pub(crate) struct InPlaceUpdate {
pub(crate) reach: InPlaceReach,
pub(crate) elements: u64,
}
impl InPlaceReach {
pub(crate) fn of(element_type: TypeId) -> Option<Self> {
if element_type == TypeId::of::<TextModifierElement>() {
Some(Self::Node)
} else if element_type == TypeId::of::<SizeElement>() {
Some(Self::Measure)
} else if element_type == TypeId::of::<FillElement>() {
Some(Self::Resolved)
} else {
None
}
}
}
impl Default for ModifierChainHandle {
fn default() -> Self {
Self::with_chain(ModifierNodeChain::new(), crate::LayoutDirection::Ltr)
}
}
fn next_revision() -> u64 {
crate::thread_state::cells(|cells| {
let revision = cells.last_modifier_revision.get() + 1;
cells.last_modifier_revision.set(revision);
revision
})
}
impl ModifierChainHandle {
pub fn new() -> Self {
Self::default()
}
fn with_chain(chain: ModifierNodeChain, layout_direction: crate::LayoutDirection) -> Self {
Self {
chain,
layout_direction,
pending_invalidations: None,
parent_data: SharedParentData::default(),
modifier_locals: None,
debug_logging: false,
revision: PackedU64::new(next_revision()),
changed_in_place: Cell::new(0),
#[cfg(feature = "inspection")]
inspector: None,
}
}
pub(crate) fn detached(layout_direction: crate::LayoutDirection) -> Self {
Self::with_chain(ModifierNodeChain::detached(), layout_direction)
}
pub(crate) fn for_layout_direction(layout_direction: crate::LayoutDirection) -> Self {
Self {
layout_direction,
..Self::default()
}
}
pub(crate) fn layout_direction(&self) -> crate::LayoutDirection {
self.layout_direction
}
pub(crate) fn revision(&self) -> u64 {
self.revision.get()
}
pub(crate) fn take_changed_in_place(&self) -> u64 {
self.changed_in_place.take()
}
pub(crate) fn set_layout_direction(&mut self, direction: crate::LayoutDirection) -> bool {
if self.layout_direction == direction {
return false;
}
self.layout_direction = direction;
self.revision = PackedU64::new(next_revision());
self.update_offset_direction();
self.resolve();
true
}
fn update_offset_direction(&mut self) {
let direction = self.layout_direction;
self.chain.visit_nodes_mut(|node, capabilities| {
if capabilities.contains(NodeCapabilities::DRAW)
&& let Some(offset) = node.as_any_mut().downcast_mut::<OffsetPxNode>()
{
offset.set_layout_direction(direction);
}
});
}
pub fn update(&mut self, modifier: &Modifier) -> ModifierInvalidations {
self.update_on_node(None, modifier)
}
pub(crate) fn update_on_node(
&mut self,
node_id: Option<NodeId>,
modifier: &Modifier,
) -> ModifierInvalidations {
let mut resolver = |_: &ModifierLocalToken| None;
let mut context = self.context(node_id);
let locals = self.update_with_resolver(modifier, &mut resolver, &mut context);
let mut invalidations = context.take_invalidations();
invalidations.extend(locals);
invalidations
}
pub(crate) fn context(&mut self, node_id: Option<NodeId>) -> BasicModifierNodeContext {
let pending = self
.pending_invalidations
.take()
.map_or_else(ModifierInvalidations::new, |pending| *pending);
BasicModifierNodeContext::for_node(node_id, pending)
}
fn keep_for_next(&mut self, mut context: BasicModifierNodeContext) {
let invalidations = context.take_invalidations();
if !invalidations.is_empty() {
self.pending_invalidations = Some(Box::new(invalidations));
}
}
pub(crate) fn update_elements_in_place(
&mut self,
modifier: &Modifier,
update: InPlaceUpdate,
context: &mut BasicModifierNodeContext,
) -> bool {
if self
.chain
.capabilities()
.contains(NodeCapabilities::MODIFIER_LOCALS)
{
return false;
}
let updated = self.chain.update_entries_in_place(
modifier.iter_elements(),
modifier.element_count(),
update.elements,
context,
);
#[cfg(feature = "inspection")]
if updated {
self.inspector = inspector_of(modifier);
}
if updated && update.reach != InPlaceReach::Node {
self.changed_in_place
.set(self.changed_in_place.get() | update.elements);
}
if updated && update.reach == InPlaceReach::Resolved {
self.resolve_fills();
}
updated
}
pub(crate) fn invalidate_last(&self, context: &mut BasicModifierNodeContext) {
self.chain.invalidate_last(context);
}
pub(crate) fn update_with_resolver(
&mut self,
modifier: &Modifier,
resolver: &mut ModifierLocalAncestorResolver<'_>,
context: &mut BasicModifierNodeContext,
) -> ModifierInvalidations {
self.revision = PackedU64::new(next_revision());
self.chain
.update_from_ref_iter(modifier.iter_elements(), context);
#[cfg(feature = "inspection")]
{
self.inspector = inspector_of(modifier);
}
if self.layout_direction.is_rtl() {
self.update_offset_direction();
}
let modifier_local_invalidations = self.sync_modifier_locals(resolver);
if !context.invalidations().is_empty() {
self.resolve();
}
let should_log = self.debug_logging || modifier_debug_enabled();
if should_log {
let snapshot = self.inspector_snapshot(modifier);
crate::debug::log_modifier_chain(self.chain(), &snapshot);
crate::debug::emit_modifier_chain_trace(&snapshot);
}
modifier_local_invalidations
}
pub fn set_debug_logging(&mut self, enabled: bool) {
self.debug_logging = enabled;
}
pub(crate) fn move_to_node(
&mut self,
previous: Option<NodeId>,
context: &mut BasicModifierNodeContext,
) {
let id = context.node_id();
if previous == id {
return;
}
self.revision = PackedU64::new(next_revision());
if id.is_some() {
self.chain.detach_nodes();
self.chain.attach_nodes(context);
}
}
pub(crate) fn move_to_node_later(&mut self, previous: Option<NodeId>, id: Option<NodeId>) {
let mut context = self.context(id);
self.move_to_node(previous, &mut context);
self.keep_for_next(context);
}
pub(crate) fn remount(&mut self, node_id: Option<NodeId>) {
if self.chain.is_attached() {
return;
}
self.revision = PackedU64::new(next_revision());
let mut context = self.context(node_id);
self.chain.attach_nodes(&mut context);
self.keep_for_next(context);
}
pub fn chain(&self) -> &ModifierNodeChain {
&self.chain
}
pub fn chain_mut(&mut self) -> &mut ModifierNodeChain {
self.revision = PackedU64::new(next_revision());
&mut self.chain
}
pub(crate) fn holds(&self, modifier: &Modifier, rule: ElementMatch) -> bool {
element_match::elements_match(self.chain.held_elements(), modifier, rule)
}
pub(crate) fn node_after(&self, modifier: &Modifier) -> Option<&Rc<RefCell<dyn ModifierNode>>> {
element_match::elements_match_but_last(
self.chain.held_elements(),
modifier,
ElementMatch::Equal,
)
.then(|| self.chain.get_node_rc(modifier.element_count()))
.flatten()
}
pub(crate) fn match_for_set(&self, modifier: &Modifier) -> element_match::SetMatch {
element_match::match_for_set(self.chain.held_elements(), modifier)
}
pub(crate) fn modifier(&self) -> Modifier {
let mut held = self.chain.held_elements();
let Some(first) = held.next() else {
return Modifier::empty();
};
let elements = if held.len() == 0 {
Elements::One(first.to_element())
} else {
Elements::Many(Rc::new(
std::iter::once(first)
.chain(held)
.map(|element| element.to_element())
.collect(),
))
};
Modifier {
provides_composition_locals: elements
.iter()
.any(|element| element.provides_composition_locals()),
kind: ModifierKind::Single {
elements,
#[cfg(feature = "inspection")]
inspector: self.inspector.clone(),
#[cfg(not(feature = "inspection"))]
inspector: None,
},
}
}
pub fn capabilities(&self) -> NodeCapabilities {
self.chain.capabilities()
}
pub fn aggregate_child_capabilities(&self) -> NodeCapabilities {
self.chain.head().aggregate_child_capabilities()
}
pub fn has_layout_nodes(&self) -> bool {
self.capabilities().contains(NodeCapabilities::LAYOUT)
}
pub fn has_draw_nodes(&self) -> bool {
self.capabilities().contains(NodeCapabilities::DRAW)
}
pub fn resolved_modifiers(&self) -> ResolvedModifiers {
let resolution = self.resolution(NodeCapabilities::LAYOUT);
let mut resolved = ResolvedModifiers::default();
resolved.set_padding(resolution.padding);
resolved.set_layout_properties(resolution.layout);
resolved.set_offset(resolution.offset);
resolved
}
pub(crate) fn parent_data(&self) -> SharedParentData {
self.parent_data.clone()
}
pub fn modifier_locals_handle(&self) -> Option<ModifierLocalsHandle> {
self.modifier_locals.clone()
}
fn sync_modifier_locals(
&mut self,
resolver: &mut ModifierLocalAncestorResolver<'_>,
) -> ModifierInvalidations {
if self.modifier_locals.is_none()
&& !self.chain.has_capability(NodeCapabilities::MODIFIER_LOCALS)
{
return ModifierInvalidations::new();
}
self.modifier_locals
.get_or_insert_with(ModifierLocalsHandle::default)
.borrow_mut()
.sync(&self.chain, resolver)
}
pub fn inspector_snapshot(&self, modifier: &Modifier) -> Vec<ModifierChainInspectorNode> {
let chain_len = self.chain.len();
let mut records: Vec<Option<ModifierInspectorRecord>> = modifier
.iter_inspector_metadata()
.take(chain_len)
.map(|metadata| Some(metadata.to_record()))
.collect();
let mut snapshot = Vec::with_capacity(chain_len);
self.chain.for_each_forward(|node_ref| {
let type_name = match node_ref.plain_words() {
Some((fns, _)) => Some(fns.type_name()),
None => node_ref.with_node(type_name_of_val),
};
let Some(type_name) = type_name else {
return;
};
let depth = node_ref.delegate_depth();
let entry_index = node_ref.entry_index();
let inspector = if depth == 0 {
entry_index
.and_then(|idx| records.get_mut(idx))
.and_then(Option::take)
} else {
None
};
snapshot.push(ModifierChainInspectorNode {
depth,
entry_index,
type_name,
capabilities: node_ref.kind_set(),
aggregate_child_capabilities: node_ref.aggregate_child_capabilities(),
inspector,
});
});
snapshot
}
fn resolution(&self, kind: NodeCapabilities) -> Resolution {
let mut resolution = Resolution {
direction: self.layout_direction,
..Resolution::default()
};
self.chain
.for_each_forward_matching(kind, |node_ref| match node_ref.plain_words() {
Some((fns, words)) => resolution.take_plain(fns, words),
None => {
node_ref.with_node(|node| resolution.take(node.as_any()));
}
});
resolution
}
fn resolve_fills(&mut self) {
let mut fill_width = None;
let mut fill_height = None;
self.chain
.for_each_forward_matching(NodeCapabilities::PARENT_DATA, |node_ref| {
if let Some(fill) = node_ref.plain::<FillElement>() {
let (horizontal, vertical) = fill.fills();
if horizontal {
fill_width.get_or_insert_with(|| fill.fraction());
}
if vertical {
fill_height.get_or_insert_with(|| fill.fraction());
}
}
});
let width = fill_width.unwrap_or_default();
let height = fill_height.unwrap_or_default();
let data = self.parent_data.read(|current| {
(current.fill_width_fraction != width || current.fill_height_fraction != height).then(
|| cranpose_ui_layout::ParentData {
fill_width_fraction: width,
fill_height_fraction: height,
..current.clone()
},
)
});
if let Some(data) = data {
self.parent_data.set(data);
}
}
fn resolve(&mut self) {
if !self.chain.has_capability(NodeCapabilities::PARENT_DATA) {
self.parent_data = SharedParentData::default();
return;
}
let Resolution {
layout,
fill_width,
fill_height,
layout_data,
..
} = self.resolution(NodeCapabilities::PARENT_DATA);
let weight = layout.weight.unwrap_or_default();
let data = cranpose_ui_layout::ParentData {
weight: weight.weight,
fill: weight.fill,
box_alignment: layout.box_alignment,
row_alignment: layout.row_alignment,
row_baseline: layout.row_baseline,
column_alignment: layout.column_alignment,
fill_width_fraction: fill_width.unwrap_or_default(),
fill_height_fraction: fill_height.unwrap_or_default(),
layout_data,
};
if !self.parent_data.read(|current| *current == data) {
self.parent_data.set(data);
}
}
}
#[derive(Default)]
struct Resolution {
layout: LayoutProperties,
padding: EdgeInsets,
direction: crate::LayoutDirection,
offset: Point,
fill_width: Option<f32>,
fill_height: Option<f32>,
layout_data: Option<Rc<dyn cranpose_ui_layout::LayoutData>>,
}
impl Resolution {
fn take_plain(&mut self, fns: &cranpose_foundation::PlainFns, words: &[u32]) {
let kind = fns.element_type();
if kind == TypeId::of::<PaddingElement>() {
self.padding += PaddingElement::from_words(words).padding();
} else if kind == TypeId::of::<SizeElement>() {
apply_size_node(&mut self.layout, &SizeElement::from_words(words));
} else if kind == TypeId::of::<FillElement>() {
self.take_fill(&FillElement::from_words(words));
} else if kind == TypeId::of::<WeightElement>() {
self.layout
.weight
.get_or_insert_with(|| WeightElement::from_words(words).layout_weight());
} else if kind == TypeId::of::<AlignmentElement>() {
apply_alignment(&mut self.layout, &AlignmentElement::from_words(words));
} else if kind == TypeId::of::<OffsetElement>() {
let delta = OffsetElement::from_words(words).offset(self.direction);
self.offset.x += delta.x;
self.offset.y += delta.y;
}
}
fn take(&mut self, node: &dyn Any) {
if self.layout_data.is_none()
&& let Some(data) = node.downcast_ref::<LayoutDataNode>()
{
self.layout_data = Some(Rc::clone(data.data()));
}
}
fn take_fill(&mut self, fill: &FillElement) {
apply_fill_node(&mut self.layout, fill);
let (horizontal, vertical) = fill.fills();
if horizontal {
self.fill_width.get_or_insert_with(|| fill.fraction());
}
if vertical {
self.fill_height.get_or_insert_with(|| fill.fraction());
}
}
}
fn apply_alignment(layout: &mut LayoutProperties, element: &AlignmentElement) {
if layout.box_alignment.is_none() {
layout.box_alignment = element.in_box();
}
if layout.column_alignment.is_none() {
layout.column_alignment = element.in_column();
}
if layout.row_alignment.is_none() && !layout.row_baseline {
layout.row_alignment = element.in_row();
layout.row_baseline = element.on_row_baseline();
}
}
fn apply_size_node(layout: &mut LayoutProperties, node: &SizeElement) {
apply_size_axis(
node.min_width(),
node.max_width(),
node.enforce_incoming(),
&mut layout.width,
&mut layout.min_width,
&mut layout.max_width,
);
apply_size_axis(
node.min_height(),
node.max_height(),
node.enforce_incoming(),
&mut layout.height,
&mut layout.min_height,
&mut layout.max_height,
);
}
fn apply_size_axis(
min: Option<f32>,
max: Option<f32>,
enforce_incoming: bool,
dimension: &mut DimensionConstraint,
min_slot: &mut Option<f32>,
max_slot: &mut Option<f32>,
) {
let fixed = min == max;
if let (true, Some(size)) = (fixed, max.or(min)) {
*dimension = DimensionConstraint::Points(size);
}
if fixed && enforce_incoming {
return;
}
if let Some(min) = min {
*min_slot = Some(min);
}
if let Some(max) = max {
*max_slot = Some(max);
}
}
fn apply_fill_node(layout: &mut LayoutProperties, node: &FillElement) {
let fraction = node.fraction();
match node.direction() {
FillDirection::Horizontal => {
layout.width = DimensionConstraint::Fraction(fraction);
}
FillDirection::Vertical => {
layout.height = DimensionConstraint::Fraction(fraction);
}
FillDirection::Both => {
layout.width = DimensionConstraint::Fraction(fraction);
layout.height = DimensionConstraint::Fraction(fraction);
}
}
}
#[cfg(test)]
#[path = "tests/chain_tests.rs"]
mod tests;