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
/// Defines a new control, creating a Rust wrapper, a `Deref` implementation, and a destructor.
/// An example of use:
/// ```ignore
/// define_control!{
/// /// Some documentation
/// #[attribute(whatever="something")]
/// rust_type: Slider,
/// sys_type: uiSlider,
/// }
/// ```
macro_rules! define_control {
// Match first any attributes (incl. doc comments) and then the actual invocation
{$(#[$attr:meta])* rust_type: $rust_type:ident, sys_type: $sys_type:ident$(,)* } => {
#[allow(non_snake_case)]
// Include all attributes
$(#[$attr])*
pub struct $rust_type {
$sys_type: *mut $sys_type,
}
impl Drop for $rust_type {
fn drop(&mut self) {
// For now this does nothing, but in the future, when `libui` supports proper
// memory management, this will likely need to twiddle reference counts.
}
}
impl Clone for $rust_type {
fn clone(&self) -> $rust_type {
$rust_type {
$sys_type: self.$sys_type,
}
}
}
impl Into<Control> for $rust_type {
fn into(self) -> Control {
unsafe {
let control = Control::from_ui_control(self.$sys_type as *mut uiControl);
mem::forget(self);
control
}
}
}
impl $rust_type {
// Show this control to the user. This will also show its non-hidden children.
pub fn show(&mut self) {
let control: Control = self.clone().into();
unsafe { libui_ffi::uiControlShow(control.ui_control) }
}
// Hide this control from the user. This will hide its children.
pub fn hide(&mut self) {
let control: Control = self.clone().into();
unsafe { libui_ffi::uiControlHide(control.ui_control) }
}
// Enable this control.
pub fn enable(&mut self) {
let control: Control = self.clone().into();
unsafe { libui_ffi::uiControlEnable(control.ui_control) }
}
// Disable this control.
pub fn disable(&mut self) {
let control: Control = self.clone().into();
unsafe { libui_ffi::uiControlDisable(control.ui_control) }
}
/// Create an `ui` struct for this control from the raw pointer for it.
///
/// # Unsafety
/// The given pointer must point to a valid control or memory unsafety may result.
#[allow(non_snake_case)]
#[allow(unused)]
pub unsafe fn from_raw($sys_type: *mut $sys_type) -> $rust_type {
$rust_type {
$sys_type: $sys_type
}
}
/// Return the underlying pointer for this control.
#[allow(non_snake_case)]
pub fn ptr(&self) -> *mut $sys_type {
self.$sys_type
}
}
}
}