Skip to content

Commit

Permalink
Setup to_do_mvc for Android
Browse files Browse the repository at this point in the history
I'm finding these cargo warnings exceptionally annoying...
  • Loading branch information
DJMcNab committed Sep 5, 2024
1 parent 7be4dd2 commit 4c3d727
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
10 changes: 9 additions & 1 deletion xilem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ path = "examples/http_cats.rs"
# cdylib is required for cargo-apk
crate-type = ["cdylib"]


[[example]]
name = "stopwatch"

Expand All @@ -55,6 +54,15 @@ path = "examples/stopwatch.rs"
# cdylib is required for cargo-apk
crate-type = ["cdylib"]

[[example]]
name = "to_do_mvc"

[[example]]
name = "to_do_mvc_android"
path = "examples/to_do_mvc.rs"
# cdylib is required for cargo-apk
crate-type = ["cdylib"]

[[example]]
name = "variable_clock"

Expand Down
52 changes: 45 additions & 7 deletions xilem/examples/to_do_mvc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
// On Windows platform, don't show a console when opening the app.
#![windows_subsystem = "windows"]

use xilem::view::{button, checkbox, flex, textbox, Axis};
use xilem::{EventLoop, WidgetView, Xilem};
use winit::error::EventLoopError;
use xilem::view::{button, checkbox, flex, textbox, Axis, FlexSpacer};
use xilem::{EventLoop, EventLoopBuilder, WidgetView, Xilem};

struct Task {
description: String,
Expand Down Expand Up @@ -66,12 +67,17 @@ fn app_logic(task_list: &mut TaskList) -> impl WidgetView<TaskList> {
})
.collect::<Vec<_>>();

flex((first_line, tasks))
flex((
FlexSpacer::Fixed(40.), // HACK: Spacer for Androird
first_line,
tasks,
))
}

fn main() {
fn run(event_loop: EventLoopBuilder) -> Result<(), EventLoopError> {
let data = TaskList {
next_task: String::new(),
// Add a placeholder task for Android, whilst the
next_task: "My Next Task".into(),
tasks: vec![
Task {
description: "Buy milk".into(),
Expand All @@ -89,6 +95,38 @@ fn main() {
};

let app = Xilem::new(data, app_logic);
app.run_windowed(EventLoop::with_user_event(), "First Example".into())
.unwrap();
app.run_windowed(event_loop, "First Example".into())
}

// Boilerplate code for android: Identical across all applications

#[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())
}

#[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!()
}

0 comments on commit 4c3d727

Please sign in to comment.