utils.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. use dioxus::prelude::*;
  2. use dioxus_core::Element;
  3. pub fn check_app_exits(app: fn() -> Element) {
  4. use dioxus_desktop::tao::window::WindowBuilder;
  5. use dioxus_desktop::Config;
  6. // This is a deadman's switch to ensure that the app exits
  7. let should_panic = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true));
  8. let should_panic_clone = should_panic.clone();
  9. std::thread::spawn(move || {
  10. std::thread::sleep(std::time::Duration::from_secs(60));
  11. if should_panic_clone.load(std::sync::atomic::Ordering::SeqCst) {
  12. eprintln!("App did not exit in time");
  13. std::process::exit(exitcode::SOFTWARE);
  14. }
  15. });
  16. LaunchBuilder::desktop()
  17. .with_cfg(Config::new().with_window(WindowBuilder::new().with_visible(true)))
  18. .launch(app);
  19. // Stop deadman's switch
  20. should_panic.store(false, std::sync::atomic::Ordering::SeqCst);
  21. }
  22. pub static EXPECTED_EVENTS: GlobalSignal<usize> = Signal::global(|| 0);
  23. pub fn mock_event(id: &'static str, value: &'static str) {
  24. mock_event_with_extra(id, value, "");
  25. }
  26. pub fn mock_event_with_extra(id: &'static str, value: &'static str, extra: &'static str) {
  27. EXPECTED_EVENTS.with_mut(|x| *x += 1);
  28. use_hook(move || {
  29. spawn(async move {
  30. tokio::time::sleep(std::time::Duration::from_millis(5000)).await;
  31. let js = format!(
  32. r#"
  33. //console.log("ran");
  34. // Dispatch a synthetic event
  35. let event = {};
  36. let element = document.getElementById('{}');
  37. console.log(element, event);
  38. {}
  39. element.dispatchEvent(event);
  40. "#,
  41. value, id, extra
  42. );
  43. eval(&js).await.unwrap();
  44. });
  45. })
  46. }