read_size.rs 1.3 KB

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