many_exprs.rsx 5.4 KB

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