Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

windowControlsOverlay.ts

100 lines3.2 KB

1import { useSyncExternalStore } from "react";
2​
3import { isElectron } from "~/env";
4​
5import { isMacPlatform, isWindowsPlatform } from "./utils";
6​
7const WCO_CLASS_NAME = "wco";
8const ELECTRON_CLASS_NAME = "electron";
9const ELECTRON_WINDOWS_CLASS_NAME = "electron-windows";
10// macOS has no overlay API; traffic lights stay inside this topbar at every zoom level.
11const MACOS_TITLEBAR_HEIGHT = 52;
12​
13interface WindowControlsOverlayLike {
14 readonly visible: boolean;
15 getTitlebarAreaRect(): DOMRect;
16 addEventListener(type: "geometrychange", listener: EventListener): void;
17 removeEventListener(type: "geometrychange", listener: EventListener): void;
19​
20interface NavigatorWithWindowControlsOverlay extends Navigator {
21 readonly windowControlsOverlay?: WindowControlsOverlayLike;
23​
24function getWindowControlsOverlay(): WindowControlsOverlayLike | null {
25 if (typeof navigator === "undefined") {
26 return null;
27 }
28​
29 return (navigator as NavigatorWithWindowControlsOverlay).windowControlsOverlay ?? null;
31​
32/** Height of the native caption strip drawn over the page, or 0 when the window has none. */
33export function windowControlsOverlayHeight(): number {
34 const overlay = getWindowControlsOverlay();
35 return overlay?.visible ? overlay.getTitlebarAreaRect().height : 0;
37​
38export 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 };
49​
50export 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);
63​
64function subscribeTitlebarArea(listener: () => void): () => void {
65 const overlay = getWindowControlsOverlay();
66 overlay?.addEventListener("geometrychange", listener);
67 return () => overlay?.removeEventListener("geometrychange", listener);
69​
70function getNativeTitlebarHeight(): number {
71 if (isElectron && isMacPlatform(navigator.platform)) return MACOS_TITLEBAR_HEIGHT;
72 const overlay = getWindowControlsOverlay();
73 return overlay?.visible ? overlay.getTitlebarAreaRect().bottom : 0;
75​
76export function useNativeTitlebarHeight(): number {
77 return useSyncExternalStore(subscribeTitlebarArea, getNativeTitlebarHeight, () => 0);
79​
80function 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];
89​
90export 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 };