dear_imgui_rs/context/platform.rs
1use crate::sys;
2
3use super::Context;
4use super::binding::{CTX_MUTEX, with_bound_context};
5
6impl Context {
7 /// Get shared access to the platform IO.
8 ///
9 /// Note: `ImGuiPlatformIO` exists even when multi-viewport is disabled. We expose it
10 /// unconditionally so callers can use ImGui 1.92+ texture management via `PlatformIO.Textures[]`.
11 pub fn platform_io(&self) -> &crate::platform_io::PlatformIo {
12 let _guard = CTX_MUTEX.lock();
13 unsafe {
14 let pio = self.platform_io_ptr("Context::platform_io()");
15 crate::platform_io::PlatformIo::from_raw(pio)
16 }
17 }
18
19 /// Get mutable access to the platform IO.
20 ///
21 /// Note: `ImGuiPlatformIO` exists even when multi-viewport is disabled. We expose it
22 /// unconditionally so callers can use ImGui 1.92+ texture management via `PlatformIO.Textures[]`.
23 pub fn platform_io_mut(&mut self) -> &mut crate::platform_io::PlatformIo {
24 let _guard = CTX_MUTEX.lock();
25 unsafe {
26 let pio = self.platform_io_ptr("Context::platform_io_mut()");
27 crate::platform_io::PlatformIo::from_raw_mut(pio)
28 }
29 }
30
31 /// Returns a reference to the main Dear ImGui viewport.
32 ///
33 /// The returned reference is owned by this ImGui context and
34 /// must not be used after the context is destroyed.
35 #[doc(alias = "GetMainViewport")]
36 pub fn main_viewport(&mut self) -> &mut crate::platform_io::Viewport {
37 let _guard = CTX_MUTEX.lock();
38 unsafe {
39 with_bound_context(self.raw, || {
40 let ptr = sys::igGetMainViewport();
41 if ptr.is_null() {
42 panic!("Context::main_viewport() requires a valid ImGui context");
43 }
44 crate::platform_io::Viewport::from_raw_mut(ptr)
45 })
46 }
47 }
48
49 /// Enable multi-viewport support flags
50 #[cfg(feature = "multi-viewport")]
51 pub fn enable_multi_viewport(&mut self) {
52 // Enable viewport flags
53 crate::viewport_backend::utils::enable_viewport_flags(self.io_mut());
54 }
55
56 /// Update platform windows
57 ///
58 /// This function should be called every frame when multi-viewport is enabled.
59 /// It updates all platform windows and handles viewport management.
60 #[cfg(feature = "multi-viewport")]
61 pub fn update_platform_windows(&mut self) {
62 let _guard = CTX_MUTEX.lock();
63 unsafe {
64 with_bound_context(self.raw, || {
65 // Ensure main viewport is properly set up before updating platform windows
66 let main_viewport = sys::igGetMainViewport();
67 if !main_viewport.is_null() && (*main_viewport).PlatformHandle.is_null() {
68 eprintln!(
69 "update_platform_windows: main viewport not set up, setting it up now"
70 );
71 // The main viewport needs to be set up - this should be done by the backend
72 // For now, we'll just log this and continue
73 }
74
75 sys::igUpdatePlatformWindows();
76 });
77 }
78 }
79
80 /// Render platform windows with default implementation
81 ///
82 /// This function renders all platform windows using the default implementation.
83 /// It calls the platform and renderer backends to render each viewport.
84 #[cfg(feature = "multi-viewport")]
85 pub fn render_platform_windows_default(&mut self) {
86 let _guard = CTX_MUTEX.lock();
87 unsafe {
88 with_bound_context(self.raw, || {
89 sys::igRenderPlatformWindowsDefault(std::ptr::null_mut(), std::ptr::null_mut());
90 });
91 }
92 }
93
94 /// Destroy all platform windows
95 ///
96 /// This function should be called during shutdown to properly clean up
97 /// all platform windows and their associated resources.
98 #[cfg(feature = "multi-viewport")]
99 pub fn destroy_platform_windows(&mut self) {
100 let _guard = CTX_MUTEX.lock();
101 unsafe {
102 with_bound_context(self.raw, || {
103 sys::igDestroyPlatformWindows();
104 });
105 }
106 }
107}