Use egui_demo_lib in android example (#5780)

* [x] I have followed the instructions in the PR template
This commit is contained in:
Lucas Meurer 2025-03-18 11:43:15 +01:00 committed by GitHub
parent 9604dae229
commit 6b38fd39a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 46 additions and 41 deletions

View File

@ -1973,6 +1973,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"android_logger", "android_logger",
"eframe", "eframe",
"egui_demo_lib",
"egui_extras", "egui_extras",
"log", "log",
"winit", "winit",

View File

@ -12,11 +12,13 @@ publish = false
# workspace = true # workspace = true
[lib] [lib]
crate-type = ["cdylib"] # cdylib is required for Android, lib is required for desktop
crate-type = ["cdylib", "lib"]
[dependencies] [dependencies]
eframe = { workspace = true, features = ["default", "android-native-activity"] } eframe = { workspace = true, features = ["default", "android-native-activity"] }
egui_demo_lib = { workspace = true, features = ["chrono"] }
# For image support: # For image support:
egui_extras = { workspace = true, features = ["default", "image"] } egui_extras = { workspace = true, features = ["default", "image"] }
@ -27,3 +29,7 @@ android_logger = "0.14"
[package.metadata.android] [package.metadata.android]
build_targets = ["armv7-linux-androideabi", "aarch64-linux-android"] build_targets = ["armv7-linux-androideabi", "aarch64-linux-android"]
[package.metadata.android.sdk]
min_sdk_version = 23
target_sdk_version = 35

View File

@ -14,7 +14,11 @@ cargo install \
Build and run: Build and run:
```sh ```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) ![](screenshot.png)

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:7add91d7d6b73f48e98f20d84cba3bd3a950cf97aa31f5e9fa93da9af98e876c oid sha256:16bb465d73b7cf8133aee8cdb773a10d213ad23359a21c0bc2af3e4f9893057f
size 120019 size 507047

View File

@ -1,15 +1,14 @@
#![cfg(target_os = "android")] #![doc = include_str!("../README.md")]
#![allow(rustdoc::missing_crate_level_docs)] // it's an example
use android_logger::Config; use eframe::{egui, CreationContext};
use eframe::egui;
use log::LevelFilter;
use winit::platform::android::activity::AndroidApp;
#[cfg(target_os = "android")]
#[no_mangle] #[no_mangle]
fn android_main(app: AndroidApp) { fn android_main(app: winit::platform::android::activity::AndroidApp) {
// Log to android output // 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 { let options = eframe::NativeOptions {
android_app: Some(app), android_app: Some(app),
@ -18,48 +17,34 @@ fn android_main(app: AndroidApp) {
eframe::run_native( eframe::run_native(
"My egui App", "My egui App",
options, options,
Box::new(|cc| { Box::new(|cc| Ok(Box::new(MyApp::new(cc)))),
// This gives us image support:
egui_extras::install_image_loaders(&cc.egui_ctx);
Ok(Box::<MyApp>::default())
}),
) )
.unwrap() .unwrap()
} }
struct MyApp { pub struct MyApp {
name: String, demo: egui_demo_lib::DemoWindows,
age: u32,
} }
impl Default for MyApp { impl MyApp {
fn default() -> Self { pub fn new(cc: &CreationContext) -> Self {
egui_extras::install_image_loaders(&cc.egui_ctx);
Self { Self {
name: "Arthur".to_owned(), demo: egui_demo_lib::DemoWindows::default(),
age: 42,
} }
} }
} }
impl eframe::App for MyApp { impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| { // Reserve some space at the top so the demo ui isn't hidden behind the android status bar
ui.heading("My egui Application"); // TODO(lucasmerlin): This is a pretty big hack, should be fixed once safe_area implemented
ui.horizontal(|ui| { // for android:
let name_label = ui.label("Your name: "); // https://github.com/rust-windowing/winit/issues/3910
ui.text_edit_singleline(&mut self.name) egui::TopBottomPanel::top("status_bar_space").show(ctx, |ui| {
.labelled_by(name_label.id); ui.set_height(32.0);
});
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"
));
}); });
self.demo.ui(ctx);
} }
} }

View File

@ -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)))),
)
}