rendering.rs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. use dioxus::prelude::*;
  2. use dioxus_core::Element;
  3. use dioxus_desktop::DesktopContext;
  4. fn main() {
  5. check_app_exits(check_html_renders);
  6. }
  7. pub(crate) fn check_app_exits(app: fn() -> Element) {
  8. use dioxus_desktop::Config;
  9. use tao::window::WindowBuilder;
  10. // This is a deadman's switch to ensure that the app exits
  11. let should_panic = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true));
  12. let should_panic_clone = should_panic.clone();
  13. std::thread::spawn(move || {
  14. std::thread::sleep(std::time::Duration::from_secs(5));
  15. if should_panic_clone.load(std::sync::atomic::Ordering::SeqCst) {
  16. std::process::exit(exitcode::SOFTWARE);
  17. }
  18. });
  19. LaunchBuilder::desktop()
  20. .with_cfg(Config::new().with_window(WindowBuilder::new().with_visible(true)))
  21. .launch(app);
  22. should_panic.store(false, std::sync::atomic::Ordering::SeqCst);
  23. }
  24. fn use_inner_html(id: &'static str) -> Option<String> {
  25. let mut value = use_signal(|| None as Option<String>);
  26. use_effect(move || {
  27. spawn(async move {
  28. tokio::time::sleep(std::time::Duration::from_millis(2000)).await;
  29. let res = eval(&format!(
  30. r#"let element = document.getElementById('{}');
  31. return element.innerHTML"#,
  32. id
  33. ))
  34. .await
  35. .unwrap();
  36. if let Some(html) = res.as_str() {
  37. // serde_json::Value::String(html)
  38. println!("html: {}", html);
  39. value.set(Some(html.to_string()));
  40. }
  41. });
  42. });
  43. value.read().clone()
  44. }
  45. const EXPECTED_HTML: &str = r#"<div style="width: 100px; height: 100px; color: rgb(0, 0, 0);" id="5"><input type="checkbox"><h1>text</h1><div><p>hello world</p></div></div>"#;
  46. fn check_html_renders() -> Element {
  47. let inner_html = use_inner_html("main_div");
  48. let desktop_context: DesktopContext = consume_context();
  49. if let Some(raw_html) = inner_html {
  50. println!("{}", raw_html);
  51. let fragment = &raw_html;
  52. let expected = EXPECTED_HTML;
  53. assert_eq!(raw_html, EXPECTED_HTML);
  54. if fragment == expected {
  55. println!("html matches");
  56. desktop_context.close();
  57. }
  58. }
  59. let dyn_value = 0;
  60. let dyn_element = rsx! {
  61. div {
  62. dangerous_inner_html: "<p>hello world</p>",
  63. }
  64. };
  65. rsx! {
  66. div {
  67. id: "main_div",
  68. div {
  69. width: "100px",
  70. height: "100px",
  71. color: "rgb({dyn_value}, {dyn_value}, {dyn_value})",
  72. id: 5,
  73. input {
  74. "type": "checkbox",
  75. },
  76. h1 {
  77. "text"
  78. }
  79. {dyn_element}
  80. }
  81. }
  82. }
  83. }