mirror of
https://github.com/NanmiCoder/cc-haha
synced 2026-07-19 13:33:35 +08:00
Introduce the Electron desktop shell alongside the existing React renderer and local Bun server boundary. The migration keeps the DesktopHost contract explicit across Tauri, Electron, and browser runtimes while adding Electron main/preload services for dialogs, shell, notifications, updates, tray/window lifecycle, terminal, preview WebContentsView, app mode, and release/package validation.
The commit also carries the latest local main desktop command updates, including agent slash entries and hidden-by-default markdown thinking details, so the packaged Electron build matches the current main UX surface.
Constraint: React renderer, local Bun server, REST/WebSocket, and sidecar boundaries must remain reusable during the migration
Constraint: macOS dev packages are ad-hoc signed and cannot prove Developer ID notarization or Gatekeeper release launch
Rejected: Browser-only smoke validation | it cannot exercise native dialogs, keychain prompts, notification behavior, or packaged app startup
Confidence: medium
Scope-risk: broad
Directive: Do not remove Tauri host support until signed Electron release artifacts pass native OS smoke on macOS, Windows, and Linux
Tested: bun run check:desktop
Tested: cd desktop && bun run check:electron
Tested: CSC_IDENTITY_AUTO_DISCOVERY=false bun run electron📦dir
Tested: bun run test:package-smoke --platform macos --package-kind dir --artifacts-dir desktop/build-artifacts/electron
Tested: Computer Use read packaged Electron app window at desktop/build-artifacts/electron/mac-arm64/Claude Code Haha.app
Not-tested: Developer ID signed/notarized Gatekeeper launch
Not-tested: Real OS notification click-to-session action
Not-tested: Windows and Linux packaged app smoke on real hosts
133 lines
4.2 KiB
TypeScript
133 lines
4.2 KiB
TypeScript
import { getDesktopHost } from './desktopHost'
|
|
|
|
export const APP_ZOOM_STORAGE_KEY = 'cc-haha-app-zoom'
|
|
export const LEGACY_UI_ZOOM_STORAGE_KEY = 'cc-haha-ui-zoom'
|
|
export const DEFAULT_APP_ZOOM = 1
|
|
export const MIN_APP_ZOOM = 0.5
|
|
export const MAX_APP_ZOOM = 2
|
|
export const APP_ZOOM_STEP = 0.1
|
|
export const APP_ZOOM_CONTROL_STEP = 0.01
|
|
|
|
export type AppZoomAction = 'in' | 'out' | 'reset'
|
|
|
|
type StorageLike = Pick<Storage, 'getItem' | 'setItem' | 'removeItem'>
|
|
type KeyboardShortcutInput = Pick<KeyboardEvent, 'altKey' | 'code' | 'ctrlKey' | 'key' | 'metaKey'>
|
|
|
|
function getDefaultStorage(): StorageLike | null {
|
|
try {
|
|
return globalThis.localStorage ?? null
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
function roundZoom(value: number): number {
|
|
return Math.round(value * 100) / 100
|
|
}
|
|
|
|
export function normalizeAppZoomLevel(value: unknown): number {
|
|
const numeric = typeof value === 'number'
|
|
? value
|
|
: typeof value === 'string' && value.trim() !== ''
|
|
? Number(value)
|
|
: DEFAULT_APP_ZOOM
|
|
if (!Number.isFinite(numeric)) return DEFAULT_APP_ZOOM
|
|
return roundZoom(Math.min(Math.max(numeric, MIN_APP_ZOOM), MAX_APP_ZOOM))
|
|
}
|
|
|
|
export function isValidStoredAppZoomLevel(value: string | null): boolean {
|
|
if (value === null) return true
|
|
if (value.trim() === '') return false
|
|
const numeric = Number(value)
|
|
return Number.isFinite(numeric) && numeric >= MIN_APP_ZOOM && numeric <= MAX_APP_ZOOM
|
|
}
|
|
|
|
export function readStoredAppZoomLevel(storage: StorageLike | null = getDefaultStorage()): number {
|
|
if (!storage) return DEFAULT_APP_ZOOM
|
|
try {
|
|
const stored = storage.getItem(APP_ZOOM_STORAGE_KEY)
|
|
if (stored !== null) return normalizeAppZoomLevel(stored)
|
|
return normalizeAppZoomLevel(storage.getItem(LEGACY_UI_ZOOM_STORAGE_KEY))
|
|
} catch {
|
|
return DEFAULT_APP_ZOOM
|
|
}
|
|
}
|
|
|
|
function persistAppZoomLevel(level: number, storage: StorageLike | null = getDefaultStorage()) {
|
|
if (!storage) return
|
|
try {
|
|
storage.setItem(APP_ZOOM_STORAGE_KEY, String(level))
|
|
} catch {
|
|
// localStorage can be unavailable in hardened browser contexts.
|
|
}
|
|
}
|
|
|
|
function setCssAppZoomMode(level: number, mode: 'css' | 'native') {
|
|
if (typeof document === 'undefined') return
|
|
document.documentElement.style.setProperty('--app-zoom', String(level))
|
|
document.documentElement.setAttribute('data-app-zoom-mode', mode)
|
|
document.documentElement.setAttribute('data-app-zoom-percent', String(Math.round(level * 100)))
|
|
document.body?.style.setProperty('zoom', mode === 'css' ? String(level) : '')
|
|
}
|
|
|
|
async function trySetNativeAppZoom(level: number): Promise<boolean> {
|
|
const host = getDesktopHost()
|
|
if (!host.capabilities.zoom) return false
|
|
try {
|
|
await host.zoom.set(level)
|
|
setCssAppZoomMode(level, 'native')
|
|
return true
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
export async function applyAppZoomLevel(
|
|
input: number,
|
|
options: { persist?: boolean } = {},
|
|
): Promise<number> {
|
|
const level = normalizeAppZoomLevel(input)
|
|
if (options.persist !== false) {
|
|
persistAppZoomLevel(level)
|
|
}
|
|
|
|
const nativeApplied = await trySetNativeAppZoom(level)
|
|
if (!nativeApplied) {
|
|
setCssAppZoomMode(level, 'css')
|
|
}
|
|
|
|
return level
|
|
}
|
|
|
|
export function nextAppZoomLevel(current: number, action: AppZoomAction): number {
|
|
if (action === 'reset') return DEFAULT_APP_ZOOM
|
|
const delta = action === 'in' ? APP_ZOOM_STEP : -APP_ZOOM_STEP
|
|
return normalizeAppZoomLevel(roundZoom(current + delta))
|
|
}
|
|
|
|
export function getAppZoomKeyboardAction(
|
|
event: KeyboardShortcutInput,
|
|
platform: string = typeof navigator === 'undefined' ? '' : navigator.platform,
|
|
): AppZoomAction | null {
|
|
if (event.altKey) return null
|
|
const isMac = /mac/i.test(platform)
|
|
const hasPrimaryModifier = isMac ? event.metaKey : event.ctrlKey
|
|
if (!hasPrimaryModifier) return null
|
|
|
|
const key = event.key.toLowerCase()
|
|
if (key === '+' || key === '=' || event.code === 'Equal' || event.code === 'NumpadAdd') {
|
|
return 'in'
|
|
}
|
|
if (key === '-' || event.code === 'Minus' || event.code === 'NumpadSubtract') {
|
|
return 'out'
|
|
}
|
|
if (key === '0' || event.code === 'Digit0' || event.code === 'Numpad0') {
|
|
return 'reset'
|
|
}
|
|
return null
|
|
}
|
|
|
|
export function initializeAppZoom(): Promise<number> {
|
|
return applyAppZoomLevel(readStoredAppZoomLevel(), { persist: false })
|
|
}
|