1
0

multiexpr-many.rsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #![allow(dead_code, unused)]
  2. use dioxus::desktop::use_window;
  3. use dioxus::prelude::*;
  4. use std::{
  5. process::exit,
  6. time::{Duration, Instant},
  7. };
  8. use tokio::time::sleep;
  9. fn main() {
  10. dioxus::LaunchBuilder::desktop().launch(app);
  11. }
  12. struct WindowPreferences {
  13. always_on_top: bool,
  14. with_decorations: bool,
  15. exiting: Option<Instant>,
  16. }
  17. impl Default for WindowPreferences {
  18. fn default() -> Self {
  19. Self {
  20. with_decorations: true,
  21. always_on_top: false,
  22. exiting: None,
  23. }
  24. }
  25. }
  26. impl WindowPreferences {
  27. fn new() -> Self {
  28. Self::default()
  29. }
  30. }
  31. #[derive(Default)]
  32. struct Timer {
  33. hours: u8,
  34. minutes: u8,
  35. seconds: u8,
  36. started_at: Option<Instant>,
  37. }
  38. impl Timer {
  39. fn new() -> Self {
  40. Self::default()
  41. }
  42. fn duration(&self) -> Duration {
  43. Duration::from_secs(
  44. (self.hours as u64 * 60 + self.minutes as u64) * 60 + self.seconds as u64,
  45. )
  46. }
  47. }
  48. const UPD_FREQ: Duration = Duration::from_millis(100);
  49. fn exit_button(
  50. delay: Duration,
  51. label: fn(Signal<Option<Instant>>, Duration) -> Option<VNode>,
  52. ) -> Element {
  53. let mut trigger: Signal<Option<Instant>> = use_signal(|| None);
  54. use_future(move || async move {
  55. loop {
  56. sleep(UPD_FREQ).await;
  57. if let Some(true) = trigger.read().map(|e| e.elapsed() > delay) {
  58. exit(0);
  59. }
  60. }
  61. });
  62. let stuff: Option<VNode> = rsx! {
  63. button {
  64. onmouseup: move |_| {
  65. trigger.set(None);
  66. },
  67. onmousedown: move |_| {
  68. trigger.set(Some(Instant::now()));
  69. },
  70. width: 100,
  71. {label(trigger, delay)}
  72. }
  73. };
  74. stuff
  75. }
  76. fn app() -> Element {
  77. let mut timer = use_signal(Timer::new);
  78. let mut window_preferences = use_signal(WindowPreferences::new);
  79. use_future(move || async move {
  80. loop {
  81. sleep(UPD_FREQ).await;
  82. timer.with_mut(|t| {
  83. if let Some(started_at) = t.started_at {
  84. if t.duration().saturating_sub(started_at.elapsed()) == Duration::ZERO {
  85. t.started_at = None;
  86. }
  87. }
  88. });
  89. }
  90. });
  91. rsx! {
  92. div {
  93. {
  94. let millis = timer
  95. .with(|t| {
  96. t
  97. .duration()
  98. .saturating_sub(
  99. t.started_at.map(|x| x.elapsed()).unwrap_or(Duration::ZERO),
  100. )
  101. .as_millis()
  102. });
  103. format!(
  104. "{:02}:{:02}:{:02}.{:01}",
  105. millis / 1000 / 3600 % 3600,
  106. millis / 1000 / 60 % 60,
  107. millis / 1000 % 60,
  108. millis / 100 % 10,
  109. )
  110. }
  111. }
  112. div {
  113. input {
  114. r#type: "number",
  115. min: 0,
  116. max: 99,
  117. value: format!("{:02}", timer.read().hours),
  118. oninput: move |e| {
  119. timer.write().hours = e.value().parse().unwrap_or(0);
  120. },
  121. }
  122. input {
  123. r#type: "number",
  124. min: 0,
  125. max: 59,
  126. value: format!("{:02}", timer.read().minutes),
  127. oninput: move |e| {
  128. timer.write().minutes = e.value().parse().unwrap_or(0);
  129. },
  130. }
  131. input {
  132. r#type: "number",
  133. min: 0,
  134. max: 59,
  135. value: format!("{:02}", timer.read().seconds),
  136. oninput: move |e| {
  137. timer.write().seconds = e.value().parse().unwrap_or(0);
  138. },
  139. }
  140. }
  141. button {
  142. id: "start_stop",
  143. onclick: move |_| {
  144. timer
  145. .with_mut(|t| {
  146. t.started_at = if t.started_at.is_none() {
  147. Some(Instant::now())
  148. } else {
  149. None
  150. };
  151. })
  152. },
  153. {timer.with(|t| if t.started_at.is_none() { "Start" } else { "Stop" })}
  154. }
  155. div { id: "app",
  156. button {
  157. onclick: move |_| {
  158. let decorations = window_preferences.read().with_decorations;
  159. use_window().set_decorations(!decorations);
  160. window_preferences.write().with_decorations = !decorations;
  161. },
  162. {
  163. format!(
  164. "with decorations{}",
  165. if window_preferences.read().with_decorations { " ✓" } else { "" },
  166. )
  167. .to_string()
  168. }
  169. }
  170. button {
  171. onclick: move |_| {
  172. window_preferences
  173. .with_mut(|wp| {
  174. use_window().set_always_on_top(!wp.always_on_top);
  175. wp.always_on_top = !wp.always_on_top;
  176. })
  177. },
  178. width: 100,
  179. {
  180. format!(
  181. "always on top{}",
  182. if window_preferences.read().always_on_top { " ✓" } else { "" },
  183. )
  184. }
  185. }
  186. }
  187. {
  188. exit_button(
  189. Duration::from_secs(3),
  190. |trigger, delay| rsx! {
  191. {
  192. format!(
  193. "{:0.1?}",
  194. trigger
  195. .read()
  196. .map(|inst| (delay.as_secs_f32() - inst.elapsed().as_secs_f32())),
  197. )
  198. }
  199. },
  200. )
  201. }
  202. }
  203. }