dioxus_use_window/hooks/use_height/mod.rs
1mod display;
2use super::*;
3
4/// Window height effect handler
5#[derive(Debug)]
6pub struct UseWindowHeight {
7 inner: UseWindow,
8}
9
10impl UseWindowBuilder {
11 /// hooks for window's height with config
12 ///
13 /// # Arguments
14 ///
15 /// returns: [`UseWindowHeight`]
16 ///
17 /// # Examples
18 ///
19 /// ```
20 /// use dioxus::prelude::*;
21 /// use dioxus_use_window::{UseWindowBuilder};
22 ///
23 /// fn App(cx: Scope) -> Element {
24 /// let hook = UseWindowBuilder::default().use_height(&cx);
25 ///
26 /// cx.render(rsx!(
27 /// h1 { "Window height: {hook}" }
28 /// ))
29 /// }
30 /// ```
31 #[inline]
32 pub fn use_height<'a>(&self, cx: &'a ScopeState) -> &'a mut UseWindowHeight {
33 let hook = UseWindowHeight::new(self.use_window_hook(cx));
34 cx.use_hook(|| hook)
35 }
36}
37
38impl UseWindowHeight {
39 #[inline]
40 pub(crate) fn new(size: UseWindow) -> Self {
41 Self { inner: size }
42 }
43}
44
45impl UseWindowHeight {
46 /// get height of current window
47 #[inline]
48 pub fn get(&self) -> usize {
49 self.inner.data_ref().inner_height() as _
50 }
51 /// set height of current window, return `false` if failed to run
52 #[inline]
53 pub fn set(&self, height: usize) -> bool {
54 self.inner.data_ref().set_inner_height(height).is_some()
55 }
56}