use cranpose_core::{MemoryApplier, NodeId, RecordId, collections::map::HashMap};
use cranpose_ui::{ModifierNodeSlices, Point, Rect, Size};
use cranpose_ui_graphics::GraphicsLayer;
use super::{
AbsOrigin, Inherited, LowerContext, ParentOrigin, TranslateAncestorContext,
inline_leaf::retext_kept_inline, placing_transform, publish_retained_window_geometry,
text_node, translate_layer::PlacedChild,
};
use crate::{
NodeUpdate,
layer_transform::laid_out_transform_to_window,
scene_store::{LayerRef, RecordKind, SceneStore},
};
#[derive(Clone, Copy)]
pub(super) struct TranslateGeometry {
pub(super) content_offset: Point,
window: Option<WindowPlace>,
}
#[derive(Clone, Copy)]
struct WindowPlace {
top_left: Point,
children: AbsOrigin,
}
impl TranslateGeometry {
pub(super) fn new(
layout_state: &cranpose_ui::widgets::LayoutState,
graphics_layer: &GraphicsLayer,
local_bounds: Rect,
parent_abs: Option<&AbsOrigin>,
) -> Self {
let content_offset = layout_state.content_offset();
let window = parent_abs.map(|parent_abs| {
let top_left = Point {
x: parent_abs.content_origin.x + layout_state.position().x,
y: parent_abs.content_origin.y + layout_state.position().y,
};
WindowPlace {
top_left,
children: AbsOrigin {
content_origin: Point {
x: top_left.x + content_offset.x,
y: top_left.y + content_offset.y,
},
window_transform: laid_out_transform_to_window(
parent_abs.window_transform,
top_left,
local_bounds,
graphics_layer,
),
},
}
});
Self {
content_offset,
window,
}
}
pub(super) fn publish(&self, slices: &ModifierNodeSlices, size: Size) {
match &self.window {
Some(window) => slices.publish_window_geometry(
window.top_left,
window.children.window_transform,
size,
),
None => slices.publish_pointer_input_size(size),
}
}
pub(super) fn children_context<'a>(
&'a self,
container: LayerRef<'_>,
ancestors: TranslateAncestorContext<'_>,
) -> TranslateAncestorContext<'a> {
TranslateAncestorContext {
inherited: Inherited {
motion_context_animated: container.motion_context_animated(),
translated_content_context: ancestors.inherited.translated_content_context
|| container.translated_content_context(),
parent_content_offset: self.content_offset,
},
parent_abs: self
.child_abs()
.map_or(ParentOrigin::Unread, ParentOrigin::Known),
}
}
pub(super) fn child_abs(&self) -> Option<&AbsOrigin> {
self.window.as_ref().map(|window| &window.children)
}
}
pub(super) fn translate_retained_child(
store: &mut SceneStore,
layer: RecordId,
position: Point,
content_offset: Point,
) {
let transform = placing_transform(
store.layer(layer).local_bounds(),
position,
store.graphics_layer(layer),
content_offset,
);
store.set_placement(layer, position, transform);
}
#[derive(Clone, Copy)]
pub(super) enum KeptChild {
Left,
Moved,
Retexted,
}
#[inline]
pub(super) fn move_kept_child(
applier: &MemoryApplier,
store: &mut SceneStore,
record: RecordId,
placed: &PlacedChild,
children: TranslateAncestorContext<'_>,
dirty_nodes: &mut HashMap<NodeId, NodeUpdate>,
) {
let position = placed.geometry.position;
match placed.kept {
KeptChild::Left => {}
KeptChild::Moved if store.layer(record).kind() == RecordKind::Leaf => {
store.move_leaf(record, position);
}
KeptChild::Moved => move_kept_layer(applier, store, record, position, children),
KeptChild::Retexted => {
write_kept_label(applier, store, record, placed, children, dirty_nodes);
}
}
}
#[inline(never)]
fn move_kept_layer(
applier: &MemoryApplier,
store: &mut SceneStore,
record: RecordId,
position: Point,
children: TranslateAncestorContext<'_>,
) {
translate_retained_child(
store,
record,
position,
children.inherited.parent_content_offset,
);
if store.layer(record).has_origin_sinks()
&& let Some(parent_abs) = children.parent_abs.resolve(store)
{
publish_retained_window_geometry(applier, store.layer(record), parent_abs);
}
}
#[inline(never)]
fn write_kept_label(
applier: &MemoryApplier,
store: &mut SceneStore,
record: RecordId,
placed: &PlacedChild,
children: TranslateAncestorContext<'_>,
dirty_nodes: &mut HashMap<NodeId, NodeUpdate>,
) {
let written = match store.layer(record).kind() {
RecordKind::Leaf => {
let context = LowerContext::beneath(children, store);
retext_kept_inline(applier, store, record, context)
}
RecordKind::Layer | RecordKind::Wrapper | RecordKind::DrawGroup => {
text_node::retext_kept_leaf(applier, store, record, children) == Some(true)
}
};
if written {
dirty_nodes.remove(&placed.id);
}
}