dioxus_use_window/hooks/builder.rs
1use super::*;
2use crate::ResponsiveLayout;
3
4/// Build window size hook with config
5#[derive(Debug, Copy, Clone)]
6pub struct UseWindowBuilder {
7 ///
8 pub missing_x: usize,
9 ///
10 pub missing_y: usize,
11}
12
13impl Default for UseWindowBuilder {
14 fn default() -> Self {
15 Self { missing_x: 375, missing_y: 812 }
16 }
17}
18
19impl UseWindowBuilder {
20 /// hooks for window's width with config
21 ///
22 /// # Arguments
23 ///
24 /// returns: [`UseWindowWidth`]
25 ///
26 /// # Examples
27 ///
28 /// ```
29 /// use dioxus::prelude::*;
30 /// use dioxus_use_window::{UseWindowBuilder};
31 ///
32 /// fn App(cx: Scope) -> Element {
33 /// let hook = UseWindowBuilder::default().use_width(&cx);
34 ///
35 /// cx.render(rsx!(
36 /// h1 { "Window width: {hook}" }
37 /// ))
38 /// }
39 /// ```
40 pub fn use_width<'a>(&self, cx: &'a ScopeState) -> &'a mut UseWindowWidth {
41 let hook = UseWindowWidth::new(self.use_window_hook(cx));
42 cx.use_hook(|| hook)
43 }
44
45 /// hooks for window's layout with config
46 ///
47 /// # Arguments
48 ///
49 /// returns: [`UseWindowLayout`]
50 ///
51 /// # Examples
52 ///
53 /// ```
54 /// use dioxus::prelude::*;
55 /// use dioxus_use_window::{ResponsiveLayout, UseWindowBuilder};
56 ///
57 /// fn App(cx: Scope) -> Element {
58 /// let hook = UseWindowBuilder::default().use_layout::<ResponsiveLayout>(&cx);
59 ///
60 /// cx.render(rsx!(
61 /// h1 { "Window layout: {hook}" }
62 /// ))
63 /// }
64 /// ```
65 pub fn use_layout<'a, T>(&self, cx: &'a ScopeState) -> &'a mut UseWindowLayout<T>
66 where
67 T: 'static,
68 {
69 let hook = UseWindowLayout::new(self.use_window_hook(cx));
70 cx.use_hook(|| hook)
71 }
72 /// hooks for window's responsive layout with config
73 ///
74 /// # Arguments
75 ///
76 /// returns: [`UseWindowLayout`]
77 ///
78 /// # Examples
79 ///
80 /// ```
81 /// use dioxus::prelude::*;
82 /// use dioxus_use_window::{ResponsiveLayout, UseWindowBuilder};
83 ///
84 /// fn App(cx: Scope) -> Element {
85 /// let hook = UseWindowBuilder::default().use_responsive_layout(&cx);
86 ///
87 /// cx.render(rsx!(
88 /// h1 { "Window layout: {hook}" }
89 /// ))
90 /// }
91 /// ```
92 #[inline]
93 pub fn use_responsive_layout<'a>(&self, cx: &'a ScopeState) -> &'a mut UseWindowLayout<ResponsiveLayout> {
94 self.use_layout(cx)
95 }
96}