lib.rs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. use anyhow::Result;
  2. use dioxus::prelude::*;
  3. use dioxus_desktop::Config;
  4. #[cfg(target_os = "android")]
  5. use wry::android_binding;
  6. #[cfg(target_os = "android")]
  7. fn init_logging() {
  8. android_logger::init_once(
  9. android_logger::Config::default()
  10. .with_min_level(log::Level::Trace)
  11. .with_tag("mobile-demo"),
  12. );
  13. }
  14. #[cfg(not(target_os = "android"))]
  15. fn init_logging() {
  16. env_logger::init();
  17. }
  18. #[cfg(any(target_os = "android", target_os = "ios"))]
  19. fn stop_unwind<F: FnOnce() -> T, T>(f: F) -> T {
  20. match std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)) {
  21. Ok(t) => t,
  22. Err(err) => {
  23. eprintln!("attempt to unwind out of `rust` with err: {:?}", err);
  24. std::process::abort()
  25. }
  26. }
  27. }
  28. #[cfg(any(target_os = "android", target_os = "ios"))]
  29. fn _start_app() {
  30. stop_unwind(|| main().unwrap());
  31. }
  32. #[no_mangle]
  33. #[inline(never)]
  34. #[cfg(any(target_os = "android", target_os = "ios"))]
  35. pub extern "C" fn start_app() {
  36. #[cfg(target_os = "android")]
  37. android_binding!(com_example, mobile_demo, _start_app);
  38. #[cfg(target_os = "ios")]
  39. _start_app()
  40. }
  41. pub fn main() -> Result<()> {
  42. init_logging();
  43. // Right now we're going through dioxus-desktop but we'd like to go through dioxus-mobile
  44. // That will seed the index.html with some fixes that prevent the page from scrolling/zooming etc
  45. dioxus_desktop::launch_cfg(
  46. app,
  47. // Note that we have to disable the viewport goofiness of the browser.
  48. // Dioxus_mobile should do this for us
  49. Config::default().with_custom_index(include_str!("index.html").to_string()),
  50. );
  51. Ok(())
  52. }
  53. fn app(cx: Scope) -> Element {
  54. let items = cx.use_hook(|| vec![1, 2, 3]);
  55. log::debug!("Hello from the app");
  56. render! {
  57. div {
  58. h1 { "Hello, Mobile"}
  59. div { margin_left: "auto", margin_right: "auto", width: "200px", padding: "10px", border: "1px solid black",
  60. button {
  61. onclick: move|_| {
  62. println!("Clicked!");
  63. items.push(items.len());
  64. cx.needs_update_any(ScopeId::ROOT);
  65. println!("Requested update");
  66. },
  67. "Add item"
  68. }
  69. for item in items.iter() {
  70. div { "- {item}" }
  71. }
  72. }
  73. }
  74. }
  75. }