freya_devtools_app/tabs/
misc.rs

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use dioxus_radio::prelude::use_radio;
use freya::prelude::*;
use freya_core::animation_clock::AnimationClock;
use freya_devtools::{
    IncomingMessage,
    IncomingMessageAction,
};
use futures_util::SinkExt;
use tokio_tungstenite::tungstenite::Message;

use crate::state::DevtoolsChannel;

#[component]
pub fn Misc() -> Element {
    rsx!(
        rect {
            height: "fill",
            width: "fill",
            padding: "8",
            Animation {}
        }
    )
}

#[component]
pub fn Animation() -> Element {
    let mut radio = use_radio(DevtoolsChannel::Misc);

    use_effect(move || {
        let radio = radio.read();

        let client = radio.client.clone();
        let animation_speed = AnimationClock::MAX_SPEED / 100. * radio.animation_speed;

        let message = Message::Text(
            serde_json::to_string(&IncomingMessage {
                action: IncomingMessageAction::SetSpeedTo {
                    speed: animation_speed,
                },
            })
            .unwrap()
            .into(),
        );
        spawn(async move {
            if let Some(client) = client.lock().await.as_mut() {
                client.send(message).await.ok();
            }
        });
    });

    let speed = radio.read().animation_speed;
    let normalized_speed = AnimationClock::MAX_SPEED / 100. * speed;

    rsx!(
        rect {
            label {
                "Animation Speed"
            }
            rect {
                direction: "horizontal",
                spacing: "4",
                Slider {
                    size: "150",
                    value: speed as f64,
                    onmoved: move |p| {
                        radio.write().animation_speed = p as f32;
                    }
                }
                label {
                    "{normalized_speed:.2}x"
                }
            }
        }
    )
}