refactor: unify and simplify the call of app_handle(2)

This commit is contained in:
huzibaca
2024-09-23 23:15:51 +08:00
parent 8a3a094414
commit a3465d292c
12 changed files with 39 additions and 62 deletions

View File

@@ -138,9 +138,5 @@ fn test_parse_check_output() {
let res2 = parse_check_output(str2.into());
let res3 = parse_check_output(str3.into());
println!("res1: {res1}");
println!("res2: {res2}");
println!("res3: {res3}");
assert_eq!(res1, res3);
}

View File

@@ -2,13 +2,13 @@ use super::tray::Tray;
use crate::log_err;
use anyhow::Result;
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use parking_lot::RwLock;
use std::sync::Arc;
use tauri::{AppHandle, Emitter, Manager, WebviewWindow};
#[derive(Debug, Default, Clone)]
pub struct Handle {
pub app_handle: Arc<Mutex<Option<AppHandle>>>,
pub app_handle: Arc<RwLock<Option<AppHandle>>>,
}
impl Handle {
@@ -16,16 +16,17 @@ impl Handle {
static HANDLE: OnceCell<Handle> = OnceCell::new();
HANDLE.get_or_init(|| Handle {
app_handle: Arc::new(Mutex::new(None)),
app_handle: Arc::new(RwLock::new(None)),
})
}
pub fn init(&self, app_handle: &AppHandle) {
*self.app_handle.lock() = Some(app_handle.clone());
let mut handle = self.app_handle.write();
*handle = Some(app_handle.clone());
}
pub fn app_handle(&self) -> Option<AppHandle> {
self.app_handle.lock().clone()
self.app_handle.read().clone()
}
pub fn get_window(&self) -> Option<WebviewWindow> {

View File

@@ -4,7 +4,7 @@ use anyhow::{bail, Result};
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use std::{collections::HashMap, sync::Arc};
use tauri::{AppHandle, Manager};
use tauri::Manager;
use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, ShortcutState};
pub struct Hotkey {
current: Arc<Mutex<Vec<String>>>, // 保存当前的热键设置
@@ -46,7 +46,7 @@ impl Hotkey {
}
pub fn register(&self, hotkey: &str, func: &str) -> Result<()> {
let app_handle: AppHandle = handle::Handle::global().app_handle().unwrap();
let app_handle = handle::Handle::global().app_handle().unwrap();
let manager = app_handle.global_shortcut();
if manager.is_registered(hotkey) {
@@ -83,7 +83,7 @@ impl Hotkey {
}
pub fn unregister(&self, hotkey: &str) -> Result<()> {
let app_handle: AppHandle = handle::Handle::global().app_handle().unwrap();
let app_handle = handle::Handle::global().app_handle().unwrap();
let manager = app_handle.global_shortcut();
manager.unregister(hotkey)?;
@@ -158,7 +158,7 @@ impl Hotkey {
impl Drop for Hotkey {
fn drop(&mut self) {
let app_handle: AppHandle = handle::Handle::global().app_handle().unwrap();
let app_handle = handle::Handle::global().app_handle().unwrap();
if let Err(e) = app_handle.global_shortcut().unregister_all() {
log::error!("Error unregistering all hotkeys: {:?}", e);
}

View File

@@ -23,7 +23,7 @@ pub struct Tray {}
impl Tray {
pub fn create_systray() -> Result<()> {
let app_handle: AppHandle = handle::Handle::global().app_handle().unwrap();
let app_handle = handle::Handle::global().app_handle().unwrap();
let tray_incon_id = TrayIconId::new("main");
let tray = app_handle.tray_by_id(&tray_incon_id).unwrap();
@@ -66,7 +66,7 @@ impl Tray {
}
pub fn update_part() -> Result<()> {
let app_handle: AppHandle = handle::Handle::global().app_handle().unwrap();
let app_handle = handle::Handle::global().app_handle().unwrap();
let use_zh = { Config::verge().latest().language == Some("zh".into()) };
let version = VERSION.get().unwrap();
let mode = {