rendering.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. use dioxus::prelude::*;
  2. use dioxus_core::Element;
  3. use dioxus_desktop::{window, 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(100));
  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(100)).await;
  29. let res = dioxus::eval(format!(
  30. r#"let element = document.getElementById('{}');
  31. return element.innerHTML"#,
  32. id
  33. ))
  34. .await;
  35. if let Ok(html) = res {
  36. // serde_json::Value::String(html)
  37. println!("html: {}", html);
  38. value.set(Some(html));
  39. }
  40. });
  41. });
  42. value.read().clone()
  43. }
  44. const EXPECTED_HTML: &str = r#"<div id="5" style="width: 100px; height: 100px; color: rgb(0, 0, 0);"><input type="checkbox"><h1>text</h1><div><p>hello world</p></div></div>"#;
  45. fn check_html_renders() -> Element {
  46. let inner_html = use_inner_html("main_div");
  47. let desktop_context: DesktopContext = consume_context();
  48. if let Some(raw_html) = inner_html {
  49. println!("{}", raw_html);
  50. let fragment = &raw_html;
  51. let expected = EXPECTED_HTML;
  52. // let fragment = scraper::Html::parse_fragment(&raw_html);
  53. // println!("fragment: {}", fragment.html());
  54. // let expected = scraper::Html::parse_fragment(EXPECTED_HTML);
  55. // println!("expected: {}", expected.html());
  56. assert_eq!(raw_html, EXPECTED_HTML);
  57. if fragment == expected {
  58. println!("html matches");
  59. desktop_context.close();
  60. }
  61. }
  62. let dyn_value = 0;
  63. let dyn_element = rsx! {
  64. div {
  65. dangerous_inner_html: "<p>hello world</p>",
  66. }
  67. };
  68. rsx! {
  69. div {
  70. id: "main_div",
  71. div {
  72. width: "100px",
  73. height: "100px",
  74. color: "rgb({dyn_value}, {dyn_value}, {dyn_value})",
  75. id: 5,
  76. input {
  77. "type": "checkbox",
  78. },
  79. h1 {
  80. "text"
  81. }
  82. {dyn_element}
  83. }
  84. }
  85. }
  86. }