Skip to content

Commit

Permalink
Improve close window request checks (#1285)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImUrX authored Jan 29, 2025
1 parent d102cb3 commit eb08cb5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 24 deletions.
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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"
'';
Expand Down
11 changes: 3 additions & 8 deletions gui/src-tauri/capabilities/migrated.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,16 @@
"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",
"core:window:allow-start-dragging",
"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",
Expand Down
3 changes: 2 additions & 1 deletion gui/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down
53 changes: 39 additions & 14 deletions gui/src/components/TopBar.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -25,11 +24,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 (
Expand Down Expand Up @@ -70,19 +74,20 @@ export function TopBar({
const doesMatchSettings = useMatch({
path: '/settings/*',
});

const closeApp = async () => {
await saveConfig();
await invoke('update_window_state');
await getCurrentWebviewWindow().close();
await getCurrentWindow().destroy();
};
const tryCloseApp = async (dontTray = false) => {
if (isTrayAvailable && config?.useTray === null) {
setShowTrayOrExitModal(true);
return;
}

if (config?.useTray && !dontTray) {
await getCurrentWebviewWindow().hide();
if (isTrayAvailable && config?.useTray && !dontTray) {
await getCurrentWindow().hide();
await invoke('update_tray_text');
} else if (
config?.connectedTrackersWarning &&
Expand All @@ -95,25 +100,42 @@ export function TopBar({
await closeApp();
}
};

const showVersionBind = useDoubleTap(() => setShowVersionMobile(true));
const unshowVersionBind = useDoubleTap(() => setShowVersionMobile(false));

useEffect(() => {
const unlisten = listen('try-close', async () => {
const window = getCurrentWebviewWindow();
const unlistenTrayClose = listen('try-close', async () => {
const window = getCurrentWindow();
await window.show();
await window.requestUserAttention(UserAttentionType.Critical);
await window.setFocus();
if (isTrayAvailable) await invoke('update_tray_text');
await tryCloseApp(true);
});

const unlistenCloseRequested = getCurrentWindow().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;
getCurrentWebviewWindow().setDecorations(config?.decorations).catch(error);
getCurrentWindow().setDecorations(config?.decorations).catch(error);
}, [config?.decorations]);

useEffect(() => {
Expand Down Expand Up @@ -252,13 +274,13 @@ export function TopBar({
<>
<div
className="flex items-center justify-center hover:bg-background-60 rounded-full w-7 h-7"
onClick={() => getCurrentWebviewWindow().minimize()}
onClick={() => getCurrentWindow().minimize()}
>
<MinimiseIcon></MinimiseIcon>
</div>
<div
className="flex items-center justify-center hover:bg-background-60 rounded-full w-7 h-7"
onClick={() => getCurrentWebviewWindow().toggleMaximize()}
onClick={() => getCurrentWindow().toggleMaximize()}
>
<MaximiseIcon></MaximiseIcon>
</div>
Expand Down Expand Up @@ -286,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 &&
Expand All @@ -304,7 +326,10 @@ export function TopBar({
<TrackersStillOnModal
isOpen={showConnectedTrackersWarning}
accept={() => closeApp()}
cancel={() => setConnectedTrackerWarning(false)}
cancel={() => {
setConnectedTrackerWarning(false);
getCurrentWindow().requestUserAttention(null);
}}
></TrackersStillOnModal>
</>
);
Expand Down

0 comments on commit eb08cb5

Please sign in to comment.