Skip to main content

animato_dioxus/
platform.rs

1//! Platform detection and animation backend selection.
2
3/// Animation backend selected for the active Dioxus target.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum AnimationBackend {
6    /// Web target, driven by browser `requestAnimationFrame`.
7    WebRaf,
8    /// Desktop or mobile target, driven by a hosted clock.
9    NativeClock,
10    /// Terminal target, driven by polling intervals.
11    TerminalPoll,
12}
13
14/// Detects the current Dioxus rendering platform.
15#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
16pub struct PlatformAdapter;
17
18impl PlatformAdapter {
19    /// Detect platform at runtime from compile-time target features.
20    pub fn detect() -> AnimationBackend {
21        detect_backend()
22    }
23}
24
25#[cfg(target_arch = "wasm32")]
26fn detect_backend() -> AnimationBackend {
27    AnimationBackend::WebRaf
28}
29
30#[cfg(all(not(target_arch = "wasm32"), feature = "native"))]
31fn detect_backend() -> AnimationBackend {
32    AnimationBackend::NativeClock
33}
34
35#[cfg(all(not(target_arch = "wasm32"), not(feature = "native")))]
36fn detect_backend() -> AnimationBackend {
37    AnimationBackend::NativeClock
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn backend_detection_returns_supported_backend() {
46        let backend = PlatformAdapter::detect();
47        assert!(matches!(
48            backend,
49            AnimationBackend::WebRaf
50                | AnimationBackend::NativeClock
51                | AnimationBackend::TerminalPoll
52        ));
53    }
54}