1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use std::{rc::Rc, sync::atomic::AtomicUsize};

use crate::{style::StyleProp, update::ANIM_UPDATE_MESSAGES};

use super::{anim_val::AnimValue, AnimPropKind, AnimUpdateMsg};

static ANIM_ID_GEN: AtomicUsize = AtomicUsize::new(1);

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AnimId(usize);

impl AnimId {
    pub fn next() -> Self {
        AnimId(ANIM_ID_GEN.fetch_add(1, std::sync::atomic::Ordering::Relaxed))
    }

    pub fn from(id: usize) -> Self {
        AnimId(id)
    }

    pub(crate) fn update_prop(&self, kind: AnimPropKind, val: AnimValue) {
        ANIM_UPDATE_MESSAGES.with(|msgs| {
            let mut msgs = msgs.borrow_mut();
            msgs.push(AnimUpdateMsg::Prop {
                id: *self,
                kind,
                val,
            });
        });
    }

    pub(crate) fn update_style_prop<P: StyleProp>(&self, _prop: P, val: P::Type) {
        ANIM_UPDATE_MESSAGES.with(|msgs| {
            let mut msgs = msgs.borrow_mut();
            msgs.push(AnimUpdateMsg::Prop {
                id: *self,
                kind: AnimPropKind::Prop {
                    prop: P::prop_ref(),
                },
                val: AnimValue::Prop(Rc::new(val)),
            });
        });
    }
}