dioxus/examples/read_size.rs

53 lines
1.2 KiB
Rust
Raw Normal View History

2023-03-26 12:23:32 +00:00
#![allow(clippy::await_holding_refcell_ref)]
2023-03-24 16:32:42 +00:00
use std::rc::Rc;
use dioxus::{html::geometry::euclid::Rect, prelude::*};
fn main() {
LaunchBuilder::new(app).cfg(
2023-03-24 16:32:42 +00:00
dioxus_desktop::Config::default().with_custom_head(
r#"
<style type="text/css">
html, body {
height: 100%;
width: 100%;
margin: 0;
}
#main {
height: 100%;
width: 100%;
}
</style>
"#
.to_owned(),
),
);
}
fn app() -> Element {
let mut div_element = use_signal(|| None as Option<Rc<MountedData>>);
let mut dimensions = use_signal(Rect::zero);
2023-03-24 16:32:42 +00:00
let mut read_dims = move |_| async move {
2024-01-15 21:06:05 +00:00
let read = div_element.read();
let client_rect = read.as_ref().map(|el| el.get_client_rect());
if let Some(client_rect) = client_rect {
if let Ok(rect) = client_rect.await {
dimensions.set(rect);
}
}
};
2023-03-24 16:32:42 +00:00
2024-01-16 19:18:46 +00:00
rsx!(
2023-03-24 16:32:42 +00:00
div {
width: "50%",
height: "50%",
background_color: "red",
2024-01-15 21:06:05 +00:00
onmounted: move |cx| div_element.set(Some(cx.inner().clone())),
"This element is {dimensions():?}"
2023-03-24 16:32:42 +00:00
}
button { onclick: read_dims, "Read dimensions" }
2024-01-14 05:12:21 +00:00
)
2023-03-24 16:32:42 +00:00
}