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 `dockspace_over_main_viewport()`.
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        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        unsafe { sys::igSetNextWindowViewport(viewport_id.into()) }
28    }
29
30    /// Returns the viewport of the current window.
31    ///
32    /// This requires a current window (i.e. must be called between `Begin`/`End`).
33    #[doc(alias = "GetWindowViewport")]
34    pub fn window_viewport(&self) -> &crate::platform_io::Viewport {
35        unsafe {
36            let ptr = sys::igGetWindowViewport();
37            if ptr.is_null() {
38                panic!("Ui::window_viewport() requires a current window");
39            }
40            crate::platform_io::Viewport::from_raw(ptr as *const sys::ImGuiViewport)
41        }
42    }
43
44    /// Find a viewport by ID.
45    #[doc(alias = "FindViewportByID")]
46    pub fn find_viewport_by_id(&self, viewport_id: Id) -> Option<&crate::platform_io::Viewport> {
47        unsafe {
48            let ptr = sys::igFindViewportByID(viewport_id.raw());
49            if ptr.is_null() {
50                None
51            } else {
52                Some(crate::platform_io::Viewport::from_raw(
53                    ptr as *const sys::ImGuiViewport,
54                ))
55            }
56        }
57    }
58
59    /// Find a viewport by its platform handle.
60    ///
61    /// The platform handle type depends on the backend (e.g. `HWND` on Windows).
62    #[doc(alias = "FindViewportByPlatformHandle")]
63    #[allow(clippy::not_unsafe_ptr_arg_deref)]
64    pub fn find_viewport_by_platform_handle(
65        &self,
66        platform_handle: *mut std::ffi::c_void,
67    ) -> Option<&crate::platform_io::Viewport> {
68        unsafe {
69            let ptr = sys::igFindViewportByPlatformHandle(platform_handle);
70            if ptr.is_null() {
71                None
72            } else {
73                Some(crate::platform_io::Viewport::from_raw(
74                    ptr as *const sys::ImGuiViewport,
75                ))
76            }
77        }
78    }
79}