diff --git a/Cargo.lock b/Cargo.lock index 36a4872b..4142002f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2171,6 +2171,14 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "hello_world_simple" +version = "0.1.0" +dependencies = [ + "eframe", + "tracing-subscriber", +] + [[package]] name = "hermit-abi" version = "0.1.19" diff --git a/crates/eframe/src/lib.rs b/crates/eframe/src/lib.rs index 28f2adb1..3a529910 100644 --- a/crates/eframe/src/lib.rs +++ b/crates/eframe/src/lib.rs @@ -54,6 +54,10 @@ //! } //! ``` //! +//! ## Simplified usage +//! If your app is only for native, and you don't need advanced features like state persistence, +//! then you can use the simpler function [`run_simple_native`]. +//! //! ## Feature flags #![cfg_attr(feature = "document-features", doc = document_features::document_features!())] //! @@ -209,6 +213,60 @@ pub fn run_native( // ---------------------------------------------------------------------------- +/// The simplest way to get started when writing a native app. +/// +/// This does NOT support persistence. For that you need to use [`run_native`]. +/// +/// # Example +/// ``` no_run +/// // Our application state: +/// let mut name = "Arthur".to_owned(); +/// let mut age = 42; +/// +/// let options = eframe::NativeOptions::default(); +/// eframe::run_simple_native("My egui App", options, move |ctx, _frame| { +/// egui::CentralPanel::default().show(ctx, |ui| { +/// ui.heading("My egui Application"); +/// ui.horizontal(|ui| { +/// let name_label = ui.label("Your name: "); +/// ui.text_edit_singleline(&mut name) +/// .labelled_by(name_label.id); +/// }); +/// ui.add(egui::Slider::new(&mut age, 0..=120).text("age")); +/// if ui.button("Click each year").clicked() { +/// age += 1; +/// } +/// ui.label(format!("Hello '{}', age {}", name, age)); +/// }); +/// }) +/// ``` +/// +/// # Errors +/// This function can fail if we fail to set up a graphics context. +#[cfg(not(target_arch = "wasm32"))] +pub fn run_simple_native( + app_name: &str, + native_options: NativeOptions, + update_fun: impl FnMut(&egui::Context, &mut Frame) + 'static, +) -> Result<()> { + struct SimpleApp { + update_fun: U, + } + impl App for SimpleApp { + fn update(&mut self, ctx: &egui::Context, frame: &mut Frame) { + (self.update_fun)(ctx, frame); + } + } + + run_native( + app_name, + native_options, + Box::new(|_cc| Box::new(SimpleApp { update_fun })), + ) +} + +// ---------------------------------------------------------------------------- + /// The different problems that can occur when trying to run `eframe`. #[derive(thiserror::Error, Debug)] pub enum EframeError { diff --git a/examples/hello_world_simple/Cargo.toml b/examples/hello_world_simple/Cargo.toml new file mode 100644 index 00000000..6c06c174 --- /dev/null +++ b/examples/hello_world_simple/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "hello_world_simple" +version = "0.1.0" +authors = ["Emil Ernerfeldt "] +license = "MIT OR Apache-2.0" +edition = "2021" +rust-version = "1.65" +publish = false + + +[dependencies] +eframe = { path = "../../crates/eframe", features = [ + "__screenshot", # __screenshot is so we can dump a ascreenshot using EFRAME_SCREENSHOT_TO +] } +tracing-subscriber = "0.3" diff --git a/examples/hello_world_simple/README.md b/examples/hello_world_simple/README.md new file mode 100644 index 00000000..08c58a7d --- /dev/null +++ b/examples/hello_world_simple/README.md @@ -0,0 +1,7 @@ +Example showing some UI controls like `Label`, `TextEdit`, `Slider`, `Button`. + +```sh +cargo run -p hello_world +``` + +![](screenshot.png) diff --git a/examples/hello_world_simple/screenshot.png b/examples/hello_world_simple/screenshot.png new file mode 100644 index 00000000..765339d4 Binary files /dev/null and b/examples/hello_world_simple/screenshot.png differ diff --git a/examples/hello_world_simple/src/main.rs b/examples/hello_world_simple/src/main.rs new file mode 100644 index 00000000..998d84a0 --- /dev/null +++ b/examples/hello_world_simple/src/main.rs @@ -0,0 +1,33 @@ +#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release + +use eframe::egui; + +fn main() -> Result<(), eframe::EframeError> { + // Log to stdout (if you run with `RUST_LOG=debug`). + tracing_subscriber::fmt::init(); + + let options = eframe::NativeOptions { + initial_window_size: Some(egui::vec2(320.0, 240.0)), + ..Default::default() + }; + + // Our application state: + let mut name = "Arthur".to_owned(); + let mut age = 42; + + eframe::run_simple_native("My egui App", options, move |ctx, _frame| { + egui::CentralPanel::default().show(ctx, |ui| { + ui.heading("My egui Application"); + ui.horizontal(|ui| { + let name_label = ui.label("Your name: "); + ui.text_edit_singleline(&mut name) + .labelled_by(name_label.id); + }); + ui.add(egui::Slider::new(&mut age, 0..=120).text("age")); + if ui.button("Click each year").clicked() { + age += 1; + } + ui.label(format!("Hello '{}', age {}", name, age)); + }); + }) +}