Skip to main content

dioxus_type_animation/
sequence.rs

1use std::rc::Rc;
2
3/// Function used to split a string into editable units.
4///
5/// The default splitter is Unicode scalar-value based (`text.chars()`), which is
6/// the closest Rust equivalent to JavaScript's `[...text]`. For grapheme-aware
7/// animation, pass a splitter backed by a crate such as `unicode-segmentation`.
8pub type StringSplitter = Rc<dyn Fn(&str) -> Vec<String>>;
9
10/// Callback entry in an animation sequence.
11pub type SequenceCallback = Rc<dyn Fn()>;
12
13/// A single animation sequence item.
14#[derive(Clone)]
15pub enum SequenceElement {
16    /// Transition from the current text to this text.
17    Text(String),
18    /// Wait for the given number of milliseconds.
19    Delay(u64),
20    /// Invoke a callback.
21    Callback(SequenceCallback),
22}
23
24impl From<&str> for SequenceElement {
25    fn from(value: &str) -> Self {
26        Self::Text(value.to_string())
27    }
28}
29
30impl From<String> for SequenceElement {
31    fn from(value: String) -> Self {
32        Self::Text(value)
33    }
34}
35
36impl From<u64> for SequenceElement {
37    fn from(value: u64) -> Self {
38        Self::Delay(value)
39    }
40}
41
42impl From<usize> for SequenceElement {
43    fn from(value: usize) -> Self {
44        Self::Delay(value as u64)
45    }
46}
47
48impl<F> From<F> for SequenceElement
49where
50    F: Fn() + 'static,
51{
52    fn from(value: F) -> Self {
53        Self::Callback(Rc::new(value))
54    }
55}