Skip to main content

dear_imgui_rs/ui/
style.rs

1use super::*;
2
3impl Ui {
4    /// Renders a style editor block (not a window) for the given `Style` structure
5    #[doc(alias = "ShowStyleEditor")]
6    pub fn show_style_editor(&self, style: &mut crate::style::Style) {
7        unsafe {
8            crate::sys::igShowStyleEditor(style.raw_mut());
9        }
10    }
11
12    /// Renders a style editor block (not a window) for the currently active style
13    #[doc(alias = "ShowStyleEditor")]
14    pub fn show_default_style_editor(&self) {
15        unsafe {
16            crate::sys::igShowStyleEditor(std::ptr::null_mut());
17        }
18    }
19
20    // ============================================================================
21    // Style Access
22    // ============================================================================
23
24    /// Returns a shared reference to the current [`Style`].
25    ///
26    /// ## Safety
27    ///
28    /// This function is tagged as `unsafe` because pushing via
29    /// [`push_style_color`](crate::Ui::push_style_color) or
30    /// [`push_style_var`](crate::Ui::push_style_var) or popping via
31    /// [`ColorStackToken::pop`](crate::ColorStackToken::pop) or
32    /// [`StyleStackToken::pop`](crate::StyleStackToken::pop) will modify the values in the returned
33    /// shared reference. Therefore, you should not retain this reference across calls to push and
34    /// pop. The [`clone_style`](Ui::clone_style) version may instead be used to avoid `unsafe`.
35    #[doc(alias = "GetStyle")]
36    pub unsafe fn style(&self) -> &crate::Style {
37        unsafe {
38            // safe because Style is a transparent wrapper around sys::ImGuiStyle
39            &*(sys::igGetStyle() as *const crate::Style)
40        }
41    }
42
43    /// Returns a copy of the current style.
44    ///
45    /// This is a safe alternative to [`style`](Self::style) that avoids the lifetime issues.
46    #[doc(alias = "GetStyle")]
47    pub fn clone_style(&self) -> crate::Style {
48        unsafe { self.style().clone() }
49    }
50
51    /// Apply the built-in Dark style to the current style.
52    #[doc(alias = "StyleColorsDark")]
53    pub fn style_colors_dark(&self) {
54        unsafe { sys::igStyleColorsDark(std::ptr::null_mut()) }
55    }
56
57    /// Apply the built-in Light style to the current style.
58    #[doc(alias = "StyleColorsLight")]
59    pub fn style_colors_light(&self) {
60        unsafe { sys::igStyleColorsLight(std::ptr::null_mut()) }
61    }
62
63    /// Apply the built-in Classic style to the current style.
64    #[doc(alias = "StyleColorsClassic")]
65    pub fn style_colors_classic(&self) {
66        unsafe { sys::igStyleColorsClassic(std::ptr::null_mut()) }
67    }
68
69    /// Write the Dark style values into the provided [`Style`] object.
70    #[doc(alias = "StyleColorsDark")]
71    pub fn style_colors_dark_into(&self, dst: &mut crate::Style) {
72        unsafe { sys::igStyleColorsDark(dst.raw_mut() as *mut sys::ImGuiStyle) }
73    }
74
75    /// Write the Light style values into the provided [`Style`] object.
76    #[doc(alias = "StyleColorsLight")]
77    pub fn style_colors_light_into(&self, dst: &mut crate::Style) {
78        unsafe { sys::igStyleColorsLight(dst.raw_mut() as *mut sys::ImGuiStyle) }
79    }
80
81    /// Write the Classic style values into the provided [`Style`] object.
82    #[doc(alias = "StyleColorsClassic")]
83    pub fn style_colors_classic_into(&self, dst: &mut crate::Style) {
84        unsafe { sys::igStyleColorsClassic(dst.raw_mut() as *mut sys::ImGuiStyle) }
85    }
86
87    /// Renders a style selector combo box.
88    ///
89    /// Returns true when a different style was selected.
90    #[doc(alias = "ShowStyleSelector")]
91    pub fn show_style_selector(&self, label: impl AsRef<str>) -> bool {
92        unsafe { sys::igShowStyleSelector(self.scratch_txt(label)) }
93    }
94
95    /// Renders a font selector combo box.
96    #[doc(alias = "ShowFontSelector")]
97    pub fn show_font_selector(&self, label: impl AsRef<str>) {
98        unsafe {
99            sys::igShowFontSelector(self.scratch_txt(label));
100        }
101    }
102}