From a77b2d491ca7c83019a15940704e99e50a520176 Mon Sep 17 00:00:00 2001 From: lashman Date: Fri, 3 Apr 2026 06:10:07 +0300 Subject: [PATCH] fix tauri module resolution in browser builds - dynamic imports for window controls --- src/components/layout/AppHeader.tsx | 37 ++++++++++++++++------------- src/components/layout/Titlebar.tsx | 33 +++++++++++++++---------- 2 files changed, 41 insertions(+), 29 deletions(-) diff --git a/src/components/layout/AppHeader.tsx b/src/components/layout/AppHeader.tsx index a9d9d60..381d12d 100644 --- a/src/components/layout/AppHeader.tsx +++ b/src/components/layout/AppHeader.tsx @@ -1,6 +1,6 @@ import { useEffect, useState } from 'react' import { NavLink, useLocation, useNavigate } from 'react-router-dom' -import { getCurrentWindow } from '@tauri-apps/api/window' + import { motion, AnimatePresence } from 'framer-motion' import { Search, @@ -110,17 +110,20 @@ export default function AppHeader({ pinned, onTogglePin }: Props) { if (!isTauri) return let cancelled = false let unlisten: (() => void) | undefined - const win = getCurrentWindow() - win.isMaximized().then(v => { - if (!cancelled) setMaximized(v) - }).catch(() => {}) - win.onResized(async () => { - const v = await win.isMaximized() - if (!cancelled) setMaximized(v) - }).then(u => { - if (cancelled) u() - else unlisten = u - }).catch(() => {}) + import('@tauri-apps/api/window').then(({ getCurrentWindow }) => { + if (cancelled) return + const win = getCurrentWindow() + win.isMaximized().then(v => { + if (!cancelled) setMaximized(v) + }).catch(() => {}) + win.onResized(async () => { + const v = await win.isMaximized() + if (!cancelled) setMaximized(v) + }).then(u => { + if (cancelled) u() + else unlisten = u + }).catch(() => {}) + }) return () => { cancelled = true unlisten?.() @@ -129,10 +132,12 @@ export default function AppHeader({ pinned, onTogglePin }: Props) { function callWin(action: 'minimize' | 'toggleMaximize' | 'close') { if (!isTauri) return - const win = getCurrentWindow() - if (action === 'minimize') win.minimize() - else if (action === 'toggleMaximize') win.toggleMaximize() - else win.close() + import('@tauri-apps/api/window').then(({ getCurrentWindow }) => { + const win = getCurrentWindow() + if (action === 'minimize') win.minimize() + else if (action === 'toggleMaximize') win.toggleMaximize() + else win.close() + }) } const showBack = !TOP_LEVEL_PATHS.has(location.pathname) diff --git a/src/components/layout/Titlebar.tsx b/src/components/layout/Titlebar.tsx index 14628be..ad8cfe2 100644 --- a/src/components/layout/Titlebar.tsx +++ b/src/components/layout/Titlebar.tsx @@ -1,5 +1,4 @@ import { useEffect, useState } from 'react' -import { getCurrentWindow } from '@tauri-apps/api/window' import { Minus, Square, RestoreDown, X } from '../../lib/icons' /** @@ -13,22 +12,30 @@ import { Minus, Square, RestoreDown, X } from '../../lib/icons' */ export default function Titlebar() { const [maximized, setMaximized] = useState(false) - const win = getCurrentWindow() + const [win, setWin] = useState(null) useEffect(() => { let cancelled = false - win.isMaximized().then(v => { - if (!cancelled) setMaximized(v) - }) - const unlistenP = win.onResized(async () => { - const v = await win.isMaximized() - if (!cancelled) setMaximized(v) + let unlisten: (() => void) | undefined + import('@tauri-apps/api/window').then(({ getCurrentWindow }) => { + if (cancelled) return + const w = getCurrentWindow() + setWin(w) + w.isMaximized().then((v: boolean) => { + if (!cancelled) setMaximized(v) + }) + w.onResized(async () => { + const v = await w.isMaximized() + if (!cancelled) setMaximized(v) + }).then((u: () => void) => { + unlisten = u + }) }) return () => { cancelled = true - unlistenP.then(u => u()) + unlisten?.() } - }, [win]) + }, []) return (
- win.minimize()} aria-label="Minimize"> + win?.minimize()} aria-label="Minimize"> - win.toggleMaximize()} aria-label={maximized ? 'Restore' : 'Maximize'}> + win?.toggleMaximize()} aria-label={maximized ? 'Restore' : 'Maximize'}> {maximized ? : } - win.close()} aria-label="Close" variant="close"> + win?.close()} aria-label="Close" variant="close">