Skip to content

Commit 49571d5

Browse files
committed
Add documentation and examples
1 parent e121ccf commit 49571d5

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

ui/examples/basic.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
extern crate ui;
2+
use std::rc::Rc;
3+
use ui::{Window, BoxControl, Button};
4+
5+
fn main() {
6+
// Start up the UI toolkit
7+
ui::init(ui::InitOptions);
8+
9+
// Create a new window, 200x100, titled "Test Window"
10+
// and put it in an Rc so it can be passed into callback functions.
11+
let main_window = Rc::new(Window::new("Test App", 200, 100, true));
12+
13+
// Add margins around the edge of the window, making it look much nicer.
14+
main_window.set_margined(true);
15+
16+
// Adding this callback means that when this window closes, the `ui::main` function returns.
17+
// This should be added to the primary window of any application.
18+
main_window.on_closing(Box::new(|_| {
19+
ui::quit();
20+
false
21+
}));
22+
23+
// Create a button that opens a dialog box.
24+
let button = Button::new("Button");
25+
{
26+
// Make a new Rc reference to the main window for this closure.
27+
let main_window = main_window.clone();
28+
// on_clicked runs the given closure when the button is clicked.
29+
// A lot of widgets provide this event, or others like it.
30+
button.on_clicked(Box::new(move |_| {
31+
// msg_box creates a modal dialog with the given title and text
32+
ui::msg_box(&main_window, "Button", "You clicked the button!");
33+
}));
34+
}
35+
36+
// 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+
// Add a box to lay out controls vertically.
41+
let vbox = BoxControl::new_vertical();
42+
vbox.set_padded(true);
43+
// Put the buttons into the vertical layout.
44+
vbox.append(button.into(), false);
45+
vbox.append(quit_button.into(), false);
46+
// Put the vertical layout into the window.
47+
main_window.set_child(vbox.clone().into());
48+
49+
// Set the main window (and all its widgets) to visible.
50+
main_window.show();
51+
52+
// Run the app.
53+
ui::main();
54+
55+
// Clean up.
56+
ui::uninit();
57+
}

