1
0
mirror of https://github.com/lencx/ChatGPT.git synced 2024-10-01 01:06:13 -04:00

Merge pull request #151 from tk103331/auto_update

This commit is contained in:
lencx 2023-01-05 12:29:54 +08:00 committed by GitHub
commit e0c4584529
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 105 additions and 21 deletions

View File

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

View File

@ -47,6 +47,10 @@ pub fn init() -> Menu {
let is_dark = chat_conf.theme == "Dark"; let is_dark = chat_conf.theme == "Dark";
let is_system = chat_conf.theme == "System"; 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 { let stay_on_top_menu = if chat_conf.stay_on_top {
stay_on_top.selected() stay_on_top.selected()
} else { } else {
@ -87,6 +91,26 @@ pub fn init() -> Menu {
}), }),
) )
.into(), .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(), stay_on_top_menu.into(),
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
titlebar_menu.into(), titlebar_menu.into(),
@ -204,7 +228,7 @@ pub fn menu_handler(event: WindowMenuEvent<tauri::Wry>) {
); );
} }
"check_update" => { "check_update" => {
utils::run_check_update(app).unwrap(); utils::run_check_update(app, false).unwrap();
} }
// Preferences // Preferences
"control_center" => window::control_window(&app), "control_center" => window::control_window(&app),
@ -248,6 +272,26 @@ pub fn menu_handler(event: WindowMenuEvent<tauri::Wry>) {
}; };
ChatConfJson::amend(&serde_json::json!({ "theme": theme }), Some(app)).unwrap(); 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" => { "stay_on_top" => {
let mut stay_on_top = state.stay_on_top.lock().unwrap(); let mut stay_on_top = state.stay_on_top.lock().unwrap();
*stay_on_top = !*stay_on_top; *stay_on_top = !*stay_on_top;

View File

@ -89,10 +89,10 @@ pub fn init(app: &mut App) -> std::result::Result<(), Box<dyn std::error::Error>
.unwrap(); .unwrap();
}); });
} }
// auto_check_update // auto_update
if chat_conf.auto_check_update { if chat_conf.auto_update != "Disable" {
let app = app.handle(); let app = app.handle();
utils::run_check_update(app).unwrap(); utils::run_check_update(app, chat_conf.auto_update == "Silent").unwrap();
} }
Ok(()) 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"; "https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv";
pub const DEFAULT_CHAT_CONF: &str = r#"{ pub const DEFAULT_CHAT_CONF: &str = r#"{
"stay_on_top": false, "stay_on_top": false,
"auto_check_update": true, "auto_update": "Prompt",
"theme": "Light", "theme": "Light",
"titlebar": true, "titlebar": true,
"global_shortcut": "", "global_shortcut": "",
@ -30,7 +30,7 @@ pub const DEFAULT_CHAT_CONF: &str = r#"{
}"#; }"#;
pub const DEFAULT_CHAT_CONF_MAC: &str = r#"{ pub const DEFAULT_CHAT_CONF_MAC: &str = r#"{
"stay_on_top": false, "stay_on_top": false,
"auto_check_update": true, "auto_update": "Prompt",
"theme": "Light", "theme": "Light",
"titlebar": false, "titlebar": false,
"global_shortcut": "", "global_shortcut": "",
@ -59,10 +59,10 @@ pub struct ChatConfJson {
pub titlebar: bool, pub titlebar: bool,
pub hide_dock_icon: bool, pub hide_dock_icon: bool,
// macOS and Windows // macOS and Windows, Light/Dark/System
pub theme: String, pub theme: String,
// auto update policy, Prompt/Silent/Disable
pub auto_check_update: bool, pub auto_update: String,
pub stay_on_top: bool, pub stay_on_top: bool,
pub default_origin: String, pub default_origin: String,
pub origin: String, pub origin: String,

View File

@ -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 { tauri::async_runtime::spawn(async move {
let result = app.updater().check().await; let result = app.updater().check().await;
let update_resp = result.unwrap(); let update_resp = result.unwrap();
if update_resp.is_update_available() { if update_resp.is_update_available() {
tauri::async_runtime::spawn(async move { if silent {
prompt_for_install(app, update_resp).await.unwrap(); 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(()) Ok(())
@ -191,3 +197,26 @@ Release Notes:
Ok(()) 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(())
}

View File

@ -68,7 +68,7 @@
}, },
"updater": { "updater": {
"active": true, "active": true,
"dialog": true, "dialog": false,
"endpoints": [ "endpoints": [
"https://lencx.github.io/ChatGPT/install.json" "https://lencx.github.io/ChatGPT/install.json"
], ],

View File

@ -23,7 +23,7 @@ const ChatLayout: FC<ChatLayoutProps> = ({ children }) => {
const go = useNavigate(); const go = useNavigate();
const checkAppUpdate = async () => { const checkAppUpdate = async () => {
await invoke('run_check_update'); await invoke('run_check_update', {silent: false});
} }
return ( return (

19
src/view/General.tsx vendored
View File

@ -10,10 +10,17 @@ import { clone, omit, isEqual } from 'lodash';
import useInit from '@/hooks/useInit'; import useInit from '@/hooks/useInit';
import { DISABLE_AUTO_COMPLETE, chatRoot } from '@/utils'; import { DISABLE_AUTO_COMPLETE, chatRoot } from '@/utils';
const CheckUpdateLabel = () => { const AutoUpdateLabel = () => {
return ( return (
<span> <span>
Auto Check Update <Tooltip title={`check update on start`}><QuestionCircleOutlined style={{ color: '#1677ff' }} /></Tooltip> Auto Update <Tooltip title={(
<div>
<div>Auto Update Policy</div>
<span><strong>Prompt</strong>: prompt to install</span><br/>
<span><strong>Silent</strong>: install silently</span><br/>
{/*<span><strong>Disable</strong>: disable auto update</span><br/>*/}
</div>
)}><QuestionCircleOutlined style={{ color: '#1677ff' }} /></Tooltip>
</span> </span>
) )
} }
@ -121,8 +128,12 @@ export default function General() {
<Form.Item label="Stay On Top" name="stay_on_top" valuePropName="checked"> <Form.Item label="Stay On Top" name="stay_on_top" valuePropName="checked">
<Switch /> <Switch />
</Form.Item> </Form.Item>
<Form.Item label={<CheckUpdateLabel />} name="auto_check_update" valuePropName="checked"> <Form.Item label={<AutoUpdateLabel />} name="auto_update">
<Switch /> <Radio.Group>
<Radio value="Prompt">Prompt</Radio>
<Radio value="Silent">Silent</Radio>
{/*<Radio value="Disable">Disable</Radio>*/}
</Radio.Group>
</Form.Item> </Form.Item>
<Form.Item label={<GlobalShortcut />} name="global_shortcut"> <Form.Item label={<GlobalShortcut />} name="global_shortcut">
<Input placeholder="CmdOrCtrl+Shift+O" {...DISABLE_AUTO_COMPLETE} /> <Input placeholder="CmdOrCtrl+Shift+O" {...DISABLE_AUTO_COMPLETE} />