Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Android support #86

Merged
merged 7 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/dioxus-blitz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ name = "dioxus-blitz"
version = "0.0.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
menu = ["dep:muda"]
default = ["menu"]

[dependencies]
winit = { version = "0.30.2", features = ["rwh_06"] }
muda = { version = "0.11.5", features = ["serde"] }
muda = { version = "0.11.5", features = ["serde"], optional = true }
tokio = { workspace = true, features = ["full"] }
dioxus = { workspace = true }
futures-util = "0.3.30"
Expand All @@ -19,3 +21,10 @@ blitz-dom = { path = "../dom" }
url = { version = "2.5.0", features = ["serde"] }
ureq = "2.9"
rustc-hash = "1.1.0"

[target.'cfg(target_os = "android")'.dependencies]
android-activity = { version = "0.6.0", features = ["native-activity"] }

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
40 changes: 34 additions & 6 deletions packages/dioxus-blitz/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(docsrs, feature(doc_cfg))]

mod documents;
mod waker;
mod window;
Expand All @@ -9,7 +11,6 @@ use blitz::RenderState;
use blitz_dom::DocumentLike;
use dioxus::prelude::*;
use documents::DioxusDocument;
use muda::{MenuEvent, MenuId};
use std::collections::HashMap;
use url::Url;
use winit::event_loop::EventLoop;
Expand Down Expand Up @@ -93,17 +94,25 @@ fn launch_with_window<Doc: DocumentLike + 'static>(window: View<'static, Doc>) {
let _guard = rt.enter();

// Build an event loop for the application
let event_loop = EventLoop::<UserWindowEvent>::with_user_event()
.build()
.unwrap();
let mut builder = EventLoop::<UserWindowEvent>::with_user_event();

#[cfg(target_os = "android")]
{
use winit::platform::android::EventLoopBuilderExtAndroid;
builder.with_android_app(current_android_app());
}

let event_loop = builder.build().unwrap();
let proxy = event_loop.create_proxy();

// Multiwindow ftw
let mut windows: HashMap<WindowId, window::View<'_, Doc>> = HashMap::new();
let mut pending_windows = Vec::new();

pending_windows.push(window);
let menu_channel = MenuEvent::receiver();

#[cfg(all(feature = "menu", not(any(target_os = "android", target_os = "ios"))))]
let menu_channel = muda::MenuEvent::receiver();

#[cfg(not(any(target_os = "android", target_os = "ios")))]
let mut initial = true;
Expand Down Expand Up @@ -193,8 +202,9 @@ fn launch_with_window<Doc: DocumentLike + 'static>(window: View<'static, Doc>) {
_ => (),
}

#[cfg(all(feature = "menu", not(any(target_os = "android", target_os = "ios"))))]
if let Ok(event) = menu_channel.try_recv() {
if event.id == MenuId::new("dev.show_layout") {
if event.id == muda::MenuId::new("dev.show_layout") {
for (_, view) in windows.iter_mut() {
view.renderer.devtools.show_layout = !view.renderer.devtools.show_layout;
view.request_redraw();
Expand All @@ -204,3 +214,21 @@ fn launch_with_window<Doc: DocumentLike + 'static>(window: View<'static, Doc>) {
})
.unwrap();
}

#[cfg(target_os = "android")]
static ANDROID_APP: std::sync::OnceLock<android_activity::AndroidApp> = std::sync::OnceLock::new();

#[cfg(target_os = "android")]
#[cfg_attr(docsrs, doc(cfg(target_os = "android")))]
/// Set the current [`AndroidApp`](android_activity::AndroidApp).
pub fn set_android_app(app: android_activity::AndroidApp) {
ANDROID_APP.set(app).unwrap()
}

#[cfg(target_os = "android")]
#[cfg_attr(docsrs, doc(cfg(target_os = "android")))]
/// Get the current [`AndroidApp`](android_activity::AndroidApp).
/// This will panic if the android activity has not been setup with [`set_android_app`].
pub fn current_android_app(app: android_activity::AndroidApp) -> AndroidApp {
ANDROID_APP.get().unwrap().clone()
}
22 changes: 14 additions & 8 deletions packages/dioxus-blitz/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use winit::keyboard::PhysicalKey;
#[allow(unused)]
use wgpu::rwh::HasWindowHandle;

use muda::{AboutMetadata, Menu, MenuId, MenuItem, PredefinedMenuItem, Submenu};
use std::sync::Arc;
use std::task::Waker;
use vello::Scene;
Expand All @@ -24,7 +23,8 @@ pub(crate) struct View<'s, Doc: DocumentLike> {
keyboard_modifiers: ModifiersState,

/// Main menu bar of this view's window.
menu: Option<Menu>,
#[cfg(all(feature = "menu", not(any(target_os = "android", target_os = "ios"))))]
menu: Option<muda::Menu>,
}

impl<'a, Doc: DocumentLike> View<'a, Doc> {
Expand All @@ -34,6 +34,7 @@ impl<'a, Doc: DocumentLike> View<'a, Doc> {
scene: Scene::new(),
waker: None,
keyboard_modifiers: Default::default(),
#[cfg(all(feature = "menu", not(any(target_os = "android", target_os = "ios"))))]
menu: None,
}
}
Expand Down Expand Up @@ -287,11 +288,13 @@ impl<'a, Doc: DocumentLike> View<'a, Doc> {
}))
.unwrap();

self.menu = Some(init_menu(
#[cfg(target_os = "windows")]
&window,
));

#[cfg(all(feature = "menu", not(any(target_os = "android", target_os = "ios"))))]
{
self.menu = Some(init_menu(
#[cfg(target_os = "windows")]
&window,
));
}
let size: winit::dpi::PhysicalSize<u32> = window.inner_size();
let mut viewport = Viewport::new((size.width, size.height));
viewport.set_hidpi_scale(window.scale_factor() as _);
Expand All @@ -316,7 +319,10 @@ impl<'a, Doc: DocumentLike> View<'a, Doc> {
}

/// Initialize the default menu bar.
pub fn init_menu(#[cfg(target_os = "windows")] window: &Window) -> Menu {
#[cfg(all(feature = "menu", not(any(target_os = "android", target_os = "ios"))))]
pub fn init_menu(#[cfg(target_os = "windows")] window: &Window) -> muda::Menu {
use muda::{AboutMetadata, Menu, MenuId, MenuItem, PredefinedMenuItem, Submenu};

let menu = Menu::new();

// Build the about section
Expand Down