1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// SPDX-License-Identifier: MPL-2.0
//! An application window.
use std::ptr;
use crate::prelude::*;
impl Ui {
/// Creates a new [`Window`].
pub fn create_window(
&self,
title: impl Into<Vec<u8>>,
width: impl Into<NonNegativeInt>,
height: impl Into<NonNegativeInt>,
has_menubar: bool,
should_quit_on_close: bool,
) -> Result<&mut Window, crate::Error> {
let title = self.make_cstring(title)?;
let window = unsafe {
call_libui_new_fn!(
ui: self,
fn: uiNewWindow(
title.as_ptr(),
width.into().to_libui(),
height.into().to_libui(),
has_menubar.into(),
) -> Window,
)?
};
if should_quit_on_close {
unsafe extern "C" fn on_closing(_: *mut uiWindow, _: *mut c_void) -> c_int {
// When the window receives an event to close, call `uiQuit`.
uiQuit();
// Returning `true` tells *libui-ng* to destroy the window, which isn't necessary in
// this case as `uiQuit` accomplishes that.
false.into()
}
unsafe extern "C" fn on_should_quit(_: *mut c_void) -> c_int {
true.into()
}
unsafe {
let window = window.as_ptr();
uiWindowOnClosing(window, Some(on_closing), ptr::null_mut());
uiOnShouldQuit(Some(on_should_quit), ptr::null_mut());
}
} else {
unsafe extern "C" fn on_closing(window: *mut uiWindow, _: *mut c_void) -> c_int {
// SAFETY: we can't return `true` here, as that would cause the window to be
// destroyed, invalidating the window's inner pointer and allowing use-after-frees
// to occur. Instead, we will simply hide the window. This is kind of a silly
// solution, but it works.
//
// FIXME: this appears visually equivalent to closing the window on Windows. We
// should test macOS and Linux as well to confirm that the window doesn't instantly
// disappear rather than display a closing animation on those platforms.
uiControlHide(window.cast());
false.into()
}
unsafe {
uiWindowOnClosing(window.as_ptr(), Some(on_closing), ptr::null_mut());
}
}
Ok(window)
}
}
/// An application window.
#[subcontrol(handle = "uiWindow")]
pub struct Window;
impl<'ui> Window<'ui> {
/// The title of this window.
#[bind_text(fn = "uiWindowTitle")]
pub fn title(&self) -> _;
/// Sets the title of the window.
///
/// This overrides the title supplied to [`Ui::create_window`].
pub fn set_title(&self, title: impl Into<Vec<u8>>) -> Result<(), crate::Error> {
let title = self.ui.make_cstring(title)?;
unsafe { uiWindowSetTitle(self.as_ptr(), title.as_ptr()) };
Ok(())
}
/// Sets a callback for when the content size of this window changes.
///
/// This callback is unset by default.
#[bind_callback(fn = "uiWindowOnContentSizeChanged")]
pub fn on_content_size_changed(&self, f: fn()) {
f();
}
/// Sets a callback for when this window is requested to close.
///
/// This overrides the default behavior of the `should_quit_on_close` argument passed to
/// [`Ui::create_window`]. As such, if you still want the window to quit or hide after your
/// callback is executed, you must manually call [`uiQuit`] or [`uiControlHide`] in your
/// callback.
#[bind_callback(fn = "uiWindowOnClosing", return = "c_int")]
pub fn on_closing(&self, f: fn()) {
f();
// SAFETY: here, we return a value to [`uiWindowOnClosing`] (but not the outer function)
// that indicates if *libui-ng* should destroy the window when the callback exits. This is
// never desirable, so we will return `false`.
break c_int::from(false);
}
/// Sets a callback for when this window changes focus.
///
/// This callback is unset by default.
#[bind_callback(fn = "uiWindowOnFocusChanged")]
pub fn on_focus_changed(&self, f: fn()) {
f();
}
/// Determines if this window is fullscreen.
///
/// Windows are not fullscreen by default.
#[inline]
pub fn is_fullscreen(&self) -> bool {
bool_from_libui(unsafe { uiWindowFullscreen(self.as_ptr()) })
}
/// Sets whether or not this window is fullscreen.
#[inline]
pub fn set_fullscreen(&self, value: bool) {
unsafe { uiWindowSetFullscreen(self.as_ptr(), value.into()) };
}
/// Determines if this window is borderless.
#[inline]
pub fn is_borderless(&self) -> bool {
bool_from_libui(unsafe { uiWindowBorderless(self.as_ptr()) })
}
/// Sets whether or not this window is borderless.
#[inline]
pub fn set_borderless(&self, value: bool) {
unsafe { uiWindowSetBorderless(self.as_ptr(), value.into()) };
}
/// Sets the child control of this window.
///
/// This is unset by default.
#[bind_set_child(fn = "uiWindowSetChild")]
pub fn set_child(&self, ...) -> _;
/// Determines if this window has margins.
#[inline]
pub fn is_margined(&self) -> bool {
bool_from_libui(unsafe { uiWindowMargined(self.as_ptr()) })
}
/// Sets whether or not this window has margins.
#[inline]
pub fn set_margined(&self, value: bool) {
unsafe { uiWindowSetMargined(self.as_ptr(), value.into()) };
}
/// Determines if this window is resizable.
///
/// Windows are resizable by default.
#[inline]
pub fn is_resizeable(&self) -> bool {
bool_from_libui(unsafe { uiWindowResizeable(self.as_ptr()) })
}
/// Sets whether or not this window is resizable.
#[inline]
pub fn set_resizeable(&self, value: bool) {
unsafe { uiWindowSetResizeable(self.as_ptr(), value.into()) };
}
/// Sets the inner size of this window.
pub fn set_content_size(
&self,
width: impl Into<NonNegativeInt>,
height: impl Into<NonNegativeInt>,
) {
unsafe {
uiWindowSetContentSize(
self.as_ptr(),
width.into().to_libui(),
height.into().to_libui(),
)
};
}
/// The inner size of this window.
pub fn content_size(&self) -> (NonNegativeInt, NonNegativeInt) {
let (mut width, mut height) = (0, 0);
unsafe {
uiWindowContentSize(
self.as_ptr(),
ptr::addr_of_mut!(width),
ptr::addr_of_mut!(height),
);
}
unsafe { (NonNegativeInt::from_libui(width), NonNegativeInt::from_libui(height)) }
}
}
macro_rules! impl_present_fn {
(
$self_fn:ident,
$libui_fn:ident $(,)?
) => {
impl Window<'_> {
pub fn $self_fn(
&self,
title: impl Into<Vec<u8>>,
desc: impl Into<Vec<u8>>,
) -> Result<(), crate::Error> {
let title = self.ui.make_cstring(title)?;
let desc = self.ui.make_cstring(desc)?;
unsafe { $libui_fn(self.as_ptr(), title.as_ptr(), desc.as_ptr()) };
Ok(())
}
}
};
}
impl_present_fn!(present_alert, uiMsgBox);
impl_present_fn!(present_error, uiMsgBoxError);