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        let name = name.map(|name| self.scratch_txt(name));
20        self.run_with_bound_context(|| unsafe {
21            match name {
22                Some(name) => sys::igSetWindowFocus_Str(name),
23                None => sys::igSetWindowFocus_Nil(),
24            }
25        });
26    }
27
28    /// Sets the position of the current window.
29    #[doc(alias = "SetWindowPos")]
30    pub fn set_window_pos(&self, pos: [f32; 2]) {
31        self.set_window_pos_with_cond(pos, crate::Condition::Always);
32    }
33
34    /// Sets the position of the current window with a condition.
35    #[doc(alias = "SetWindowPos")]
36    pub fn set_window_pos_with_cond(&self, pos: [f32; 2], cond: crate::Condition) {
37        Self::assert_finite_vec2("Ui::set_window_pos_with_cond()", "position", pos);
38        let pos_vec = sys::ImVec2_c {
39            x: pos[0],
40            y: pos[1],
41        };
42        self.run_with_bound_context(|| unsafe {
43            sys::igSetWindowPos_Vec2(pos_vec, cond as sys::ImGuiCond)
44        });
45    }
46
47    /// Sets the position of a named window.
48    #[doc(alias = "SetWindowPos")]
49    pub fn set_window_pos_by_name(&self, name: impl AsRef<str>, pos: [f32; 2]) {
50        self.set_window_pos_by_name_with_cond(name, pos, crate::Condition::Always);
51    }
52
53    /// Sets the position of a named window with a condition.
54    #[doc(alias = "SetWindowPos")]
55    pub fn set_window_pos_by_name_with_cond(
56        &self,
57        name: impl AsRef<str>,
58        pos: [f32; 2],
59        cond: crate::Condition,
60    ) {
61        Self::assert_finite_vec2("Ui::set_window_pos_by_name_with_cond()", "position", pos);
62        let pos_vec = sys::ImVec2_c {
63            x: pos[0],
64            y: pos[1],
65        };
66        let name = self.scratch_txt(name);
67        self.run_with_bound_context(|| unsafe {
68            sys::igSetWindowPos_Str(name, pos_vec, cond as sys::ImGuiCond)
69        });
70    }
71
72    /// Sets the size of the current window.
73    #[doc(alias = "SetWindowSize")]
74    pub fn set_window_size(&self, size: [f32; 2]) {
75        self.set_window_size_with_cond(size, crate::Condition::Always);
76    }
77
78    /// Sets the size of the current window with a condition.
79    #[doc(alias = "SetWindowSize")]
80    pub fn set_window_size_with_cond(&self, size: [f32; 2], cond: crate::Condition) {
81        Self::assert_finite_vec2("Ui::set_window_size_with_cond()", "size", size);
82        let size_vec = sys::ImVec2_c {
83            x: size[0],
84            y: size[1],
85        };
86        self.run_with_bound_context(|| unsafe {
87            sys::igSetWindowSize_Vec2(size_vec, cond as sys::ImGuiCond)
88        });
89    }
90
91    /// Sets the size of a named window.
92    #[doc(alias = "SetWindowSize")]
93    pub fn set_window_size_by_name(&self, name: impl AsRef<str>, size: [f32; 2]) {
94        self.set_window_size_by_name_with_cond(name, size, crate::Condition::Always);
95    }
96
97    /// Sets the size of a named window with a condition.
98    #[doc(alias = "SetWindowSize")]
99    pub fn set_window_size_by_name_with_cond(
100        &self,
101        name: impl AsRef<str>,
102        size: [f32; 2],
103        cond: crate::Condition,
104    ) {
105        Self::assert_finite_vec2("Ui::set_window_size_by_name_with_cond()", "size", size);
106        let size_vec = sys::ImVec2_c {
107            x: size[0],
108            y: size[1],
109        };
110        let name = self.scratch_txt(name);
111        self.run_with_bound_context(|| unsafe {
112            sys::igSetWindowSize_Str(name, size_vec, cond as sys::ImGuiCond);
113        });
114    }
115
116    /// Collapses or expands the current window.
117    #[doc(alias = "SetWindowCollapsed")]
118    pub fn set_window_collapsed(&self, collapsed: bool) {
119        self.set_window_collapsed_with_cond(collapsed, crate::Condition::Always);
120    }
121
122    /// Collapses or expands the current window with a condition.
123    #[doc(alias = "SetWindowCollapsed")]
124    pub fn set_window_collapsed_with_cond(&self, collapsed: bool, cond: crate::Condition) {
125        self.run_with_bound_context(|| unsafe {
126            sys::igSetWindowCollapsed_Bool(collapsed, cond as sys::ImGuiCond)
127        });
128    }
129
130    /// Collapses or expands a named window.
131    #[doc(alias = "SetWindowCollapsed")]
132    pub fn set_window_collapsed_by_name(&self, name: impl AsRef<str>, collapsed: bool) {
133        self.set_window_collapsed_by_name_with_cond(name, collapsed, crate::Condition::Always);
134    }
135
136    /// Collapses or expands a named window with a condition.
137    #[doc(alias = "SetWindowCollapsed")]
138    pub fn set_window_collapsed_by_name_with_cond(
139        &self,
140        name: impl AsRef<str>,
141        collapsed: bool,
142        cond: crate::Condition,
143    ) {
144        let name = self.scratch_txt(name);
145        self.run_with_bound_context(|| unsafe {
146            sys::igSetWindowCollapsed_Str(name, collapsed, cond as sys::ImGuiCond);
147        });
148    }
149
150    /// Returns DPI scale currently associated to the current window's viewport.
151    #[doc(alias = "GetWindowDpiScale")]
152    pub fn window_dpi_scale(&self) -> f32 {
153        self.run_with_bound_context(|| unsafe { sys::igGetWindowDpiScale() })
154    }
155
156    /// Get current window width (shortcut for `GetWindowSize().x`).
157    #[doc(alias = "GetWindowWidth")]
158    pub fn window_width(&self) -> f32 {
159        self.run_with_bound_context(|| unsafe { sys::igGetWindowWidth() })
160    }
161
162    /// Get current window height (shortcut for `GetWindowSize().y`).
163    #[doc(alias = "GetWindowHeight")]
164    pub fn window_height(&self) -> f32 {
165        self.run_with_bound_context(|| unsafe { sys::igGetWindowHeight() })
166    }
167
168    /// Get current window position in screen space.
169    #[doc(alias = "GetWindowPos")]
170    pub fn window_pos(&self) -> [f32; 2] {
171        let v = self.run_with_bound_context(|| unsafe { sys::igGetWindowPos() });
172        [v.x, v.y]
173    }
174
175    /// Get current window size.
176    #[doc(alias = "GetWindowSize")]
177    pub fn window_size(&self) -> [f32; 2] {
178        let v = self.run_with_bound_context(|| unsafe { sys::igGetWindowSize() });
179        [v.x, v.y]
180    }
181}