diff --git a/src/main/window/createMainWindow.test.ts b/src/main/window/createMainWindow.test.ts index acd93f539..6de088db2 100644 --- a/src/main/window/createMainWindow.test.ts +++ b/src/main/window/createMainWindow.test.ts @@ -97,6 +97,10 @@ describe('createMainWindow', () => { expect(browserWindowOptions).toMatchObject({ titleBarStyle: 'hiddenInset' }) + } else if (process.platform === 'win32') { + expect(browserWindowOptions).toMatchObject({ + titleBarStyle: 'hidden' + }) } else { expect(browserWindowOptions.titleBarStyle).toBeUndefined() } diff --git a/src/main/window/createMainWindow.ts b/src/main/window/createMainWindow.ts index ec92d3a93..b0dd4fc67 100644 --- a/src/main/window/createMainWindow.ts +++ b/src/main/window/createMainWindow.ts @@ -1,5 +1,5 @@ /* oxlint-disable max-lines */ -import { app, BrowserWindow, ipcMain, nativeTheme, screen, shell } from 'electron' +import { app, BrowserWindow, ipcMain, Menu, nativeTheme, screen, shell } from 'electron' import { join } from 'path' import { is } from '@electron-toolkit/utils' import icon from '../../../resources/icon.png?asset' @@ -155,7 +155,17 @@ export function createMainWindow( // conventions (File Explorer, Firefox, etc.). autoHideMenuBar: true, backgroundColor: nativeTheme.shouldUseDarkColors ? '#0a0a0a' : '#ffffff', - titleBarStyle: process.platform === 'darwin' ? 'hiddenInset' : undefined, + // Why: on macOS 'hiddenInset' keeps the native traffic lights positioned + // inside our custom 42px titlebar. On Windows 'hidden' removes the default + // OS title bar (which would otherwise stack on top of our renderer titlebar + // and waste vertical space) while still allowing our renderer to draw its + // own drag region and window controls. + titleBarStyle: + process.platform === 'darwin' + ? 'hiddenInset' + : process.platform === 'win32' + ? 'hidden' + : undefined, // Why: initial position for 1x zoom; syncTrafficLightPosition() adjusts // dynamically when the user changes UI zoom. ...(process.platform === 'darwin' @@ -296,11 +306,13 @@ export function createMainWindow( return } store?.updateUI({ windowMaximized: true }) + mainWindow.webContents.send('window:maximize-changed', true) }) mainWindow.on('unmaximize', () => { if (windowClosing) { return } + mainWindow.webContents.send('window:maximize-changed', false) const bounds = mainWindow.getBounds() // Why: mirror the saveBounds guard — unmaximize during teardown can land // at MIN_WIDTH × MIN_HEIGHT and we must not persist those as the user's @@ -590,6 +602,51 @@ export function createMainWindow( } ipcMain.on(trafficLightChannel, onSyncTrafficLights) + // Why: renderer-drawn window controls on Windows send these to replicate the + // native title bar buttons that 'hidden' titleBarStyle removes. + const minimizeChannel = 'window:minimize' + const onMinimize = (): void => { + if (!mainWindow.isDestroyed()) { + mainWindow.minimize() + } + } + const maximizeChannel = 'window:maximize' + const onMaximize = (): void => { + if (mainWindow.isDestroyed()) { + return + } + if (mainWindow.isMaximized()) { + mainWindow.unmaximize() + } else { + mainWindow.maximize() + } + } + // Why: send window:close-requested directly rather than calling + // mainWindow.close() and letting the 'close' event re-send it. Calling + // mainWindow.close() from within an IPC message handler on Windows can cause + // the 'close' event to misfire (e.preventDefault() doesn't suppress the OS + // close in all Windows configurations). Going straight to the renderer's + // close guard (Terminal.tsx onWindowCloseRequested) keeps the flow identical + // to what happens when confirmWindowClose() ultimately calls mainWindow.close() + // with windowCloseConfirmed = true. + const requestCloseChannel = 'window:request-close' + const onRequestClose = (): void => { + if (!mainWindow.isDestroyed()) { + mainWindow.webContents.send('window:close-requested', { isQuitting: false }) + } + } + // Why: the ··· button in the renderer-drawn title bar on Windows pops up + // the application menu at the cursor position, replicating the Alt-key + // reveal that autoHideMenuBar normally provides. + const popupMenuChannel = 'menu:popup' + const onPopupMenu = (): void => { + Menu.getApplicationMenu()?.popup({ window: mainWindow }) + } + ipcMain.on(minimizeChannel, onMinimize) + ipcMain.on(maximizeChannel, onMaximize) + ipcMain.on(requestCloseChannel, onRequestClose) + ipcMain.on(popupMenuChannel, onPopupMenu) + ipcMain.on(confirmCloseChannel, onConfirmClose) mainWindow.on('closed', () => { // Why: default-deny the Cmd+B carve-out after the window is gone so a @@ -597,6 +654,10 @@ export function createMainWindow( // with the webContents lifecycle resets above. markdownEditorFocused = false ipcMain.removeListener(trafficLightChannel, onSyncTrafficLights) + ipcMain.removeListener(minimizeChannel, onMinimize) + ipcMain.removeListener(maximizeChannel, onMaximize) + ipcMain.removeListener(requestCloseChannel, onRequestClose) + ipcMain.removeListener(popupMenuChannel, onPopupMenu) ipcMain.removeListener(confirmCloseChannel, onConfirmClose) ipcMain.removeListener(markdownFocusChannel, onMarkdownEditorFocused) app.removeListener('before-quit', freezeBoundsOnQuit) diff --git a/src/preload/api-types.ts b/src/preload/api-types.ts index c52235917..a8458795b 100644 --- a/src/preload/api-types.ts +++ b/src/preload/api-types.ts @@ -1074,6 +1074,11 @@ export type PreloadApi = { syncTrafficLights: (zoomFactor: number) => void setMarkdownEditorFocused: (focused: boolean) => void onFullscreenChanged: (callback: (isFullScreen: boolean) => void) => () => void + minimize: () => void + maximize: () => void + onMaximizeChanged: (callback: (isMaximized: boolean) => void) => () => void + requestClose: () => void + popupMenu: () => void onWindowCloseRequested: (callback: (data: { isQuitting: boolean }) => void) => () => void confirmWindowClose: () => void } diff --git a/src/preload/index.ts b/src/preload/index.ts index 99eb0a0a7..8d3eb0306 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -1914,6 +1914,35 @@ const api = { ipcRenderer.on('window:fullscreen-changed', listener) return () => ipcRenderer.removeListener('window:fullscreen-changed', listener) }, + /** Windows only: minimize the window via the renderer-drawn title bar. */ + minimize: (): void => { + ipcRenderer.send('window:minimize') + }, + /** Windows only: toggle maximize/restore via the renderer-drawn title bar. */ + maximize: (): void => { + ipcRenderer.send('window:maximize') + }, + /** Windows only: subscribe to maximize state changes so the renderer-drawn + * maximize button can show the correct restore/maximize icon. */ + onMaximizeChanged: (callback: (isMaximized: boolean) => void): (() => void) => { + const listener = (_event: Electron.IpcRendererEvent, isMaximized: boolean) => + callback(isMaximized) + ipcRenderer.on('window:maximize-changed', listener) + return () => ipcRenderer.removeListener('window:maximize-changed', listener) + }, + /** Windows only: request a close from the renderer-drawn close button. + * Routes through main so the BrowserWindow 'close' event fires and the + * terminal-running confirmation guard in the renderer stays active. + * window.close() is unreliable in sandboxed renderers. */ + requestClose: (): void => { + ipcRenderer.send('window:request-close') + }, + /** Windows only: pop up the application menu at the cursor position. + * Replicates the Alt-key reveal that autoHideMenuBar normally provides, + * triggered by the ··· button in the renderer-drawn title bar. */ + popupMenu: (): void => { + ipcRenderer.send('menu:popup') + }, /** Fired by the main process when the user tries to close the window * (X button, Cmd+Q, etc.). Renderer should show a confirmation dialog * if terminals are still running, then call confirmWindowClose(). diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsx index 5946d8d29..88b1f659d 100644 --- a/src/renderer/src/App.tsx +++ b/src/renderer/src/App.tsx @@ -2,7 +2,8 @@ import { lazy, Suspense, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react' import { getDefaultUIState } from '../../shared/constants' -import { ArrowLeft, ArrowRight, Minimize2, PanelLeft, PanelRight } from 'lucide-react' +import { ArrowLeft, ArrowRight, Minimize2, MoreHorizontal, PanelLeft, PanelRight } from 'lucide-react' +import logo from '../../../resources/logo.svg' import { SYNC_FIT_PANES_EVENT, TOGGLE_TERMINAL_PANE_EXPAND_EVENT } from '@/constants/terminal' import { syncZoomCSSVar } from '@/lib/ui-zoom' import { buildAppFontFamily } from '@/lib/app-font-family' @@ -45,6 +46,67 @@ import { } from '@/store/slices/worktree-nav-history' const isMac = navigator.userAgent.includes('Mac') +const isWindows = !isMac && navigator.userAgent.includes('Windows') + +// Why: 'hidden' titleBarStyle on Windows removes the native OS title bar, +// so we render our own minimize/maximize/close buttons. These SVG icons match +// the Fluent/Win11 style: thin 10×10 paths on a 40×30 hit area. +function WindowControls(): React.JSX.Element { + const [maximized, setMaximized] = useState(false) + useEffect(() => { + return window.api.ui.onMaximizeChanged(setMaximized) + }, []) + return ( +