Skip to main content

dear_imgui_rs/ui/
core.rs

1use super::*;
2
3impl Ui {
4    pub(super) fn assert_finite_f32(caller: &str, name: &str, value: f32) {
5        assert!(value.is_finite(), "{caller} {name} must be finite");
6    }
7
8    pub(super) fn assert_finite_vec2(caller: &str, name: &str, value: [f32; 2]) {
9        assert!(
10            value[0].is_finite() && value[1].is_finite(),
11            "{caller} {name} must contain finite values"
12        );
13    }
14
15    /// Creates a new Ui instance
16    ///
17    /// This should only be called by Context::create()
18    pub(crate) fn new() -> Self {
19        Ui {
20            buffer: UnsafeCell::new(UiBuffer::new(1024)),
21        }
22    }
23
24    /// Returns an immutable reference to the inputs/outputs object
25    #[doc(alias = "GetIO")]
26    pub fn io(&self) -> &crate::io::Io {
27        unsafe {
28            let io = sys::igGetIO_Nil();
29            if io.is_null() {
30                panic!("Ui::io() requires an active ImGui context");
31            }
32            &*(io as *const crate::io::Io)
33        }
34    }
35
36    /// Internal method to push a single text to our scratch buffer.
37    pub(crate) fn scratch_txt(&self, txt: impl AsRef<str>) -> *const std::os::raw::c_char {
38        unsafe {
39            let handle = &mut *self.buffer.get();
40            handle.scratch_txt(txt)
41        }
42    }
43
44    /// Helper method for two strings
45    pub(crate) fn scratch_txt_two(
46        &self,
47        txt_0: impl AsRef<str>,
48        txt_1: impl AsRef<str>,
49    ) -> (*const std::os::raw::c_char, *const std::os::raw::c_char) {
50        unsafe {
51            let handle = &mut *self.buffer.get();
52            handle.scratch_txt_two(txt_0, txt_1)
53        }
54    }
55
56    /// Helper method with one optional value
57    pub(crate) fn scratch_txt_with_opt(
58        &self,
59        txt_0: impl AsRef<str>,
60        txt_1: Option<impl AsRef<str>>,
61    ) -> (*const std::os::raw::c_char, *const std::os::raw::c_char) {
62        unsafe {
63            let handle = &mut *self.buffer.get();
64            handle.scratch_txt_with_opt(txt_0, txt_1)
65        }
66    }
67
68    /// Get access to the scratch buffer for complex string operations
69    pub(crate) fn scratch_buffer(&self) -> &UnsafeCell<UiBuffer> {
70        &self.buffer
71    }
72
73    /// Returns an ID from a string label in the current ID scope.
74    ///
75    /// This mirrors `ImGui::GetID(label)`. Useful for building stable IDs
76    /// for widgets or dockspaces inside the current window/scope.
77    #[doc(alias = "GetID")]
78    pub fn get_id(&self, label: &str) -> Id {
79        unsafe { Id::from(sys::igGetID_Str(self.scratch_txt(label))) }
80    }
81}