|
| 1 | +/** |
| 2 | + * @file src/renderer/components/layout/TitleBar.tsx |
| 3 | + * |
| 4 | + * @created 08.03.2026 |
| 5 | + * @modified 08.03.2026 |
| 6 | + * |
| 7 | + * @author Christian Blank <christianblank91@protonmail.com> |
| 8 | + * @copyright 2026 |
| 9 | + * |
| 10 | + * @description Custom Electron title bar rendered in the renderer process. |
| 11 | + * Replaces the OS-native title bar (hidden via `titleBarStyle: 'hidden'`). |
| 12 | + * Provides an app label, a full-width drag region, and Win32-style window |
| 13 | + * control buttons (minimize, maximize/restore, close) that delegate to the |
| 14 | + * main process via IPC. |
| 15 | + */ |
| 16 | + |
| 17 | +import { useEffect, useState } from 'react' |
| 18 | +import { Minus, Square, Copy, X } from 'lucide-react' |
| 19 | +import '../../lib/electron.d' |
| 20 | + |
| 21 | +/** |
| 22 | + * Renders the custom application title bar with a drag region and window |
| 23 | + * control buttons. Subscribes to the `window:maximize-changed` push event |
| 24 | + * so the maximize/restore icon stays in sync with the actual window state. |
| 25 | + */ |
| 26 | +const TitleBar = () => { |
| 27 | + const [isMaximized, setIsMaximized] = useState(false) |
| 28 | + |
| 29 | + useEffect(() => { |
| 30 | + const cleanup = window.api.onMaximizeChanged(({ isMaximized: maximized }) => { |
| 31 | + setIsMaximized(maximized) |
| 32 | + }) |
| 33 | + return cleanup |
| 34 | + }, []) |
| 35 | + |
| 36 | + return ( |
| 37 | + <header |
| 38 | + className="flex h-8 shrink-0 items-center justify-between bg-background border-b select-none" |
| 39 | + style={{ WebkitAppRegion: 'drag' }} |
| 40 | + data-testid="title-bar" |
| 41 | + > |
| 42 | + <span className="pl-3 text-xs text-muted-foreground" data-testid="title-bar-label"> |
| 43 | + aidrelay |
| 44 | + </span> |
| 45 | + |
| 46 | + <div |
| 47 | + className="flex h-full" |
| 48 | + style={{ WebkitAppRegion: 'no-drag' }} |
| 49 | + data-testid="title-bar-controls" |
| 50 | + > |
| 51 | + {/* Minimize */} |
| 52 | + <button |
| 53 | + type="button" |
| 54 | + aria-label="Minimize window" |
| 55 | + className="flex w-12 h-full items-center justify-center text-muted-foreground hover:bg-muted transition-colors" |
| 56 | + onClick={() => void window.api.windowMinimize()} |
| 57 | + data-testid="title-bar-minimize" |
| 58 | + > |
| 59 | + <Minus size={14} /> |
| 60 | + </button> |
| 61 | + |
| 62 | + {/* Maximize / Restore */} |
| 63 | + <button |
| 64 | + type="button" |
| 65 | + aria-label={isMaximized ? 'Restore window' : 'Maximize window'} |
| 66 | + className="flex w-12 h-full items-center justify-center text-muted-foreground hover:bg-muted transition-colors" |
| 67 | + onClick={() => void window.api.windowMaximize()} |
| 68 | + data-testid="title-bar-maximize" |
| 69 | + > |
| 70 | + {isMaximized ? <Copy size={14} /> : <Square size={14} />} |
| 71 | + </button> |
| 72 | + |
| 73 | + {/* Close */} |
| 74 | + <button |
| 75 | + type="button" |
| 76 | + aria-label="Close window" |
| 77 | + className="flex w-12 h-full items-center justify-center text-muted-foreground hover:bg-destructive hover:text-destructive-foreground transition-colors" |
| 78 | + onClick={() => void window.api.windowClose()} |
| 79 | + data-testid="title-bar-close" |
| 80 | + > |
| 81 | + <X size={14} /> |
| 82 | + </button> |
| 83 | + </div> |
| 84 | + </header> |
| 85 | + ) |
| 86 | +} |
| 87 | + |
| 88 | +export { TitleBar } |
0 commit comments