Support running `calc` on Android (#474)

This works without any other code modifications.

![Xilem Calc on
Android.](https://github.com/user-attachments/assets/b6648c04-bc9e-464c-9a19-0f03e958ade9)
This commit is contained in:
Daniel McNab 2024-08-01 14:48:58 +01:00 committed by GitHub
parent 210afb4048
commit 3405f6ee69
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 50 additions and 3 deletions

View File

@ -21,6 +21,9 @@ env:
# This is required because `cargo hack` does not support -p {x} --exclude {x}
RUST_MIN_VER_WASM_PKGS: "-p xilem_core"
# Only some of our examples support Android (primarily due to extra required boilerplate).
ANDROID_TARGETS: -p xilem --example mason_android --example calc_android
# We do not run the masonry snapshot tests, because those currently require a specific font stack
# See https://github.com/linebender/xilem/pull/233
SKIP_RENDER_SNAPSHOTS: 1
@ -230,7 +233,7 @@ jobs:
run: cargo install cargo-apk
- name: cargo apk check (android)
run: cargo apk check -p xilem --example mason_android
run: cargo apk check ${{ env.ANDROID_TARGETS }}
env:
# This is a bit of a hack, but cargo apk doesn't seem to allow customising this otherwise
RUSTFLAGS: '-D warnings'

View File

@ -18,6 +18,7 @@ cargo-args = ["-Zunstable-options", "-Zrustdoc-scrape-examples"]
[[example]]
name = "mason"
# Also add to ANDROID_TARGETS in .github/ci.yml if adding a new Android example
[[example]]
# A custom example target which uses the same `mason.rs` file but for android
name = "mason_android"
@ -25,6 +26,15 @@ path = "examples/mason.rs"
# cdylib is required for cargo-apk
crate-type = ["cdylib"]
[[example]]
name = "calc"
[[example]]
name = "calc_android"
path = "examples/calc.rs"
# cdylib is required for cargo-apk
crate-type = ["cdylib"]
[lints]
workspace = true

View File

@ -6,6 +6,7 @@ use winit::dpi::LogicalSize;
use winit::error::EventLoopError;
use winit::window::Window;
use xilem::view::Flex;
use xilem::EventLoopBuilder;
use xilem::{
view::{button, flex, label, sized_box, Axis, FlexExt as _, FlexSpacer},
EventLoop, WidgetView, Xilem,
@ -300,7 +301,7 @@ fn digit_button(digit: &'static str) -> impl WidgetView<Calculator> {
})
}
fn main() -> Result<(), EventLoopError> {
fn run(event_loop: EventLoopBuilder) -> Result<(), EventLoopError> {
let data = Calculator {
current_num_index: 0,
clear_current_entry_on_input: false,
@ -317,6 +318,39 @@ fn main() -> Result<(), EventLoopError> {
.with_resizable(true)
.with_min_inner_size(min_window_size)
.with_inner_size(window_size);
app.run_windowed_in(EventLoop::with_user_event(), window_attributes)?;
app.run_windowed_in(event_loop, window_attributes)?;
Ok(())
}
#[cfg(not(target_os = "android"))]
#[allow(dead_code)]
// This is treated as dead code by the Android version of the example, but is actually live
// This hackery is required because Cargo doesn't care to support this use case, of one
// example which works across Android and desktop
fn main() -> Result<(), EventLoopError> {
run(EventLoop::with_user_event())
}
// Boilerplate code for android: Identical across all applications
#[cfg(target_os = "android")]
// Safety: We are following `android_activity`'s docs here
// We believe that there are no other declarations using this name in the compiled objects here
#[allow(unsafe_code)]
#[no_mangle]
fn android_main(app: winit::platform::android::activity::AndroidApp) {
use winit::platform::android::EventLoopBuilderExtAndroid;
let mut event_loop = EventLoop::with_user_event();
event_loop.with_android_app(app);
run(event_loop).expect("Can create app");
}
// TODO: This is a hack because of how we handle our examples in Cargo.toml
// Ideally, we change Cargo to be more sensible here?
#[cfg(target_os = "android")]
#[allow(dead_code)]
fn main() {
unreachable!()
}