-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental Android support for eframe
- Loading branch information
Showing
7 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
[package] | ||
name = "hello_android" | ||
version = "0.1.0" | ||
authors = ["Emil Ernerfeldt <[email protected]>"] | ||
license = "MIT OR Apache-2.0" | ||
edition = "2021" | ||
rust-version = "1.76" | ||
publish = false | ||
|
||
# `unsafe_code` is required for `#[no_mangle]`, disable workspace lints to workaround lint error. | ||
# [lints] | ||
# workspace = true | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
|
||
[dependencies] | ||
eframe = { workspace = true, features = [ | ||
"default", | ||
"android-native-activity", | ||
] } | ||
|
||
# For image support: | ||
egui_extras = { workspace = true, features = ["default", "image"] } | ||
|
||
log = { workspace = true } | ||
winit = { workspace = true } | ||
android_logger = "0.14" | ||
|
||
[package.metadata.android] | ||
build_targets = [ "armv7-linux-androideabi", "aarch64-linux-android" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Hello world example for Android. | ||
|
||
Use `cargo-apk` to build and run. Requires a patch to workaround [an upstream bug](https://github.com/rust-mobile/cargo-subcommand/issues/29). | ||
|
||
One-time setup: | ||
|
||
```sh | ||
cargo install \ | ||
--git https://github.com/parasyte/cargo-apk.git \ | ||
--rev 282639508eeed7d73f2e1eaeea042da2716436d5 \ | ||
cargo-apk | ||
``` | ||
|
||
Build and run: | ||
|
||
```sh | ||
cargo apk run -p hello_android | ||
``` | ||
|
||
![](screenshot.png) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#![cfg(target_os = "android")] | ||
#![allow(rustdoc::missing_crate_level_docs)] // it's an example | ||
|
||
use android_logger::Config; | ||
use eframe::egui; | ||
use log::LevelFilter; | ||
use winit::platform::android::activity::AndroidApp; | ||
|
||
#[no_mangle] | ||
fn android_main(app: AndroidApp) { | ||
// Log to android output | ||
android_logger::init_once(Config::default().with_max_level(LevelFilter::Info)); | ||
|
||
let options = eframe::NativeOptions { | ||
android_app: Some(app), | ||
..Default::default() | ||
}; | ||
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()) | ||
}), | ||
) | ||
.unwrap() | ||
} | ||
|
||
struct MyApp { | ||
name: String, | ||
age: u32, | ||
} | ||
|
||
impl Default for MyApp { | ||
fn default() -> Self { | ||
Self { | ||
name: "Arthur".to_owned(), | ||
age: 42, | ||
} | ||
} | ||
} | ||
|
||
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" | ||
)); | ||
}); | ||
} | ||
} |