From c44fea7755c2ceb098ef66ce34f6142c00ac19b2 Mon Sep 17 00:00:00 2001 From: Uriel Date: Wed, 22 Jan 2025 16:52:27 +0100 Subject: [PATCH 1/4] improve close request checks --- flake.nix | 2 +- gui/src-tauri/capabilities/migrated.json | 11 ++----- gui/src-tauri/src/main.rs | 3 +- gui/src/components/TopBar.tsx | 38 ++++++++++++++++++++---- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/flake.nix b/flake.nix index 2bdecbac9d..d33070ad51 100644 --- a/flake.nix +++ b/flake.nix @@ -140,7 +140,7 @@ enterShell = with pkgs; '' # Export a LD_LIBRARY_PATH without libudev-zero as libgudev not likey export SLIMEVR_RUST_LD_LIBRARY_PATH="$LD_LIBRARY_PATH" - export LD_LIBRARY_PATH="${libudev-zero}/lib:$LD_LIBRARY_PATH" + export LD_LIBRARY_PATH="${libudev-zero}/lib:${libayatana-appindicator}/lib:$LD_LIBRARY_PATH" # GStreamer plugins won't be found without this export GST_PLUGIN_SYSTEM_PATH_1_0="${pkgs.gst_all_1.gstreamer.out}/lib/gstreamer-1.0:${pkgs.gst_all_1.gst-plugins-base}/lib/gstreamer-1.0:${pkgs.gst_all_1.gst-plugins-good}/lib/gstreamer-1.0:${pkgs.gst_all_1.gst-plugins-bad}/lib/gstreamer-1.0" ''; diff --git a/gui/src-tauri/capabilities/migrated.json b/gui/src-tauri/capabilities/migrated.json index 5e8916e035..c284614949 100644 --- a/gui/src-tauri/capabilities/migrated.json +++ b/gui/src-tauri/capabilities/migrated.json @@ -6,14 +6,7 @@ "main" ], "permissions": [ - "core:path:default", - "core:event:default", - "core:window:default", - "core:app:default", - "core:resources:default", - "core:menu:default", - "core:tray:default", - "core:webview:default", + "core:default", "core:window:allow-close", "core:window:allow-toggle-maximize", "core:window:allow-minimize", @@ -21,6 +14,8 @@ "core:window:allow-hide", "core:window:allow-show", "core:window:allow-set-focus", + "core:window:allow-destroy", + "core:window:allow-request-user-attention", "core:window:allow-set-decorations", "store:default", "os:allow-os-type", diff --git a/gui/src-tauri/src/main.rs b/gui/src-tauri/src/main.rs index fda7f594a1..f40da30ab0 100644 --- a/gui/src-tauri/src/main.rs +++ b/gui/src-tauri/src/main.rs @@ -13,7 +13,8 @@ use clap::Parser; use color_eyre::Result; use state::WindowState; use tauri::Emitter; -use tauri::{Manager, RunEvent, WindowEvent}; +use tauri::WindowEvent; +use tauri::{Manager, RunEvent}; use tauri_plugin_shell::process::CommandChild; use crate::util::{ diff --git a/gui/src/components/TopBar.tsx b/gui/src/components/TopBar.tsx index 4e5f76f44d..3e0ed24598 100644 --- a/gui/src/components/TopBar.tsx +++ b/gui/src/components/TopBar.tsx @@ -25,11 +25,16 @@ import { invoke } from '@tauri-apps/api/core'; import { useTrackers } from '@/hooks/tracker'; import { TrackersStillOnModal } from './TrackersStillOnModal'; import { useConfig } from '@/hooks/config'; -import { listen } from '@tauri-apps/api/event'; +import { listen, TauriEvent } from '@tauri-apps/api/event'; import { TrayOrExitModal } from './TrayOrExitModal'; import { error } from '@/utils/logging'; import { useDoubleTap } from 'use-double-tap'; import { isTrayAvailable } from '@/utils/tauri'; +import { + CloseRequestedEvent, + getCurrentWindow, + UserAttentionType, +} from '@tauri-apps/api/window'; export function VersionTag() { return ( @@ -70,10 +75,11 @@ export function TopBar({ const doesMatchSettings = useMatch({ path: '/settings/*', }); + const closeApp = async () => { await saveConfig(); await invoke('update_window_state'); - await getCurrentWebviewWindow().close(); + await getCurrentWebviewWindow().destroy(); }; const tryCloseApp = async (dontTray = false) => { if (isTrayAvailable && config?.useTray === null) { @@ -95,21 +101,38 @@ export function TopBar({ await closeApp(); } }; + const showVersionBind = useDoubleTap(() => setShowVersionMobile(true)); const unshowVersionBind = useDoubleTap(() => setShowVersionMobile(false)); useEffect(() => { - const unlisten = listen('try-close', async () => { + const unlistenTrayClose = listen('try-close', async () => { const window = getCurrentWebviewWindow(); await window.show(); + await window.requestUserAttention(UserAttentionType.Critical); await window.setFocus(); if (isTrayAvailable) await invoke('update_tray_text'); await tryCloseApp(true); }); + + const unlistenCloseRequested = getCurrentWebviewWindow().listen( + TauriEvent.WINDOW_CLOSE_REQUESTED, + async (data) => { + const ev = new CloseRequestedEvent(data); + ev.preventDefault(); + await tryCloseApp(); + } + ); + return () => { - unlisten.then((fn) => fn()); + unlistenTrayClose.then((fn) => fn()); + unlistenCloseRequested.then((fn) => fn()); }; - }, [config?.useTray, config?.connectedTrackersWarning]); + }, [ + config?.useTray, + config?.connectedTrackersWarning, + JSON.stringify(connectedIMUTrackers.map((t) => t.tracker.status)), + ]); useEffect(() => { if (config === null || !isTauri) return; @@ -304,7 +327,10 @@ export function TopBar({ closeApp()} - cancel={() => setConnectedTrackerWarning(false)} + cancel={() => { + setConnectedTrackerWarning(false); + getCurrentWindow().requestUserAttention(UserAttentionType.Critical); + }} > ); From c3c200dd519108e09ee9723b94c51d89108fb23d Mon Sep 17 00:00:00 2001 From: Uriel Date: Wed, 22 Jan 2025 16:59:18 +0100 Subject: [PATCH 2/4] use getCurrentWindow instead --- gui/src/components/TopBar.tsx | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/gui/src/components/TopBar.tsx b/gui/src/components/TopBar.tsx index 3e0ed24598..1c639e997f 100644 --- a/gui/src/components/TopBar.tsx +++ b/gui/src/components/TopBar.tsx @@ -1,4 +1,3 @@ -import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow'; import { ReactNode, useContext, useEffect, useState } from 'react'; import { NavLink, useMatch } from 'react-router-dom'; import { @@ -79,7 +78,7 @@ export function TopBar({ const closeApp = async () => { await saveConfig(); await invoke('update_window_state'); - await getCurrentWebviewWindow().destroy(); + await getCurrentWindow().destroy(); }; const tryCloseApp = async (dontTray = false) => { if (isTrayAvailable && config?.useTray === null) { @@ -88,7 +87,7 @@ export function TopBar({ } if (config?.useTray && !dontTray) { - await getCurrentWebviewWindow().hide(); + await getCurrentWindow().hide(); await invoke('update_tray_text'); } else if ( config?.connectedTrackersWarning && @@ -107,7 +106,7 @@ export function TopBar({ useEffect(() => { const unlistenTrayClose = listen('try-close', async () => { - const window = getCurrentWebviewWindow(); + const window = getCurrentWindow(); await window.show(); await window.requestUserAttention(UserAttentionType.Critical); await window.setFocus(); @@ -115,7 +114,7 @@ export function TopBar({ await tryCloseApp(true); }); - const unlistenCloseRequested = getCurrentWebviewWindow().listen( + const unlistenCloseRequested = getCurrentWindow().listen( TauriEvent.WINDOW_CLOSE_REQUESTED, async (data) => { const ev = new CloseRequestedEvent(data); @@ -136,7 +135,7 @@ export function TopBar({ useEffect(() => { if (config === null || !isTauri) return; - getCurrentWebviewWindow().setDecorations(config?.decorations).catch(error); + getCurrentWindow().setDecorations(config?.decorations).catch(error); }, [config?.decorations]); useEffect(() => { @@ -275,13 +274,13 @@ export function TopBar({ <>
getCurrentWebviewWindow().minimize()} + onClick={() => getCurrentWindow().minimize()} >
getCurrentWebviewWindow().toggleMaximize()} + onClick={() => getCurrentWindow().toggleMaximize()} >
@@ -309,7 +308,7 @@ export function TopBar({ // Doing this in here just in case config doesn't get updated in time if (useTray) { - await getCurrentWebviewWindow().hide(); + await getCurrentWindow().hide(); await invoke('update_tray_text'); } else if ( config?.connectedTrackersWarning && From 45a5cdad52e074bb5e6dfbc10ca4715fd6b342da Mon Sep 17 00:00:00 2001 From: Uriel Date: Wed, 22 Jan 2025 19:20:44 +0100 Subject: [PATCH 3/4] check if tray is still available when trying to hide --- gui/src/components/TopBar.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gui/src/components/TopBar.tsx b/gui/src/components/TopBar.tsx index 1c639e997f..33f9945617 100644 --- a/gui/src/components/TopBar.tsx +++ b/gui/src/components/TopBar.tsx @@ -86,7 +86,7 @@ export function TopBar({ return; } - if (config?.useTray && !dontTray) { + if (isTrayAvailable && config?.useTray && !dontTray) { await getCurrentWindow().hide(); await invoke('update_tray_text'); } else if ( From 46d63c1f8b028a6f8d66650d0c5bc3c635e352ea Mon Sep 17 00:00:00 2001 From: Uriel Date: Sun, 26 Jan 2025 12:38:48 -0300 Subject: [PATCH 4/4] remove user attention request --- gui/src/components/TopBar.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gui/src/components/TopBar.tsx b/gui/src/components/TopBar.tsx index 33f9945617..c9495a4d6c 100644 --- a/gui/src/components/TopBar.tsx +++ b/gui/src/components/TopBar.tsx @@ -328,7 +328,7 @@ export function TopBar({ accept={() => closeApp()} cancel={() => { setConnectedTrackerWarning(false); - getCurrentWindow().requestUserAttention(UserAttentionType.Critical); + getCurrentWindow().requestUserAttention(null); }} >