-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy path(window-chrome).tsx
More file actions
123 lines (108 loc) · 3.05 KB
/
Copy path(window-chrome).tsx
File metadata and controls
123 lines (108 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import type { RouteSectionProps } from "@solidjs/router";
import type { UnlistenFn } from "@tauri-apps/api/event";
import { getCurrentWindow } from "@tauri-apps/api/window";
import { type as ostype } from "@tauri-apps/plugin-os";
import { cx } from "cva";
import { onCleanup, onMount, type ParentProps, Suspense } from "solid-js";
import { AbsoluteInsetLoader } from "~/components/Loader";
import CaptionControlsMacOS from "~/components/titlebar/controls/CaptionControlsMacOS";
import CaptionControlsWindows11 from "~/components/titlebar/controls/CaptionControlsWindows11";
import { initializeTitlebar } from "~/utils/titlebar-state";
import {
useWindowChromeContext,
WindowChromeContext,
} from "./(window-chrome)/Context";
export default function (props: RouteSectionProps) {
let unlistenResize: UnlistenFn | undefined;
onMount(async () => {
console.log("window chrome mounted");
void initializeTitlebar().then((unlisten) => {
unlistenResize = unlisten;
});
});
const handleKeyDown = (e: KeyboardEvent) => {
const isMac = ostype() === "macos";
const closeShortcut = isMac
? e.metaKey && e.key === "w"
: e.ctrlKey && e.key === "w";
if (closeShortcut) {
e.preventDefault();
getCurrentWindow().close();
}
};
onMount(() => {
window.addEventListener("keydown", handleKeyDown);
});
onCleanup(() => {
unlistenResize?.();
window.removeEventListener("keydown", handleKeyDown);
});
const isMacOS = ostype() === "macos";
return (
<WindowChromeContext>
<div
class={cx(
"flex overflow-hidden flex-col w-screen h-screen max-h-screen divide-y divide-gray-5 bg-gray-1",
isMacOS && "rounded-[16px]",
)}
>
<Header />
{/* breaks sometimes */}
{/* <Transition
mode="outin"
enterActiveClass="transition-opacity duration-100"
exitActiveClass="transition-opacity duration-100"
enterClass="opacity-0"
exitToClass="opacity-0"
> */}
<Suspense fallback={<AbsoluteInsetLoader />}>
<Inner>
<Suspense fallback={null}>{props.children}</Suspense>
</Inner>
</Suspense>
{/* </Transition> */}
</div>
</WindowChromeContext>
);
}
function Header() {
const ctx = useWindowChromeContext();
if (!ctx)
throw new Error(
"useWindowChrome must be used within a WindowChromeContext",
);
const isWindows = ostype() === "windows";
const isMacOS = ostype() === "macos";
return (
<header
class={cx(
"flex items-center min-w-0 w-full h-9 select-none shrink-0 bg-gray-2",
isWindows ? "flex-row" : "flex-row-reverse",
)}
data-tauri-drag-region
>
{ctx.state()?.items}
{isWindows && <CaptionControlsWindows11 class="ml-auto!" />}
{isMacOS && (
<CaptionControlsMacOS
class="mr-auto! ml-3"
showMinimize={false}
showZoom={false}
/>
)}
</header>
);
}
function Inner(props: ParentProps) {
onMount(() => {
if (location.pathname !== "/") getCurrentWindow().show();
});
return (
<div
data-tauri-drag-region="none"
class="flex overflow-y-hidden flex-col flex-1 animate-in fade-in"
>
{props.children}
</div>
);
}