Skip to content

Commit 79f5fd6

Browse files
Fixed window undefined issue, had to wait on component to mount and use dynamic imports.
1 parent d70648d commit 79f5fd6

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/components/title-bar.tsx

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
2-
import React from "react";
3-
import { appWindow } from "@tauri-apps/api/window";
2+
import React, { useEffect, useState } from "react";
3+
import { WebviewWindow, appWindow } from "@tauri-apps/api/window";
44

55
const TitleBarButton = ({ src = "", onClick }: { onClick: any; src: any }) => {
66
return (
@@ -15,6 +15,20 @@ const TitleBarButton = ({ src = "", onClick }: { onClick: any; src: any }) => {
1515
};
1616

1717
const TitleBar = () => {
18+
const [appWindow, setAppWindow] = useState<WebviewWindow>();
19+
20+
const setupAppWindow = async () => {
21+
const appWindow = (await import("@tauri-apps/api/window")).appWindow;
22+
setAppWindow(appWindow);
23+
};
24+
25+
useEffect(() => {
26+
setupAppWindow();
27+
}, []);
28+
29+
const windowMinimize = () => appWindow?.minimize();
30+
const windowMaximize = () => appWindow?.maximize();
31+
const windowClose = () => appWindow?.close();
1832
return (
1933
<div
2034
className="w-full h-[25px] fixed top-0 flex items-center justify-end bg-gray-700 text-white webview-drag-region gap-1"
@@ -32,7 +46,7 @@ const TitleBar = () => {
3246
<path d="M20,14H4V10H20" />
3347
</svg>
3448
}
35-
onClick={() => appWindow.minimize()}
49+
onClick={windowMinimize}
3650
/>
3751
<TitleBarButton
3852
src={
@@ -45,7 +59,7 @@ const TitleBar = () => {
4559
<path fill="currentColor" d="M4 4h16v16H4zm2 4v10h12V8z" />
4660
</svg>
4761
}
48-
onClick={() => appWindow.toggleMaximize()}
62+
onClick={windowMaximize}
4963
/>
5064
<TitleBarButton
5165
src={
@@ -61,7 +75,7 @@ const TitleBar = () => {
6175
/>
6276
</svg>
6377
}
64-
onClick={() => appWindow.close()}
78+
onClick={windowClose}
6579
/>
6680
</div>
6781
);

0 commit comments

Comments
 (0)