Skip to main content

dear_imgui_rs/ui/
widgets.rs

1use super::*;
2
3impl Ui {
4    /// Display text
5    #[doc(alias = "TextUnformatted")]
6    pub fn text<T: AsRef<str>>(&self, text: T) {
7        let s = text.as_ref();
8        unsafe {
9            let start = s.as_ptr();
10            let end = start.add(s.len());
11            crate::sys::igTextUnformatted(
12                start as *const std::os::raw::c_char,
13                end as *const std::os::raw::c_char,
14            );
15        }
16    }
17
18    /// Convenience: draw an image with background and tint (ImGui 1.92+)
19    ///
20    /// Equivalent to using `image_config(...).build_with_bg(bg, tint)` but in one call.
21    #[doc(alias = "ImageWithBg")]
22    pub fn image_with_bg<'tex>(
23        &self,
24        texture: impl Into<TextureRef<'tex>>,
25        size: [f32; 2],
26        bg_color: [f32; 4],
27        tint_color: [f32; 4],
28    ) {
29        crate::widget::image::Image::new(self, texture, size).build_with_bg(bg_color, tint_color)
30    }
31
32    // Drag widgets
33
34    /// Creates a drag float slider
35    #[doc(alias = "DragFloat")]
36    pub fn drag_float(&self, label: impl AsRef<str>, value: &mut f32) -> bool {
37        crate::widget::drag::Drag::new(label).build(self, value)
38    }
39
40    /// Creates a drag float slider with configuration
41    #[doc(alias = "DragFloat")]
42    pub fn drag_float_config<L: AsRef<str>>(&self, label: L) -> crate::widget::drag::Drag<f32, L> {
43        crate::widget::drag::Drag::new(label)
44    }
45
46    /// Creates a drag int slider
47    #[doc(alias = "DragInt")]
48    pub fn drag_int(&self, label: impl AsRef<str>, value: &mut i32) -> bool {
49        crate::widget::drag::Drag::new(label).build(self, value)
50    }
51
52    /// Creates a drag int slider with configuration
53    #[doc(alias = "DragInt")]
54    pub fn drag_int_config<L: AsRef<str>>(&self, label: L) -> crate::widget::drag::Drag<i32, L> {
55        crate::widget::drag::Drag::new(label)
56    }
57
58    /// Creates a drag float range slider
59    #[doc(alias = "DragFloatRange2")]
60    pub fn drag_float_range2(&self, label: impl AsRef<str>, min: &mut f32, max: &mut f32) -> bool {
61        crate::widget::drag::DragRange::<f32, _>::new(label).build(self, min, max)
62    }
63
64    /// Creates a drag float range slider with configuration
65    #[doc(alias = "DragFloatRange2")]
66    pub fn drag_float_range2_config<L: AsRef<str>>(
67        &self,
68        label: L,
69    ) -> crate::widget::drag::DragRange<f32, L> {
70        crate::widget::drag::DragRange::new(label)
71    }
72
73    /// Creates a drag int range slider
74    #[doc(alias = "DragIntRange2")]
75    pub fn drag_int_range2(&self, label: impl AsRef<str>, min: &mut i32, max: &mut i32) -> bool {
76        crate::widget::drag::DragRange::<i32, _>::new(label).build(self, min, max)
77    }
78
79    /// Creates a drag int range slider with configuration
80    #[doc(alias = "DragIntRange2")]
81    pub fn drag_int_range2_config<L: AsRef<str>>(
82        &self,
83        label: L,
84    ) -> crate::widget::drag::DragRange<i32, L> {
85        crate::widget::drag::DragRange::new(label)
86    }
87
88    /// Set next item to be open by default.
89    ///
90    /// This is useful for tree nodes, collapsing headers, etc.
91    #[doc(alias = "SetNextItemOpen")]
92    pub fn set_next_item_open(&self, is_open: bool) {
93        unsafe {
94            sys::igSetNextItemOpen(is_open, 0); // 0 = ImGuiCond_Always
95        }
96    }
97
98    /// Set next item to be open by default with condition.
99    #[doc(alias = "SetNextItemOpen")]
100    pub fn set_next_item_open_with_cond(&self, is_open: bool, cond: crate::Condition) {
101        unsafe { sys::igSetNextItemOpen(is_open, cond as sys::ImGuiCond) }
102    }
103
104    /// Set next item width.
105    ///
106    /// Set to 0.0 for default width, >0.0 for explicit width, <0.0 for relative width.
107    #[doc(alias = "SetNextItemWidth")]
108    pub fn set_next_item_width(&self, item_width: f32) {
109        Self::assert_finite_f32("Ui::set_next_item_width()", "item_width", item_width);
110        unsafe {
111            sys::igSetNextItemWidth(item_width);
112        }
113    }
114
115    /// Display a text label with a boolean value (for quick debug UIs).
116    #[doc(alias = "Value")]
117    pub fn value_bool(&self, prefix: impl AsRef<str>, v: bool) {
118        unsafe { sys::igValue_Bool(self.scratch_txt(prefix), v) }
119    }
120}