@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 100 lines 3.22 KB
Raw
1 import { useSyncExternalStore } from "react";
2
3 import { isElectron } from "~/env";
4
5 import { isMacPlatform, isWindowsPlatform } from "./utils";
6
7 const WCO_CLASS_NAME = "wco";
8 const ELECTRON_CLASS_NAME = "electron";
9 const ELECTRON_WINDOWS_CLASS_NAME = "electron-windows";
10 // macOS has no overlay API; traffic lights stay inside this topbar at every zoom level.
11 const MACOS_TITLEBAR_HEIGHT = 52;
12
13 interface WindowControlsOverlayLike {
14 readonly visible: boolean;
15 getTitlebarAreaRect(): DOMRect;
16 addEventListener(type: "geometrychange", listener: EventListener): void;
17 removeEventListener(type: "geometrychange", listener: EventListener): void;
18 }
19
20 interface NavigatorWithWindowControlsOverlay extends Navigator {
21 readonly windowControlsOverlay?: WindowControlsOverlayLike;
22 }
23
24 function getWindowControlsOverlay(): WindowControlsOverlayLike | null {
25 if (typeof navigator === "undefined") {
26 return null;
27 }
28
29 return (navigator as NavigatorWithWindowControlsOverlay).windowControlsOverlay ?? null;
30 }
31
32 /** Height of the native caption strip drawn over the page, or 0 when the window has none. */
33 export function windowControlsOverlayHeight(): number {
34 const overlay = getWindowControlsOverlay();
35 return overlay?.visible ? overlay.getTitlebarAreaRect().height : 0;
36 }
37
38 export function onWindowControlsOverlayGeometryChange(listener: () => void): () => void {
39 const overlay = getWindowControlsOverlay();
40 if (!overlay) {
41 return () => {};
42 }
43
44 overlay.addEventListener("geometrychange", listener);
45 return () => {
46 overlay.removeEventListener("geometrychange", listener);
47 };
48 }
49
50 export function syncDocumentWindowControlsOverlayClass(): () => void {
51 if (typeof document === "undefined") {
52 return () => {};
53 }
54
55 const overlay = getWindowControlsOverlay();
56 const update = () => {
57 document.documentElement.classList.toggle(WCO_CLASS_NAME, overlay !== null && overlay.visible);
58 };
59
60 update();
61 return onWindowControlsOverlayGeometryChange(update);
62 }
63
64 function subscribeTitlebarArea(listener: () => void): () => void {
65 const overlay = getWindowControlsOverlay();
66 overlay?.addEventListener("geometrychange", listener);
67 return () => overlay?.removeEventListener("geometrychange", listener);
68 }
69
70 function getNativeTitlebarHeight(): number {
71 if (isElectron && isMacPlatform(navigator.platform)) return MACOS_TITLEBAR_HEIGHT;
72 const overlay = getWindowControlsOverlay();
73 return overlay?.visible ? overlay.getTitlebarAreaRect().bottom : 0;
74 }
75
76 export function useNativeTitlebarHeight(): number {
77 return useSyncExternalStore(subscribeTitlebarArea, getNativeTitlebarHeight, () => 0);
78 }
79
80 function getElectronPlatformClassNames(
81 platform: string,
82 ):
83 | readonly [typeof ELECTRON_CLASS_NAME]
84 | readonly [typeof ELECTRON_CLASS_NAME, typeof ELECTRON_WINDOWS_CLASS_NAME] {
85 return isWindowsPlatform(platform)
86 ? [ELECTRON_CLASS_NAME, ELECTRON_WINDOWS_CLASS_NAME]
87 : [ELECTRON_CLASS_NAME];
88 }
89
90 export function syncDocumentElectronPlatformClasses(platform: string): () => void {
91 if (typeof document === "undefined") {
92 return () => {};
93 }
94
95 const classNames = getElectronPlatformClassNames(platform);
96 document.documentElement.classList.add(...classNames);
97 return () => {
98 document.documentElement.classList.remove(...classNames);
99 };
100 }