read_size.rs 1.3 KB

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