Skip to content

Commit d7f069f

Browse files
committed
Initial API reorganization.
Adds a UI struct that acts like a token for initialization (which lets us auto-deinit with impl Drop). Also moves some functions requiring &Window to methods on Window, becuase that's how Rust works.
1 parent 32ce32a commit d7f069f

File tree

6 files changed

+215
-173
lines changed

6 files changed

+215
-173
lines changed

ui/examples/basic.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
extern crate ui;
22
use std::rc::Rc;
3-
use ui::{Window, BoxControl, Button};
3+
use ui::{UI, Window, BoxControl, Button};
44

55
fn main() {
66
// Start up the UI toolkit
7-
ui::init(ui::InitOptions);
7+
let ui = Rc::new(UI::init().unwrap());
88

99
// Create a new window, 200x100, titled "Test Window"
1010
// and put it in an Rc so it can be passed into callback functions.
@@ -15,10 +15,13 @@ fn main() {
1515

1616
// Adding this callback means that when this window closes, the `ui::main` function returns.
1717
// This should be added to the primary window of any application.
18-
main_window.on_closing(Box::new(|_| {
19-
ui::quit();
20-
false
21-
}));
18+
{
19+
let ui = ui.clone();
20+
main_window.on_closing(Box::new(move |_| {
21+
ui.quit();
22+
false
23+
}));
24+
}
2225

2326
// Create a button that opens a dialog box.
2427
let button = Button::new("Button");
@@ -29,14 +32,16 @@ fn main() {
2932
// A lot of widgets provide this event, or others like it.
3033
button.on_clicked(Box::new(move |_| {
3134
// msg_box creates a modal dialog with the given title and text
32-
ui::msg_box(&main_window, "Button", "You clicked the button!");
35+
main_window.msg_box("Button", "You clicked the button!");
3336
}));
3437
}
3538

3639
// Create a button that quits the app.
37-
let mut quit_button = Button::new("Quit");
38-
quit_button.on_clicked(Box::new(|_| { ui::quit(); }));
39-
40+
let quit_button = Button::new("Quit");
41+
{
42+
let ui = ui.clone();
43+
quit_button.on_clicked(Box::new(move |_| { ui.quit(); }));
44+
}
4045
// Add a box to lay out controls vertically.
4146
let vbox = BoxControl::new_vertical();
4247
vbox.set_padded(true);
@@ -50,8 +55,5 @@ fn main() {
5055
main_window.show();
5156

5257
// Run the app.
53-
ui::main();
54-
55-
// Clean up.
56-
ui::uninit();
58+
ui.main();
5759
}

ui/examples/controlgallery.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
33
extern crate ui;
44

5-
use ui::{BoxControl, Button, Checkbox, ColorButton, Combobox, EditableCombobox, DateTimePicker,
6-
Entry};
7-
use ui::{FontButton, Group, InitOptions, Label, Menu, MenuItem, ProgressBar, RadioButtons};
8-
use ui::{Separator, Slider, Spinbox, Tab, Window};
5+
use std::rc::Rc;
96

10-
fn run() {
7+
use ui::{UI, BoxControl, Button, Checkbox, ColorButton, Combobox, EditableCombobox, DateTimePicker,
8+
Entry, FontButton, Group, Label, Menu, MenuItem, ProgressBar, RadioButtons, Separator, Slider, Spinbox, Tab, Window};
9+
10+
fn run(ui: Rc<UI>) {
1111
let menu = Menu::new("File");
1212
menu.append_item("Open").on_clicked(Box::new(open_clicked));
1313
menu.append_item("Save").on_clicked(Box::new(save_clicked));
@@ -27,10 +27,13 @@ fn run() {
2727

2828
let mainwin = Window::new("ui Control Gallery", 640, 480, true);
2929
mainwin.set_margined(true);
30-
mainwin.on_closing(Box::new(|_| {
31-
ui::quit();
32-
false
33-
}));
30+
{
31+
let ui = ui.clone();
32+
mainwin.on_closing(Box::new(move |_| {
33+
ui.quit();
34+
false
35+
}));
36+
}
3437

3538
let vbox = BoxControl::new_vertical();
3639
vbox.set_padded(true);
@@ -120,32 +123,30 @@ fn run() {
120123
inner2.append(tab.into(), true);
121124

122125
mainwin.show();
123-
ui::main();
126+
ui.main();
124127
}
125128

126129
pub fn main() {
127-
ui::init(InitOptions).unwrap();
128-
run();
129-
ui::uninit();
130+
let ui = Rc::new(UI::init().unwrap());
131+
run(ui.clone());
130132
}
131133

132134
fn open_clicked(_: &MenuItem, mainwin: &Window) {
133-
match ui::open_file(mainwin) {
134-
Some(filename) => ui::msg_box(mainwin, "File selected", &*filename),
135-
None => ui::msg_box_error(mainwin, "No file selected", "Don't be alarmed!"),
135+
match mainwin.open_file() {
136+
Some(filename) => mainwin.msg_box("File selected", &*filename),
137+
None => mainwin.msg_box_error("No file selected", "Don't be alarmed!"),
136138
}
137139
}
138140

139141
fn save_clicked(_: &MenuItem, mainwin: &Window) {
140-
match ui::open_file(mainwin) {
142+
match mainwin.open_file() {
141143
Some(filename) => {
142-
ui::msg_box(
143-
mainwin,
144+
mainwin.msg_box(
144145
"File selected (don't worry, it's still there)",
145146
&*filename,
146147
)
147148
}
148-
None => ui::msg_box_error(mainwin, "No file selected", "Don't be alarmed!"),
149+
None => mainwin.msg_box_error("No file selected", "Don't be alarmed!"),
149150
}
150151
}
151152

ui/examples/rustlogo.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
55
extern crate ui;
66

7-
use ui::{Area, AreaDrawParams, AreaHandler, InitOptions, Window};
7+
use std::rc::Rc;
8+
9+
use ui::{UI, Area, AreaDrawParams, AreaHandler, Window};
810
use ui::draw::{Brush, FillMode, Path, SolidBrush};
911

1012
struct RustLogoArea;
@@ -617,24 +619,28 @@ impl AreaHandler for RustLogoArea {
617619
a: 255.0,
618620
});
619621
area_draw_params.context.fill(&path, &brush);
622+
area.show();
623+
// FIXME (leotindall): This doesn't actually draw anything?
620624
}
621625
}
622626

623-
fn run() {
627+
fn run(ui: Rc<UI>) {
624628
let window = Window::new("Rust logo", 640, 480, true);
625-
window.on_closing(Box::new(|_| {
626-
ui::quit();
627-
false
628-
}));
629+
{
630+
let ui = ui.clone();
631+
window.on_closing(Box::new(move |_| {
632+
ui.quit();
633+
false
634+
}));
635+
}
629636

630637
let area = Area::new(Box::new(RustLogoArea));
631638
window.set_child(area.into());
632639
window.show();
633-
ui::main();
640+
ui.main();
634641
}
635642

636643
fn main() {
637-
ui::init(InitOptions).unwrap();
638-
run();
639-
ui::uninit();
644+
let ui = Rc::new(UI::init().unwrap());
645+
run(ui);
640646
}

ui/src/lib.rs

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,48 @@
99
//! ```
1010
//! extern crate ui;
1111
//! use std::rc::Rc;
12-
//! use ui::{Window, BoxControl, Button};
13-
//!
12+
//! use ui::{UI, Window, BoxControl, Button};
13+
//!
1414
//! fn main() {
1515
//! // Start up the UI toolkit
16-
//! ui::init(ui::InitOptions);
17-
//!
16+
//! let ui = Rc::new(UI::init().unwrap());
17+
//!
1818
//! // Create a new window, 200x100, titled "Test Window"
1919
//! // and put it in an Rc so it can be passed into callback functions.
2020
//! let main_window = Rc::new(Window::new("Test App", 200, 100, true));
21-
//!
21+
//!
2222
//! // Add margins around the edge of the window, making it look much nicer.
2323
//! main_window.set_margined(true);
24-
//!
25-
//! // Adding this callback means that when this window closes,
26-
//! // the `ui::main` function returns. This should be added to the
27-
//! // primary window of any application.
28-
//! main_window.on_closing(Box::new(|_| {
29-
//! ui::quit();
30-
//! false
31-
//! }));
32-
//!
24+
//!
25+
//! // Adding this callback means that when this window closes, the `ui::main` function returns.
26+
//! // This should be added to the primary window of any application.
27+
//! {
28+
//! let ui = ui.clone();
29+
//! main_window.on_closing(Box::new(move |_| {
30+
//! ui.quit();
31+
//! false
32+
//! }));
33+
//! }
34+
//!
3335
//! // Create a button that opens a dialog box.
3436
//! let button = Button::new("Button");
35-
//! // on_clicked runs the given closure when the button is clicked.
36-
//! // A lot of widgets provide this event, or others like it.
37-
//! button.on_clicked(Box::new(|_| { println!("Clicked!"); }));
38-
//!
37+
//! {
38+
//! // Make a new Rc reference to the main window for this closure.
39+
//! let main_window = main_window.clone();
40+
//! // on_clicked runs the given closure when the button is clicked.
41+
//! // A lot of widgets provide this event, or others like it.
42+
//! button.on_clicked(Box::new(move |_| {
43+
//! // msg_box creates a modal dialog with the given title and text
44+
//! main_window.msg_box("Button", "You clicked the button!");
45+
//! }));
46+
//! }
47+
//!
3948
//! // Create a button that quits the app.
40-
//! let mut quit_button = Button::new("Quit");
41-
//! quit_button.on_clicked(Box::new(|_| { ui::quit(); }));
42-
//!
49+
//! let quit_button = Button::new("Quit");
50+
//! {
51+
//! let ui = ui.clone();
52+
//! quit_button.on_clicked(Box::new(move |_| { ui.quit(); }));
53+
//! }
4354
//! // Add a box to lay out controls vertically.
4455
//! let vbox = BoxControl::new_vertical();
4556
//! vbox.set_padded(true);
@@ -48,18 +59,16 @@
4859
//! vbox.append(quit_button.into(), false);
4960
//! // Put the vertical layout into the window.
5061
//! main_window.set_child(vbox.clone().into());
51-
//!
62+
//!
5263
//! // Set the main window (and all its widgets) to visible.
5364
//! main_window.show();
54-
//!
55-
//! // ONLY for testing, quit the app as soon as it starts.
56-
//! ui::quit();
57-
//!
65+
//!
66+
//! // Just for testing, quit the app before it even starts.
67+
//! // Otherwise the test would never end!
68+
//! ui.quit();
69+
//!
5870
//! // Run the app.
59-
//! ui::main();
60-
//!
61-
//! // Clean up.
62-
//! ui::uninit();
71+
//! ui.main();
6372
//! }
6473
//! ```
6574
@@ -75,8 +84,7 @@ pub use controls::{Combobox, EditableCombobox, Control, DateTimePicker, Entry, F
7584
pub use controls::{MultilineEntry, ProgressBar, RadioButtons, Separator, Slider, Spinbox, Tab};
7685
pub use ffi_utils::Text;
7786
pub use menus::{Menu, MenuItem};
78-
pub use ui::{InitError, InitOptions, init, main, msg_box, msg_box_error, on_should_quit};
79-
pub use ui::{open_file, queue_main, quit, save_file, uninit};
87+
pub use ui::{InitError, UI};
8088
pub use windows::Window;
8189
pub use image::Image;
8290

0 commit comments

Comments
 (0)