From 6b38fd39a1b1f63da0754bbe90c87a66c5819319 Mon Sep 17 00:00:00 2001 From: Lucas Meurer Date: Tue, 18 Mar 2025 11:43:15 +0100 Subject: [PATCH] Use egui_demo_lib in android example (#5780) * [x] I have followed the instructions in the PR template --- Cargo.lock | 1 + examples/hello_android/Cargo.toml | 8 +++- examples/hello_android/README.md | 6 ++- examples/hello_android/screenshot.png | 4 +- examples/hello_android/src/lib.rs | 59 ++++++++++----------------- examples/hello_android/src/main.rs | 9 ++++ 6 files changed, 46 insertions(+), 41 deletions(-) create mode 100644 examples/hello_android/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index a7f57e57..b562a1ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1973,6 +1973,7 @@ version = "0.1.0" dependencies = [ "android_logger", "eframe", + "egui_demo_lib", "egui_extras", "log", "winit", diff --git a/examples/hello_android/Cargo.toml b/examples/hello_android/Cargo.toml index b3bf4407..ddeaea9d 100644 --- a/examples/hello_android/Cargo.toml +++ b/examples/hello_android/Cargo.toml @@ -12,11 +12,13 @@ publish = false # workspace = true [lib] -crate-type = ["cdylib"] +# cdylib is required for Android, lib is required for desktop +crate-type = ["cdylib", "lib"] [dependencies] eframe = { workspace = true, features = ["default", "android-native-activity"] } +egui_demo_lib = { workspace = true, features = ["chrono"] } # For image support: egui_extras = { workspace = true, features = ["default", "image"] } @@ -27,3 +29,7 @@ android_logger = "0.14" [package.metadata.android] build_targets = ["armv7-linux-androideabi", "aarch64-linux-android"] + +[package.metadata.android.sdk] +min_sdk_version = 23 +target_sdk_version = 35 diff --git a/examples/hello_android/README.md b/examples/hello_android/README.md index fe14eb9f..6ad26348 100644 --- a/examples/hello_android/README.md +++ b/examples/hello_android/README.md @@ -14,7 +14,11 @@ cargo install \ Build and run: ```sh -cargo apk run -p hello_android +# Run on android +cargo apk run -p hello_android --lib + +# Run on your desktop +cargo run -p hello_android ``` ![](screenshot.png) diff --git a/examples/hello_android/screenshot.png b/examples/hello_android/screenshot.png index 91179fa2..ff376284 100644 --- a/examples/hello_android/screenshot.png +++ b/examples/hello_android/screenshot.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:7add91d7d6b73f48e98f20d84cba3bd3a950cf97aa31f5e9fa93da9af98e876c -size 120019 +oid sha256:16bb465d73b7cf8133aee8cdb773a10d213ad23359a21c0bc2af3e4f9893057f +size 507047 diff --git a/examples/hello_android/src/lib.rs b/examples/hello_android/src/lib.rs index adda66ca..c91c0e75 100644 --- a/examples/hello_android/src/lib.rs +++ b/examples/hello_android/src/lib.rs @@ -1,15 +1,14 @@ -#![cfg(target_os = "android")] -#![allow(rustdoc::missing_crate_level_docs)] // it's an example +#![doc = include_str!("../README.md")] -use android_logger::Config; -use eframe::egui; -use log::LevelFilter; -use winit::platform::android::activity::AndroidApp; +use eframe::{egui, CreationContext}; +#[cfg(target_os = "android")] #[no_mangle] -fn android_main(app: AndroidApp) { +fn android_main(app: winit::platform::android::activity::AndroidApp) { // Log to android output - android_logger::init_once(Config::default().with_max_level(LevelFilter::Info)); + android_logger::init_once( + android_logger::Config::default().with_max_level(log::LevelFilter::Info), + ); let options = eframe::NativeOptions { android_app: Some(app), @@ -18,48 +17,34 @@ fn android_main(app: AndroidApp) { eframe::run_native( "My egui App", options, - Box::new(|cc| { - // This gives us image support: - egui_extras::install_image_loaders(&cc.egui_ctx); - - Ok(Box::::default()) - }), + Box::new(|cc| Ok(Box::new(MyApp::new(cc)))), ) .unwrap() } -struct MyApp { - name: String, - age: u32, +pub struct MyApp { + demo: egui_demo_lib::DemoWindows, } -impl Default for MyApp { - fn default() -> Self { +impl MyApp { + pub fn new(cc: &CreationContext) -> Self { + egui_extras::install_image_loaders(&cc.egui_ctx); Self { - name: "Arthur".to_owned(), - age: 42, + demo: egui_demo_lib::DemoWindows::default(), } } } impl eframe::App for MyApp { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::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 self.name) - .labelled_by(name_label.id); - }); - ui.add(egui::Slider::new(&mut self.age, 0..=120).text("age")); - if ui.button("Increment").clicked() { - self.age += 1; - } - ui.label(format!("Hello '{}', age {}", self.name, self.age)); - - ui.image(egui::include_image!( - "../../../crates/egui/assets/ferris.png" - )); + // Reserve some space at the top so the demo ui isn't hidden behind the android status bar + // TODO(lucasmerlin): This is a pretty big hack, should be fixed once safe_area implemented + // for android: + // https://github.com/rust-windowing/winit/issues/3910 + egui::TopBottomPanel::top("status_bar_space").show(ctx, |ui| { + ui.set_height(32.0); }); + + self.demo.ui(ctx); } } diff --git a/examples/hello_android/src/main.rs b/examples/hello_android/src/main.rs new file mode 100644 index 00000000..e3cdb05a --- /dev/null +++ b/examples/hello_android/src/main.rs @@ -0,0 +1,9 @@ +use hello_android::MyApp; + +fn main() -> eframe::Result { + eframe::run_native( + "hello_android", + Default::default(), + Box::new(|cc| Ok(Box::new(MyApp::new(cc)))), + ) +}