[ui] add convenience functions

This commit is contained in:
Emil Ernerfeldt 2020-05-30 09:51:57 +02:00
parent 3de20d033e
commit 9f46d8f0be
4 changed files with 41 additions and 26 deletions

View File

@ -199,8 +199,8 @@ impl ExampleWindow {
)); ));
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.add_label("Project home page:"); ui.label("Project home page:");
ui.add_hyperlink("https://github.com/emilk/emigui/"); ui.hyperlink("https://github.com/emilk/emigui/");
}); });
}); });
@ -236,7 +236,7 @@ impl ExampleWindow {
.default_open(false) .default_open(false)
.show(ui, |ui| { .show(ui, |ui| {
ScrollArea::default().show(ui, |ui| { ScrollArea::default().show(ui, |ui| {
ui.add_label(LOREM_IPSUM); ui.label(LOREM_IPSUM);
}); });
}); });
@ -258,23 +258,23 @@ impl ExampleWindow {
}); });
ui.collapsing("Name clash example", |ui| { ui.collapsing("Name clash example", |ui| {
ui.add_label("\ ui.label("\
Widgets that store state require unique identifiers so we can track their state between frames. \ Widgets that store state require unique identifiers so we can track their state between frames. \
Identifiers are normally derived from the titles of the widget."); Identifiers are normally derived from the titles of the widget.");
ui.add_label("\ ui.label("\
For instance, collapsable headers needs to store wether or not they are open. \ For instance, collapsable headers needs to store wether or not they are open. \
If you fail to give them unique names then clicking one will open both. \ If you fail to give them unique names then clicking one will open both. \
To help you debug this, an error message is printed on screen:"); To help you debug this, an error message is printed on screen:");
ui.collapsing("Collapsing header", |ui| { ui.collapsing("Collapsing header", |ui| {
ui.add_label("Contents of first folddable ui"); ui.label("Contents of first folddable ui");
}); });
ui.collapsing("Collapsing header", |ui| { ui.collapsing("Collapsing header", |ui| {
ui.add_label("Contents of second folddable ui"); ui.label("Contents of second folddable ui");
}); });
ui.add_label("\ ui.label("\
Most widgets don't need unique names, but are tracked \ Most widgets don't need unique names, but are tracked \
based on their position on screen. For instance, buttons:"); based on their position on screen. For instance, buttons:");
ui.add(Button::new("Button")); ui.add(Button::new("Button"));
@ -423,7 +423,7 @@ struct Painting {
impl Painting { impl Painting {
pub fn ui(&mut self, ui: &mut Ui) { pub fn ui(&mut self, ui: &mut Ui) {
ui.add_label("Draw with your mouse to paint"); ui.label("Draw with your mouse to paint");
if ui.add(Button::new("Clear")).clicked { if ui.add(Button::new("Clear")).clicked {
self.lines.clear(); self.lines.clear();
} }
@ -599,7 +599,7 @@ impl Tree {
}) })
.collect(); .collect();
if ui.button("+") { if ui.button("+").clicked {
self.0.push(Tree::default()); self.0.push(Tree::default());
} }

View File

@ -52,5 +52,6 @@ pub use {
style::Style, style::Style,
types::*, types::*,
ui::Ui, ui::Ui,
widgets::Widget, widgets::*,
containers::*,
}; };

View File

@ -496,18 +496,32 @@ impl Ui {
// Convenience functions: // Convenience functions:
pub fn add_label(&mut self, text: impl Into<String>) -> GuiResponse { pub fn label(&mut self, label: impl Into<Label>) -> GuiResponse {
self.add(Label::new(text)) self.add(label.into())
} }
pub fn button(&mut self, text: impl Into<String>) -> bool { pub fn hyperlink(&mut self, url: impl Into<String>) -> GuiResponse {
self.add(Button::new(text)).clicked
}
pub fn add_hyperlink(&mut self, url: impl Into<String>) -> GuiResponse {
self.add(Hyperlink::new(url)) self.add(Hyperlink::new(url))
} }
pub fn button(&mut self, text: impl Into<String>) -> GuiResponse {
self.add(Button::new(text))
}
// TODO: argument order?
pub fn checkbox(&mut self, text: impl Into<String>, checked: &mut bool) -> GuiResponse {
self.add(Checkbox::new(checked, text))
}
// TODO: argument order?
pub fn radio(&mut self, text: impl Into<String>, checked: bool) -> GuiResponse {
self.add(RadioButton::new(checked, text))
}
pub fn separator(&mut self) -> GuiResponse {
self.add(Separator::new())
}
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// Addding Containers / Sub-uis: // Addding Containers / Sub-uis:

View File

@ -59,21 +59,21 @@ impl State {
let mut ui = ui.centered_column(ui.available().width().min(480.0)); let mut ui = ui.centered_column(ui.available().width().min(480.0));
ui.set_layout(Layout::vertical(Align::Min)); ui.set_layout(Layout::vertical(Align::Min));
ui.add(label!("Emigui!").text_style(TextStyle::Heading)); ui.add(label!("Emigui!").text_style(TextStyle::Heading));
ui.add_label("Emigui is an immediate mode GUI written in Rust, compiled to WebAssembly, rendered with WebGL."); ui.label("Emigui is an immediate mode GUI written in Rust, compiled to WebAssembly, rendered with WebGL.");
ui.add_label( ui.label(
"Everything you see is rendered as textured triangles. There is no DOM. There are no HTML elements." "Everything you see is rendered as textured triangles. There is no DOM. There are no HTML elements."
); );
ui.add_label("This is not JavaScript. This is Rust, running at 60 FPS. This is the web page, reinvented with game tech."); ui.label("This is not JavaScript. This is Rust, running at 60 FPS. This is the web page, reinvented with game tech.");
ui.add_label("This is also work in progress, and not ready for production... yet :)"); ui.label("This is also work in progress, and not ready for production... yet :)");
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.add_label("Project home page:"); ui.label("Project home page:");
ui.add_hyperlink("https://github.com/emilk/emigui/"); ui.hyperlink("https://github.com/emilk/emigui/");
}); });
ui.add(Separator::new()); ui.add(Separator::new());
ui.add_label("WebGl painter info:"); ui.label("WebGl painter info:");
ui.indent("webgl region id", |ui| { ui.indent("webgl region id", |ui| {
ui.add_label(self.webgl_painter.debug_info()); ui.label(self.webgl_painter.debug_info());
}); });
ui.add( ui.add(