1use crate::draw::DrawListMut;
16use crate::input::MouseCursor;
17use crate::internal::RawWrapper;
18use crate::string::UiBuffer;
19use crate::sys;
20use crate::texture::TextureRef;
21use std::cell::UnsafeCell;
22
23#[derive(Debug)]
25pub struct Ui {
26 buffer: UnsafeCell<UiBuffer>,
28}
29
30impl Ui {
31 #[doc(alias = "GetMainViewport")]
35 pub fn main_viewport(&self) -> &'static crate::platform_io::Viewport {
36 crate::platform_io::Viewport::main()
37 }
38 pub(crate) fn new() -> Self {
42 Ui {
43 buffer: UnsafeCell::new(UiBuffer::new(1024)),
44 }
45 }
46
47 #[doc(alias = "GetIO")]
49 pub fn io(&self) -> &crate::io::Io {
50 unsafe { &*(sys::igGetIO_Nil() as *const crate::io::Io) }
51 }
52
53 pub(crate) fn scratch_txt(&self, txt: impl AsRef<str>) -> *const std::os::raw::c_char {
55 unsafe {
56 let handle = &mut *self.buffer.get();
57 handle.scratch_txt(txt)
58 }
59 }
60
61 pub(crate) fn scratch_txt_opt(
63 &self,
64 txt: Option<impl AsRef<str>>,
65 ) -> *const std::os::raw::c_char {
66 unsafe {
67 let handle = &mut *self.buffer.get();
68 handle.scratch_txt_opt(txt)
69 }
70 }
71
72 pub(crate) fn scratch_txt_two(
74 &self,
75 txt_0: impl AsRef<str>,
76 txt_1: impl AsRef<str>,
77 ) -> (*const std::os::raw::c_char, *const std::os::raw::c_char) {
78 unsafe {
79 let handle = &mut *self.buffer.get();
80 handle.scratch_txt_two(txt_0, txt_1)
81 }
82 }
83
84 pub(crate) fn scratch_txt_with_opt(
86 &self,
87 txt_0: impl AsRef<str>,
88 txt_1: Option<impl AsRef<str>>,
89 ) -> (*const std::os::raw::c_char, *const std::os::raw::c_char) {
90 unsafe {
91 let handle = &mut *self.buffer.get();
92 handle.scratch_txt_with_opt(txt_0, txt_1)
93 }
94 }
95
96 pub(crate) fn scratch_buffer(&self) -> &UnsafeCell<UiBuffer> {
98 &self.buffer
99 }
100
101 #[doc(alias = "TextUnformatted")]
103 pub fn text<T: AsRef<str>>(&self, text: T) {
104 let s = text.as_ref();
105 unsafe {
106 let start = s.as_ptr();
107 let end = start.add(s.len());
108 crate::sys::igTextUnformatted(
109 start as *const std::os::raw::c_char,
110 end as *const std::os::raw::c_char,
111 );
112 }
113 }
114
115 #[doc(alias = "GetWindowDrawList")]
117 pub fn get_window_draw_list(&self) -> DrawListMut<'_> {
118 DrawListMut::window(self)
119 }
120
121 #[doc(alias = "GetBackgroundDrawList")]
123 pub fn get_background_draw_list(&self) -> DrawListMut<'_> {
124 DrawListMut::background(self)
125 }
126
127 #[doc(alias = "GetForegroundDrawList")]
129 pub fn get_foreground_draw_list(&self) -> DrawListMut<'_> {
130 DrawListMut::foreground(self)
131 }
132
133 pub fn window(&self, name: impl Into<String>) -> crate::window::Window<'_> {
135 crate::window::Window::new(self, name)
136 }
137
138 #[doc(alias = "ShowDemoWindow")]
141 pub fn show_demo_window(&self, opened: &mut bool) {
142 unsafe {
143 crate::sys::igShowDemoWindow(opened);
144 }
145 }
146
147 #[doc(alias = "ImageWithBg")]
151 pub fn image_with_bg(
152 &self,
153 texture: impl Into<TextureRef>,
154 size: [f32; 2],
155 bg_color: [f32; 4],
156 tint_color: [f32; 4],
157 ) {
158 crate::widget::image::Image::new(self, texture, size).build_with_bg(bg_color, tint_color)
159 }
160
161 #[doc(alias = "ShowAboutWindow")]
165 pub fn show_about_window(&self, opened: &mut bool) {
166 unsafe {
167 crate::sys::igShowAboutWindow(opened);
168 }
169 }
170
171 #[doc(alias = "ShowMetricsWindow")]
176 pub fn show_metrics_window(&self, opened: &mut bool) {
177 unsafe {
178 crate::sys::igShowMetricsWindow(opened);
179 }
180 }
181
182 #[doc(alias = "ShowStyleEditor")]
184 pub fn show_style_editor(&self, style: &mut crate::style::Style) {
185 unsafe {
186 crate::sys::igShowStyleEditor(style.raw_mut());
187 }
188 }
189
190 #[doc(alias = "ShowStyleEditor")]
192 pub fn show_default_style_editor(&self) {
193 unsafe {
194 crate::sys::igShowStyleEditor(std::ptr::null_mut());
195 }
196 }
197
198 #[doc(alias = "ShowUserGuide")]
200 pub fn show_user_guide(&self) {
201 unsafe {
202 crate::sys::igShowUserGuide();
203 }
204 }
205
206 #[doc(alias = "DragFloat")]
210 pub fn drag_float(&self, label: impl AsRef<str>, value: &mut f32) -> bool {
211 crate::widget::drag::Drag::new(label).build(self, value)
212 }
213
214 #[doc(alias = "DragFloat")]
216 pub fn drag_float_config<L: AsRef<str>>(&self, label: L) -> crate::widget::drag::Drag<f32, L> {
217 crate::widget::drag::Drag::new(label)
218 }
219
220 #[doc(alias = "DragInt")]
222 pub fn drag_int(&self, label: impl AsRef<str>, value: &mut i32) -> bool {
223 crate::widget::drag::Drag::new(label).build(self, value)
224 }
225
226 #[doc(alias = "DragInt")]
228 pub fn drag_int_config<L: AsRef<str>>(&self, label: L) -> crate::widget::drag::Drag<i32, L> {
229 crate::widget::drag::Drag::new(label)
230 }
231
232 #[doc(alias = "DragFloatRange2")]
234 pub fn drag_float_range2(&self, label: impl AsRef<str>, min: &mut f32, max: &mut f32) -> bool {
235 crate::widget::drag::DragRange::<f32, _>::new(label).build(self, min, max)
236 }
237
238 #[doc(alias = "DragFloatRange2")]
240 pub fn drag_float_range2_config<L: AsRef<str>>(
241 &self,
242 label: L,
243 ) -> crate::widget::drag::DragRange<f32, L> {
244 crate::widget::drag::DragRange::new(label)
245 }
246
247 #[doc(alias = "DragIntRange2")]
249 pub fn drag_int_range2(&self, label: impl AsRef<str>, min: &mut i32, max: &mut i32) -> bool {
250 crate::widget::drag::DragRange::<i32, _>::new(label).build(self, min, max)
251 }
252
253 #[doc(alias = "DragIntRange2")]
255 pub fn drag_int_range2_config<L: AsRef<str>>(
256 &self,
257 label: L,
258 ) -> crate::widget::drag::DragRange<i32, L> {
259 crate::widget::drag::DragRange::new(label)
260 }
261
262 #[doc(alias = "GetMouseCursor")]
266 pub fn mouse_cursor(&self) -> Option<MouseCursor> {
267 unsafe {
268 match sys::igGetMouseCursor() {
269 sys::ImGuiMouseCursor_Arrow => Some(MouseCursor::Arrow),
270 sys::ImGuiMouseCursor_TextInput => Some(MouseCursor::TextInput),
271 sys::ImGuiMouseCursor_ResizeAll => Some(MouseCursor::ResizeAll),
272 sys::ImGuiMouseCursor_ResizeNS => Some(MouseCursor::ResizeNS),
273 sys::ImGuiMouseCursor_ResizeEW => Some(MouseCursor::ResizeEW),
274 sys::ImGuiMouseCursor_ResizeNESW => Some(MouseCursor::ResizeNESW),
275 sys::ImGuiMouseCursor_ResizeNWSE => Some(MouseCursor::ResizeNWSE),
276 sys::ImGuiMouseCursor_Hand => Some(MouseCursor::Hand),
277 sys::ImGuiMouseCursor_NotAllowed => Some(MouseCursor::NotAllowed),
278 _ => None,
279 }
280 }
281 }
282
283 #[doc(alias = "SetMouseCursor")]
287 pub fn set_mouse_cursor(&self, cursor_type: Option<MouseCursor>) {
288 unsafe {
289 let val: sys::ImGuiMouseCursor = cursor_type
290 .map(|x| x as sys::ImGuiMouseCursor)
291 .unwrap_or(sys::ImGuiMouseCursor_None);
292 sys::igSetMouseCursor(val);
293 }
294 }
295
296 #[doc(alias = "SetKeyboardFocusHere")]
305 pub fn set_keyboard_focus_here(&self) {
306 self.set_keyboard_focus_here_with_offset(0);
307 }
308
309 #[doc(alias = "SetKeyboardFocusHere")]
313 pub fn set_keyboard_focus_here_with_offset(&self, offset: i32) {
314 unsafe {
315 sys::igSetKeyboardFocusHere(offset);
316 }
317 }
318
319 #[doc(alias = "SetNextItemOpen")]
323 pub fn set_next_item_open(&self, is_open: bool) {
324 unsafe {
325 sys::igSetNextItemOpen(is_open, 0); }
327 }
328
329 #[doc(alias = "SetNextItemOpen")]
331 pub fn set_next_item_open_with_cond(&self, is_open: bool, cond: crate::Condition) {
332 unsafe { sys::igSetNextItemOpen(is_open, cond as sys::ImGuiCond) }
333 }
334
335 #[doc(alias = "SetNextItemWidth")]
339 pub fn set_next_item_width(&self, item_width: f32) {
340 unsafe {
341 sys::igSetNextItemWidth(item_width);
342 }
343 }
344
345 #[doc(alias = "GetStyle")]
361 pub unsafe fn style(&self) -> &crate::Style {
362 unsafe {
363 &*(sys::igGetStyle() as *const crate::Style)
365 }
366 }
367
368 #[doc(alias = "GetStyle")]
372 pub fn clone_style(&self) -> crate::Style {
373 unsafe { self.style().clone() }
374 }
375
376 #[doc(alias = "StyleColorsDark")]
378 pub fn style_colors_dark(&self) {
379 unsafe { sys::igStyleColorsDark(std::ptr::null_mut()) }
380 }
381
382 #[doc(alias = "StyleColorsLight")]
384 pub fn style_colors_light(&self) {
385 unsafe { sys::igStyleColorsLight(std::ptr::null_mut()) }
386 }
387
388 #[doc(alias = "StyleColorsClassic")]
390 pub fn style_colors_classic(&self) {
391 unsafe { sys::igStyleColorsClassic(std::ptr::null_mut()) }
392 }
393
394 #[doc(alias = "StyleColorsDark")]
396 pub fn style_colors_dark_into(&self, dst: &mut crate::Style) {
397 unsafe { sys::igStyleColorsDark(dst as *mut _ as *mut sys::ImGuiStyle) }
398 }
399
400 #[doc(alias = "StyleColorsLight")]
402 pub fn style_colors_light_into(&self, dst: &mut crate::Style) {
403 unsafe { sys::igStyleColorsLight(dst as *mut _ as *mut sys::ImGuiStyle) }
404 }
405
406 #[doc(alias = "StyleColorsClassic")]
408 pub fn style_colors_classic_into(&self, dst: &mut crate::Style) {
409 unsafe { sys::igStyleColorsClassic(dst as *mut _ as *mut sys::ImGuiStyle) }
410 }
411
412 #[doc(alias = "GetWindowDpiScale")]
414 pub fn window_dpi_scale(&self) -> f32 {
415 unsafe { sys::igGetWindowDpiScale() }
416 }
417
418 #[doc(alias = "Value")]
420 pub fn value_bool(&self, prefix: impl AsRef<str>, v: bool) {
421 unsafe { sys::igValue_Bool(self.scratch_txt(prefix), v) }
422 }
423
424 #[doc(alias = "GetWindowWidth")]
426 pub fn window_width(&self) -> f32 {
427 unsafe { sys::igGetWindowWidth() }
428 }
429
430 #[doc(alias = "GetWindowHeight")]
432 pub fn window_height(&self) -> f32 {
433 unsafe { sys::igGetWindowHeight() }
434 }
435
436 #[doc(alias = "GetWindowPos")]
438 pub fn window_pos(&self) -> [f32; 2] {
439 unsafe {
440 let mut v = sys::ImVec2 { x: 0.0, y: 0.0 };
441 sys::igGetWindowPos(&mut v);
442 [v.x, v.y]
443 }
444 }
445
446 #[doc(alias = "GetWindowSize")]
448 pub fn window_size(&self) -> [f32; 2] {
449 unsafe {
450 let mut v = sys::ImVec2 { x: 0.0, y: 0.0 };
451 sys::igGetWindowSize(&mut v);
452 [v.x, v.y]
453 }
454 }
455
456 #[doc(alias = "ShowDebugLogWindow")]
464 pub fn show_debug_log_window(&self, opened: &mut bool) {
465 unsafe {
466 sys::igShowDebugLogWindow(opened);
467 }
468 }
469
470 #[doc(alias = "ShowIDStackToolWindow")]
474 pub fn show_id_stack_tool_window(&self, opened: &mut bool) {
475 unsafe {
476 sys::igShowIDStackToolWindow(opened);
477 }
478 }
479
480 #[doc(alias = "ShowStyleSelector")]
484 pub fn show_style_selector(&self, label: impl AsRef<str>) -> bool {
485 unsafe { sys::igShowStyleSelector(self.scratch_txt(label)) }
486 }
487
488 #[doc(alias = "ShowFontSelector")]
490 pub fn show_font_selector(&self, label: impl AsRef<str>) {
491 unsafe {
492 sys::igShowFontSelector(self.scratch_txt(label));
493 }
494 }
495
496 #[doc(alias = "GetVersion")]
498 pub fn get_version(&self) -> &str {
499 unsafe {
500 let version_ptr = sys::igGetVersion();
501 let c_str = std::ffi::CStr::from_ptr(version_ptr);
502 c_str.to_str().unwrap_or("Unknown")
503 }
504 }
505}