mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-26 20:06:26 -08:00
fix(tray): avoid reading rooms hidden to tray
This commit is contained in:
3 files changed
+52
-12
No files matched your search
@@ -10,7 +10,7 @@ use serde_json::json;
|
||||
use tauri::{
|
||||
menu::{Menu, MenuEvent, MenuItem},
|
||||
tray::TrayIconBuilder,
|
||||
AppHandle, Manager, RunEvent, WebviewWindow,
|
||||
AppHandle, Emitter, Manager, RunEvent, WebviewWindow,
|
||||
};
|
||||
use tauri_plugin_store::StoreExt;
|
||||
|
||||
@@ -18,6 +18,7 @@ use tauri_plugin_store::StoreExt;
|
||||
use tauri::tray::{MouseButton, TrayIconEvent};
|
||||
|
||||
pub(crate) const MAIN_TRAY_ID: &str = "main";
|
||||
pub(crate) const WINDOW_HIDDEN_TO_TRAY_EVENT: &str = "window-hidden-to-tray";
|
||||
const TRAY_MENU_SHOW_ID: &str = "tray_show";
|
||||
const TRAY_MENU_QUIT_ID: &str = "tray_quit";
|
||||
|
||||
@@ -52,6 +53,9 @@ pub fn setup_close_to_background(webview_window: &WebviewWindow<crate::BrowserEn
|
||||
})
|
||||
{
|
||||
api.prevent_close();
|
||||
// Hiding does not reliably emit a native blur event, so notify the
|
||||
// webview before it can process new timeline events as read.
|
||||
let _ = window.emit(WINDOW_HIDDEN_TO_TRAY_EVENT, ());
|
||||
let _ = window.hide();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -3,14 +3,22 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { TauriWindowFocus } from './TauriWindowFocus';
|
||||
import { isWindowFocused, setNativeWindowFocused, subscribeWindowFocus } from '$utils/dom';
|
||||
|
||||
const { mockIsTauri, mockOsType, mockIsFocused, mockOnFocusChanged, mockGetCurrentWindow } =
|
||||
vi.hoisted(() => ({
|
||||
mockIsTauri: vi.fn<() => boolean>(),
|
||||
mockOsType: vi.fn<() => string>(),
|
||||
mockIsFocused: vi.fn<() => Promise<boolean>>(),
|
||||
mockOnFocusChanged: vi.fn<(cb: (e: { payload: boolean }) => void) => Promise<() => void>>(),
|
||||
mockGetCurrentWindow: vi.fn<() => unknown>(),
|
||||
}));
|
||||
const {
|
||||
mockIsTauri,
|
||||
mockOsType,
|
||||
mockIsFocused,
|
||||
mockOnFocusChanged,
|
||||
mockListen,
|
||||
mockGetCurrentWindow,
|
||||
} = vi.hoisted(() => ({
|
||||
mockIsTauri: vi.fn<() => boolean>(),
|
||||
mockOsType: vi.fn<() => string>(),
|
||||
mockIsFocused: vi.fn<() => Promise<boolean>>(),
|
||||
mockOnFocusChanged: vi.fn<(cb: (e: { payload: boolean }) => void) => Promise<() => void>>(),
|
||||
mockListen:
|
||||
vi.fn<(event: string, cb: (e: { payload: unknown }) => void) => Promise<() => void>>(),
|
||||
mockGetCurrentWindow: vi.fn<() => unknown>(),
|
||||
}));
|
||||
|
||||
vi.mock('@tauri-apps/api/core', () => ({ isTauri: mockIsTauri }));
|
||||
vi.mock('@tauri-apps/plugin-os', () => ({ type: mockOsType }));
|
||||
@@ -28,9 +36,11 @@ describe('TauriWindowFocus', () => {
|
||||
mockOsType.mockReturnValue('linux');
|
||||
mockIsFocused.mockResolvedValue(true);
|
||||
mockOnFocusChanged.mockResolvedValue(() => {});
|
||||
mockListen.mockResolvedValue(() => {});
|
||||
mockGetCurrentWindow.mockReturnValue({
|
||||
isFocused: mockIsFocused,
|
||||
onFocusChanged: mockOnFocusChanged,
|
||||
listen: mockListen,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -64,6 +74,18 @@ describe('TauriWindowFocus', () => {
|
||||
unsubscribe();
|
||||
});
|
||||
|
||||
it('treats a window hidden to tray as unfocused', async () => {
|
||||
render(<TauriWindowFocus />);
|
||||
await waitFor(() =>
|
||||
expect(mockListen).toHaveBeenCalledWith('window-hidden-to-tray', expect.any(Function))
|
||||
);
|
||||
|
||||
const hide = mockListen.mock.calls[0]![1];
|
||||
hide({ payload: undefined });
|
||||
|
||||
expect(isWindowFocused()).toBe(false);
|
||||
});
|
||||
|
||||
it('does not subscribe on mobile, leaving the DOM path authoritative', async () => {
|
||||
mockOsType.mockReturnValue('android');
|
||||
render(<TauriWindowFocus />);
|
||||
|
||||
@@ -6,6 +6,7 @@ import { setNativeWindowFocused } from '$utils/dom';
|
||||
import { createLogger } from '$utils/debug';
|
||||
|
||||
const log = createLogger('TauriWindowFocus');
|
||||
const WINDOW_HIDDEN_TO_TRAY_EVENT = 'window-hidden-to-tray';
|
||||
|
||||
// Mobile has no window focus to report; the DOM path stays authoritative there.
|
||||
const DESKTOP = new Set(['windows', 'linux', 'macos']);
|
||||
@@ -14,7 +15,8 @@ export function TauriWindowFocus() {
|
||||
useEffect(() => {
|
||||
if (!isTauri() || !DESKTOP.has(osType())) return undefined;
|
||||
|
||||
let unlisten: (() => void) | undefined;
|
||||
let unlistenFocus: (() => void) | undefined;
|
||||
let unlistenTrayHide: (() => void) | undefined;
|
||||
let cancelled = false;
|
||||
// isFocused() is an IPC round-trip and can resolve after a newer focus change.
|
||||
let sawEvent = false;
|
||||
@@ -35,13 +37,25 @@ export function TauriWindowFocus() {
|
||||
})
|
||||
.then((stop) => {
|
||||
if (cancelled) stop();
|
||||
else unlisten = stop;
|
||||
else unlistenFocus = stop;
|
||||
})
|
||||
.catch((error: unknown) => log.warn('Failed to subscribe to window focus:', error));
|
||||
|
||||
appWindow
|
||||
.listen(WINDOW_HIDDEN_TO_TRAY_EVENT, () => {
|
||||
sawEvent = true;
|
||||
setNativeWindowFocused(false);
|
||||
})
|
||||
.then((stop) => {
|
||||
if (cancelled) stop();
|
||||
else unlistenTrayHide = stop;
|
||||
})
|
||||
.catch((error: unknown) => log.warn('Failed to subscribe to tray hide:', error));
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
unlisten?.();
|
||||
unlistenFocus?.();
|
||||
unlistenTrayHide?.();
|
||||
setNativeWindowFocused(undefined);
|
||||
};
|
||||
}, []);
|
||||
|
||||
Reference in new issue
Block a user