control_focus.rs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. use std::rc::Rc;
  2. use dioxus::prelude::*;
  3. fn main() {
  4. launch_desktop(app);
  5. }
  6. fn app() -> Element {
  7. let mut elements = use_signal(Vec::<Rc<MountedData>>::new);
  8. let mut running = use_signal(|| true);
  9. use_future(move || async move {
  10. let mut focused = 0;
  11. loop {
  12. tokio::time::sleep(std::time::Duration::from_millis(10)).await;
  13. if !running() {
  14. continue;
  15. }
  16. if let Some(element) = elements.with(|f| f.get(focused).cloned()) {
  17. _ = element.set_focus(true).await;
  18. } else {
  19. focused = 0;
  20. }
  21. focused += 1;
  22. }
  23. });
  24. rsx! {
  25. div {
  26. h1 { "Input Roulette" }
  27. for i in 0..100 {
  28. input {
  29. value: "{i}",
  30. onmounted: move |cx| {
  31. elements.write().push(cx.data());
  32. },
  33. oninput: move |_| {
  34. running.set(false);
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }