use std::{
hash::{Hash, Hasher},
rc::Rc,
};
use cranpose_foundation::{
Constraints, DelegatableNode, InvalidationKind, LayoutModifierNode, Measurable, ModifierNode,
ModifierNodeContext, ModifierNodeElement, NodeCapabilities, NodeState,
};
use cranpose_ui_graphics::Size;
use cranpose_ui_layout::{
Alignment, HorizontalAlignment, LayoutDirection, LayoutModifierMeasureResult,
VerticalAlignment, bias_offset, round_to_px,
};
use crate::{
modifier::{Modifier, inspector_metadata},
modifier_nodes::impl_layout_modifier_node,
widgets::{
animated_content::EntryMotion,
animated_visibility::{EnterTransition, ExitTransition, TransitionEffects, enter, exit},
},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Axes {
Both,
Horizontal,
Vertical,
}
impl Axes {
fn collapse(self, full: Size) -> Size {
match self {
Self::Both => Size::ZERO,
Self::Horizontal => Size::new(0.0, full.height),
Self::Vertical => Size::new(full.width, 0.0),
}
}
}
#[derive(Clone)]
enum Away {
Collapsed(Axes),
Custom(Rc<dyn Fn(Size) -> Size>),
}
impl PartialEq for Away {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Collapsed(a), Self::Collapsed(b)) => a == b,
(Self::Custom(a), Self::Custom(b)) => Rc::ptr_eq(a, b),
_ => false,
}
}
}
#[derive(Clone)]
pub(crate) struct SizeChange {
alignment: Alignment,
pub(crate) clip: bool,
away: Away,
}
impl std::fmt::Debug for SizeChange {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SizeChange")
.field("alignment", &self.alignment)
.field("clip", &self.clip)
.finish_non_exhaustive()
}
}
impl PartialEq for SizeChange {
fn eq(&self, other: &Self) -> bool {
self.alignment == other.alignment && self.clip == other.clip && self.away == other.away
}
}
impl SizeChange {
fn new(alignment: Alignment, away: Away) -> Self {
Self {
alignment,
clip: true,
away,
}
}
fn custom(alignment: Alignment, away: impl Fn(Size) -> Size + 'static) -> Self {
Self::new(alignment, Away::Custom(Rc::new(away)))
}
pub(crate) fn size_at(&self, full: Size, progress: f32, density: f32) -> Size {
let away = match &self.away {
Away::Collapsed(axes) => axes.collapse(full),
Away::Custom(size) => size(full),
};
let between = |away: f32, full: f32| {
(full - round_to_px((full - away) * (1.0 - progress), density)).max(0.0)
};
Size::new(
between(away.width, full.width),
between(away.height, full.height),
)
}
pub(crate) fn offset(
&self,
full: Size,
size: Size,
direction: LayoutDirection,
density: f32,
) -> (f32, f32) {
let horizontal = self.alignment.horizontal.bias();
let horizontal = if direction.is_rtl() {
-horizontal
} else {
horizontal
};
(
bias_offset(horizontal, size.width, full.width, density),
bias_offset(
self.alignment.vertical.bias(),
size.height,
full.height,
density,
),
)
}
}
fn horizontal(alignment: HorizontalAlignment) -> Alignment {
Alignment::new(alignment, VerticalAlignment::CenterVertically)
}
fn vertical(alignment: VerticalAlignment) -> Alignment {
Alignment::new(HorizontalAlignment::CenterHorizontally, alignment)
}
fn expand(change: SizeChange) -> EnterTransition {
enter(TransitionEffects::sized(change))
}
fn shrink(change: SizeChange) -> ExitTransition {
exit(TransitionEffects::sized(change))
}
pub fn expand_in() -> EnterTransition {
expand(SizeChange::new(
Alignment::BOTTOM_END,
Away::Collapsed(Axes::Both),
))
}
pub fn expand_in_with(
expand_from: Alignment,
initial_size: impl Fn(Size) -> Size + 'static,
) -> EnterTransition {
expand(SizeChange::custom(expand_from, initial_size))
}
pub fn shrink_out() -> ExitTransition {
shrink(SizeChange::new(
Alignment::BOTTOM_END,
Away::Collapsed(Axes::Both),
))
}
pub fn shrink_out_with(
shrink_towards: Alignment,
target_size: impl Fn(Size) -> Size + 'static,
) -> ExitTransition {
shrink(SizeChange::custom(shrink_towards, target_size))
}
pub fn expand_horizontally() -> EnterTransition {
expand(SizeChange::new(
horizontal(HorizontalAlignment::End),
Away::Collapsed(Axes::Horizontal),
))
}
pub fn expand_horizontally_with(
expand_from: HorizontalAlignment,
initial_width: impl Fn(f32) -> f32 + 'static,
) -> EnterTransition {
expand(SizeChange::custom(horizontal(expand_from), move |full| {
Size::new(initial_width(full.width), full.height)
}))
}
pub fn expand_vertically() -> EnterTransition {
expand(SizeChange::new(
vertical(VerticalAlignment::Bottom),
Away::Collapsed(Axes::Vertical),
))
}
pub fn expand_vertically_with(
expand_from: VerticalAlignment,
initial_height: impl Fn(f32) -> f32 + 'static,
) -> EnterTransition {
expand(SizeChange::custom(vertical(expand_from), move |full| {
Size::new(full.width, initial_height(full.height))
}))
}
pub fn shrink_horizontally() -> ExitTransition {
shrink(SizeChange::new(
horizontal(HorizontalAlignment::End),
Away::Collapsed(Axes::Horizontal),
))
}
pub fn shrink_horizontally_with(
shrink_towards: HorizontalAlignment,
target_width: impl Fn(f32) -> f32 + 'static,
) -> ExitTransition {
shrink(SizeChange::custom(
horizontal(shrink_towards),
move |full| Size::new(target_width(full.width), full.height),
))
}
pub fn shrink_vertically() -> ExitTransition {
shrink(SizeChange::new(
vertical(VerticalAlignment::Bottom),
Away::Collapsed(Axes::Vertical),
))
}
pub fn shrink_vertically_with(
shrink_towards: VerticalAlignment,
target_height: impl Fn(f32) -> f32 + 'static,
) -> ExitTransition {
shrink(SizeChange::custom(vertical(shrink_towards), move |full| {
Size::new(full.width, target_height(full.height))
}))
}
impl Modifier {
pub(crate) fn size_change(self, motion: Rc<EntryMotion>, direction: LayoutDirection) -> Self {
let modifier = Self::with_element(SizeChangeElement { motion, direction })
.with_inspector_metadata(inspector_metadata("sizeChange", |_| {}));
self.then(modifier)
}
}
#[derive(Clone)]
struct SizeChangeElement {
motion: Rc<EntryMotion>,
direction: LayoutDirection,
}
impl std::fmt::Debug for SizeChangeElement {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SizeChangeElement")
.field("direction", &self.direction)
.finish_non_exhaustive()
}
}
impl PartialEq for SizeChangeElement {
fn eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.motion, &other.motion) && self.direction == other.direction
}
}
impl Eq for SizeChangeElement {}
impl Hash for SizeChangeElement {
fn hash<H: Hasher>(&self, state: &mut H) {
Rc::as_ptr(&self.motion).hash(state);
self.direction.hash(state);
}
}
impl ModifierNodeElement for SizeChangeElement {
type Node = SizeChangeNode;
fn create(&self) -> Self::Node {
SizeChangeNode {
motion: Rc::clone(&self.motion),
direction: self.direction,
state: NodeState::new(),
}
}
fn update(&self, node: &mut Self::Node) {
node.motion = Rc::clone(&self.motion);
node.direction = self.direction;
}
fn capabilities(&self) -> NodeCapabilities {
NodeCapabilities::LAYOUT
}
}
struct SizeChangeNode {
motion: Rc<EntryMotion>,
direction: LayoutDirection,
state: NodeState,
}
impl DelegatableNode for SizeChangeNode {
fn node_state(&self) -> &NodeState {
&self.state
}
}
impl_layout_modifier_node!(SizeChangeNode, invalidate = InvalidationKind::Layout);
impl LayoutModifierNode for SizeChangeNode {
fn measure(
&self,
context: &mut dyn ModifierNodeContext,
measurable: &dyn Measurable,
constraints: Constraints,
) -> LayoutModifierMeasureResult {
let placeable = measurable.measure(constraints);
let full = Size::new(placeable.width(), placeable.height());
self.motion
.fit(full, constraints, context.density(), self.direction)
}
}