read_size.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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() -> Element {
  26. let div_element = use_signal(|| None as Option<Rc<MountedData>>);
  27. let dimensions = use_signal(Rect::zero);
  28. let read_dims = move |_| async move {
  29. let read = div_element.read();
  30. let client_rect = read.as_ref().map(|el| el.get_client_rect());
  31. if let Some(client_rect) = client_rect {
  32. if let Ok(rect) = client_rect.await {
  33. dimensions.set(rect);
  34. }
  35. }
  36. };
  37. rsx!(
  38. div {
  39. width: "50%",
  40. height: "50%",
  41. background_color: "red",
  42. onmounted: move |cx| div_element.set(Some(cx.inner().clone())),
  43. "This element is {dimensions():?}"
  44. }
  45. button {
  46. onclick: read_dims,
  47. "Read dimensions"
  48. }
  49. )
  50. }