From c19698bc41c8eb759c8db2c0b0c52f6ecd8015ad Mon Sep 17 00:00:00 2001 From: tk103331 Date: Wed, 4 Jan 2023 12:43:26 +0800 Subject: [PATCH 1/4] Manually check for updates --- src-tauri/src/app/menu.rs | 12 ++++++++++ src-tauri/src/utils.rs | 50 ++++++++++++++++++++++++++++++++++++++- src/layout/index.scss | 5 +++- src/layout/index.tsx | 30 ++++++++++++++++++++++- 4 files changed, 94 insertions(+), 3 deletions(-) diff --git a/src-tauri/src/app/menu.rs b/src-tauri/src/app/menu.rs index 690860f..3b83a44 100644 --- a/src-tauri/src/app/menu.rs +++ b/src-tauri/src/app/menu.rs @@ -24,6 +24,7 @@ pub fn init() -> Menu { MenuItem::About(name.into(), AboutMetadata::default()).into(), #[cfg(not(target_os = "macos"))] CustomMenuItem::new("about".to_string(), "About ChatGPT").into(), + CustomMenuItem::new("check_update".to_string(), "Check for Updates").into(), MenuItem::Services.into(), MenuItem::Hide.into(), MenuItem::HideOthers.into(), @@ -192,6 +193,17 @@ pub fn menu_handler(event: WindowMenuEvent) { format!("Version {}", tauri_conf.package.version.unwrap()), ); } + "check_update" => { + 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 { + utils::prompt_for_install(app, update_resp).await.unwrap(); + }); + } + }); + } // Preferences "control_center" => window::control_window(&app), "restart" => tauri::api::process::restart(&app.env()), diff --git a/src-tauri/src/utils.rs b/src-tauri/src/utils.rs index 61a153d..0d5145d 100644 --- a/src-tauri/src/utils.rs +++ b/src-tauri/src/utils.rs @@ -8,7 +8,8 @@ use std::{ path::{Path, PathBuf}, process::Command, }; -use tauri::{utils::config::Config, Manager}; +use tauri::{utils::config::Config, Manager, AppHandle, Wry}; +use tauri::updater::UpdateResponse; pub fn chat_root() -> PathBuf { tauri::api::path::home_dir().unwrap().join(".chatgpt") @@ -128,3 +129,50 @@ pub async fn get_data( Ok(None) } } +// 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, + update: UpdateResponse +) -> Result<()> { + let windows = app.windows(); + let parent_window = windows.values().next(); + let package_info = app.package_info().clone(); + + let body = update.body().unwrap(); + // todo(lemarier): We should review this and make sure we have + // something more conventional. + let should_install = tauri::api::dialog::blocking::ask( + parent_window, + format!(r#"A new version of {} is available! "#, package_info.name), + format!( + r#"{} {} is now available -- you have {}. + +Would you like to install it now? + +Release Notes: +{}"#, + package_info.name, update.latest_version(), package_info.version, body), + ); + + if should_install { + // 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 installation was successful, do you want to restart the application now?", + ); + if should_exit { + app.restart(); + } + } + + Ok(()) +} diff --git a/src/layout/index.scss b/src/layout/index.scss index 9c0a917..00ef8bf 100644 --- a/src/layout/index.scss +++ b/src/layout/index.scss @@ -7,7 +7,10 @@ height: 48px; } } - +.chat-info { + text-align: center; + font-weight: bold; +} .ant-layout-sider-trigger { user-select: none; -webkit-user-select: none; diff --git a/src/layout/index.tsx b/src/layout/index.tsx index 36c514e..9ca866a 100644 --- a/src/layout/index.tsx +++ b/src/layout/index.tsx @@ -1,12 +1,19 @@ import { FC, useState } from 'react'; -import { Layout, Menu } from 'antd'; +import {Layout, Menu, Button, Tooltip, message} from 'antd'; +import { SyncOutlined } from '@ant-design/icons'; import { useNavigate, useLocation } from 'react-router-dom'; +import { getName, getVersion } from '@tauri-apps/api/app'; +import { checkUpdate, installUpdate } from '@tauri-apps/api/updater'; +import { relaunch } from '@tauri-apps/api/process'; import Routes, { menuItems } from '@/routes'; import './index.scss'; const { Content, Footer, Sider } = Layout; +const appName = await getName(); +const appVersion = await getVersion(); + interface ChatLayoutProps { children?: React.ReactNode; } @@ -16,6 +23,14 @@ const ChatLayout: FC = ({ children }) => { const location = useLocation(); const go = useNavigate(); + const checkAppUpdate = async () => { + try { + await checkUpdate(); + }catch (e) { + console.log(e) + } + } + return ( = ({ children }) => { }} >
+
+ {appName} +
+
+ {appVersion} + + { + + + + } +
+ Date: Wed, 4 Jan 2023 13:10:46 +0800 Subject: [PATCH 2/4] Add 'Auto Check Update' config to control whether to check for updates on start --- src-tauri/src/app/menu.rs | 10 +--------- src-tauri/src/app/setup.rs | 8 +++++++- src-tauri/src/conf.rs | 3 +++ src-tauri/src/utils.rs | 15 ++++++++++++++- src-tauri/tauri.conf.json | 4 ++-- src/view/General.tsx | 11 +++++++++++ 6 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src-tauri/src/app/menu.rs b/src-tauri/src/app/menu.rs index 3b83a44..58ced06 100644 --- a/src-tauri/src/app/menu.rs +++ b/src-tauri/src/app/menu.rs @@ -194,15 +194,7 @@ pub fn menu_handler(event: WindowMenuEvent) { ); } "check_update" => { - 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 { - utils::prompt_for_install(app, update_resp).await.unwrap(); - }); - } - }); + utils::run_check_update(app).unwrap(); } // Preferences "control_center" => window::control_window(&app), diff --git a/src-tauri/src/app/setup.rs b/src-tauri/src/app/setup.rs index 91f812d..3f73284 100644 --- a/src-tauri/src/app/setup.rs +++ b/src-tauri/src/app/setup.rs @@ -1,6 +1,7 @@ +use std::borrow::Borrow; use crate::{app::window, conf::ChatConfJson, utils}; use log::info; -use tauri::{utils::config::WindowUrl, window::WindowBuilder, App, GlobalShortcutManager, Manager}; +use tauri::{utils::config::WindowUrl, window::WindowBuilder, App, GlobalShortcutManager, Manager, AppHandle, Wry}; use wry::application::accelerator::Accelerator; pub fn init(app: &mut App) -> std::result::Result<(), Box> { @@ -89,6 +90,11 @@ pub fn init(app: &mut App) -> std::result::Result<(), Box .unwrap(); }); } + // auto_check_update + if chat_conf.auto_check_update { + let app = app.handle(); + utils::run_check_update(app).unwrap(); + } Ok(()) } diff --git a/src-tauri/src/conf.rs b/src-tauri/src/conf.rs index 499b054..8c07c1b 100644 --- a/src-tauri/src/conf.rs +++ b/src-tauri/src/conf.rs @@ -18,6 +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, "theme": "Light", "titlebar": true, "global_shortcut": "", @@ -29,6 +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, "theme": "Light", "titlebar": false, "global_shortcut": "", @@ -60,6 +62,7 @@ pub struct ChatConfJson { // macOS and Windows pub theme: String, + pub auto_check_update: bool, pub stay_on_top: bool, pub default_origin: String, pub origin: String, diff --git a/src-tauri/src/utils.rs b/src-tauri/src/utils.rs index 0d5145d..4f55cff 100644 --- a/src-tauri/src/utils.rs +++ b/src-tauri/src/utils.rs @@ -10,7 +10,6 @@ use std::{ }; use tauri::{utils::config::Config, Manager, AppHandle, Wry}; use tauri::updater::UpdateResponse; - pub fn chat_root() -> PathBuf { tauri::api::path::home_dir().unwrap().join(".chatgpt") } @@ -129,6 +128,20 @@ pub async fn get_data( Ok(None) } } + +pub fn run_check_update(app: AppHandle) -> 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(); + }); + } + }); + Ok(()) +} + // 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. diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 4ed2837..71a52cb 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -7,7 +7,7 @@ }, "package": { "productName": "ChatGPT", - "version": "0.7.4" + "version": "0.7.2" }, "tauri": { "allowlist": { @@ -67,7 +67,7 @@ "csp": null }, "updater": { - "active": true, + "active": false, "dialog": true, "endpoints": [ "https://lencx.github.io/ChatGPT/install.json" diff --git a/src/view/General.tsx b/src/view/General.tsx index 68f23b8..b231cd9 100644 --- a/src/view/General.tsx +++ b/src/view/General.tsx @@ -10,6 +10,14 @@ import { clone, omit, isEqual } from 'lodash'; import useInit from '@/hooks/useInit'; import { DISABLE_AUTO_COMPLETE, chatRoot } from '@/utils'; +const CheckUpdateLabel = () => { + return ( + + Auto Check Update + + ) +} + const OriginLabel = ({ url }: { url: string }) => { return ( @@ -108,6 +116,9 @@ export default function General() { + } name="auto_check_update" valuePropName="checked"> + + } name="global_shortcut"> From 883f36b26df900b0b6ca4ba2eede27b12634fb1e Mon Sep 17 00:00:00 2001 From: tk103331 Date: Wed, 4 Jan 2023 13:19:01 +0800 Subject: [PATCH 3/4] Manually check for updates in 'Control Center' --- src-tauri/src/app/cmd.rs | 5 +++++ src-tauri/src/app/setup.rs | 3 +-- src-tauri/src/main.rs | 1 + src/layout/index.tsx | 9 ++------- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src-tauri/src/app/cmd.rs b/src-tauri/src/app/cmd.rs index 8feddd7..104a93e 100644 --- a/src-tauri/src/app/cmd.rs +++ b/src-tauri/src/app/cmd.rs @@ -43,6 +43,11 @@ pub fn reset_chat_conf() -> ChatConfJson { ChatConfJson::reset_chat_conf() } +#[command] +pub fn run_check_update(app: AppHandle) -> () { + utils::run_check_update(app).unwrap(); +} + #[command] pub fn form_confirm(_app: AppHandle, data: serde_json::Value) { ChatConfJson::amend(&serde_json::json!(data), None).unwrap(); diff --git a/src-tauri/src/app/setup.rs b/src-tauri/src/app/setup.rs index 3f73284..b926a9f 100644 --- a/src-tauri/src/app/setup.rs +++ b/src-tauri/src/app/setup.rs @@ -1,7 +1,6 @@ -use std::borrow::Borrow; use crate::{app::window, conf::ChatConfJson, utils}; use log::info; -use tauri::{utils::config::WindowUrl, window::WindowBuilder, App, GlobalShortcutManager, Manager, AppHandle, Wry}; +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> { diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index a6f6f67..05e9161 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -57,6 +57,7 @@ async fn main() { cmd::open_link, cmd::get_chat_conf, cmd::reset_chat_conf, + cmd::run_check_update, cmd::form_cancel, cmd::form_confirm, cmd::form_msg, diff --git a/src/layout/index.tsx b/src/layout/index.tsx index 9ca866a..667792a 100644 --- a/src/layout/index.tsx +++ b/src/layout/index.tsx @@ -3,8 +3,7 @@ import {Layout, Menu, Button, Tooltip, message} from 'antd'; import { SyncOutlined } from '@ant-design/icons'; import { useNavigate, useLocation } from 'react-router-dom'; import { getName, getVersion } from '@tauri-apps/api/app'; -import { checkUpdate, installUpdate } from '@tauri-apps/api/updater'; -import { relaunch } from '@tauri-apps/api/process'; +import { invoke } from '@tauri-apps/api'; import Routes, { menuItems } from '@/routes'; import './index.scss'; @@ -24,11 +23,7 @@ const ChatLayout: FC = ({ children }) => { const go = useNavigate(); const checkAppUpdate = async () => { - try { - await checkUpdate(); - }catch (e) { - console.log(e) - } + await invoke('run_check_update'); } return ( From f26dace129a7bb3e4310f1123e13ead8e6dc3e54 Mon Sep 17 00:00:00 2001 From: tk Date: Wed, 4 Jan 2023 13:30:51 +0800 Subject: [PATCH 4/4] fix --- src-tauri/tauri.conf.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 71a52cb..7483925 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -7,7 +7,7 @@ }, "package": { "productName": "ChatGPT", - "version": "0.7.2" + "version": "0.7.4" }, "tauri": { "allowlist": { @@ -75,4 +75,4 @@ "pubkey": "dW50cnVzdGVkIGNvbW1lbnQ6IG1pbmlzaWduIHB1YmxpYyBrZXk6IEIxMjY4OUI5MTVFNjBEMDUKUldRRkRlWVZ1WWttc1NGWEE0RFNSb0RqdnhsekRJZTkwK2hVLzhBZTZnaHExSEZ1ZEdzWkpXTHkK" } } -} \ No newline at end of file +}