dear_imgui_rs/
state_storage.rs1use crate::context::binding::CTX_MUTEX;
7use crate::{ContextThreadLease, Id, ImGuiResult, sys};
8use std::marker::PhantomData;
9use std::ptr::NonNull;
10
11#[derive(Copy, Clone, Debug)]
13pub struct StateStorage<'ui> {
14 raw: NonNull<sys::ImGuiStorage>,
15 _phantom: PhantomData<&'ui mut sys::ImGuiStorage>,
16}
17
18impl<'ui> StateStorage<'ui> {
19 pub unsafe fn from_raw(raw: *mut sys::ImGuiStorage) -> Self {
26 let raw = NonNull::new(raw).expect("StateStorage::from_raw() requires non-null pointer");
27 Self {
28 raw,
29 _phantom: PhantomData,
30 }
31 }
32
33 pub fn as_raw(self) -> *mut sys::ImGuiStorage {
35 self.raw.as_ptr()
36 }
37
38 pub fn clear(&mut self) {
40 let _guard = CTX_MUTEX.lock();
41 unsafe { sys::ImGuiStorage_Clear(self.raw.as_ptr()) }
42 }
43
44 pub fn get_int(&self, key: Id, default: i32) -> i32 {
45 unsafe { sys::ImGuiStorage_GetInt(self.raw.as_ptr(), key.raw(), default) }
46 }
47
48 pub fn set_int(&mut self, key: Id, value: i32) {
49 let _guard = CTX_MUTEX.lock();
50 unsafe { sys::ImGuiStorage_SetInt(self.raw.as_ptr(), key.raw(), value) }
51 }
52
53 pub fn get_bool(&self, key: Id, default: bool) -> bool {
54 unsafe { sys::ImGuiStorage_GetBool(self.raw.as_ptr(), key.raw(), default) }
55 }
56
57 pub fn set_bool(&mut self, key: Id, value: bool) {
58 self.set_int(key, i32::from(value));
59 }
60
61 pub fn get_float(&self, key: Id, default: f32) -> f32 {
62 unsafe { sys::ImGuiStorage_GetFloat(self.raw.as_ptr(), key.raw(), default) }
63 }
64
65 pub fn set_float(&mut self, key: Id, value: f32) {
66 let _guard = CTX_MUTEX.lock();
67 unsafe { sys::ImGuiStorage_SetFloat(self.raw.as_ptr(), key.raw(), value) }
68 }
69}
70
71#[derive(Debug)]
77pub struct OwnedStateStorage {
78 raw: sys::ImGuiStorage,
79 _thread_lease: ContextThreadLease,
80}
81
82impl OwnedStateStorage {
83 pub fn new() -> Self {
89 Self::try_new().expect("failed to acquire Dear ImGui thread ownership for state storage")
90 }
91
92 pub fn try_new() -> ImGuiResult<Self> {
94 let thread_lease = ContextThreadLease::acquire()?;
95 Ok(Self {
96 raw: sys::ImGuiStorage::default(),
97 _thread_lease: thread_lease,
98 })
99 }
100
101 pub unsafe fn as_mut(&mut self) -> &mut sys::ImGuiStorage {
113 &mut self.raw
114 }
115
116 pub fn as_ref(&self) -> &sys::ImGuiStorage {
117 &self.raw
118 }
119
120 pub fn as_raw_mut(&mut self) -> *mut sys::ImGuiStorage {
121 &mut self.raw as *mut sys::ImGuiStorage
122 }
123
124 pub fn as_raw(&self) -> *const sys::ImGuiStorage {
125 &self.raw as *const sys::ImGuiStorage
126 }
127}
128
129impl Default for OwnedStateStorage {
130 fn default() -> Self {
131 Self::new()
132 }
133}
134
135impl Drop for OwnedStateStorage {
136 fn drop(&mut self) {
137 unsafe { StateStorage::from_raw(self.as_raw_mut()) }.clear();
138 }
139}
140
141struct StateStorageOverride {
142 window: NonNull<sys::ImGuiWindow>,
143 previous: *mut sys::ImGuiStorage,
144}
145
146impl Drop for StateStorageOverride {
147 fn drop(&mut self) {
148 unsafe { (*self.window.as_ptr()).DC.StateStorage = self.previous };
150 }
151}
152
153impl crate::ui::Ui {
154 #[doc(alias = "GetStateStorage")]
159 pub fn with_current_state_storage<R>(
160 &self,
161 f: impl for<'storage> FnOnce(StateStorage<'storage>) -> R,
162 ) -> R {
163 self.run_with_bound_context(|| unsafe {
164 f(StateStorage::from_raw(sys::igGetStateStorage()))
165 })
166 }
167
168 #[doc(alias = "SetStateStorage")]
185 pub fn with_state_storage<R>(
186 &self,
187 storage: &mut OwnedStateStorage,
188 f: impl for<'storage> FnOnce(StateStorage<'storage>) -> R,
189 ) -> R {
190 self.run_with_bound_context(|| {
191 let replacement = storage.as_raw_mut();
192 let scoped_storage = unsafe { StateStorage::from_raw(replacement) };
193 let window = NonNull::new(unsafe { sys::igGetCurrentWindowRead() })
194 .expect("with_state_storage requires a current window");
195 let previous = unsafe {
196 std::ptr::replace(&raw mut (*window.as_ptr()).DC.StateStorage, replacement)
197 };
198 let storage_override = StateStorageOverride { window, previous };
199 let result = f(scoped_storage);
200 drop(storage_override);
201 result
202 })
203 }
204
205 #[doc(alias = "SetNextItemStorageID")]
207 pub fn set_next_item_storage_id(&self, storage_id: Id) {
208 self.run_with_bound_context(|| unsafe { sys::igSetNextItemStorageID(storage_id.raw()) });
209 }
210}