rendering.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. .unwrap()
  35. .await
  36. .unwrap();
  37. if let Some(html) = res.as_str() {
  38. // serde_json::Value::String(html)
  39. println!("html: {}", html);
  40. value.set(Some(html.to_string()));
  41. }
  42. });
  43. });
  44. value.read().clone()
  45. }
  46. 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>"#;
  47. fn check_html_renders() -> Element {
  48. let inner_html = use_inner_html("main_div");
  49. let desktop_context: DesktopContext = consume_context();
  50. if let Some(raw_html) = inner_html {
  51. println!("{}", raw_html);
  52. let fragment = &raw_html;
  53. let expected = EXPECTED_HTML;
  54. assert_eq!(raw_html, EXPECTED_HTML);
  55. if fragment == expected {
  56. println!("html matches");
  57. desktop_context.close();
  58. }
  59. }
  60. let dyn_value = 0;
  61. let dyn_element = rsx! {
  62. div {
  63. dangerous_inner_html: "<p>hello world</p>",
  64. }
  65. };
  66. rsx! {
  67. div {
  68. id: "main_div",
  69. div {
  70. width: "100px",
  71. height: "100px",
  72. color: "rgb({dyn_value}, {dyn_value}, {dyn_value})",
  73. id: 5,
  74. input {
  75. "type": "checkbox",
  76. },
  77. h1 {
  78. "text"
  79. }
  80. {dyn_element}
  81. }
  82. }
  83. }
  84. }