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

windowControlsOverlay.ts

102 lines3.4 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// The workspace topbar the desktop app centers the traffic lights in. macOS has
11// no overlay API to measure, and the lights stay inside it at any zoom level.
12const MACOS_TITLEBAR_HEIGHT = 52;
13​
14interface WindowControlsOverlayLike {
15 readonly visible: boolean;
16 getTitlebarAreaRect(): DOMRect;
17 addEventListener(type: "geometrychange", listener: EventListener): void;
18 removeEventListener(type: "geometrychange", listener: EventListener): void;
20​
21interface NavigatorWithWindowControlsOverlay extends Navigator {
22 readonly windowControlsOverlay?: WindowControlsOverlayLike;
24​
25function getWindowControlsOverlay(): WindowControlsOverlayLike | null {
26 if (typeof navigator === "undefined") {
27 return null;
28 }
29​
30 return (navigator as NavigatorWithWindowControlsOverlay).windowControlsOverlay ?? null;
32​
33/** Height of the native caption strip drawn over the page, or 0 when the window has none. */
34export function windowControlsOverlayHeight(): number {
35 const overlay = getWindowControlsOverlay();
36 return overlay?.visible ? overlay.getTitlebarAreaRect().height : 0;
38​
39/** Calls `listener` when the native caption strip appears, disappears, or changes size. */
40export function onWindowControlsOverlayGeometryChange(listener: () => void): () => void {
41 const overlay = getWindowControlsOverlay();
42 if (!overlay) {
43 return () => {};
44 }
45​
46 overlay.addEventListener("geometrychange", listener);
47 return () => {
48 overlay.removeEventListener("geometrychange", listener);
49 };
51​
52export function syncDocumentWindowControlsOverlayClass(): () => void {
53 if (typeof document === "undefined") {
54 return () => {};
55 }
56​
57 const overlay = getWindowControlsOverlay();
58 const update = () => {
59 document.documentElement.classList.toggle(WCO_CLASS_NAME, overlay !== null && overlay.visible);
60 };
61​
62 update();
63 return onWindowControlsOverlayGeometryChange(update);
65​
66function subscribeTitlebarArea(listener: () => void): () => void {
67 const overlay = getWindowControlsOverlay();
68 overlay?.addEventListener("geometrychange", listener);
69 return () => overlay?.removeEventListener("geometrychange", listener);
71​
72function getNativeTitlebarHeight(): number {
73 if (isElectron && isMacPlatform(navigator.platform)) return MACOS_TITLEBAR_HEIGHT;
74 const overlay = getWindowControlsOverlay();
75 return overlay?.visible ? overlay.getTitlebarAreaRect().bottom : 0;
77​
78export function useNativeTitlebarHeight(): number {
79 return useSyncExternalStore(subscribeTitlebarArea, getNativeTitlebarHeight, () => 0);
81​
82function getElectronPlatformClassNames(
83 platform: string,
84):
85 | readonly [typeof ELECTRON_CLASS_NAME]
86 | readonly [typeof ELECTRON_CLASS_NAME, typeof ELECTRON_WINDOWS_CLASS_NAME] {
87 return isWindowsPlatform(platform)
88 ? [ELECTRON_CLASS_NAME, ELECTRON_WINDOWS_CLASS_NAME]
89 : [ELECTRON_CLASS_NAME];
91​
92export function syncDocumentElectronPlatformClasses(platform: string): () => void {
93 if (typeof document === "undefined") {
94 return () => {};
95 }
96​
97 const classNames = getElectronPlatformClassNames(platform);
98 document.documentElement.classList.add(...classNames);
99 return () => {
100 document.documentElement.classList.remove(...classNames);
101 };