1use crate::draw::DrawListMut;
2use crate::input::MouseCursor;
3use crate::internal::RawWrapper;
4use crate::string::UiBuffer;
5use crate::sys;
6use crate::texture::TextureRef;
7use std::cell::UnsafeCell;
8
9#[derive(Debug)]
11pub struct Ui {
12 buffer: UnsafeCell<UiBuffer>,
14}
15
16impl Ui {
17 #[doc(alias = "GetMainViewport")]
21 pub fn main_viewport(&self) -> &'static crate::platform_io::Viewport {
22 crate::platform_io::Viewport::main()
23 }
24 pub(crate) fn new() -> Self {
28 Ui {
29 buffer: UnsafeCell::new(UiBuffer::new(1024)),
30 }
31 }
32
33 #[doc(alias = "GetIO")]
35 pub fn io(&self) -> &crate::io::Io {
36 unsafe { &*(sys::igGetIO_Nil() as *const crate::io::Io) }
37 }
38
39 pub(crate) fn scratch_txt(&self, txt: impl AsRef<str>) -> *const std::os::raw::c_char {
41 unsafe {
42 let handle = &mut *self.buffer.get();
43 handle.scratch_txt(txt)
44 }
45 }
46
47 pub(crate) fn scratch_txt_opt(
49 &self,
50 txt: Option<impl AsRef<str>>,
51 ) -> *const std::os::raw::c_char {
52 unsafe {
53 let handle = &mut *self.buffer.get();
54 handle.scratch_txt_opt(txt)
55 }
56 }
57
58 pub(crate) fn scratch_txt_two(
60 &self,
61 txt_0: impl AsRef<str>,
62 txt_1: impl AsRef<str>,
63 ) -> (*const std::os::raw::c_char, *const std::os::raw::c_char) {
64 unsafe {
65 let handle = &mut *self.buffer.get();
66 handle.scratch_txt_two(txt_0, txt_1)
67 }
68 }
69
70 pub(crate) fn scratch_txt_with_opt(
72 &self,
73 txt_0: impl AsRef<str>,
74 txt_1: Option<impl AsRef<str>>,
75 ) -> (*const std::os::raw::c_char, *const std::os::raw::c_char) {
76 unsafe {
77 let handle = &mut *self.buffer.get();
78 handle.scratch_txt_with_opt(txt_0, txt_1)
79 }
80 }
81
82 pub(crate) fn scratch_buffer(&self) -> &UnsafeCell<UiBuffer> {
84 &self.buffer
85 }
86
87 #[doc(alias = "TextUnformatted")]
89 pub fn text<T: AsRef<str>>(&self, text: T) {
90 let s = text.as_ref();
91 unsafe {
92 let start = s.as_ptr();
93 let end = start.add(s.len());
94 crate::sys::igTextUnformatted(
95 start as *const std::os::raw::c_char,
96 end as *const std::os::raw::c_char,
97 );
98 }
99 }
100
101 #[doc(alias = "GetWindowDrawList")]
103 pub fn get_window_draw_list(&self) -> DrawListMut<'_> {
104 DrawListMut::window(self)
105 }
106
107 #[doc(alias = "GetBackgroundDrawList")]
109 pub fn get_background_draw_list(&self) -> DrawListMut<'_> {
110 DrawListMut::background(self)
111 }
112
113 #[doc(alias = "GetForegroundDrawList")]
115 pub fn get_foreground_draw_list(&self) -> DrawListMut<'_> {
116 DrawListMut::foreground(self)
117 }
118
119 pub fn window(&self, name: impl Into<String>) -> crate::window::Window<'_> {
121 crate::window::Window::new(self, name)
122 }
123
124 #[doc(alias = "ShowDemoWindow")]
127 pub fn show_demo_window(&self, opened: &mut bool) {
128 unsafe {
129 crate::sys::igShowDemoWindow(opened);
130 }
131 }
132
133 #[doc(alias = "ImageWithBg")]
137 pub fn image_with_bg(
138 &self,
139 texture: impl Into<TextureRef>,
140 size: [f32; 2],
141 bg_color: [f32; 4],
142 tint_color: [f32; 4],
143 ) {
144 crate::widget::image::Image::new(self, texture, size).build_with_bg(bg_color, tint_color)
145 }
146
147 #[doc(alias = "ShowAboutWindow")]
151 pub fn show_about_window(&self, opened: &mut bool) {
152 unsafe {
153 crate::sys::igShowAboutWindow(opened);
154 }
155 }
156
157 #[doc(alias = "ShowMetricsWindow")]
162 pub fn show_metrics_window(&self, opened: &mut bool) {
163 unsafe {
164 crate::sys::igShowMetricsWindow(opened);
165 }
166 }
167
168 #[doc(alias = "ShowStyleEditor")]
170 pub fn show_style_editor(&self, style: &mut crate::style::Style) {
171 unsafe {
172 crate::sys::igShowStyleEditor(style.raw_mut());
173 }
174 }
175
176 #[doc(alias = "ShowStyleEditor")]
178 pub fn show_default_style_editor(&self) {
179 unsafe {
180 crate::sys::igShowStyleEditor(std::ptr::null_mut());
181 }
182 }
183
184 #[doc(alias = "ShowUserGuide")]
186 pub fn show_user_guide(&self) {
187 unsafe {
188 crate::sys::igShowUserGuide();
189 }
190 }
191
192 #[doc(alias = "DragFloat")]
196 pub fn drag_float(&self, label: impl AsRef<str>, value: &mut f32) -> bool {
197 crate::widget::drag::Drag::new(label).build(self, value)
198 }
199
200 #[doc(alias = "DragFloat")]
202 pub fn drag_float_config<L: AsRef<str>>(&self, label: L) -> crate::widget::drag::Drag<f32, L> {
203 crate::widget::drag::Drag::new(label)
204 }
205
206 #[doc(alias = "DragInt")]
208 pub fn drag_int(&self, label: impl AsRef<str>, value: &mut i32) -> bool {
209 crate::widget::drag::Drag::new(label).build(self, value)
210 }
211
212 #[doc(alias = "DragInt")]
214 pub fn drag_int_config<L: AsRef<str>>(&self, label: L) -> crate::widget::drag::Drag<i32, L> {
215 crate::widget::drag::Drag::new(label)
216 }
217
218 #[doc(alias = "DragFloatRange2")]
220 pub fn drag_float_range2(&self, label: impl AsRef<str>, min: &mut f32, max: &mut f32) -> bool {
221 crate::widget::drag::DragRange::<f32, _>::new(label).build(self, min, max)
222 }
223
224 #[doc(alias = "DragFloatRange2")]
226 pub fn drag_float_range2_config<L: AsRef<str>>(
227 &self,
228 label: L,
229 ) -> crate::widget::drag::DragRange<f32, L> {
230 crate::widget::drag::DragRange::new(label)
231 }
232
233 #[doc(alias = "DragIntRange2")]
235 pub fn drag_int_range2(&self, label: impl AsRef<str>, min: &mut i32, max: &mut i32) -> bool {
236 crate::widget::drag::DragRange::<i32, _>::new(label).build(self, min, max)
237 }
238
239 #[doc(alias = "DragIntRange2")]
241 pub fn drag_int_range2_config<L: AsRef<str>>(
242 &self,
243 label: L,
244 ) -> crate::widget::drag::DragRange<i32, L> {
245 crate::widget::drag::DragRange::new(label)
246 }
247
248 #[doc(alias = "GetMouseCursor")]
252 pub fn mouse_cursor(&self) -> Option<MouseCursor> {
253 unsafe {
254 match sys::igGetMouseCursor() {
255 sys::ImGuiMouseCursor_Arrow => Some(MouseCursor::Arrow),
256 sys::ImGuiMouseCursor_TextInput => Some(MouseCursor::TextInput),
257 sys::ImGuiMouseCursor_ResizeAll => Some(MouseCursor::ResizeAll),
258 sys::ImGuiMouseCursor_ResizeNS => Some(MouseCursor::ResizeNS),
259 sys::ImGuiMouseCursor_ResizeEW => Some(MouseCursor::ResizeEW),
260 sys::ImGuiMouseCursor_ResizeNESW => Some(MouseCursor::ResizeNESW),
261 sys::ImGuiMouseCursor_ResizeNWSE => Some(MouseCursor::ResizeNWSE),
262 sys::ImGuiMouseCursor_Hand => Some(MouseCursor::Hand),
263 sys::ImGuiMouseCursor_NotAllowed => Some(MouseCursor::NotAllowed),
264 _ => None,
265 }
266 }
267 }
268
269 #[doc(alias = "SetMouseCursor")]
273 pub fn set_mouse_cursor(&self, cursor_type: Option<MouseCursor>) {
274 unsafe {
275 let val: sys::ImGuiMouseCursor = cursor_type
276 .map(|x| x as sys::ImGuiMouseCursor)
277 .unwrap_or(sys::ImGuiMouseCursor_None);
278 sys::igSetMouseCursor(val);
279 }
280 }
281
282 #[doc(alias = "SetKeyboardFocusHere")]
291 pub fn set_keyboard_focus_here(&self) {
292 self.set_keyboard_focus_here_with_offset(0);
293 }
294
295 #[doc(alias = "SetKeyboardFocusHere")]
299 pub fn set_keyboard_focus_here_with_offset(&self, offset: i32) {
300 unsafe {
301 sys::igSetKeyboardFocusHere(offset);
302 }
303 }
304
305 #[doc(alias = "SetNextItemOpen")]
309 pub fn set_next_item_open(&self, is_open: bool) {
310 unsafe {
311 sys::igSetNextItemOpen(is_open, 0); }
313 }
314
315 #[doc(alias = "SetNextItemOpen")]
317 pub fn set_next_item_open_with_cond(&self, is_open: bool, cond: crate::Condition) {
318 unsafe { sys::igSetNextItemOpen(is_open, cond as sys::ImGuiCond) }
319 }
320
321 #[doc(alias = "SetNextItemWidth")]
325 pub fn set_next_item_width(&self, item_width: f32) {
326 unsafe {
327 sys::igSetNextItemWidth(item_width);
328 }
329 }
330
331 #[doc(alias = "GetStyle")]
347 pub unsafe fn style(&self) -> &crate::Style {
348 unsafe {
349 &*(sys::igGetStyle() as *const crate::Style)
351 }
352 }
353
354 #[doc(alias = "GetStyle")]
358 pub fn clone_style(&self) -> crate::Style {
359 unsafe { self.style().clone() }
360 }
361
362 #[doc(alias = "StyleColorsDark")]
364 pub fn style_colors_dark(&self) {
365 unsafe { sys::igStyleColorsDark(std::ptr::null_mut()) }
366 }
367
368 #[doc(alias = "StyleColorsLight")]
370 pub fn style_colors_light(&self) {
371 unsafe { sys::igStyleColorsLight(std::ptr::null_mut()) }
372 }
373
374 #[doc(alias = "StyleColorsClassic")]
376 pub fn style_colors_classic(&self) {
377 unsafe { sys::igStyleColorsClassic(std::ptr::null_mut()) }
378 }
379
380 #[doc(alias = "StyleColorsDark")]
382 pub fn style_colors_dark_into(&self, dst: &mut crate::Style) {
383 unsafe { sys::igStyleColorsDark(dst as *mut _ as *mut sys::ImGuiStyle) }
384 }
385
386 #[doc(alias = "StyleColorsLight")]
388 pub fn style_colors_light_into(&self, dst: &mut crate::Style) {
389 unsafe { sys::igStyleColorsLight(dst as *mut _ as *mut sys::ImGuiStyle) }
390 }
391
392 #[doc(alias = "StyleColorsClassic")]
394 pub fn style_colors_classic_into(&self, dst: &mut crate::Style) {
395 unsafe { sys::igStyleColorsClassic(dst as *mut _ as *mut sys::ImGuiStyle) }
396 }
397
398 #[doc(alias = "GetWindowDpiScale")]
400 pub fn window_dpi_scale(&self) -> f32 {
401 unsafe { sys::igGetWindowDpiScale() }
402 }
403
404 #[doc(alias = "Value")]
406 pub fn value_bool(&self, prefix: impl AsRef<str>, v: bool) {
407 unsafe { sys::igValue_Bool(self.scratch_txt(prefix), v) }
408 }
409
410 #[doc(alias = "GetWindowWidth")]
412 pub fn window_width(&self) -> f32 {
413 unsafe { sys::igGetWindowWidth() }
414 }
415
416 #[doc(alias = "GetWindowHeight")]
418 pub fn window_height(&self) -> f32 {
419 unsafe { sys::igGetWindowHeight() }
420 }
421
422 #[doc(alias = "GetWindowPos")]
424 pub fn window_pos(&self) -> [f32; 2] {
425 unsafe {
426 let mut v = sys::ImVec2 { x: 0.0, y: 0.0 };
427 sys::igGetWindowPos(&mut v);
428 [v.x, v.y]
429 }
430 }
431
432 #[doc(alias = "GetWindowSize")]
434 pub fn window_size(&self) -> [f32; 2] {
435 unsafe {
436 let mut v = sys::ImVec2 { x: 0.0, y: 0.0 };
437 sys::igGetWindowSize(&mut v);
438 [v.x, v.y]
439 }
440 }
441
442 #[doc(alias = "ShowDebugLogWindow")]
450 pub fn show_debug_log_window(&self, opened: &mut bool) {
451 unsafe {
452 sys::igShowDebugLogWindow(opened);
453 }
454 }
455
456 #[doc(alias = "ShowIDStackToolWindow")]
460 pub fn show_id_stack_tool_window(&self, opened: &mut bool) {
461 unsafe {
462 sys::igShowIDStackToolWindow(opened);
463 }
464 }
465
466 #[doc(alias = "ShowStyleSelector")]
470 pub fn show_style_selector(&self, label: impl AsRef<str>) -> bool {
471 unsafe { sys::igShowStyleSelector(self.scratch_txt(label)) }
472 }
473
474 #[doc(alias = "ShowFontSelector")]
476 pub fn show_font_selector(&self, label: impl AsRef<str>) {
477 unsafe {
478 sys::igShowFontSelector(self.scratch_txt(label));
479 }
480 }
481
482 #[doc(alias = "GetVersion")]
484 pub fn get_version(&self) -> &str {
485 unsafe {
486 let version_ptr = sys::igGetVersion();
487 let c_str = std::ffi::CStr::from_ptr(version_ptr);
488 c_str.to_str().unwrap_or("Unknown")
489 }
490 }
491}