dioxus_use_window/hooks/use_browser/
mod.rs

1mod display;
2
3use super::*;
4
5/// Window height effect handler
6#[derive(Debug)]
7pub struct UseBrowser {
8    inner: UseWindow,
9}
10
11impl UseWindowBuilder {
12    /// hooks for window's height with config
13    ///
14    /// # Arguments
15    ///
16    /// returns: [`UseWindowHeight`]
17    ///
18    /// # Examples
19    ///
20    /// ```
21    /// use dioxus::prelude::*;
22    /// use dioxus_use_window::{UseWindowBuilder};
23    ///
24    /// fn App(cx: Scope) -> Element {
25    ///     let hook = UseWindowBuilder::default().use_browser(&cx);
26    ///
27    ///     cx.render(rsx!(
28    ///         h1 { "Window height: {hook}" }
29    ///     ))
30    /// }
31    /// ```
32    #[inline]
33    pub fn use_browser<'a>(&self, cx: &'a ScopeState) -> &'a mut UseBrowser {
34        let hook = UseBrowser::new(self.use_window_hook(cx));
35        cx.use_hook(|| hook)
36    }
37}
38
39impl UseBrowser {
40    #[inline]
41    pub(crate) fn new(size: UseWindow) -> Self {
42        Self { inner: size }
43    }
44    /// get size of current browser
45    #[inline]
46    pub fn size(&self) -> (usize, usize) {
47        (self.width(), self.height())
48    }
49    /// get width of current browser
50    ///
51    /// **read-only**
52    #[inline]
53    pub fn width(&self) -> usize {
54        self.inner.data_ref().outer_width() as usize
55    }
56    /// get height of current window
57    ///
58    /// **read-only**
59    #[inline]
60    pub fn height(&self) -> usize {
61        self.inner.data_ref().outer_height() as usize
62    }
63    /// get aspect radio of current window
64    #[inline]
65    pub fn aspect_radio(&self) -> f64 {
66        self.inner.data_ref().outer_aspect_radio()
67    }
68    /// get height of current window
69    #[inline]
70    pub fn resize_to(&self, size: (usize, usize)) -> bool {
71        self.inner.data_ref().resize_outer_size(size.0 as _, size.1 as _).is_some()
72    }
73    /// get height of current window
74    #[inline]
75    pub fn resize_delta(&self, size: (i32, i32)) -> bool {
76        self.inner.data_ref().resize_outer_delta(size.0, size.1).is_some()
77    }
78}
79
80impl UseBrowser {
81    /// check if is full screen
82    #[inline]
83    pub fn is_fullscreen(&self) -> bool {
84        self.inner.data_ref().fullscreen_element().is_some()
85    }
86    /// get width of current browser
87    #[inline]
88    pub fn fullscreen_toggle(&self) -> bool {
89        self.inner.data_ref().fullscreen_toggle().is_some()
90    }
91    /// set full screen on
92    #[inline]
93    pub fn fullscreen_on(&self) -> bool {
94        self.inner.data_ref().fullscreen_set(true).is_some()
95    }
96    /// set full screen off
97    #[inline]
98    pub fn fullscreen_off(&self) -> bool {
99        self.inner.data_ref().fullscreen_set(false).is_some()
100    }
101}