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        self.run_with_bound_context(|| 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        self.run_with_bound_context(|| 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        self.run_with_bound_context(|| 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        self.run_with_bound_context(|| 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        self.run_with_bound_context(|| 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        self.run_with_bound_context(|| 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        self.run_with_bound_context(|| unsafe {
73            sys::igStyleColorsDark(dst.raw_mut() as *mut sys::ImGuiStyle)
74        });
75    }
76
77    /// Write the Light style values into the provided [`Style`] object.
78    #[doc(alias = "StyleColorsLight")]
79    pub fn style_colors_light_into(&self, dst: &mut crate::Style) {
80        self.run_with_bound_context(|| unsafe {
81            sys::igStyleColorsLight(dst.raw_mut() as *mut sys::ImGuiStyle)
82        });
83    }
84
85    /// Write the Classic style values into the provided [`Style`] object.
86    #[doc(alias = "StyleColorsClassic")]
87    pub fn style_colors_classic_into(&self, dst: &mut crate::Style) {
88        self.run_with_bound_context(|| unsafe {
89            sys::igStyleColorsClassic(dst.raw_mut() as *mut sys::ImGuiStyle)
90        });
91    }
92
93    /// Renders a style selector combo box.
94    ///
95    /// Returns true when a different style was selected.
96    #[doc(alias = "ShowStyleSelector")]
97    pub fn show_style_selector(&self, label: impl AsRef<str>) -> bool {
98        self.run_with_bound_context(|| unsafe { sys::igShowStyleSelector(self.scratch_txt(label)) })
99    }
100
101    /// Renders a font selector combo box.
102    #[doc(alias = "ShowFontSelector")]
103    pub fn show_font_selector(&self, label: impl AsRef<str>) {
104        self.run_with_bound_context(|| unsafe {
105            sys::igShowFontSelector(self.scratch_txt(label));
106        });
107    }
108}