Skip to content

Commit de605da

Browse files
committed
Fixups for ui crate and ui controlgallery example
1 parent 1d1ae67 commit de605da

File tree

5 files changed

+100
-62
lines changed

5 files changed

+100
-62
lines changed

ui-sys/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ extern {
2323
pub fn uiUninit();
2424
pub fn uiFreeInitError(err: *const c_char);
2525

26-
pub fn uiMainSteps();
27-
pub fn uiMainStep(wait: c_int) -> c_int;
2826
pub fn uiMain();
2927
pub fn uiMainStep(wait: c_int) -> c_int;
3028
pub fn uiMainSteps();

ui/examples/controlgallery.rs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,98 +28,99 @@ fn run(ui: Rc<UI>) {
2828
mainwin.set_margined(true);
2929
{
3030
let ui = ui.clone();
31-
mainwin.on_closing(Box::new(move |_| {
31+
mainwin.on_closing(move |_| {
3232
ui.quit();
3333
false
34-
}));
34+
});
3535
}
3636

3737
let vbox = BoxControl::new_vertical();
3838
vbox.set_padded(true);
39-
mainwin.set_child(vbox.clone().into());
39+
mainwin.set_child(vbox.deref().clone());
4040

4141
let hbox = BoxControl::new_horizontal();
4242
hbox.set_padded(true);
43-
vbox.append(hbox.clone().into(), true);
43+
use std::ops::Deref;
44+
vbox.append(hbox.deref().clone());
4445

4546
let group = Group::new("Basic Controls");
4647
group.set_margined(true);
47-
hbox.append(group.clone().into(), false);
48+
hbox.append(group.deref().clone());
4849

4950
let inner = BoxControl::new_vertical();
5051
inner.set_padded(true);
51-
group.set_child(inner.clone().into());
52+
group.set_child(inner.deref().clone());
5253

53-
inner.append(Button::new("Button").into(), false);
54-
inner.append(Checkbox::new("Checkbox").into(), false);
54+
inner.append(Button::new("Button"));
55+
inner.append(Checkbox::new("Checkbox"));
5556
let entry = Entry::new();
5657
entry.set_text("Entry");
57-
inner.append(entry.into(), false);
58-
inner.append(Label::new("Label").into(), false);
59-
inner.append(Separator::new_horizontal().into(), false);
58+
inner.append(entry);
59+
inner.append(Label::new("Label"));
60+
inner.append(Separator::new_horizontal());
6061

61-
inner.append(DateTimePicker::new_date_picker().into(), false);
62-
inner.append(DateTimePicker::new_time_picker().into(), false);
63-
inner.append(DateTimePicker::new_date_time_picker().into(), false);
62+
inner.append(DateTimePicker::new_date_picker());
63+
inner.append(DateTimePicker::new_time_picker());
64+
inner.append(DateTimePicker::new_date_time_picker());
6465

65-
inner.append(FontButton::new().into(), false);
66-
inner.append(ColorButton::new().into(), false);
66+
inner.append(FontButton::new());
67+
inner.append(ColorButton::new());
6768

6869
let inner2 = BoxControl::new_vertical();
6970
inner2.set_padded(true);
70-
hbox.append(inner2.clone().into(), true);
71+
hbox.append(inner2.deref().clone());
7172

7273
let group = Group::new("Numbers");
7374
group.set_margined(true);
74-
inner2.append(group.clone().into(), false);
75+
inner2.append(group.deref().clone());
7576

7677
let inner = BoxControl::new_vertical();
7778
inner.set_padded(true);
78-
group.set_child(inner.clone().into());
79+
group.set_child(inner.deref().clone());
7980

8081
let spinbox = Spinbox::new(0, 100);
81-
spinbox.on_changed(Box::new(|spinbox| update(spinbox.value())));
82-
inner.append(spinbox.into(), false);
82+
spinbox.on_changed(|spinbox| update(spinbox.value()));
83+
inner.append(spinbox);
8384

8485
let slider = Slider::new(0, 100);
85-
slider.on_changed(Box::new(|slider| update(slider.value())));
86-
inner.append(slider.into(), false);
86+
slider.on_changed(|slider| update(slider.value()));
87+
inner.append(slider);
8788

8889
let progress_bar = ProgressBar::new();
89-
inner.append(progress_bar.into(), false);
90+
inner.append(progress_bar);
9091

9192
let group = Group::new("Lists");
9293
group.set_margined(true);
93-
inner2.append(group.clone().into(), false);
94+
inner2.append(group.deref().clone());
9495

9596
let inner = BoxControl::new_vertical();
9697
inner.set_padded(true);
97-
group.set_child(inner.clone().into());
98+
group.set_child(inner.deref().clone());
9899

99100
let cbox = Combobox::new();
100101
cbox.append("Combobox Item 1");
101102
cbox.append("Combobox Item 2");
102103
cbox.append("Combobox Item 3");
103-
inner.append(cbox.into(), false);
104+
inner.append(cbox);
104105

105106
let cbox = EditableCombobox::new();
106107
cbox.append("Editable Item 1");
107108
cbox.append("Editable Item 2");
108109
cbox.append("Editable Item 3");
109-
inner.append(cbox.into(), false);
110+
inner.append(cbox);
110111

111112

112113
let rb = RadioButtons::new();
113114
rb.append("Radio Button 1");
114115
rb.append("Radio Button 2");
115116
rb.append("Radio Button 3");
116-
inner.append(rb.into(), true);
117+
inner.append(rb);
117118

118119
let tab = Tab::new();
119-
tab.append("Page 1", BoxControl::new_horizontal().into());
120-
tab.append("Page 2", BoxControl::new_horizontal().into());
121-
tab.append("Page 3", BoxControl::new_horizontal().into());
122-
inner2.append(tab.into(), true);
120+
tab.append("Page 1", BoxControl::new_horizontal());
121+
tab.append("Page 2", BoxControl::new_horizontal());
122+
tab.append("Page 3", BoxControl::new_horizontal());
123+
inner2.append(tab);
123124

124125
mainwin.show();
125126
ui.main();

ui/src/draw.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ pub use ui_sys::uiDrawLineJoin as LineJoin;
1717
pub use ui_sys::uiDrawDefaultMiterLimit as DEFAULT_MITER_LIMIT;
1818
pub use ui_sys::uiDrawFillMode as FillMode;
1919

20-
use image;
2120

2221
pub struct Context {
2322
ui_draw_context: *mut uiDrawContext,
@@ -90,21 +89,6 @@ impl Context {
9089
unsafe { ui_sys::uiDrawText(self.ui_draw_context, x, y, layout.as_ui_draw_text_layout()) }
9190
}
9291

93-
#[inline]
94-
pub fn draw_image(&self, x: f64, y: f64, width: f64, height: f64, image: &mut image::Image) {
95-
ffi_utils::ensure_initialized();
96-
unsafe {
97-
ui_sys::uiImageAppend(
98-
image.ui_image,
99-
image.data.as_mut_ptr() as *mut c_void,
100-
image.width as i32,
101-
image.height as i32,
102-
image.width as i32 * 4,
103-
);
104-
ui_sys::uiDrawImage(self.ui_draw_context, x, y, width, height, image.ui_image);
105-
}
106-
}
107-
10892
pub fn draw_image(&self, x: f64, y: f64, img: &image::Image) {
10993
unsafe {
11094
ui_sys::uiDrawPixmapImage(self.ui_draw_context, x, y, img.as_ui_draw_image())

ui/src/lib.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,9 @@ extern crate bitflags;
8686
extern crate libc;
8787
extern crate ui_sys;
8888

89-
pub use controls::{Area, AreaDrawParams, AreaKeyEvent, AreaHandler, ExtKey, BoxControl, Button, Checkbox, ColorButton};
90-
pub use controls::{Combobox, Control, DateTimePicker, Entry, FontButton, Group, Label};
91-
pub use controls::{MultilineEntry, ProgressBar, RadioButtons, Separator, Slider, Spinbox, Tab};
9289
pub use ffi_utils::Text;
93-
pub use menus::{Menu, MenuItem};
94-
pub use ui::{InitError, InitOptions, init, main_steps, main_step, main, msg_box, msg_box_error, on_should_quit};
95-
pub use ui::{open_file, queue_main, quit, save_file, uninit};
96-
pub use windows::Window;
90+
pub use ui::{InitError, UI};
91+
pub use window::Window;
9792
pub use image::Image;
9893

9994
#[macro_use]
@@ -103,6 +98,5 @@ pub mod ffi_utils;
10398
pub mod menus;
10499
pub mod prelude;
105100
mod ui;
106-
mod windows;
107-
mod image;
108-
101+
pub mod window;
102+
mod image;

ui/src/windows.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ thread_local! {
1212
static WINDOWS: RefCell<Vec<Window>> = RefCell::new(Vec::new())
1313
}
1414

15-
define_control!(Window, uiWindow, ui_window);
15+
define_control_alt!(Window, uiWindow, ui_window);
1616

1717
impl Window {
1818
#[inline]
@@ -125,3 +125,64 @@ impl Window {
125125
}
126126
}
127127

128+
// Defines a new control, creating a Rust wrapper, a `Deref` implementation, and a destructor.
129+
// An example of use:
130+
//
131+
// define_control!(Slider, uiSlider, ui_slider)
132+
#[macro_export]
133+
macro_rules! define_control_alt {
134+
($rust_type:ident, $ui_type:ident, $ui_field:ident) => {
135+
pub struct $rust_type {
136+
$ui_field: *mut $ui_type,
137+
}
138+
139+
impl ::std::ops::Deref for $rust_type {
140+
type Target = ::controls::Control;
141+
142+
#[inline]
143+
fn deref(&self) -> &::controls::Control {
144+
// FIXME(pcwalton): $10 says this is undefined behavior. How do I make it not so?
145+
unsafe {
146+
mem::transmute::<&$rust_type, &::controls::Control>(self)
147+
}
148+
}
149+
}
150+
151+
impl Drop for $rust_type {
152+
#[inline]
153+
fn drop(&mut self) {
154+
// For now this does nothing, but in the future, when `libui` supports proper
155+
// memory management, this will likely need to twiddle reference counts.
156+
}
157+
}
158+
159+
impl Clone for $rust_type {
160+
#[inline]
161+
fn clone(&self) -> $rust_type {
162+
$rust_type {
163+
$ui_field: self.$ui_field,
164+
}
165+
}
166+
}
167+
168+
impl Into<Control> for $rust_type {
169+
#[inline]
170+
fn into(self) -> Control {
171+
unsafe {
172+
let control = Control::from_ui_control(self.$ui_field as *mut uiControl);
173+
mem::forget(self);
174+
control
175+
}
176+
}
177+
}
178+
179+
impl $rust_type {
180+
#[inline]
181+
pub unsafe fn from_ui_control($ui_field: *mut $ui_type) -> $rust_type {
182+
$rust_type {
183+
$ui_field: $ui_field
184+
}
185+
}
186+
}
187+
}
188+
}

0 commit comments

Comments
 (0)