Skip to content

Commit

Permalink
fix(portable): do not use system notification api while app is portab…
Browse files Browse the repository at this point in the history
…le (#334)

* fix(updater): use release body as updater note

* fix(portable): do not use system notification api while app is portable

* fix: lint

* fix: lint
  • Loading branch information
greenhat616 authored Jan 27, 2024
1 parent 9db0bac commit e9abc47
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
12 changes: 12 additions & 0 deletions backend/tauri/src/cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ pub fn get_profiles() -> CmdResult<IProfiles> {
Ok(Config::profiles().data().clone())
}

#[cfg(target_os = "windows")]
#[tauri::command]
pub fn is_portable() -> CmdResult<bool> {
Ok(crate::utils::dirs::get_portable_flag())
}

#[cfg(not(target_os = "windows"))]
#[tauri::command]
pub fn is_portable() -> CmdResult<bool> {
Ok(false)
}

#[tauri::command]
pub async fn enhance_profiles() -> CmdResult {
wrap_err!(CoreManager::global().update_config().await)?;
Expand Down
1 change: 1 addition & 0 deletions backend/tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ fn main() -> std::io::Result<()> {
cmds::service::check_service,
cmds::service::install_service,
cmds::service::uninstall_service,
cmds::is_portable,
]);

#[cfg(target_os = "macos")]
Expand Down
9 changes: 7 additions & 2 deletions src/hooks/use-notification.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Notice } from "@/components/base";
import { isPortable } from "@/services/cmds";
import {
Options,
isPermissionGranted,
requestPermission,
sendNotification,
} from "@tauri-apps/api/notification";
let permissionGranted: boolean | null = null;
let portable: boolean | null = null;

const checkPermission = async () => {
if (permissionGranted == null) {
Expand Down Expand Up @@ -39,8 +41,11 @@ export const useNotification = async ({
if (!title) {
throw new Error("missing message argument!");
}
const permissionGranted = await checkPermission();
if (!permissionGranted) {
if (portable === null) {
portable = await isPortable();
}
const permissionGranted = portable || (await checkPermission());
if (portable || !permissionGranted) {
// fallback to mui notification
Notice[type](`${title} ${body ? `: ${body}` : ""}}`);
// throw new Error("notification permission not granted!");
Expand Down
5 changes: 5 additions & 0 deletions src/services/cmds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,8 @@ export async function cmdGetProxyDelay(name: string, url?: string) {
name = encodeURIComponent(name);
return invoke<{ delay: number }>("clash_api_get_proxy_delay", { name, url });
}

export async function isPortable() {
if (OS_PLATFORM !== "win32") return false;
return invoke<boolean>("is_portable");
}

0 comments on commit e9abc47

Please sign in to comment.