Skip to main content

dear_imgui_rs/ui/
viewport.rs

1use super::*;
2
3impl Ui {
4    /// Returns a reference to the main Dear ImGui viewport (safe wrapper)
5    ///
6    /// Same viewport used by [`Ui::dockspace`](crate::Ui::dockspace)'s default host.
7    ///
8    /// The returned reference is owned by the currently active ImGui context and
9    /// must not be used after the context is destroyed.
10    #[doc(alias = "GetMainViewport")]
11    pub fn main_viewport(&self) -> &crate::platform_io::Viewport {
12        self.run_with_bound_context(|| unsafe {
13            let ptr = sys::igGetMainViewport();
14            if ptr.is_null() {
15                panic!("Ui::main_viewport() requires an active ImGui context");
16            }
17            crate::platform_io::Viewport::from_raw(ptr as *const sys::ImGuiViewport)
18        })
19    }
20
21    /// Set the viewport for the next window.
22    ///
23    /// This is a convenience wrapper over `ImGui::SetNextWindowViewport`.
24    /// Useful when hosting a fullscreen DockSpace window inside the main viewport.
25    #[doc(alias = "SetNextWindowViewport")]
26    pub fn set_next_window_viewport(&self, viewport_id: Id) {
27        self.run_with_bound_context(|| unsafe { sys::igSetNextWindowViewport(viewport_id.into()) });
28    }
29
30    /// Returns the viewport of the current window.
31    ///
32    /// Dear ImGui keeps an implicit fallback window current for the duration of an open frame, so
33    /// this may be called before, after, or outside an explicit user window's `Begin`/`End` scope.
34    /// Inside an explicit window it returns that window's viewport; after the window ends it
35    /// returns the fallback window's viewport again.
36    ///
37    /// # Panics
38    ///
39    /// Panics if raw/native API use has cleared the current viewport while this [`Ui`] still
40    /// represents an active frame.
41    #[doc(alias = "GetWindowViewport")]
42    pub fn window_viewport(&self) -> &crate::platform_io::Viewport {
43        self.run_with_bound_context(|| unsafe {
44            let ptr = sys::igGetWindowViewport();
45            if ptr.is_null() {
46                panic!("Ui::window_viewport() requires a current window");
47            }
48            crate::platform_io::Viewport::from_raw(ptr as *const sys::ImGuiViewport)
49        })
50    }
51
52    /// Find a viewport by ID.
53    #[doc(alias = "FindViewportByID")]
54    pub fn find_viewport_by_id(&self, viewport_id: Id) -> Option<&crate::platform_io::Viewport> {
55        self.run_with_bound_context(|| unsafe {
56            let ptr = sys::igFindViewportByID(viewport_id.raw());
57            if ptr.is_null() {
58                None
59            } else {
60                Some(crate::platform_io::Viewport::from_raw(
61                    ptr as *const sys::ImGuiViewport,
62                ))
63            }
64        })
65    }
66
67    /// Find a viewport by its platform handle.
68    ///
69    /// The platform handle type depends on the backend (e.g. `HWND` on Windows).
70    #[doc(alias = "FindViewportByPlatformHandle")]
71    #[allow(clippy::not_unsafe_ptr_arg_deref)]
72    pub fn find_viewport_by_platform_handle(
73        &self,
74        platform_handle: *mut std::ffi::c_void,
75    ) -> Option<&crate::platform_io::Viewport> {
76        self.run_with_bound_context(|| unsafe {
77            let ptr = sys::igFindViewportByPlatformHandle(platform_handle);
78            if ptr.is_null() {
79                None
80            } else {
81                Some(crate::platform_io::Viewport::from_raw(
82                    ptr as *const sys::ImGuiViewport,
83                ))
84            }
85        })
86    }
87}