1
0

read_size.rs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #![allow(clippy::await_holding_refcell_ref)]
  2. use std::rc::Rc;
  3. use dioxus::{html::geometry::euclid::Rect, prelude::*};
  4. fn main() {
  5. LaunchBuilder::desktop().with_cfg(
  6. dioxus::desktop::Config::default().with_custom_head(
  7. r#"
  8. <style type="text/css">
  9. html, body {
  10. height: 100%;
  11. width: 100%;
  12. margin: 0;
  13. }
  14. #main {
  15. height: 100%;
  16. width: 100%;
  17. }
  18. </style>
  19. "#
  20. .to_owned(),
  21. ),
  22. );
  23. }
  24. fn app() -> Element {
  25. let mut div_element = use_signal(|| None as Option<Rc<MountedData>>);
  26. let mut dimensions = use_signal(Rect::zero);
  27. let mut read_dims = move |_| async move {
  28. let read = div_element.read();
  29. let client_rect = read.as_ref().map(|el| el.get_client_rect());
  30. if let Some(client_rect) = client_rect {
  31. if let Ok(rect) = client_rect.await {
  32. dimensions.set(rect);
  33. }
  34. }
  35. };
  36. rsx!(
  37. div {
  38. width: "50%",
  39. height: "50%",
  40. background_color: "red",
  41. onmounted: move |cx| div_element.set(Some(cx.inner().clone())),
  42. "This element is {dimensions():?}"
  43. }
  44. button { onclick: read_dims, "Read dimensions" }
  45. )
  46. }