multiexpr-many.wrong.rsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. let millis = timer.with(|t| t.duration().saturating_sub(t.started_at.map(|x| x.elapsed()).unwrap_or(Duration::ZERO)).as_millis());
  94. format!("{:02}:{:02}:{:02}.{:01}",
  95. millis / 1000 / 3600 % 3600,
  96. millis / 1000 / 60 % 60,
  97. millis / 1000 % 60,
  98. millis / 100 % 10)
  99. }}
  100. div {
  101. input { r#type: "number", min: 0, max: 99, value: format!("{:02}",timer.read().hours), oninput: move |e| {
  102. timer.write().hours = e.value().parse().unwrap_or(0);
  103. }
  104. }
  105. input { r#type: "number", min: 0, max: 59, value: format!("{:02}",timer.read().minutes), oninput: move |e| {
  106. timer.write().minutes = e.value().parse().unwrap_or(0);
  107. }
  108. }
  109. input { r#type: "number", min: 0, max: 59, value: format!("{:02}",timer.read().seconds), oninput: move |e| {
  110. timer.write().seconds = e.value().parse().unwrap_or(0);
  111. }
  112. }
  113. }
  114. button {
  115. id: "start_stop",
  116. onclick: move |_| timer.with_mut(|t| t.started_at = if t.started_at.is_none() { Some(Instant::now()) } else { None } ),
  117. { timer.with(|t| if t.started_at.is_none() { "Start" } else { "Stop" }) },
  118. }
  119. div { id: "app",
  120. button { onclick: move |_| {
  121. let decorations = window_preferences.read().with_decorations;
  122. use_window().set_decorations(!decorations);
  123. window_preferences.write().with_decorations = !decorations;
  124. }, {
  125. format!("with decorations{}", if window_preferences.read().with_decorations { " ✓" } else { "" }).to_string()
  126. }
  127. }
  128. button {
  129. onclick: move |_| {
  130. window_preferences.with_mut(|wp| {
  131. use_window().set_always_on_top(!wp.always_on_top);
  132. wp.always_on_top = !wp.always_on_top;
  133. })},
  134. width: 100,
  135. {
  136. format!("always on top{}", if window_preferences.read().always_on_top { " ✓" } else { "" })
  137. }
  138. }
  139. }
  140. {
  141. exit_button(
  142. Duration::from_secs(3),
  143. |trigger, delay| rsx! {
  144. {format!("{:0.1?}", trigger.read().map(|inst| (delay.as_secs_f32() - inst.elapsed().as_secs_f32()))) }
  145. }
  146. )
  147. }
  148. }
  149. }