Skip to main content

dear_imgui_rs/ui/
window.rs

1use super::*;
2
3impl Ui {
4    /// Creates a window builder
5    pub fn window<'ui>(
6        &'ui self,
7        name: impl Into<std::borrow::Cow<'ui, str>>,
8    ) -> crate::window::Window<'ui> {
9        crate::window::Window::new(self, name)
10    }
11
12    /// Focus a window by name, or clear focus from all windows.
13    ///
14    /// Passing `None` is equivalent to `ImGui::SetWindowFocus(NULL)` in the C++ API.
15    /// This can be used to "unfocus" the entire UI (e.g. on Escape, to behave like
16    /// clicking outside of the UI).
17    #[doc(alias = "SetWindowFocus")]
18    pub fn set_window_focus(&self, name: Option<&str>) {
19        unsafe {
20            match name {
21                Some(name) => sys::igSetWindowFocus_Str(self.scratch_txt(name)),
22                None => sys::igSetWindowFocus_Nil(),
23            }
24        }
25    }
26
27    /// Sets the position of the current window.
28    #[doc(alias = "SetWindowPos")]
29    pub fn set_window_pos(&self, pos: [f32; 2]) {
30        self.set_window_pos_with_cond(pos, crate::Condition::Always);
31    }
32
33    /// Sets the position of the current window with a condition.
34    #[doc(alias = "SetWindowPos")]
35    pub fn set_window_pos_with_cond(&self, pos: [f32; 2], cond: crate::Condition) {
36        Self::assert_finite_vec2("Ui::set_window_pos_with_cond()", "position", pos);
37        let pos_vec = sys::ImVec2_c {
38            x: pos[0],
39            y: pos[1],
40        };
41        unsafe { sys::igSetWindowPos_Vec2(pos_vec, cond as sys::ImGuiCond) }
42    }
43
44    /// Sets the position of a named window.
45    #[doc(alias = "SetWindowPos")]
46    pub fn set_window_pos_by_name(&self, name: impl AsRef<str>, pos: [f32; 2]) {
47        self.set_window_pos_by_name_with_cond(name, pos, crate::Condition::Always);
48    }
49
50    /// Sets the position of a named window with a condition.
51    #[doc(alias = "SetWindowPos")]
52    pub fn set_window_pos_by_name_with_cond(
53        &self,
54        name: impl AsRef<str>,
55        pos: [f32; 2],
56        cond: crate::Condition,
57    ) {
58        Self::assert_finite_vec2("Ui::set_window_pos_by_name_with_cond()", "position", pos);
59        let pos_vec = sys::ImVec2_c {
60            x: pos[0],
61            y: pos[1],
62        };
63        unsafe { sys::igSetWindowPos_Str(self.scratch_txt(name), pos_vec, cond as sys::ImGuiCond) }
64    }
65
66    /// Sets the size of the current window.
67    #[doc(alias = "SetWindowSize")]
68    pub fn set_window_size(&self, size: [f32; 2]) {
69        self.set_window_size_with_cond(size, crate::Condition::Always);
70    }
71
72    /// Sets the size of the current window with a condition.
73    #[doc(alias = "SetWindowSize")]
74    pub fn set_window_size_with_cond(&self, size: [f32; 2], cond: crate::Condition) {
75        Self::assert_finite_vec2("Ui::set_window_size_with_cond()", "size", size);
76        let size_vec = sys::ImVec2_c {
77            x: size[0],
78            y: size[1],
79        };
80        unsafe { sys::igSetWindowSize_Vec2(size_vec, cond as sys::ImGuiCond) }
81    }
82
83    /// Sets the size of a named window.
84    #[doc(alias = "SetWindowSize")]
85    pub fn set_window_size_by_name(&self, name: impl AsRef<str>, size: [f32; 2]) {
86        self.set_window_size_by_name_with_cond(name, size, crate::Condition::Always);
87    }
88
89    /// Sets the size of a named window with a condition.
90    #[doc(alias = "SetWindowSize")]
91    pub fn set_window_size_by_name_with_cond(
92        &self,
93        name: impl AsRef<str>,
94        size: [f32; 2],
95        cond: crate::Condition,
96    ) {
97        Self::assert_finite_vec2("Ui::set_window_size_by_name_with_cond()", "size", size);
98        let size_vec = sys::ImVec2_c {
99            x: size[0],
100            y: size[1],
101        };
102        unsafe {
103            sys::igSetWindowSize_Str(self.scratch_txt(name), size_vec, cond as sys::ImGuiCond);
104        }
105    }
106
107    /// Collapses or expands the current window.
108    #[doc(alias = "SetWindowCollapsed")]
109    pub fn set_window_collapsed(&self, collapsed: bool) {
110        self.set_window_collapsed_with_cond(collapsed, crate::Condition::Always);
111    }
112
113    /// Collapses or expands the current window with a condition.
114    #[doc(alias = "SetWindowCollapsed")]
115    pub fn set_window_collapsed_with_cond(&self, collapsed: bool, cond: crate::Condition) {
116        unsafe { sys::igSetWindowCollapsed_Bool(collapsed, cond as sys::ImGuiCond) }
117    }
118
119    /// Collapses or expands a named window.
120    #[doc(alias = "SetWindowCollapsed")]
121    pub fn set_window_collapsed_by_name(&self, name: impl AsRef<str>, collapsed: bool) {
122        self.set_window_collapsed_by_name_with_cond(name, collapsed, crate::Condition::Always);
123    }
124
125    /// Collapses or expands a named window with a condition.
126    #[doc(alias = "SetWindowCollapsed")]
127    pub fn set_window_collapsed_by_name_with_cond(
128        &self,
129        name: impl AsRef<str>,
130        collapsed: bool,
131        cond: crate::Condition,
132    ) {
133        unsafe {
134            sys::igSetWindowCollapsed_Str(
135                self.scratch_txt(name),
136                collapsed,
137                cond as sys::ImGuiCond,
138            );
139        }
140    }
141
142    /// Returns DPI scale currently associated to the current window's viewport.
143    #[doc(alias = "GetWindowDpiScale")]
144    pub fn window_dpi_scale(&self) -> f32 {
145        unsafe { sys::igGetWindowDpiScale() }
146    }
147
148    /// Get current window width (shortcut for `GetWindowSize().x`).
149    #[doc(alias = "GetWindowWidth")]
150    pub fn window_width(&self) -> f32 {
151        unsafe { sys::igGetWindowWidth() }
152    }
153
154    /// Get current window height (shortcut for `GetWindowSize().y`).
155    #[doc(alias = "GetWindowHeight")]
156    pub fn window_height(&self) -> f32 {
157        unsafe { sys::igGetWindowHeight() }
158    }
159
160    /// Get current window position in screen space.
161    #[doc(alias = "GetWindowPos")]
162    pub fn window_pos(&self) -> [f32; 2] {
163        let v = unsafe { sys::igGetWindowPos() };
164        [v.x, v.y]
165    }
166
167    /// Get current window size.
168    #[doc(alias = "GetWindowSize")]
169    pub fn window_size(&self) -> [f32; 2] {
170        let v = unsafe { sys::igGetWindowSize() };
171        [v.x, v.y]
172    }
173}