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 = [
"android_logger",
"eframe",
"egui_demo_lib",
"egui_extras",
"log",
"winit",

View File

@ -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

View File

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

View File

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

View File

@ -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::<MyApp>::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);
}
}

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