Skip to content

Instantly share code, notes, and snippets.

@killercup
Created July 8, 2016 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save killercup/65e37b5ccb80af9a12b2fa97e78ce56b to your computer and use it in GitHub Desktop.
Save killercup/65e37b5ccb80af9a12b2fa97e78ce56b to your computer and use it in GitHub Desktop.
Syntax experiment for a macro for defining UIs with libui-rs
//! An example control gallery: a port of the same `ui` example.
extern crate ui;
use ui::{BoxControl, Button, Checkbox, ColorButton, Combobox, DateTimePicker, Entry};
use ui::{FontButton, Group, InitOptions, Label, Menu, MenuItem, ProgressBar, RadioButtons};
use ui::{Separator, Slider, Spinbox, Tab, Window};
fn run() {
menu![
("File" {} [
("Open" {onClick => open_clicked}),
("Save" {onClick => save_clicked}),
]),
("Edit" {} [
("Checkable Item" {checkable}),
(---),
("Disabled Item" {disabled}),
(preferences),
]),
("Help" {}, [
("Help"),
(about),
]),
];
window!("ui Control Gallery" {
w => 640, h => 480, menubar, margined,
onClosing => |_| {
ui::quit();
false
},
} [
(BoxControl {vertical, padded} [
(Group "Basic Controls" {margined} [
(Button "Button"),
(Checkbox "Checkbox"),
(Entry "Entry" {text => "Lorem ipsum"}),
(label "label"),
(---),
(DatePicker),
(TimePicker),
(DateTimePicker),
(FontButton),
(ColorButton),
]),
]),
(BoxControl {vertical, padded} [
(Group "Numbers" {margined} [
(BoxControl {padded} [
(Spinbox {min => 0, max => 100,
onChange => |spinbox| update(spinbox.value()),
}),
]),
(BoxControl {padded} [
(Slider {min => 0, max => 100,
onChange => |slider| update(slider.value()),
}),
]),
(ProgressBar),
]),
(Group "Lists" {margined} [
(BoxControl {padded} [
(Combobox {} [
"Combobox Item 1",
"Combobox Item 2",
"Combobox Item 3",
]),
(Combobox {editable} [
"Editable Item 1",
"Editable Item 2",
"Editable Item 3",
]),
(RadioButtons {} [
"Radio Button 1",
"Radio Button 2",
"Radio Button 3",
]),
]),
]),
(Tab {} [
("Page 1" {} [(BoxControl),]),
("Page 2" {} [(BoxControl),]),
("Page 3" {} [(BoxControl),]),
]),
]),
]);
mainwin.show();
ui::main();
}
pub fn main() {
ui::init(InitOptions).unwrap();
run();
ui::uninit();
}
fn open_clicked(_: &MenuItem, mainwin: &Window) {
match ui::open_file(mainwin) {
Some(filename) => ui::msg_box(mainwin, "File selected", &*filename),
None => ui::msg_box_error(mainwin, "No file selected", "Don't be alarmed!"),
}
}
fn save_clicked(_: &MenuItem, mainwin: &Window) {
match ui::open_file(mainwin) {
Some(filename) => {
ui::msg_box(mainwin, "File selected (don't worry, it's still there)", &*filename)
}
None => ui::msg_box_error(mainwin, "No file selected", "Don't be alarmed!"),
}
}
fn update(_: i64) {
// TODO(pcwalton)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment