|
1 |
| -# The Improved User Interface Crate |
| 1 | +# Improved User Interface |
2 | 2 | [](https://travis-ci.org/LeoTindall/libui-rs/)
|
| 3 | +[](https://crates.io/crates/iui/) |
| 4 | +[](https://isitmaintained.com/project/LeoTindall/libui-rs) |
| 5 | +[](https://isitmaintained.com/project/LeoTindall/libui-rs) |
| 6 | + |
3 | 7 |
|
4 | 8 | `iui` is a simple, small, easy to distribute GUI library, a Rusty user interface library that binds to platform native APIs.
|
5 |
| -These are work-in-progress bindings to the minimalistic native UI library [libui][libui]. |
| 9 | +These are work-in-progress bindings to the minimalistic native UI library [libui][libui] via the `ui-sys` bindings crate. |
6 | 10 |
|
7 |
| -Add this to your crate with: |
| 11 | +Add `iui` to your project with: |
8 | 12 |
|
| 13 | +```toml |
| 14 | +iui = "0.2" |
9 | 15 | ```
|
10 |
| -iui = "0.1.0" |
11 |
| -``` |
| 16 | + |
| 17 | +## Organization |
| 18 | + |
| 19 | +`iui` is the safe Rust wrapper, to be used by most users. |
| 20 | +`ui` is the old version of the safe wrapper. Don't use this. |
| 21 | +`ui-sys` is the raw unsafe bindings to the `libui` C code. Requires `cmake` so it can build `libui`. |
| 22 | +`libui` is included as a submodule. You will need CMake to build `libui` itself. |
| 23 | + |
| 24 | +Based on work by [@pcwalton](https://github.com/pcwalton/). Licensed MIT. |
12 | 25 |
|
13 | 26 | ## Example
|
14 | 27 |
|
15 | 28 | ```
|
16 | 29 | extern crate iui;
|
17 | 30 | use iui::prelude::*;
|
18 |
| -use iui::controls::{VerticalBox, MultilineEntry, Button}; |
19 |
| -use std::io::prelude::*; |
20 |
| -use std::error::Error; |
21 |
| -use std::fs::File; |
| 31 | +use iui::controls::{Label, Button, VerticalBox, Group}; |
22 | 32 |
|
23 | 33 | fn main() {
|
24 |
| - // Initialize the UI |
25 |
| - let ui = UI::init().unwrap(); |
26 |
| -
|
27 |
| - // Create the input controls |
28 |
| - let entry = MultilineEntry::new(&ui); |
29 |
| - let button = Button::new(&ui, "Save Buffer"); |
30 |
| -
|
31 |
| - // Set up the application's layout |
32 |
| - let window = Window::new(&ui, "Save Buffer to File", 640, 480, WindowType::NoMenubar); |
33 |
| - let vbox = VerticalBox::new(&ui); |
34 |
| - vbox.append(&ui, entry.clone(), LayoutStrategy::Stretchy); |
35 |
| - vbox.append(&ui, button.clone(), LayoutStrategy::Compact); |
36 |
| - window.set_child(&ui, vbox); |
37 |
| - window.show(&ui); |
38 |
| -
|
39 |
| - // When the button is clicked, get the name of a file and then write the entry's contents to it. |
40 |
| - // Note the in real code you should spin off a thread to do the actual writing, do it between UI events, |
41 |
| - // or use Tokio. Even with minmal content, this method shows noticable lag. |
| 34 | + // Initialize the UI library |
| 35 | + let ui = UI::init().expect("Couldn't initialize UI library"); |
| 36 | + // Create a window into which controls can be placed |
| 37 | + let mut win = Window::new(&ui, "Test App", 200, 200, WindowType::NoMenubar); |
| 38 | + |
| 39 | + // Create a vertical layout to hold the controls |
| 40 | + let mut vbox = VerticalBox::new(&ui); |
| 41 | + vbox.set_padded(&ui, true); |
| 42 | +
|
| 43 | + let mut group_vbox = VerticalBox::new(&ui); |
| 44 | + let mut group = Group::new(&ui, "Group"); |
| 45 | +
|
| 46 | + // Create two buttons to place in the window |
| 47 | + let mut button = Button::new(&ui, "Button"); |
42 | 48 | button.on_clicked(&ui, {
|
| 49 | + let ui = ui.clone(); |
| 50 | + move |btn| { |
| 51 | + btn.set_text(&ui, "Clicked!"); |
| 52 | + } |
| 53 | + }); |
| 54 | +
|
| 55 | + let mut quit_button = Button::new(&ui, "Quit"); |
| 56 | + quit_button.on_clicked(&ui, { |
43 | 57 | let ui = ui.clone();
|
44 | 58 | move |_| {
|
45 |
| - if let Some(path) = window.save_file(&ui) { |
46 |
| - let mut file = match File::create(&path) { |
47 |
| - Err(why) => { window.modal_err(&ui, "I/O Error", &format!("Could not open file {}: {}", path.display(), why.description())); return; } |
48 |
| - Ok(f) => f |
49 |
| - }; |
50 |
| - match file.write_all(entry.value(&ui).as_bytes()) { |
51 |
| - Err(why) => { window.modal_err(&ui, "I/O Error", &format!("Could not write to file {}: {}", path.display(), why.description())); return; } |
52 |
| - Ok(_) => () |
53 |
| - }; |
54 |
| - } |
| 59 | + ui.quit(); |
55 | 60 | }
|
56 | 61 | });
|
57 | 62 |
|
| 63 | + // Create a new label. Note that labels don't auto-wrap! |
| 64 | + let mut label_text = String::new(); |
| 65 | + label_text.push_str("There is a ton of text in this label.\n"); |
| 66 | + label_text.push_str("Pretty much every unicode character is supported.\n"); |
| 67 | + label_text.push_str("🎉 用户界面 사용자 인터페이스"); |
| 68 | + let label = Label::new(&ui, &label_text); |
| 69 | +
|
| 70 | + vbox.append(&ui, label, LayoutStrategy::Stretchy); |
| 71 | + group_vbox.append(&ui, button, LayoutStrategy::Compact); |
| 72 | + group_vbox.append(&ui, quit_button, LayoutStrategy::Compact); |
| 73 | + group.set_child(&ui, group_vbox); |
| 74 | + vbox.append(&ui, group, LayoutStrategy::Compact); |
| 75 | +
|
| 76 | + // Actually put the button in the window |
| 77 | + win.set_child(&ui, vbox); |
| 78 | + // Show the window |
| 79 | + win.show(&ui); |
| 80 | + // Run the application |
58 | 81 | ui.main();
|
59 | 82 | }
|
60 | 83 | ```
|
61 | 84 |
|
62 |
| -## Organization |
63 |
| - |
64 |
| -`iui` is the safe Rust wrapper, to be used by most users. |
65 |
| -`ui` is the old version of the safe wrapper. Don't use this. |
66 |
| -`ui-sys` is the raw unsafe bindings to the `libui` C code. Requires `cmake` so it can build `libui`. |
67 |
| - |
68 |
| -## Building |
69 |
| -`libui` is included as a submodule. You will need CMake to build `libui` itself. |
70 |
| - |
71 |
| -Based on work by @pcwalton. Licensed MIT. |
72 |
| - |
73 | 85 | ## Testing Note
|
74 | 86 | Travis does not connect video devices to their testing environments, so the tests cannot be run. Therefore, the "tests" only check compilation.
|
75 | 87 |
|
|
0 commit comments