-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathrestyle.rs
More file actions
85 lines (74 loc) · 2.2 KB
/
restyle.rs
File metadata and controls
85 lines (74 loc) · 2.2 KB
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
76
77
78
79
80
81
82
83
84
85
use dioxus::prelude::*;
use tokio::time::{Duration, sleep};
fn main() {
// Turn on the runtime and enter it
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap();
let _guard = rt.enter();
dioxus_native::launch(app);
}
#[derive(Copy, Clone)]
enum AnimationState {
Increasing,
Decreasing,
}
impl std::ops::Not for AnimationState {
type Output = Self;
fn not(self) -> Self::Output {
match self {
AnimationState::Increasing => AnimationState::Decreasing,
AnimationState::Decreasing => AnimationState::Increasing,
}
}
}
const MIN_SIZE: i32 = 12;
const MAX_SIZE: i32 = 120;
fn app() -> Element {
let mut size = use_signal(|| 12);
let mut direction = use_signal(|| AnimationState::Increasing);
let mut running = use_signal(|| true);
// `use_future` will spawn an infinitely direction future that can be started and stopped
use_future(move || async move {
loop {
if running() {
match direction() {
AnimationState::Increasing => size += 1,
AnimationState::Decreasing => size -= 1,
}
let size = *size.read();
if size <= MIN_SIZE {
*direction.write() = AnimationState::Increasing;
}
if size >= MAX_SIZE {
*direction.write() = AnimationState::Decreasing;
}
}
sleep(Duration::from_millis(16)).await;
}
});
rsx! {
div {
style { {STYLES} }
h1 { "Current size: {size}" }
div { style: "display: flex",
div { class: "button", onclick: move |_| running.toggle(), "Start/Stop" }
div { class: "button", onclick: move |_| size.set(12), "Reset the size" }
}
p { style: "font-size: {size}px", "Animate Font Size" }
}
}
}
static STYLES: &str = r#"
.button {
padding: 6px;
border: 1px solid #999;
margin-left: 12px;
cursor: pointer;
}
.button:hover {
background: #999;
color: white;
}
"#;