mirror of
https://github.com/emilk/egui.git
synced 2026-06-28 07:23:13 -04:00
Add example_web app
This commit is contained in:
17
example_web/Cargo.toml
Normal file
17
example_web/Cargo.toml
Normal file
@@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "example_web"
|
||||
version = "0.1.0"
|
||||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[dependencies]
|
||||
egui = { path = "../egui", features = ["serde"] }
|
||||
egui_web = { path = "../egui_web" }
|
||||
js-sys = "0.3"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
wasm-bindgen = "0.2"
|
||||
49
example_web/src/example_app.rs
Normal file
49
example_web/src/example_app.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
/// We derive Deserialize/Serialize so we can persist app state on shutdown.
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
pub struct ExampleApp {
|
||||
name: String,
|
||||
age: u32,
|
||||
}
|
||||
|
||||
impl Default for ExampleApp {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
name: "Arthur".to_owned(),
|
||||
age: 42,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl egui::app::App for ExampleApp {
|
||||
/// Called each time the UI needs repainting, which may be many times per second.
|
||||
/// Put your widgets into a `SidePanel`, `TopPanel`, `CentralPanel`, `Window` or `Area`.
|
||||
fn ui(
|
||||
&mut self,
|
||||
ctx: &std::sync::Arc<egui::Context>,
|
||||
integration_context: &mut egui::app::IntegrationContext,
|
||||
) {
|
||||
let ExampleApp { name, age } = self;
|
||||
|
||||
// Example used in `README.md`.
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
ui.heading("My Egui Application");
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Your name: ");
|
||||
ui.text_edit_singleline(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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
20
example_web/src/lib.rs
Normal file
20
example_web/src/lib.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
#![forbid(unsafe_code)]
|
||||
#![deny(warnings)]
|
||||
#![warn(clippy::all)]
|
||||
|
||||
mod example_app;
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
/// This is the entry-point for all the web-assembly.
|
||||
/// This is called once from the HTML.
|
||||
/// It loads the app, installs some callbacks, then returns.
|
||||
/// You can add more callbacks like this if you want to call in to your code.
|
||||
#[wasm_bindgen]
|
||||
pub fn start(canvas_id: &str) -> Result<(), wasm_bindgen::JsValue> {
|
||||
let app = example_app::ExampleApp::default();
|
||||
let backend = egui_web::WebBackend::new(canvas_id)?;
|
||||
let runner = egui_web::AppRunner::new(backend, Box::new(app))?;
|
||||
egui_web::start(runner)?;
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user