control_focus.rs 1.2 KB

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