libobs_wrapper\display\window_manager/
show_hide.rs

1#[cfg(windows)]
2use std::sync::atomic::Ordering;
3#[cfg(windows)]
4use windows::Win32::UI::WindowsAndMessaging::{ShowWindow, SW_HIDE, SW_SHOWNA};
5
6use crate::display::window_manager::ShowHideTrait;
7use crate::display::ObsDisplayRef;
8use crate::run_with_obs;
9use crate::utils::ObsError;
10
11impl ShowHideTrait for ObsDisplayRef {
12    /// Shows the window.
13    ///
14    /// # Panics
15    /// if the internal lock is poisoned.
16    fn show(&mut self) -> Result<(), ObsError> {
17        log::trace!("show");
18
19        #[cfg(windows)]
20        if let Some(m) = &self.child_window_handler {
21            let m = m
22                .read()
23                .map_err(|e| ObsError::LockError(format!("{:?}", e)))?;
24            unsafe {
25                // Safety: The window handle is valid as long as the ObsDisplayRef exists
26                let _ = ShowWindow(m.window_handle.get_hwnd(), SW_SHOWNA);
27            }
28
29            m.is_hidden.store(false, Ordering::Relaxed);
30            return Ok(());
31        }
32
33        let ptr = self.as_ptr();
34        run_with_obs!(self.runtime, (ptr), move || {
35            unsafe {
36                // Safety: The pointer is valid because we are using a smart pointer
37                libobs::obs_display_set_enabled(ptr.get_ptr(), true);
38            }
39        })?;
40        Ok(())
41    }
42
43    fn hide(&mut self) -> Result<(), ObsError> {
44        log::trace!("hide");
45        #[cfg(windows)]
46        if let Some(m) = &self.child_window_handler {
47            let m = m
48                .read()
49                .map_err(|e| ObsError::LockError(format!("{:?}", e)))?;
50
51            unsafe {
52                // Safety: The window handle is valid as long as the ObsDisplayRef exists
53                let _ = ShowWindow(m.window_handle.get_hwnd(), SW_HIDE);
54            }
55
56            m.is_hidden.store(true, Ordering::Relaxed);
57            return Ok(());
58        }
59
60        let ptr = self.as_ptr();
61        run_with_obs!(self.runtime, (ptr), move || {
62            unsafe {
63                // Safety: The pointer is valid because we are using a smart pointer
64                libobs::obs_display_set_enabled(ptr.get_ptr(), false);
65            }
66        })?;
67        Ok(())
68    }
69
70    fn is_visible(&self) -> Result<bool, ObsError> {
71        #[cfg(windows)]
72        if let Some(m) = &self.child_window_handler {
73            let m = m
74                .read()
75                .map_err(|e| ObsError::LockError(format!("{:?}", e)))?;
76
77            return Ok(!m.is_hidden.load(Ordering::Relaxed));
78        }
79
80        let ptr = self.as_ptr();
81        run_with_obs!(self.runtime, (ptr), move || {
82            let enabled = unsafe {
83                // Safety: The pointer is valid because we are using a smart pointer
84                libobs::obs_display_enabled(ptr.get_ptr())
85            };
86
87            Ok(enabled)
88        })?
89    }
90}