ui/examples/controlgallery.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ fn run() {
1010
let menu = Menu::new("File");
1111
menu.append_item("Open").on_clicked(Box::new(open_clicked));
1212
menu.append_item("Save").on_clicked(Box::new(save_clicked));
13+
menu.append_separator();
14+
menu.append_quit_item();
1315

1416
let menu = Menu::new("Edit");
1517
menu.append_check_item("Checkable Item");

ui/src/lib.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,65 @@
33
//! Main C source repository: https://github.com/andlabs/libui
44
//!
55
//! Copyright © 2016 Mozilla Foundation
6+
//!
7+
//! # Example
8+
//!
9+
//! ```
10+
//! extern crate ui;
11+
//! use std::rc::Rc;
12+
//! use ui::{Window, BoxControl, Button};
13+
//!
14+
//! fn main() {
15+
//! // Start up the UI toolkit
16+
//! ui::init(ui::InitOptions);
17+
//!
18+
//! // Create a new window, 200x100, titled "Test Window"
19+
//! // and put it in an Rc so it can be passed into callback functions.
20+
//! let main_window = Rc::new(Window::new("Test App", 200, 100, true));
21+
//!
22+
//! // Add margins around the edge of the window, making it look much nicer.
23+
//! 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+
//!
33+
//! // Create a button that opens a dialog box.
34+
//! 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+
//!
39+
//! // 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+
//!
43+
//! // Add a box to lay out controls vertically.
44+
//! let vbox = BoxControl::new_vertical();
45+
//! vbox.set_padded(true);
46+
//! // Put the buttons into the vertical layout.
47+
//! vbox.append(button.into(), false);
48+
//! vbox.append(quit_button.into(), false);
49+
//! // Put the vertical layout into the window.
50+
//! main_window.set_child(vbox.clone().into());
51+
//!
52+
//! // Set the main window (and all its widgets) to visible.
53+
//! main_window.show();
54+
//!
55+
//! // ONLY for testing, quit the app as soon as it starts.
56+
//! ui::quit();
57+
//!
58+
//! // Run the app.
59+
//! ui::main();
60+
//!
61+
//! // Clean up.
62+
//! ui::uninit();
63+
//! }
64+
//! ```
665
766
#[macro_use]
867
extern crate bitflags;

ui/src/menus.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,31 @@ use windows::Window;
88

99
// NB: If there ever becomes a way to destroy menus and/or menu items, we'll need to reference
1010
// count these for memory safety.
11+
/// An item that goes in a [`Menu`](struct.Menu.html).
1112
#[derive(Clone)]
1213
pub struct MenuItem {
1314
ui_menu_item: *mut uiMenuItem,
1415
}
1516

1617
impl MenuItem {
1718
#[inline]
19+
/// Enables the item, allowing it to be selected. This is the default state of a menu item.
1820
pub fn enable(&self) {
1921
unsafe {
2022
ui_sys::uiMenuItemEnable(self.ui_menu_item)
2123
}
2224
}
2325

2426
#[inline]
27+
/// Disables the item, preventing it from being selected and providing a visual cue to the user that it cannot be selected.
2528
pub fn disable(&self) {
2629
unsafe {
2730
ui_sys::uiMenuItemDisable(self.ui_menu_item)
2831
}
2932
}
3033

3134
#[inline]
35+
/// Sets the function to be executed when the item is clicked/selected.
3236
pub fn on_clicked(&self, callback: Box<FnMut(&MenuItem, &Window)>) {
3337
unsafe {
3438
let mut data: Box<Box<FnMut(&MenuItem, &Window)>> = Box::new(callback);
@@ -55,13 +59,17 @@ impl MenuItem {
5559
}
5660

5761
#[inline]
62+
/// Returns `true` if the menu item is checked, and false if it is not checked (or not checkable).
5863
pub fn checked(&self) -> bool {
5964
unsafe {
6065
ui_sys::uiMenuItemChecked(self.ui_menu_item) != 0
6166
}
6267
}
6368

6469
#[inline]
70+
/// Sets the menu item to either checked or unchecked based on the given value.
71+
///
72+
/// Setting the checked value of a non-checkable menu item has no effect.
6573
pub fn set_checked(&self, checked: bool) {
6674
unsafe {
6775
ui_sys::uiMenuItemSetChecked(self.ui_menu_item, checked as c_int)
@@ -71,13 +79,15 @@ impl MenuItem {
7179

7280
// NB: If there ever becomes a way to destroy menus, we'll need to reference count these for memory
7381
// safety.
82+
/// A named menu, to be displayed at the top of the application window or in the titlebar.
7483
#[derive(Clone)]
7584
pub struct Menu {
7685
ui_menu: *mut uiMenu,
7786
}
7887

7988
impl Menu {
8089
#[inline]
90+
/// Adds a new item with the given name to the menu.
8191
pub fn append_item(&self, name: &str) -> MenuItem {
8292
unsafe {
8393
let c_string = CString::new(name.as_bytes().to_vec()).unwrap();
@@ -87,6 +97,7 @@ impl Menu {
8797
}
8898
}
8999

100+
/// Adds a new togglable (checkbox) item with the given name to the menu.
90101
#[inline]
91102
pub fn append_check_item(&self, name: &str) -> MenuItem {
92103
unsafe {
@@ -98,6 +109,7 @@ impl Menu {
98109
}
99110

100111
#[inline]
112+
/// Adds a new item to the menu with the lable "Quit".
101113
pub fn append_quit_item(&self) -> MenuItem {
102114
unsafe {
103115
MenuItem {
@@ -107,6 +119,7 @@ impl Menu {
107119
}
108120

109121
#[inline]
122+
/// Adds a new item to the menu with the label "Preferences...".
110123
pub fn append_preferences_item(&self) -> MenuItem {
111124
unsafe {
112125
MenuItem {
@@ -116,6 +129,7 @@ impl Menu {
116129
}
117130

118131
#[inline]
132+
/// Adds a new item to the menu with the label "About".
119133
pub fn append_about_item(&self) -> MenuItem {
120134
unsafe {
121135
MenuItem {
@@ -125,13 +139,15 @@ impl Menu {
125139
}
126140

127141
#[inline]
142+
/// Adds a seperator to the menu, which will create a line in the menu.
128143
pub fn append_separator(&self) {
129144
unsafe {
130145
ui_sys::uiMenuAppendSeparator(self.ui_menu)
131146
}
132147
}
133148

134149
#[inline]
150+
/// Creates a new menu with the given name to be displayed in the menubar at the top of the window.
135151
pub fn new(name: &str) -> Menu {
136152
unsafe {
137153
let c_string = CString::new(name.as_bytes().to_vec()).unwrap();

ui/src/ui.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use windows::Window;
1313
pub struct InitOptions;
1414

1515
#[inline]
16+
/// Sets up the `libui` environment. Run this prior to constructing your UI.
1617
pub fn init(_: InitOptions) -> Result<(),InitError> {
1718
unsafe {
1819
let mut init_options = uiInitOptions {
@@ -31,6 +32,7 @@ pub fn init(_: InitOptions) -> Result<(),InitError> {
3132
}
3233

3334
#[inline]
35+
/// Undoes the work of [init](fn.init.html). Calling this will free all the memory used by the UI toolkit.
3436
pub fn uninit() {
3537
unsafe {
3638
ffi_utils::unset_initialized();
@@ -40,13 +42,15 @@ pub fn uninit() {
4042
}
4143

4244
#[inline]
45+
/// Hands control of this thread to the UI toolkit, allowing it to display the UI and respond to events. Does not return until the UI [quit](fn.quit.html)s.
4346
pub fn main() {
4447
unsafe {
4548
ui_sys::uiMain()
4649
}
4750
}
4851

4952
#[inline]
53+
/// Running this function causes the UI to quit, exiting from [main](fn.main.html) and no longer showing any widgets.
5054
pub fn quit() {
5155
unsafe {
5256
ui_sys::uiQuit()
@@ -91,6 +95,7 @@ pub fn queue_main(callback: Box<FnMut()>) {
9195
}
9296

9397
#[inline]
98+
/// Set a callback to be run when the application quits.
9499
pub fn on_should_quit(callback: Box<FnMut()>) {
95100
unsafe {
96101
let mut data: Box<Box<FnMut()>> = Box::new(callback);
@@ -101,20 +106,23 @@ pub fn on_should_quit(callback: Box<FnMut()>) {
101106
}
102107

103108
#[inline]
109+
/// Allow the user to select an existing file.
104110
pub fn open_file(parent: &Window) -> Option<Text> {
105111
unsafe {
106112
Text::optional(ui_sys::uiOpenFile(parent.as_ui_window()))
107113
}
108114
}
109115

110116
#[inline]
117+
/// Allow the user to select a new or existing file.
111118
pub fn save_file(parent: &Window) -> Option<Text> {
112119
unsafe {
113120
Text::optional(ui_sys::uiSaveFile(parent.as_ui_window()))
114121
}
115122
}
116123

117124
#[inline]
125+
/// Open a generic message box to show a message to the user.
118126
pub fn msg_box(parent: &Window, title: &str, description: &str) {
119127
unsafe {
120128
let c_title = CString::new(title.as_bytes().to_vec()).unwrap();
@@ -124,6 +132,7 @@ pub fn msg_box(parent: &Window, title: &str, description: &str) {
124132
}
125133

126134
#[inline]
135+
/// Open an error-themed message box to show a message to the user.
127136
pub fn msg_box_error(parent: &Window, title: &str, description: &str) {
128137
unsafe {
129138
let c_title = CString::new(title.as_bytes().to_vec()).unwrap();

0 commit comments

Comments
 (0)