Sfoglia il codice sorgente

fix: portable flag

MystiPanda 1 anno fa
parent
commit
e3a500e12c

+ 0 - 1
.github/workflows/release.yml

@@ -92,7 +92,6 @@ jobs:
           GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
           TAURI_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }}
           TAURI_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }}
-          VITE_WIN_PORTABLE: 1
 
   release-for-linux:
     strategy:

+ 5 - 0
src-tauri/src/cmds.rs

@@ -256,6 +256,11 @@ pub async fn clash_api_get_proxy_delay(
     }
 }
 
+#[tauri::command]
+pub fn get_portable_flag() -> CmdResult<bool> {
+    Ok(*dirs::PORTABLE_FLAG.get().unwrap_or(&false))
+}
+
 #[cfg(windows)]
 pub mod service {
     use super::*;

+ 1 - 0
src-tauri/src/main.rs

@@ -34,6 +34,7 @@ fn main() -> std::io::Result<()> {
             cmds::open_logs_dir,
             cmds::open_web_url,
             cmds::open_core_dir,
+            cmds::get_portable_flag,
             // cmds::kill_sidecar,
             cmds::restart_sidecar,
             cmds::grant_permission,

+ 24 - 7
src-tauri/src/utils/dirs.rs

@@ -1,5 +1,6 @@
 use crate::core::handle;
 use anyhow::Result;
+use once_cell::sync::OnceCell;
 use std::path::PathBuf;
 use tauri::{
     api::path::{data_dir, resource_dir},
@@ -11,12 +12,14 @@ static APP_ID: &str = "io.github.clash-verge-rev.clash-verge-rev";
 #[cfg(feature = "verge-dev")]
 static APP_ID: &str = "io.github.clash-verge-rev.clash-verge-rev.dev";
 
+pub static PORTABLE_FLAG: OnceCell<bool> = OnceCell::new();
+
 static CLASH_CONFIG: &str = "config.yaml";
 static VERGE_CONFIG: &str = "verge.yaml";
 static PROFILE_YAML: &str = "profiles.yaml";
 
-/// get the verge app home dir
-pub fn app_home_dir() -> Result<PathBuf> {
+/// init portable flag
+pub fn init_portable_flag() -> Result<()> {
     use tauri::utils::platform::current_exe;
 
     let app_exe = current_exe()?;
@@ -24,13 +27,27 @@ pub fn app_home_dir() -> Result<PathBuf> {
         let dir = PathBuf::from(dir).join(".config/PORTABLE");
 
         if dir.exists() {
-            let app_exe = dunce::canonicalize(app_exe)?;
-            let app_dir = app_exe
-                .parent()
-                .ok_or(anyhow::anyhow!("failed to get the portable app dir"))?;
-            return Ok(PathBuf::from(app_dir).join(".config").join(APP_ID));
+            PORTABLE_FLAG.get_or_init(|| true);
         }
     }
+    PORTABLE_FLAG.get_or_init(|| false);
+    Ok(())
+}
+
+/// get the verge app home dir
+pub fn app_home_dir() -> Result<PathBuf> {
+    use tauri::utils::platform::current_exe;
+
+    let flag = PORTABLE_FLAG.get().unwrap_or(&false);
+    if *flag {
+        let app_exe = current_exe()?;
+        let app_exe = dunce::canonicalize(app_exe)?;
+        let app_dir = app_exe
+            .parent()
+            .ok_or(anyhow::anyhow!("failed to get the portable app dir"))?;
+        return Ok(PathBuf::from(app_dir).join(".config").join(APP_ID));
+    }
+
     Ok(data_dir()
         .ok_or(anyhow::anyhow!("failed to get app home dir"))?
         .join(APP_ID))

+ 1 - 0
src-tauri/src/utils/init.rs

@@ -141,6 +141,7 @@ pub fn delete_log() -> Result<()> {
 /// Initialize all the config files
 /// before tauri setup
 pub fn init_config() -> Result<()> {
+    let _ = dirs::init_portable_flag();
     let _ = init_log();
     let _ = delete_log();
 

+ 2 - 1
src/components/setting/setting-verge.tsx

@@ -18,6 +18,7 @@ import { GuardState } from "./mods/guard-state";
 import { LayoutViewer } from "./mods/layout-viewer";
 import { UpdateViewer } from "./mods/update-viewer";
 import getSystem from "@/utils/get-system";
+import { portableFlag } from "@/pages/_layout";
 
 interface Props {
   onError?: (err: Error) => void;
@@ -213,7 +214,7 @@ const SettingVerge = ({ onError }: Props) => {
         </IconButton>
       </SettingItem>
 
-      {!(OS === "windows" && WIN_PORTABLE) && (
+      {!portableFlag && (
         <SettingItem label={t("Check for Updates")}>
           <IconButton
             color="inherit"

+ 10 - 7
src/pages/_layout.tsx

@@ -22,6 +22,9 @@ import { useCustomTheme } from "@/components/layout/use-custom-theme";
 import getSystem from "@/utils/get-system";
 import "dayjs/locale/ru";
 import "dayjs/locale/zh-cn";
+import { getPortableFlag } from "@/services/cmds";
+
+export let portableFlag = false;
 
 dayjs.extend(relativeTime);
 
@@ -71,10 +74,12 @@ const Layout = () => {
           break;
       }
     });
-    setTimeout(() => {
-      void appWindow.unminimize();
-      void appWindow.show();
-      void appWindow.setFocus();
+
+    setTimeout(async () => {
+      portableFlag = await getPortableFlag();
+      await appWindow.unminimize();
+      await appWindow.show();
+      await appWindow.setFocus();
     }, 50);
   }, []);
 
@@ -119,9 +124,7 @@ const Layout = () => {
             <div className="the-logo" data-windrag>
               <LogoSvg />
 
-              {!(OS === "windows" && WIN_PORTABLE) && (
-                <UpdateButton className="the-newbtn" />
-              )}
+              {!portableFlag && <UpdateButton className="the-newbtn" />}
             </div>
 
             <List className="the-menu">

+ 4 - 0
src/services/cmds.ts

@@ -191,3 +191,7 @@ export async function invoke_uwp_tool() {
     Notice.error(err?.message || err.toString(), 1500)
   );
 }
+
+export async function getPortableFlag() {
+  return invoke<boolean>("get_portable_flag");
+}

+ 0 - 1
src/services/types.d.ts

@@ -14,7 +14,6 @@ type Platform =
 /**
  * defines in `vite.config.ts`
  */
-declare const WIN_PORTABLE: boolean;
 declare const OS_PLATFORM: Platform;
 
 /**

+ 0 - 1
vite.config.ts

@@ -25,6 +25,5 @@ export default defineConfig({
   },
   define: {
     OS_PLATFORM: `"${process.platform}"`,
-    WIN_PORTABLE: !!process.env.VITE_WIN_PORTABLE,
   },
 });