read_size.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #![allow(clippy::await_holding_refcell_ref)]
  2. use std::rc::Rc;
  3. use dioxus::{html::geometry::euclid::Rect, prelude::*};
  4. fn main() {
  5. dioxus_desktop::launch_cfg(
  6. app,
  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. }
  25. fn app(cx: Scope) -> Element {
  26. let div_element: &UseRef<Option<Rc<MountedData>>> = use_ref(cx, || None);
  27. let dimentions = use_ref(cx, Rect::zero);
  28. cx.render(rsx!(
  29. div {
  30. width: "50%",
  31. height: "50%",
  32. background_color: "red",
  33. onmounted: move |cx| {
  34. div_element.set(Some(cx.inner().clone()));
  35. },
  36. "This element is {dimentions.read():?}"
  37. }
  38. button {
  39. onclick: move |_| {
  40. to_owned![div_element, dimentions];
  41. async move {
  42. let read = div_element.read();
  43. let client_rect = read.as_ref().map(|el| el.get_client_rect());
  44. if let Some(client_rect) = client_rect {
  45. if let Ok(rect) = client_rect.await {
  46. dimentions.set(rect);
  47. }
  48. }
  49. }
  50. },
  51. "Read dimentions"
  52. }
  53. ))
  54. }