use std::cell::Cell;
use cranpose_core::{NodeId, PackedU64};
use super::layout_state::{ChildPlacement, LayoutState, count_z_index_placer};
use crate::{
modifier::{Point, Size},
render_state::AppContext,
};
#[derive(Default)]
pub(crate) struct LayoutCells {
size: Cell<Size>,
position: Cell<Point>,
is_placed: Cell<bool>,
cleared_in_pass: Cell<PackedU64>,
content_offset: Cell<Point>,
semantics_stamp: Cell<PackedU64>,
moved_noted_in: Cell<PackedU64>,
reshaped_noted_in: Cell<PackedU64>,
z_index: Cell<f32>,
places_children_with_z_index: Cell<bool>,
}
impl LayoutCells {
pub(crate) fn snapshot(&self) -> LayoutState {
LayoutState {
size: self.size.get(),
position: self.position.get(),
is_placed: self.is_placed.get(),
cleared_in_pass: self.cleared_in_pass.get(),
content_offset: self.content_offset.get(),
z_index: self.z_index.get(),
places_children_with_z_index: self.places_children_with_z_index.get(),
}
}
pub(crate) fn size(&self) -> Size {
self.size.get()
}
pub(crate) fn position(&self) -> Point {
self.position.get()
}
pub(crate) fn is_placed(&self) -> bool {
self.is_placed.get()
}
pub(crate) fn z_index(&self) -> f32 {
self.z_index.get()
}
pub(crate) fn places_children_with_z_index(&self) -> bool {
self.places_children_with_z_index.get()
}
pub(crate) fn set_content_offset(&self, node_id: Option<NodeId>, offset: Point) {
if self.content_offset.get() != offset {
self.content_offset.set(offset);
self.note_semantics_change(node_id);
}
}
#[inline]
pub(crate) fn note_geometry_change(&self, node_id: NodeId, context: &AppContext) {
stamp(&self.reshaped_noted_in, |noted| {
context.note_geometry_scene_node(node_id, noted);
});
}
#[inline]
pub(crate) fn set_size(&self, node_id: Option<NodeId>, size: Size, context: &AppContext) {
if self.size.get() != size {
if let Some(id) = node_id {
self.note_geometry_change(id, context);
}
self.size.set(size);
self.note_semantics_change(node_id);
}
}
pub(crate) fn set_places_children_with_z_index(&self, places: bool) {
if self.places_children_with_z_index.replace(places) != places {
count_z_index_placer(places);
}
}
pub(crate) fn start_over(&self) {
let Self {
size,
position,
is_placed,
cleared_in_pass,
content_offset,
semantics_stamp,
moved_noted_in,
reshaped_noted_in,
z_index,
places_children_with_z_index: _,
} = self;
size.take();
position.take();
is_placed.take();
cleared_in_pass.take();
content_offset.take();
semantics_stamp.take();
moved_noted_in.take();
reshaped_noted_in.take();
z_index.take();
self.set_places_children_with_z_index(false);
}
pub(crate) fn place_child(
&self,
node_id: Option<NodeId>,
at: ChildPlacement,
placer: Option<NodeId>,
context: &AppContext,
) {
self.place(node_id, at.position, placer, context);
self.set_z_index(node_id, at.z_index);
}
pub(crate) fn set_z_index(&self, node_id: Option<NodeId>, z_index: f32) {
if self.z_index.replace(z_index).to_bits() == z_index.to_bits() {
return;
}
if let Some(id) = node_id {
crate::schedule_z_order_change(id);
}
}
pub(crate) fn place(
&self,
node_id: Option<NodeId>,
position: Point,
placer: Option<NodeId>,
context: &AppContext,
) {
let was_placed = self.is_placed.get();
let newly_placed = !was_placed
&& !context
.placement_pass()
.restore(self.cleared_in_pass.get().get());
let moved = self.position.replace(position) != position;
if let Some(id) = node_id {
if newly_placed {
self.note_geometry_change(id, context);
} else if moved {
stamp(&self.moved_noted_in, |noted| {
context.record_moved_scene_node(id, placer, noted);
});
}
}
if moved || !was_placed {
self.is_placed.set(true);
self.note_semantics_change(node_id);
}
}
pub(crate) fn clear_placed(&self, node_id: Option<NodeId>, context: &AppContext) {
if !self.is_placed.replace(false) {
return;
}
self.cleared_in_pass
.set(PackedU64::new(context.placement_pass().clear(node_id)));
self.note_semantics_change(node_id);
}
#[inline]
pub(crate) fn note_semantics_change(&self, node_id: Option<NodeId>) {
if let Some(id) = node_id {
stamp(&self.semantics_stamp, |noted| {
crate::semantics_layout_log::record_layout_change(id, noted);
});
}
}
}
#[inline]
fn stamp(cell: &Cell<PackedU64>, note: impl FnOnce(&mut PackedU64)) {
let mut noted = cell.get();
note(&mut noted);
cell.set(noted);
}