From 4bf36cbdfa6fdef490342f7c2b39eb80f3c7ffac Mon Sep 17 00:00:00 2001 From: AkiChase <1003019131@qq.com> Date: Wed, 22 May 2024 09:03:44 +0800 Subject: [PATCH] feat(adb): add adb path setting --- src-tauri/src/adb.rs | 7 +++++-- src-tauri/src/main.rs | 31 +++++++++++++++++++++++++++++-- src-tauri/src/share.rs | 4 ++++ src/components/Device.vue | 9 +++++++-- src/components/Mask.vue | 16 ++++++++++++++-- src/components/setting/Basic.vue | 29 +++++++++++++++++++++++++++++ src/i18n/en-US.json | 11 +++++++++-- src/i18n/zh-CN.json | 11 +++++++++-- src/invoke.ts | 6 +++++- src/store/global.ts | 2 ++ 10 files changed, 113 insertions(+), 13 deletions(-) diff --git a/src-tauri/src/adb.rs b/src-tauri/src/adb.rs index c800913..3c418d4 100644 --- a/src-tauri/src/adb.rs +++ b/src-tauri/src/adb.rs @@ -8,6 +8,8 @@ use std::os::windows::process::CommandExt; use anyhow::{Context, Ok, Result}; +use crate::share; + #[derive(Clone, Debug, serde::Serialize)] pub struct Device { pub id: String, @@ -85,13 +87,14 @@ pub struct Adb; /// But some output of command won't be output, like adb service startup information. impl Adb { pub fn cmd_base() -> Command { + let adb_path = share::ADB_PATH.lock().unwrap().clone(); #[cfg(target_os = "windows")] { - let mut cmd = Command::new("adb"); + let mut cmd = Command::new(adb_path); cmd.creation_flags(0x08000000); // CREATE_NO_WINDOW return cmd; } - Command::new("adb") + Command::new(adb_path) } /// execute "adb devices" and return devices list diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index fe05d0d..01290b6 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -157,6 +157,24 @@ fn check_adb_available() -> Result<(), String> { } } +#[tauri::command] +fn set_adb_path(adb_path: String, app: tauri::AppHandle) -> Result<(), String> { + let app_h = app.app_handle().clone(); + let stores = app_h.state::>(); + let path = std::path::PathBuf::from("store.bin"); + let store_res: Result<(), tauri_plugin_store::Error> = + tauri_plugin_store::with_store(app, stores, path, |store| { + store.insert("adbPath".to_string(), serde_json::json!(adb_path))?; + *share::ADB_PATH.lock().unwrap() = adb_path; + Ok(()) + }); + + match store_res { + Ok(_) => Ok(()), + Err(e) => Err(e.to_string()), + } +} + #[tokio::main] async fn main() { tauri::Builder::default() @@ -168,8 +186,16 @@ async fn main() { let stores = app .app_handle() .state::>(); - let path = std::path::PathBuf::from("store.bin"); + let path: std::path::PathBuf = std::path::PathBuf::from("store.bin"); tauri_plugin_store::with_store(app.app_handle().clone(), stores, path, |store| { + // load adb path + match store.get("adbPath") { + Some(value) => *share::ADB_PATH.lock().unwrap() = value.to_string(), + None => store + .insert("adbPath".to_string(), serde_json::json!("adb")) + .unwrap(), + }; + // restore window position and size match store.get("maskArea") { Some(value) => { @@ -239,7 +265,8 @@ async fn main() { get_device_screen_size, adb_connect, load_default_keyconfig, - check_adb_available + check_adb_available, + set_adb_path ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); diff --git a/src-tauri/src/share.rs b/src-tauri/src/share.rs index 74173fa..3402b9a 100644 --- a/src-tauri/src/share.rs +++ b/src-tauri/src/share.rs @@ -21,3 +21,7 @@ impl ClientInfo { lazy_static! { pub static ref CLIENT_INFO: Mutex> = Mutex::new(None); } + +lazy_static! { + pub static ref ADB_PATH: Mutex = Mutex::new(String::from("adb")); +} diff --git a/src/components/Device.vue b/src/components/Device.vue index fc5915b..617fdec 100644 --- a/src/components/Device.vue +++ b/src/components/Device.vue @@ -320,8 +320,13 @@ async function connectDevice() { } store.showLoading(); - message.info(await adbConnect(wireless_address.value)); - await refreshDevices(); + try { + message.info(await adbConnect(wireless_address.value)); + await refreshDevices(); + } catch (e) { + message.error("t('pages.Device.adbConnectError')"); + console.error(e); + } } function connectWS() { diff --git a/src/components/Mask.vue b/src/components/Mask.vue index ca9be6f..2aaa9c5 100644 --- a/src/components/Mask.vue +++ b/src/components/Mask.vue @@ -1,6 +1,12 @@