From 7b12d3ebfe46336e0655adae5346ed075a253237 Mon Sep 17 00:00:00 2001 From: lencx Date: Thu, 15 Dec 2022 11:29:06 +0800 Subject: [PATCH] chore: control center --- src-tauri/src/app/cmd.rs | 2 +- src-tauri/src/app/menu.rs | 15 +++++++++---- src-tauri/src/conf.rs | 47 +++++++++++++++++++++++++++++++-------- src-tauri/src/main.rs | 1 - src/view/General.tsx | 29 ++++++++++++++---------- 5 files changed, 67 insertions(+), 27 deletions(-) diff --git a/src-tauri/src/app/cmd.rs b/src-tauri/src/app/cmd.rs index da56c18..b407979 100644 --- a/src-tauri/src/app/cmd.rs +++ b/src-tauri/src/app/cmd.rs @@ -36,7 +36,7 @@ pub fn get_chat_conf() -> ChatConfJson { #[command] pub fn form_confirm(_app: AppHandle, data: serde_json::Value) { - ChatConfJson::amend(&serde_json::json!(data)).unwrap(); + ChatConfJson::amend(&serde_json::json!(data), None).unwrap(); } #[command] diff --git a/src-tauri/src/app/menu.rs b/src-tauri/src/app/menu.rs index 875a707..cb769ad 100644 --- a/src-tauri/src/app/menu.rs +++ b/src-tauri/src/app/menu.rs @@ -167,7 +167,11 @@ pub fn menu_handler(event: WindowMenuEvent) { "awesome" => open(&app, conf::AWESOME_URL.to_string()), "titlebar" => { let chat_conf = conf::ChatConfJson::get_chat_conf(); - ChatConfJson::amend(&serde_json::json!({ "titlebar": !chat_conf.titlebar })).unwrap(); + ChatConfJson::amend( + &serde_json::json!({ "titlebar": !chat_conf.titlebar }), + None, + ) + .unwrap(); tauri::api::process::restart(&app.env()); } "theme_light" | "theme_dark" => { @@ -176,8 +180,7 @@ pub fn menu_handler(event: WindowMenuEvent) { } else { "Light" }; - ChatConfJson::amend(&serde_json::json!({ "theme": theme })).unwrap(); - tauri::api::process::restart(&app.env()); + ChatConfJson::amend(&serde_json::json!({ "theme": theme }), Some(app)).unwrap(); } "always_on_top" => { let mut always_on_top = state.always_on_top.lock().unwrap(); @@ -187,7 +190,11 @@ pub fn menu_handler(event: WindowMenuEvent) { .set_selected(*always_on_top) .unwrap(); win.set_always_on_top(*always_on_top).unwrap(); - ChatConfJson::amend(&serde_json::json!({ "always_on_top": *always_on_top })).unwrap(); + ChatConfJson::amend( + &serde_json::json!({ "always_on_top": *always_on_top }), + None, + ) + .unwrap(); } // View "reload" => win.eval("window.location.reload()").unwrap(), diff --git a/src-tauri/src/conf.rs b/src-tauri/src/conf.rs index 7765763..f57e076 100644 --- a/src-tauri/src/conf.rs +++ b/src-tauri/src/conf.rs @@ -2,7 +2,7 @@ use crate::utils::{chat_root, create_file, exists}; use anyhow::Result; use serde_json::Value; use std::{collections::BTreeMap, fs, path::PathBuf, sync::Mutex}; -use tauri::Theme; +use tauri::{Manager, Theme}; #[cfg(target_os = "macos")] use tauri::TitleBarStyle; @@ -60,15 +60,34 @@ impl ChatConfJson { /// path: ~/.chatgpt/chat.conf.json pub fn init() -> PathBuf { let conf_file = ChatConfJson::conf_path(); + if !exists(&conf_file) { create_file(&conf_file).unwrap(); #[cfg(target_os = "macos")] - fs::write(&conf_file, DEFAULT_CHAT_CONF_MAC).unwrap(); + fs::write(conf_file.clone(), DEFAULT_CHAT_CONF_MAC).unwrap(); #[cfg(not(target_os = "macos"))] - fs::write(&conf_file, DEFAULT_CHAT_CONF).unwrap(); + fs::write(conf_file.clone(), DEFAULT_CHAT_CONF).unwrap(); + + return conf_file; } + + let file_content = fs::read_to_string(&conf_file).unwrap(); + match serde_json::from_str(&file_content) { + Ok(v) => v, + Err(err) => { + if err.to_string() == "invalid type: map, expected unit at line 1 column 0" { + return conf_file; + } + + #[cfg(target_os = "macos")] + fs::write(&conf_file, DEFAULT_CHAT_CONF_MAC).unwrap(); + + #[cfg(not(target_os = "macos"))] + fs::write(&conf_file, DEFAULT_CHAT_CONF).unwrap(); + } + }; conf_file } @@ -81,11 +100,11 @@ impl ChatConfJson { .unwrap_or_else(|_| DEFAULT_CHAT_CONF.to_string()); let config: Value = serde_json::from_str(&config_file).expect("failed to parse chat.conf.json"); - serde_json::from_value(config).unwrap_or_else(|_| ChatConfJson::chat_conf_default()) + serde_json::from_value(config).unwrap() } // https://users.rust-lang.org/t/updating-object-fields-given-dynamic-json/39049/3 - pub fn amend(new_rules: &Value) -> Result<()> { + pub fn amend(new_rules: &Value, app: Option) -> Result<()> { let config = ChatConfJson::get_chat_conf(); let config: Value = serde_json::to_value(&config)?; let mut config: BTreeMap = serde_json::from_value(config)?; @@ -99,6 +118,20 @@ impl ChatConfJson { ChatConfJson::conf_path(), serde_json::to_string_pretty(&config)?, )?; + + if let Some(handle) = app { + tauri::api::process::restart(&handle.env()); + // tauri::api::dialog::ask( + // handle.get_window("core").as_ref(), + // "ChatGPT Restart", + // "Whether to restart immediately?", + // move |is_restart| { + // if is_restart { + // } + // }, + // ); + } + Ok(()) } @@ -120,8 +153,4 @@ impl ChatConfJson { TitleBarStyle::Overlay } } - - pub fn chat_conf_default() -> Self { - serde_json::from_value(serde_json::json!(DEFAULT_CHAT_CONF)).unwrap() - } } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index d98406d..d1cab6c 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -9,7 +9,6 @@ mod utils; use app::{cmd, menu, setup}; use conf::{ChatConfJson, ChatState}; -use tauri::Manager; fn main() { ChatConfJson::init(); diff --git a/src/view/General.tsx b/src/view/General.tsx index 40efef4..d1934ec 100644 --- a/src/view/General.tsx +++ b/src/view/General.tsx @@ -5,9 +5,7 @@ import { invoke } from '@tauri-apps/api'; import { platform } from '@tauri-apps/api/os'; import { ask } from '@tauri-apps/api/dialog'; import { relaunch } from '@tauri-apps/api/process'; -import { clone, pick, isEqual } from 'lodash'; - -const restartNames = ['theme', 'titlebar', 'origin', 'ua_window', 'ua_tray']; +import { clone, omit, isEqual } from 'lodash'; const OriginLabel = ({ url }: { url: string }) => { return ( @@ -17,6 +15,12 @@ const OriginLabel = ({ url }: { url: string }) => { ) } +const disableAuto = { + autoCapitalize: 'off', + autoComplete: 'off', + spellCheck: false +} + export default function General() { const [form] = Form.useForm(); const [platformInfo, setPlatform] = useState(''); @@ -41,16 +45,17 @@ export default function General() { }; const onFinish = async (values: any) => { - await invoke('form_confirm', { data: values, label: 'main' }); - if (!isEqual(pick(chatConf, restartNames), pick(values, restartNames))) { + if (!isEqual(omit(chatConf, ['default_origin']), values)) { + await invoke('form_confirm', { data: values, label: 'main' }); const isOk = await ask(`Configuration saved successfully, whether to restart?`, { title: 'ChatGPT Preferences' }); - if (isOk) relaunch(); - return; + if (isOk) { + relaunch(); + return; + } + message.success('Configuration saved successfully'); } - - message.success('Configuration saved successfully'); }; return ( @@ -76,13 +81,13 @@ export default function General() { )} } name="origin"> - + - + - +