many_exprs.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.duration()
  97. .saturating_sub(
  98. t.started_at.map(|x| x.elapsed()).unwrap_or(Duration::ZERO),
  99. )
  100. .as_millis()
  101. });
  102. format!(
  103. "{:02}:{:02}:{:02}.{:01}",
  104. millis / 1000 / 3600 % 3600,
  105. millis / 1000 / 60 % 60,
  106. millis / 1000 % 60,
  107. millis / 100 % 10,
  108. )
  109. }
  110. }
  111. div {
  112. input {
  113. r#type: "number",
  114. min: 0,
  115. max: 99,
  116. value: format!("{:02}", timer.read().hours),
  117. oninput: move |e| {
  118. timer.write().hours = e.value().parse().unwrap_or(0);
  119. },
  120. }
  121. input {
  122. r#type: "number",
  123. min: 0,
  124. max: 59,
  125. value: format!("{:02}", timer.read().minutes),
  126. oninput: move |e| {
  127. timer.write().minutes = e.value().parse().unwrap_or(0);
  128. },
  129. }
  130. input {
  131. r#type: "number",
  132. min: 0,
  133. max: 59,
  134. value: format!("{:02}", timer.read().seconds),
  135. oninput: move |e| {
  136. timer.write().seconds = e.value().parse().unwrap_or(0);
  137. },
  138. }
  139. }
  140. button {
  141. id: "start_stop",
  142. onclick: move |_| {
  143. timer
  144. .with_mut(|t| {
  145. t.started_at = if t.started_at.is_none() {
  146. Some(Instant::now())
  147. } else {
  148. None
  149. }
  150. })
  151. },
  152. {timer.with(|t| if t.started_at.is_none() { "Start" } else { "Stop" })}
  153. }
  154. div { id: "app",
  155. button {
  156. onclick: move |_| {
  157. let decorations = window_preferences.read().with_decorations;
  158. use_window().set_decorations(!decorations);
  159. window_preferences.write().with_decorations = !decorations;
  160. },
  161. {
  162. format!(
  163. "with decorations{}",
  164. if window_preferences.read().with_decorations { " ✓" } else { "" },
  165. )
  166. }
  167. }
  168. button {
  169. onclick: move |_| {
  170. window_preferences
  171. .with_mut(|wp| {
  172. use_window().set_always_on_top(!wp.always_on_top);
  173. wp.always_on_top = !wp.always_on_top;
  174. })
  175. },
  176. width: 100,
  177. {
  178. format!(
  179. "always on top{}",
  180. if window_preferences.read().always_on_top { " ✓" } else { "" },
  181. )
  182. }
  183. }
  184. }
  185. {
  186. exit_button(
  187. Duration::from_secs(3),
  188. |trigger, delay| {
  189. rsx! {
  190. {
  191. format!(
  192. "{:0.1?}",
  193. trigger
  194. .read()
  195. .map(|inst| (delay.as_secs_f32() - inst.elapsed().as_secs_f32())),
  196. )
  197. }
  198. }
  199. },
  200. )
  201. }
  202. }
  203. }