/* eslint-disable react-refresh/only-export-components */ import { useEffect, useState } from "react"; import { isElectron } from "@/lib/electron"; import { useBranding } from "@/contexts/BrandingContext"; /** * The height of the bar the desktop app draws on top of its window. The * Electron main process uses the same number for the window controls the * system draws, so both line up. */ export const TITLE_BAR_HEIGHT = 40; /** * How far content starts below the top of the window. The bar sets the * variable only while it shows, so it is 0 on the web, in full screen and in * windows that keep the native title bar. */ export const TITLE_BAR_OFFSET = "var(--termix-titlebar-height, 0px)"; /** Room the macOS traffic lights take on the left. */ const MAC_TRAFFIC_LIGHTS_WIDTH = 78; /** Room the Windows and Linux window controls take on the right. */ const OVERLAY_CONTROLS_WIDTH = 140; type Platform = "darwin" | "win32" | "linux" | "other"; /** * The title bar of the desktop app. The system draws its own frame, so the * bar is drawn by the app instead: it takes the colors of the active theme * and keeps the window controls the system draws on top of it. */ export function TitleBar() { const [platform, setPlatform] = useState(null); const [visible, setVisible] = useState(false); useEffect(() => { if (!isElectron()) return; let cancelled = false; const api = window.electronAPI; if (typeof api?.getPlatform === "function") { void api .getPlatform() .then((value: string) => { if (!cancelled) setPlatform(value as Platform); }) .catch(() => {}); } else { // Without the bridge, fall back to what the user agent says. setPlatform(/mac/i.test(navigator.userAgent) ? "darwin" : "other"); } // Older builds without the visibility bridge always show the bar. if (typeof api?.getTitleBarVisible === "function") { void api .getTitleBarVisible() .then((value) => { if (!cancelled) setVisible(value); }) .catch(() => {}); } else { setVisible(true); } const unsubscribe = api?.onTitleBarVisibleChange?.(setVisible); return () => { cancelled = true; unsubscribe?.(); }; }, []); const shown = isElectron() && platform !== null && visible; useEffect(() => { if (!shown) return; const root = document.documentElement; root.style.setProperty("--termix-titlebar-height", `${TITLE_BAR_HEIGHT}px`); return () => { root.style.removeProperty("--termix-titlebar-height"); }; }, [shown]); if (!shown) return null; const isMac = platform === "darwin"; const hasOverlayControls = platform === "win32" || platform === "linux"; return (
{isMac && (