From 838110430289c4e55188697e2b856e0b257cbd97 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Fri, 10 Nov 2023 15:55:50 -0800 Subject: [PATCH] Sync window title (Closes #13) --- src-tauri/Cargo.toml | 1 + src-tauri/tauri.conf.json | 3 ++- src-web/components/GlobalHooks.tsx | 3 +++ src-web/hooks/useSyncWindowTitle.ts | 22 ++++++++++++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src-web/hooks/useSyncWindowTitle.ts diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 97a67e79..db5f58a3 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -41,6 +41,7 @@ tauri = { version = "1.3", features = [ "window-start-dragging", "dialog-open", "dialog-save", + "window-set-title", ] } tauri-plugin-window-state = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" } tauri-plugin-log = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1", features = ["colored"] } diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 7d492820..d18f858d 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -51,7 +51,8 @@ "open": true }, "window": { - "startDragging": true + "startDragging": true, + "setTitle": true }, "dialog": { "all": false, diff --git a/src-web/components/GlobalHooks.tsx b/src-web/components/GlobalHooks.tsx index fca2c7aa..3c1c7f37 100644 --- a/src-web/components/GlobalHooks.tsx +++ b/src-web/components/GlobalHooks.tsx @@ -11,6 +11,7 @@ import { useRecentWorkspaces } from '../hooks/useRecentWorkspaces'; import { requestsQueryKey } from '../hooks/useRequests'; import { useRequestUpdateKey } from '../hooks/useRequestUpdateKey'; import { responsesQueryKey } from '../hooks/useResponses'; +import { useSyncWindowTitle } from '../hooks/useSyncWindowTitle'; import { workspacesQueryKey } from '../hooks/useWorkspaces'; import { trackPage } from '../lib/analytics'; import { DEFAULT_FONT_SIZE } from '../lib/constants'; @@ -26,6 +27,8 @@ export function GlobalHooks() { useRecentEnvironments(); useRecentRequests(); + useSyncWindowTitle(); + const queryClient = useQueryClient(); const { wasUpdatedExternally } = useRequestUpdateKey(null); diff --git a/src-web/hooks/useSyncWindowTitle.ts b/src-web/hooks/useSyncWindowTitle.ts new file mode 100644 index 00000000..4e71bcac --- /dev/null +++ b/src-web/hooks/useSyncWindowTitle.ts @@ -0,0 +1,22 @@ +import { appWindow } from '@tauri-apps/api/window'; +import { useEffect } from 'react'; +import { fallbackRequestName } from '../lib/fallbackRequestName'; +import { useActiveEnvironment } from './useActiveEnvironment'; +import { useActiveRequest } from './useActiveRequest'; +import { useActiveWorkspace } from './useActiveWorkspace'; + +export function useSyncWindowTitle() { + const activeRequest = useActiveRequest(); + const activeWorkspace = useActiveWorkspace(); + const activeEnvironment = useActiveEnvironment(); + useEffect(() => { + let newTitle = activeWorkspace ? activeWorkspace.name : 'Yaak'; + if (activeEnvironment) { + newTitle += ` [${activeEnvironment.name}]`; + } + if (activeRequest) { + newTitle += ` – ${fallbackRequestName(activeRequest)}`; + } + appWindow.setTitle(newTitle).catch(console.error); + }, [activeEnvironment, activeRequest, activeWorkspace]); +}