Skip to content

Commit

Permalink
New example code
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Oct 24, 2020
1 parent 72285e7 commit 2e80aba
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ Sections:
### Example

``` rust
Window::new("Debug").show(ui.ctx(), |ui| {
ui.label(format!("Hello, world {}", 123));
if ui.button("Save").clicked {
my_save_function();
}
ui.text_edit(&mut my_string);
ui.add(Slider::f32(&mut value, 0.0..=1.0).text("float"));
ui.heading("My Egui Application");
ui.horizontal(|ui| {
ui.label("Your name: ");
ui.text_edit(&mut name);
});
ui.add(egui::Slider::u32(&mut age, 0..=120).text("age"));
if ui.button("Click each year").clicked {
age += 1;
}
ui.label(format!("Hello '{}', age {}", name, age));
```

<img src="media/demo-2020-08-21.png" width="50%">
<img src="media/demo-2020-10-24.png" width="40%">

## Goals

Expand Down
49 changes: 34 additions & 15 deletions example_glium/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@
#![warn(clippy::all)]

/// We derive Deserialize/Serialize so we can persist app state on shutdown.
#[derive(Default, serde::Deserialize, serde::Serialize)]
#[derive(serde::Deserialize, serde::Serialize)]
struct MyApp {
my_string: String,
value: f32,
name: String,
age: u32,
}

impl Default for MyApp {
fn default() -> Self {
Self {
name: "Arthur".to_owned(),
age: 42,
}
}
}

impl egui::app::App for MyApp {
Expand All @@ -16,19 +25,33 @@ impl egui::app::App for MyApp {
fn ui(
&mut self,
ctx: &std::sync::Arc<egui::Context>,
_integration_context: &mut egui::app::IntegrationContext,
integration_context: &mut egui::app::IntegrationContext,
) {
let MyApp { my_string, value } = self;
let MyApp { name, age } = self;

// Example used in `README.md`.
egui::Window::new("Debug").show(ctx, |ui| {
ui.label(format!("Hello, world {}", 123));
if ui.button("Save").clicked {
my_save_function();
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("My Egui Application");

ui.horizontal(|ui| {
ui.label("Your name: ");
ui.text_edit(name);
});

ui.add(egui::Slider::u32(age, 0..=120).text("age"));
if ui.button("Click each year").clicked {
*age += 1;
}

ui.label(format!("Hello '{}', age {}", name, age));

ui.advance_cursor(16.0);
if ui.button("Quit").clicked {
integration_context.output.quit = true;
}
ui.text_edit(my_string);
ui.add(egui::Slider::f32(value, 0.0..=1.0).text("float"));
});

integration_context.output.window_size = Some(ctx.used_size()); // resize the window to be just the size we need it to be
}

fn on_exit(&mut self, storage: &mut dyn egui::app::Storage) {
Expand All @@ -48,7 +71,3 @@ fn main() {
let app: MyApp = egui::app::get_value(&storage, egui::app::APP_KEY).unwrap_or_default(); // Restore `MyApp` from file, or create new `MyApp`.
egui_glium::run(title, Box::new(storage), app);
}

fn my_save_function() {
// dummy
}
Binary file added media/demo-2020-10-24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2e80aba

Please sign in to comment.