Skip to main content

pax_std/drawing/pax_logo/
animated_pax_logo.rs

1#![allow(unused_imports)]
2
3use crate::*;
4use pax_engine::api::cursor::CursorStyle;
5use pax_engine::api::*;
6use pax_engine::*;
7
8/// The surface behind the logo, not the signboard's own color.
9#[derive(PartialEq, Eq)]
10#[pax]
11#[engine_import_path("pax_engine")]
12pub enum LogoBackgroundMode {
13    /// Dark signboard with light letters, for a light surrounding surface.
14    #[default]
15    Light,
16    /// Light signboard with dark letters, for a dark surrounding surface.
17    Dark,
18}
19
20/// The animated Pax logo, with a normalized playhead and optional click replay.
21///
22/// Bind `progress` to coordinate a slider or a parent-owned timeline. The
23/// component does not start playback on mount or paint its surrounding surface.
24/// Scale an enclosing Group from the original 920.72 × 463.95 coordinate system.
25#[pax]
26#[engine_import_path("pax_engine")]
27#[custom(Default)]
28#[file("drawing/pax_logo/animated_pax_logo.pax")]
29pub struct AnimatedPaxLogo {
30    /// Selects the contrasting signboard palette. Defaults to Light to preserve
31    /// existing consumers; Dark exchanges `fill` and `letter_fill` throughout.
32    pub background_mode: Property<LogoBackgroundMode>,
33    /// Signboard/post fill in Light mode, lettering/counters in Dark mode.
34    pub fill: Property<Paint>,
35    /// Lettering/backing fill in Light mode, signboard/post in Dark mode.
36    pub letter_fill: Property<Paint>,
37    /// Normalized animation position from the initial pose (0.0) to the
38    /// finished logo (1.0). Bind this property for coordinated playback. Clicking
39    /// restarts it unless `click_to_replay` is false.
40    pub progress: Property<f64>,
41    /// Restart the 1440ms animation when clicked or tapped. Defaults to true.
42    /// Disable for externally driven playback or a noninteractive still logo.
43    pub click_to_replay: Property<bool>,
44}
45
46impl Default for AnimatedPaxLogo {
47    fn default() -> Self {
48        Self {
49            background_mode: Property::new(LogoBackgroundMode::Light),
50            fill: Property::new(Paint::Solid(Color::BLACK)),
51            letter_fill: Property::new(Paint::Solid(Color::WHITE)),
52            progress: Property::new(0.0),
53            click_to_replay: Property::new(true),
54        }
55    }
56}
57
58impl AnimatedPaxLogo {
59    fn replay_if_enabled(&self) {
60        if self.click_to_replay.get() {
61            // Reset the same owner-thread view that starts the transition. A
62            // shared publication is not imported until the next UI settlement.
63            let progress = self.progress.local();
64            progress.cancel_transitions();
65            progress.set(0.0);
66            progress.ease_to(
67                1.0,
68                Duration::Milliseconds(1440.into()),
69                EasingCurve::Linear,
70            );
71        }
72    }
73
74    pub fn replay(&mut self, _: &NodeContext, _: Event<Click>) {
75        self.replay_if_enabled();
76    }
77
78    pub fn hover(&mut self, ctx: &NodeContext, _: Event<MouseOver>) {
79        if self.click_to_replay.get() {
80            ctx.set_cursor(CursorStyle::Pointer);
81        }
82    }
83
84    pub fn leave(&mut self, ctx: &NodeContext, _: Event<MouseOut>) {
85        if self.click_to_replay.get() {
86            ctx.set_cursor(CursorStyle::Auto);
87        }
88    }
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94    use pax_engine::api::pax_value::ToFromPaxAny;
95
96    #[test]
97    fn light_mode_preserves_the_original_palette() {
98        let logo = AnimatedPaxLogo::default();
99        assert_eq!(logo.background_mode.get(), LogoBackgroundMode::Light);
100        assert_eq!(logo.fill.get(), Paint::Solid(Color::BLACK));
101        assert_eq!(logo.letter_fill.get(), Paint::Solid(Color::WHITE));
102    }
103
104    #[test]
105    fn background_modes_roundtrip_as_pax_property_values() {
106        for mode in [LogoBackgroundMode::Light, LogoBackgroundMode::Dark] {
107            let restored = LogoBackgroundMode::from_pax_any(mode.clone().to_pax_any()).unwrap();
108            assert_eq!(restored, mode);
109        }
110    }
111
112    #[test]
113    fn replay_restarts_the_shared_progress_even_mid_animation() {
114        use pax_engine::api::properties::{drain_effects, register_millis, PropertyGraph};
115        let graph = PropertyGraph::new(|| {});
116        let _entered = graph.enter();
117        let millis = LocalProperty::new(0_u64);
118        register_millis(&millis);
119        let logo = AnimatedPaxLogo::default();
120        let bound_progress = logo.progress.local();
121        bound_progress.set(1.0);
122        assert!(logo.click_to_replay.get());
123        logo.replay_if_enabled();
124        assert_eq!(bound_progress.get(), 0.0);
125        millis.set(720);
126        drain_effects(64);
127        assert!((bound_progress.get() - 0.5).abs() < 1e-9);
128        logo.replay_if_enabled();
129        assert_eq!(bound_progress.get(), 0.0);
130        millis.set(2160);
131        drain_effects(64);
132        assert_eq!(bound_progress.get(), 1.0);
133    }
134
135    #[test]
136    fn disabled_replay_preserves_external_playback() {
137        use pax_engine::api::properties::{drain_effects, register_millis, PropertyGraph};
138        let graph = PropertyGraph::new(|| {});
139        let _entered = graph.enter();
140        let millis = LocalProperty::new(0_u64);
141        register_millis(&millis);
142        let logo = AnimatedPaxLogo::default();
143        logo.click_to_replay.set(false);
144        logo.progress.local().ease_to(
145            1.0,
146            Duration::Milliseconds(1000.into()),
147            EasingCurve::Linear,
148        );
149        millis.set(250);
150        drain_effects(64);
151        logo.replay_if_enabled();
152        assert!((logo.progress.local().get() - 0.25).abs() < 1e-9);
153        millis.set(750);
154        drain_effects(64);
155        assert!((logo.progress.local().get() - 0.75).abs() < 1e-9);
156    }
157}