use std::{
cell::RefCell,
hash::{Hash, Hasher},
};
use cranpose_animation::{Animatable, AnimationType, Spring, spring};
use cranpose_core::{RuntimeHandle, current_runtime_handle};
use cranpose_foundation::{
Constraints, DelegatableNode, InvalidationKind, LayoutModifierNode, Measurable, ModifierNode,
ModifierNodeContext, ModifierNodeElement, NodeCapabilities, NodeState,
};
use cranpose_ui_graphics::Size;
use cranpose_ui_layout::{LayoutDirection, LayoutModifierMeasureResult, round_to_px};
use crate::modifier::{Modifier, inspector_metadata};
impl Modifier {
pub fn animate_content_size(self) -> Self {
self.animate_content_size_with(spring(
Spring::DampingRatioNoBouncy,
Spring::StiffnessMediumLow,
))
}
pub fn animate_content_size_with(self, animation: AnimationType) -> Self {
let element = SizeAnimationElement {
animation,
direction: crate::layout_direction::layout_direction(),
runtime: current_runtime_handle(),
};
let modifier = Self::with_element(element).with_inspector_metadata(inspector_metadata(
"animateContentSize",
move |info| {
info.add_property("animationSpec", format!("{animation:?}"));
},
));
self.clip_to_bounds().then(modifier)
}
}
#[derive(Clone)]
struct SizeAnimationElement {
animation: AnimationType,
direction: LayoutDirection,
runtime: Option<RuntimeHandle>,
}
impl std::fmt::Debug for SizeAnimationElement {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SizeAnimationElement")
.field("animation", &self.animation)
.field("direction", &self.direction)
.finish_non_exhaustive()
}
}
impl PartialEq for SizeAnimationElement {
fn eq(&self, other: &Self) -> bool {
self.animation == other.animation && self.direction == other.direction
}
}
impl Hash for SizeAnimationElement {
fn hash<H: Hasher>(&self, state: &mut H) {
std::mem::discriminant(&self.animation).hash(state);
self.direction.hash(state);
}
}
impl ModifierNodeElement for SizeAnimationElement {
type Node = SizeAnimationNode;
fn create(&self) -> Self::Node {
SizeAnimationNode {
animation: self.animation,
direction: self.direction,
runtime: self.runtime.clone(),
size: RefCell::new(None),
state: NodeState::new(),
}
}
fn update(&self, node: &mut Self::Node) {
node.animation = self.animation;
node.direction = self.direction;
}
fn capabilities(&self) -> NodeCapabilities {
NodeCapabilities::LAYOUT
}
}
struct SizeAnimationNode {
animation: AnimationType,
direction: LayoutDirection,
runtime: Option<RuntimeHandle>,
size: RefCell<Option<Animatable<Size>>>,
state: NodeState,
}
impl SizeAnimationNode {
fn shown(&self, target: Size, density: f32) -> Size {
let animatable = {
let mut size = self.size.borrow_mut();
if size.is_none() {
let runtime = self.runtime.clone().or_else(current_runtime_handle);
*size = runtime.map(|runtime| Animatable::new(target, runtime));
}
size.clone()
};
let Some(mut animatable) = animatable else {
return target;
};
if animatable.target() != target {
animatable.animateTo(target, self.animation);
}
let value = animatable.state().value();
Size::new(
target.width - round_to_px(target.width - value.width, density),
target.height - round_to_px(target.height - value.height, density),
)
}
}
impl DelegatableNode for SizeAnimationNode {
fn node_state(&self) -> &NodeState {
&self.state
}
}
impl ModifierNode for SizeAnimationNode {
fn as_layout_node(&self) -> Option<&dyn LayoutModifierNode> {
Some(self)
}
fn on_reset(&mut self) {
self.size.get_mut().take();
}
fn as_layout_node_mut(&mut self) -> Option<&mut dyn LayoutModifierNode> {
Some(self)
}
fn on_attach(&mut self, context: &mut dyn ModifierNodeContext) {
context.invalidate(InvalidationKind::Layout);
}
}
impl LayoutModifierNode for SizeAnimationNode {
fn measure(
&self,
context: &mut dyn ModifierNodeContext,
measurable: &dyn Measurable,
constraints: Constraints,
) -> LayoutModifierMeasureResult {
let placeable = measurable.measure(constraints);
let target = Size::new(placeable.width(), placeable.height());
let shown = self.shown(target, context.density());
let (width, height) = constraints.constrain(shown.width, shown.height);
let x = self.direction.place_x(0.0, width, target.width);
LayoutModifierMeasureResult::new(Size::new(width, height), x, 0.0)
}
}