use cranpose_core::{MemoryApplier, NodeId, RecordId, collections::map::HashMap};
use cranpose_ui::{Point, Rect};
use cranpose_ui_graphics::GraphicsLayer;
use super::{
AbsOrigin, Inherited, LowerContext, TranslateAncestorContext, inline_leaf::retext_kept_inline,
placing_transform, publish_retained_window_geometry, text_node, translate_layer::PlacedChild,
};
use crate::{
NodeUpdate,
graph::ProjectiveTransform,
layer_transform::layer_transform_to_window,
scene_store::{LayerRef, RecordKind, SceneStore},
};
#[derive(Clone, Copy)]
pub(super) struct TranslateGeometry {
pub(super) content_offset: Point,
pub(super) window_transform: ProjectiveTransform,
pub(super) top_left: Point,
child_origin: Point,
}
impl TranslateGeometry {
pub(super) fn new(
layout_state: &cranpose_ui::widgets::LayoutState,
graphics_layer: &GraphicsLayer,
local_bounds: Rect,
parent_abs: AbsOrigin,
) -> Self {
let content_offset = layout_state.content_offset();
let top_left = Point {
x: parent_abs.content_origin.x + layout_state.position().x,
y: parent_abs.content_origin.y + layout_state.position().y,
};
let window_transform = layer_transform_to_window(
parent_abs.window_transform,
top_left,
local_bounds,
graphics_layer,
);
Self {
content_offset,
window_transform,
top_left,
child_origin: Point {
x: top_left.x + content_offset.x,
y: top_left.y + content_offset.y,
},
}
}
pub(super) fn children_context(
self,
container: LayerRef<'_>,
ancestors: TranslateAncestorContext,
) -> TranslateAncestorContext {
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(),
}
}
pub(super) fn child_abs(self) -> AbsOrigin {
AbsOrigin {
content_origin: self.child_origin,
window_transform: self.window_transform,
}
}
}
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() {
publish_retained_window_geometry(applier, store.layer(record), children.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 => {
retext_kept_inline(applier, store, record, LowerContext::beneath(children))
}
RecordKind::Layer | RecordKind::Wrapper | RecordKind::DrawGroup => {
text_node::retext_kept_leaf(applier, store, record, children) == Some(true)
}
};
if written {
dirty_nodes.remove(&placed.id);
}
}