From ba1fe9a603250ebe688c8c366af184e66d03f299 Mon Sep 17 00:00:00 2001 From: lencx Date: Sat, 31 Dec 2022 20:07:09 +0800 Subject: [PATCH] refactor: global shortcut --- UPDATE_LOG.md | 1 + src-tauri/Cargo.toml | 1 + src-tauri/src/app/cmd.rs | 24 ++++++++++++---- src-tauri/src/app/setup.rs | 43 +++++++++++++++++------------ src-tauri/src/app/window.rs | 2 +- src/view/model/SyncCustom/index.tsx | 8 ++++-- 6 files changed, 53 insertions(+), 26 deletions(-) diff --git a/UPDATE_LOG.md b/UPDATE_LOG.md index c457bc7..8a6b166 100644 --- a/UPDATE_LOG.md +++ b/UPDATE_LOG.md @@ -3,6 +3,7 @@ ## v0.7.4 fix: +- trying to resolve linux errors: `error while loading shared libraries` - customize global shortcuts (`Menu -> Preferences -> Control Center -> General -> Global Shortcut`) ## v0.7.3 diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index a421bf3..2bb7c08 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -26,6 +26,7 @@ walkdir = "2.3.2" regex = "1.7.0" tokio = { version = "1.23.0", features = ["macros"] } reqwest = "0.11.13" +wry = "0.23.4" [dependencies.tauri-plugin-log] git = "https://github.com/tauri-apps/tauri-plugin-log" diff --git a/src-tauri/src/app/cmd.rs b/src-tauri/src/app/cmd.rs index c2cefe2..6d29a89 100644 --- a/src-tauri/src/app/cmd.rs +++ b/src-tauri/src/app/cmd.rs @@ -88,8 +88,17 @@ pub fn parse_prompt(data: String) -> Vec { let mut rdr = csv::Reader::from_reader(data.as_bytes()); let mut list = vec![]; for result in rdr.deserialize() { - let record: PromptRecord = result.unwrap(); - list.push(record); + let record: PromptRecord = result.unwrap_or_else(|err| { + info!("parse_prompt_error: {}", err); + PromptRecord { + cmd: None, + act: "".to_string(), + prompt: "".to_string(), + } + }); + if !record.act.is_empty() { + list.push(record); + } } list } @@ -222,19 +231,22 @@ pub async fn sync_prompts(app: AppHandle, time: u64) -> Option> #[command] pub async fn sync_user_prompts(url: String, data_type: String) -> Option> { - let res = utils::get_data(&url, None).await.unwrap(); + let res = utils::get_data(&url, None).await.unwrap_or_else(|err| { + info!("chatgpt_http_error: {}", err); + None + }); info!("chatgpt_http_url: {}", url); if let Some(v) = res { let data; if data_type == "csv" { - info!("chatgpt_http_csv_parser"); + info!("chatgpt_http_csv_parse"); data = parse_prompt(v); } else if data_type == "json" { - info!("chatgpt_http_json_parser"); + info!("chatgpt_http_json_parse"); data = serde_json::from_str(&v).unwrap_or_else(|err| { - info!("chatgpt_http_json_parser_error: {}", err); + info!("chatgpt_http_json_parse_error: {}", err); vec![] }); } else { diff --git a/src-tauri/src/app/setup.rs b/src-tauri/src/app/setup.rs index 7a1dbf1..91f812d 100644 --- a/src-tauri/src/app/setup.rs +++ b/src-tauri/src/app/setup.rs @@ -1,6 +1,7 @@ use crate::{app::window, conf::ChatConfJson, utils}; use log::info; use tauri::{utils::config::WindowUrl, window::WindowBuilder, App, GlobalShortcutManager, Manager}; +use wry::application::accelerator::Accelerator; pub fn init(app: &mut App) -> std::result::Result<(), Box> { info!("stepup"); @@ -14,23 +15,31 @@ pub fn init(app: &mut App) -> std::result::Result<(), Box }); if let Some(v) = chat_conf.global_shortcut { - info!("global_shortcut"); - let handle = app.app_handle(); - let mut shortcut = app.global_shortcut_manager(); - shortcut - .register(&v, move || { - if let Some(w) = handle.get_window("core") { - if w.is_visible().unwrap() { - w.hide().unwrap(); - } else { - w.show().unwrap(); - w.set_focus().unwrap(); - } - } - }) - .unwrap_or_else(|err| { - info!("global_shortcut_register_error: {}", err); - }); + info!("global_shortcut: `{}`", v); + match v.parse::() { + Ok(_) => { + info!("global_shortcut_register"); + let handle = app.app_handle(); + let mut shortcut = app.global_shortcut_manager(); + shortcut + .register(&v, move || { + if let Some(w) = handle.get_window("core") { + if w.is_visible().unwrap() { + w.hide().unwrap(); + } else { + w.show().unwrap(); + w.set_focus().unwrap(); + } + } + }) + .unwrap_or_else(|err| { + info!("global_shortcut_register_error: {}", err); + }); + } + Err(err) => { + info!("global_shortcut_parse_error: {}", err); + } + } } else { info!("global_shortcut_unregister"); }; diff --git a/src-tauri/src/app/window.rs b/src-tauri/src/app/window.rs index 55c171e..546be04 100644 --- a/src-tauri/src/app/window.rs +++ b/src-tauri/src/app/window.rs @@ -30,7 +30,7 @@ pub fn control_window(handle: &tauri::AppHandle) { let app = handle.clone(); tokio::spawn(async move { WindowBuilder::new(&app, "main", WindowUrl::App("index.html".into())) - .title("ChatGPT") + .title("Control Center") .resizable(true) .fullscreen(false) .inner_size(800.0, 600.0) diff --git a/src/view/model/SyncCustom/index.tsx b/src/view/model/SyncCustom/index.tsx index 4d41b68..936283c 100644 --- a/src/view/model/SyncCustom/index.tsx +++ b/src/view/model/SyncCustom/index.tsx @@ -34,7 +34,9 @@ export default function SyncCustom() { if (!opInfo.opType) return; if (opInfo.opType === 'sync') { const filename = `${opInfo?.opRecord?.id}.json`; - handleSync(filename).then(() => { + handleSync(filename).then((isOk: boolean) => { + opInfo.resetRecord(); + if (!isOk) return; const data = opReplace(opInfo?.opRecord?.[opSafeKey], { ...opInfo?.opRecord, last_updated: Date.now() }); modelSet(data); opInfo.resetRecord(); @@ -70,10 +72,11 @@ export default function SyncCustom() { await modelCacheSet(data as [], file); await modelCacheCmd(); message.success('ChatGPT Prompts data has been synchronized!'); + return true; } else { message.error('ChatGPT Prompts data sync failed, please try again!'); + return false; } - return; } // local if (isJson) { @@ -87,6 +90,7 @@ export default function SyncCustom() { await modelCacheSet(fmtData(list), file); } await modelCacheCmd(); + return true; }; const handleOk = () => {