Merge branch 'main' into dev

This commit is contained in:
lencx
2023-01-05 12:38:58 +08:00
10 changed files with 197 additions and 103 deletions

View File

@@ -51,8 +51,8 @@ pub fn reset_chat_conf() -> ChatConfJson {
}
#[command]
pub fn run_check_update(app: AppHandle) -> () {
utils::run_check_update(app).unwrap();
pub fn run_check_update(app: AppHandle, silent: bool) {
utils::run_check_update(app, silent).unwrap();
}
#[command]

View File

@@ -36,20 +36,27 @@ pub fn init() -> Menu {
let stay_on_top =
CustomMenuItem::new("stay_on_top".to_string(), "Stay On Top").accelerator("CmdOrCtrl+T");
#[cfg(target_os = "macos")]
let titlebar =
CustomMenuItem::new("titlebar".to_string(), "Titlebar").accelerator("CmdOrCtrl+B");
let theme_light = CustomMenuItem::new("theme_light".to_string(), "Light");
let theme_dark = CustomMenuItem::new("theme_dark".to_string(), "Dark");
let theme_system = CustomMenuItem::new("theme_system".to_string(), "System");
let is_dark = chat_conf.theme == "Dark";
let is_system = chat_conf.theme == "System";
let update_prompt = CustomMenuItem::new("update_prompt".to_string(), "Prompt");
let update_silent = CustomMenuItem::new("update_silent".to_string(), "Silent");
let _update_disable = CustomMenuItem::new("update_disable".to_string(), "Disable");
let stay_on_top_menu = if chat_conf.stay_on_top {
stay_on_top.selected()
} else {
stay_on_top
};
#[cfg(target_os = "macos")]
let titlebar_menu = if chat_conf.titlebar {
titlebar.selected()
@@ -84,6 +91,26 @@ pub fn init() -> Menu {
}),
)
.into(),
Submenu::new(
"Auto Update",
Menu::new()
.add_item(if chat_conf.auto_update == "Prompt" {
update_prompt.selected()
} else {
update_prompt
})
.add_item(if chat_conf.auto_update == "Silent" {
update_silent.selected()
} else {
update_silent
})
// .add_item(if chat_conf.auto_update == "Disable" {
// update_disable.selected()
// } else {
// update_disable
// })
,
).into(),
stay_on_top_menu.into(),
#[cfg(target_os = "macos")]
titlebar_menu.into(),
@@ -201,7 +228,7 @@ pub fn menu_handler(event: WindowMenuEvent<tauri::Wry>) {
);
}
"check_update" => {
utils::run_check_update(app).unwrap();
utils::run_check_update(app, false).unwrap();
}
// Preferences
"control_center" => window::control_window(&app),
@@ -237,16 +264,34 @@ pub fn menu_handler(event: WindowMenuEvent<tauri::Wry>) {
.unwrap();
tauri::api::process::restart(&app.env());
}
"theme_light" | "theme_dark" | "theme_system" => {
let theme = if menu_id == "theme_dark" {
"Dark"
} else if menu_id == "theme_system" {
"System"
} else {
"Light"
"theme_light" | "theme_dark" | "theme_system" => {
let theme = match menu_id {
"theme_dark" => "Dark",
"theme_system" => "System",
_ => "Light",
};
ChatConfJson::amend(&serde_json::json!({ "theme": theme }), Some(app)).unwrap();
}
"update_prompt" | "update_silent" | "update_disable" => {
for id in ["update_prompt" , "update_silent" , "update_disable"] {
menu_handle.get_item(id).set_selected(false).unwrap();
}
let auto_update = match menu_id {
"update_silent" => {
menu_handle.get_item("update_silent").set_selected(true).unwrap();
"Silent"
},
"update_disable" => {
menu_handle.get_item("update_disable").set_selected(true).unwrap();
"Disable"
},
_ => {
menu_handle.get_item("update_prompt").set_selected(true).unwrap();
"Prompt"
},
};
ChatConfJson::amend(&serde_json::json!({ "auto_update": auto_update }), None).unwrap();
}
"stay_on_top" => {
let mut stay_on_top = state.stay_on_top.lock().unwrap();
*stay_on_top = !*stay_on_top;

View File

@@ -95,10 +95,10 @@ pub fn init(app: &mut App) -> std::result::Result<(), Box<dyn std::error::Error>
.unwrap();
});
}
// auto_check_update
if chat_conf.auto_check_update {
// auto_update
if chat_conf.auto_update != "Disable" {
let app = app.handle();
utils::run_check_update(app).unwrap();
utils::run_check_update(app, chat_conf.auto_update == "Silent").unwrap();
}
Ok(())

View File

@@ -18,7 +18,7 @@ pub const GITHUB_PROMPTS_CSV_URL: &str =
"https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv";
pub const DEFAULT_CHAT_CONF: &str = r#"{
"stay_on_top": false,
"auto_check_update": true,
"auto_update": "Prompt",
"theme": "Light",
"titlebar": true,
"global_shortcut": "",
@@ -30,7 +30,7 @@ pub const DEFAULT_CHAT_CONF: &str = r#"{
}"#;
pub const DEFAULT_CHAT_CONF_MAC: &str = r#"{
"stay_on_top": false,
"auto_check_update": true,
"auto_update": "Prompt",
"theme": "Light",
"titlebar": false,
"global_shortcut": "",
@@ -59,10 +59,10 @@ pub struct ChatConfJson {
pub titlebar: bool,
pub hide_dock_icon: bool,
// macOS and Windows
// macOS and Windows, Light/Dark/System
pub theme: String,
pub auto_check_update: bool,
// auto update policy, Prompt/Silent/Disable
pub auto_update: String,
pub stay_on_top: bool,
pub default_origin: String,
pub origin: String,
@@ -177,21 +177,20 @@ impl ChatConfJson {
pub fn theme() -> Option<Theme> {
let conf = ChatConfJson::get_chat_conf();
if conf.theme == "System" {
let mode = dark_light::detect();
return match mode {
let theme = match conf.theme.as_str() {
"System" => match dark_light::detect() {
// Dark mode
dark_light::Mode::Dark => Some(Theme::Dark),
dark_light::Mode::Dark => Theme::Dark,
// Light mode
dark_light::Mode::Light => Some(Theme::Light),
dark_light::Mode::Light => Theme::Light,
// Unspecified
dark_light::Mode::Default => Some(Theme::Light),
}
} if conf.theme == "Dark" {
Some(Theme::Dark)
} else {
Some(Theme::Light)
}
dark_light::Mode::Default => Theme::Light,
},
"Dark" => Theme::Dark,
_ => Theme::Light,
};
Some(theme)
}
#[cfg(target_os = "macos")]

View File

@@ -8,8 +8,8 @@ use std::{
path::{Path, PathBuf},
process::Command,
};
use tauri::{utils::config::Config, Manager, AppHandle, Wry};
use tauri::updater::UpdateResponse;
use tauri::{utils::config::Config, AppHandle, Manager, Wry};
pub fn chat_root() -> PathBuf {
tauri::api::path::home_dir().unwrap().join(".chatgpt")
@@ -130,14 +130,20 @@ pub async fn get_data(
}
}
pub fn run_check_update(app: AppHandle<Wry>) -> Result<()> {
pub fn run_check_update(app: AppHandle<Wry>, silent: bool) -> Result<()> {
tauri::async_runtime::spawn(async move {
let result = app.updater().check().await;
let update_resp = result.unwrap();
if update_resp.is_update_available() {
tauri::async_runtime::spawn(async move {
prompt_for_install(app, update_resp).await.unwrap();
});
if silent {
tauri::async_runtime::spawn(async move {
silent_install(app, update_resp).await.unwrap();
});
} else {
tauri::async_runtime::spawn(async move {
prompt_for_install(app, update_resp).await.unwrap();
});
}
}
});
Ok(())
@@ -146,10 +152,7 @@ pub fn run_check_update(app: AppHandle<Wry>) -> Result<()> {
// Copy private api in tauri/updater/mod.rs. TODO: refactor to public api
// Prompt a dialog asking if the user want to install the new version
// Maybe we should add an option to customize it in future versions.
pub async fn prompt_for_install (
app: AppHandle<Wry>,
update: UpdateResponse<Wry>
) -> Result<()> {
pub async fn prompt_for_install(app: AppHandle<Wry>, update: UpdateResponse<Wry>) -> Result<()> {
let windows = app.windows();
let parent_window = windows.values().next();
let package_info = app.package_info().clone();
@@ -167,7 +170,11 @@ Would you like to install it now?
Release Notes:
{}"#,
package_info.name, update.latest_version(), package_info.version, body),
package_info.name,
update.latest_version(),
package_info.version,
body
),
);
if should_install {
@@ -190,3 +197,26 @@ Release Notes:
Ok(())
}
pub async fn silent_install(app: AppHandle<Wry>, update: UpdateResponse<Wry>) -> Result<()> {
let windows = app.windows();
let parent_window = windows.values().next();
// Launch updater download process
// macOS we display the `Ready to restart dialog` asking to restart
// Windows is closing the current App and launch the downloaded MSI when ready (the process stop here)
// Linux we replace the AppImage by launching a new install, it start a new AppImage instance, so we're closing the previous. (the process stop here)
update.download_and_install().await?;
// Ask user if we need to restart the application
let should_exit = tauri::api::dialog::blocking::ask(
parent_window,
"Ready to Restart",
"The silent installation was successful, do you want to restart the application now?",
);
if should_exit {
app.restart();
}
Ok(())
}