dioxus/examples/read_size.rs

61 lines
1.5 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() {
dioxus_desktop::launch_cfg(
app,
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(cx: Scope) -> Element {
let div_element: &UseRef<Option<Rc<MountedData>>> = use_ref(cx, || None);
2023-03-26 12:23:32 +00:00
let dimentions = use_ref(cx, Rect::zero);
2023-03-24 16:32:42 +00:00
cx.render(rsx!(
div {
width: "50%",
height: "50%",
background_color: "red",
onmounted: move |cx| {
div_element.set(Some(cx.inner().clone()));
},
"This element is {dimentions.read():?}"
}
button {
onclick: move |_| {
to_owned![div_element, dimentions];
async move {
2023-03-26 12:23:32 +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 {
2023-07-26 17:18:39 +00:00
if let Ok(rect) = client_rect.await {
2023-03-24 16:32:42 +00:00
dimentions.set(rect);
}
}
}
},
"Read dimentions"
}
))
}