feat(cli): add mobile support to the app template (#5046)

This commit is contained in:
Lucas Fernandes Nogueira
2022-08-25 16:43:29 -03:00
committed by GitHub
parent badad2b9a1
commit 80a301ea63
8 changed files with 91 additions and 12 deletions

View File

@@ -31,7 +31,7 @@ pub fn command(options: Options) -> Result<()> {
} else {
Profile::Debug
};
let noise_level = NoiseLevel::FranklyQuitePedantic;
let noise_level = NoiseLevel::LoudAndProud;
with_config(None, |root_conf, config, metadata| {
ensure_init(config.project_dir(), MobileTarget::Android)

View File

@@ -44,7 +44,7 @@ pub fn command(options: Options) -> Result<()> {
let profile = profile_from_configuration(&options.configuration);
let macos = macos_from_platform(&options.platform);
let noise_level = NoiseLevel::FranklyQuitePedantic;
let noise_level = NoiseLevel::LoudAndProud;
with_config(None, |root_conf, config, metadata| {
let env = env()?;

View File

@@ -5,12 +5,14 @@ description = "A Tauri App"
authors = ["you"]
license = ""
repository = ""
default-run = "app"
edition = "2021"
rust-version = "1.57"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["staticlib", "cdylib", "rlib"]
[build-dependencies]
tauri-build = {{{ tauri_build_dep }}}
@@ -19,6 +21,15 @@ serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = {{{ tauri_dep }}}
[target.'cfg(any(target_os = "android", target_os = "ios"))'.dependencies]
log = "0.4"
[target.'cfg(target_os = "android")'.dependencies]
android_logger = "0.9.0"
[target.'cfg(target_os = "ios")'.dependencies]
env_logger = "0.9.0"
[features]
# by default Tauri runs in production mode
# when `tauri dev` runs it is executed with `cargo run --no-default-features` if `devPath` is an URL

View File

@@ -0,0 +1,8 @@
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
fn main() {
app::AppBuilder::new()
}

View File

@@ -0,0 +1,41 @@
use tauri::App;
#[cfg(mobile)]
mod mobile;
#[cfg(mobile)]
pub use mobile::*;
pub type SetupHook = Box<dyn FnOnce(&mut App) -> Result<(), Box<dyn std::error::Error>> + Send>;
#[derive(Default)]
pub struct AppBuilder {
setup: Option<SetupHook>,
}
impl AppBuilder {
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn setup<F>(mut self, setup: F) -> Self
where
F: FnOnce(&mut App) -> Result<(), Box<dyn std::error::Error>> + Send + 'static,
{
self.setup.replace(Box::new(setup));
self
}
pub fn run(self) {
let setup = self.setup;
tauri::Builder::default()
.setup(move |app| {
if let Some(setup) = setup {
(setup)(app)?;
}
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
}

View File

@@ -1,10 +1,7 @@
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
#[cfg(desktop)]
mod desktop;
fn main() {
tauri::Builder::default()
.run(tauri::generate_context!())
.expect("error while running tauri application");
#[cfg(desktop)]
desktop::main();
}

View File

@@ -0,0 +1,23 @@
#[cfg(target_os = "android")]
fn init_logging(app_name: &str) {
android_logger::init_once(
android_logger::Config::default()
.with_min_level(log::Level::Trace)
.with_tag(app_name),
);
}
#[cfg(not(target_os = "android"))]
fn init_logging(_app_name: &str) {
env_logger::init();
}
#[tauri::mobile_entry_point]
fn main() {
super::AppBuilder::new()
.setup(|app| {
init_logging(&app.package_info().name);
Ok(())
})
.run()
}

View File

@@ -1,6 +1,5 @@
package {{app-domain-reversed}}.{{app-name-snake-case}}
package {{reverse-domain app.domain}}.{{snake-case app.name}}
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
abstract class TauriActivity : AppCompatActivity()