dear_imgui_rs/ui/
debug_tools.rs1use super::*;
2
3impl Ui {
4 #[doc(alias = "ShowDemoWindow")]
13 pub fn show_demo_window(&self, opened: &mut bool) {
14 self.run_with_bound_context(|| unsafe {
15 crate::sys::dear_imgui_rs_show_demo_window_without_font_atlas(opened);
16 });
17 }
18
19 pub unsafe fn show_upstream_demo_window(&self, opened: &mut bool) {
31 self.run_with_bound_context(|| unsafe {
32 crate::sys::igShowDemoWindow(opened);
33 });
34 }
35
36 #[doc(alias = "ShowAboutWindow")]
40 pub fn show_about_window(&self, opened: &mut bool) {
41 self.run_with_bound_context(|| unsafe {
42 crate::sys::igShowAboutWindow(opened);
43 });
44 }
45
46 #[doc(alias = "ShowMetricsWindow")]
51 pub fn show_metrics_window(&self, opened: &mut bool) {
52 self.run_with_bound_context(|| unsafe {
53 crate::sys::dear_imgui_rs_show_metrics_window_without_font_atlas(opened);
54 });
55 }
56
57 pub unsafe fn show_upstream_metrics_window(&self, opened: &mut bool) {
64 self.run_with_bound_context(|| unsafe {
65 crate::sys::igShowMetricsWindow(opened);
66 });
67 }
68
69 #[doc(alias = "ShowFontAtlas")]
79 pub unsafe fn show_font_atlas_debug_panel(&self) {
80 self.run_with_bound_context(|| unsafe {
81 crate::sys::dear_imgui_rs_show_font_atlas_debug_panel();
82 });
83 }
84
85 #[doc(alias = "ShowUserGuide")]
87 pub fn show_user_guide(&self) {
88 self.run_with_bound_context(|| unsafe {
89 crate::sys::igShowUserGuide();
90 });
91 }
92
93 #[doc(alias = "ShowDebugLogWindow")]
101 pub fn show_debug_log_window(&self, opened: &mut bool) {
102 self.run_with_bound_context(|| unsafe {
103 sys::igShowDebugLogWindow(opened);
104 });
105 }
106
107 #[doc(alias = "ShowIDStackToolWindow")]
111 pub fn show_id_stack_tool_window(&self, opened: &mut bool) {
112 self.run_with_bound_context(|| unsafe {
113 sys::igShowIDStackToolWindow(opened);
114 });
115 }
116
117 #[doc(alias = "DebugTextEncoding")]
126 pub fn debug_text_encoding(&self, text: impl AsRef<str>) {
127 let text = text.as_ref();
128 assert!(
129 !text.contains('\0'),
130 "Ui::debug_text_encoding() text must not contain interior NUL bytes"
131 );
132 let text = self.scratch_txt(text);
133 self.run_with_bound_context(|| unsafe { sys::igDebugTextEncoding(text) });
134 }
135
136 #[doc(alias = "DebugFlashStyleColor")]
138 pub fn debug_flash_style_color(&self, color: crate::StyleColor) {
139 self.run_with_bound_context(|| unsafe { sys::igDebugFlashStyleColor(color as i32) });
140 }
141
142 #[doc(alias = "DebugStartItemPicker")]
144 pub fn debug_start_item_picker(&self) {
145 self.run_with_bound_context(|| unsafe { sys::igDebugStartItemPicker() });
146 }
147
148 #[doc(alias = "GetVersion")]
150 pub fn get_version(&self) -> &str {
151 self.run_with_bound_context(|| unsafe {
152 let version_ptr = sys::igGetVersion();
153 if version_ptr.is_null() {
154 return "Unknown";
155 }
156 let c_str = std::ffi::CStr::from_ptr(version_ptr);
157 c_str.to_str().unwrap_or("Unknown")
158 })
159 }
160}
161
162#[cfg(test)]
163mod tests {
164 #[test]
165 fn safe_debug_windows_keep_font_atlas_controls_explicit() {
166 let _: fn(&crate::Ui, &mut bool) = crate::Ui::show_demo_window;
167 let _: fn(&crate::Ui, &mut bool) = crate::Ui::show_metrics_window;
168 let _: unsafe fn(&crate::Ui, &mut bool) = crate::Ui::show_upstream_demo_window;
169 let _: unsafe fn(&crate::Ui, &mut bool) = crate::Ui::show_upstream_metrics_window;
170 let _: unsafe fn(&crate::Ui) = crate::Ui::show_font_atlas_debug_panel;
171 }
172
173 #[test]
174 fn public_debug_helpers_are_safe_to_call_in_a_frame() {
175 let mut ctx = crate::Context::create();
176 ctx.io_mut().set_display_size([128.0, 128.0]);
177 ctx.io_mut().set_delta_time(1.0 / 60.0);
178 let _ = ctx.font_atlas().build();
179 let ui = ctx.frame();
180
181 let mut demo_open = true;
182 ui.show_demo_window(&mut demo_open);
183 let mut metrics_open = true;
184 ui.show_metrics_window(&mut metrics_open);
185
186 ui.window("debug_helpers").build(|| {
187 ui.show_default_style_editor();
188 let cursor_y = ui.cursor_pos_y();
189 ui.debug_text_encoding("A UTF-8 string: 界");
190 assert!(ui.cursor_pos_y() > cursor_y);
191
192 ui.debug_flash_style_color(crate::StyleColor::Text);
193 ui.debug_start_item_picker();
194 });
195
196 assert!(
197 std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
198 ui.debug_text_encoding("A\0B");
199 }))
200 .is_err()
201 );
202 }
203}