Files
ForgeBase/Max Avatar/src/main/coach/drawingWindow.ts
T
williamandClaude Sonnet 5 1c2f002eb3 refactor: move app code into Max Avatar/ subfolder (monorepo prep for backend)
Step 1 of adding a backend proxy server (API keys held server-side instead
of shipped in the installer). Moves everything app-specific into
"Max Avatar/" via git mv (preserves history): src/, tests/, scripts/,
assets/, build/, resources/, package.json/-lock.json, electron-builder.yml,
electron.vite.config.ts, eslint.config.mjs, playwright.config.ts,
vitest.config.mts, sonar-project.properties, tsconfig*.json, Dockerfile,
.editorconfig/.env*/.gitignore/.prettier*.

CLAUDE.md, README.md, docs/, .claude/, .gitea/, .husky/, .vscode/, and the
docker-compose files stay at the repo root, since they'll apply to both the
app and the future backend.

No application code needed changes (all its internal paths are relative to
its own tree). Adjusted what referenced the old root from outside:
- .husky/pre-commit: cd into "Max Avatar" before lint-staged
- .gitea/workflows/*.yml: working-directory + cache-dependency-path +
  artifact paths + SonarQube projectBaseDir
- docker-compose.yml: build context -> ./Max Avatar
- Max Avatar/Dockerfile: node:20-alpine -> node:24-alpine (same node:sqlite
  issue already fixed elsewhere, never applied here before)
- .vscode/launch.json, README.md: updated paths/instructions

Verified from Max Avatar/ after the move: npm ci, typecheck, lint,
test:coverage (473/473, same coverage numbers), build, and dev all work
unchanged.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-31 12:15:30 +01:00

149 lines
4.9 KiB
TypeScript

import { randomUUID } from 'crypto'
import { BrowserWindow, ipcMain, type IpcMainEvent } from 'electron'
import { join } from 'path'
import { is } from '@electron-toolkit/utils'
import type { DrawShape } from '../../shared/drawing'
import { SECURE_WEB_PREFERENCES, blockExternalNavigation } from '../utils/windowSecurity'
let win: BrowserWindow | null = null
let windowReady: Promise<void> | null = null
// Fenetre normale et independante de l'avatar, meme pattern singleton que progressWindow.ts.
function ensureWindow(): { window: BrowserWindow; ready: Promise<void> } {
if (win && !win.isDestroyed()) return { window: win, ready: windowReady ?? Promise.resolve() }
win = new BrowserWindow({
width: 700,
height: 560,
title: 'Max — tableau',
alwaysOnTop: true,
webPreferences: {
preload: join(__dirname, '../preload/index.js'),
...SECURE_WEB_PREFERENCES
}
})
blockExternalNavigation(win)
// Meme raison que exerciseWindow.ts : visible au-dessus de tout (y compris plein ecran), mais en
// dessous de l'avatar ('screen-saver') qui reste donc toujours devant.
win.setAlwaysOnTop(true, 'floating')
win.maximize()
win.on('closed', () => {
win = null
windowReady = null
})
const window = win
windowReady = new Promise((resolve) => {
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
void window.loadURL(`${process.env['ELECTRON_RENDERER_URL']}#coach-drawing`)
} else {
void window.loadFile(join(__dirname, '../renderer/index.html'), { hash: 'coach-drawing' })
}
if (window.webContents.isLoading()) {
window.webContents.once('did-finish-load', () => resolve())
} else {
resolve()
}
})
return { window, ready: windowReady }
}
/** Ouvre (ou reutilise) la fenetre et initialise le canvas (titre, dimensions, vide). */
export async function openDrawingWindow(
title: string,
width: number,
height: number
): Promise<void> {
const { window, ready } = ensureWindow()
window.show()
window.moveTop()
window.focus()
await ready
window.webContents.send('init-drawing', { title, width, height })
}
/**
* Attend un accuse de reception a usage unique du renderer (evenement `ackEvent` portant le meme
* `requestId` que celui envoye), avec expiration et nettoyage si la fenetre se ferme entre-temps.
* Factorise le boilerplate partage par revealShape() et playCaption().
*/
function waitForAck(
window: BrowserWindow,
ackEvent: string,
timeoutMs: number,
send: (requestId: string) => void
): Promise<void> {
return new Promise((resolve) => {
const requestId = randomUUID()
let settled = false
const finish = (): void => {
if (settled) return
settled = true
clearTimeout(timeout)
ipcMain.removeListener(ackEvent, handler)
resolve()
}
const timeout = setTimeout(finish, timeoutMs)
function handler(_event: IpcMainEvent, id: string): void {
if (id !== requestId) return
finish()
}
ipcMain.on(ackEvent, handler)
window.once('closed', finish)
send(requestId)
})
}
/**
* Revele une forme dans la fenetre deja ouverte, avec un audio optionnel (narration pas-a-pas).
* Attend l'accuse de reception du renderer (forme dessinee + audio termine) avant de se resoudre,
* pour que draw_schema puisse enchainer les formes au rythme de la narration.
*/
export function revealShape(
shape: DrawShape,
audio: ArrayBuffer | null,
timeoutMs = 20000
): Promise<void> {
if (!win || win.isDestroyed()) return Promise.resolve()
const window = win
return waitForAck(window, 'shape-reveal-done', timeoutMs, (requestId) => {
window.webContents.send('reveal-shape', { requestId, shape, audio })
})
}
/** Rend un diagramme Mermaid (cycle/hierarchie/flux) dans la fenetre, d'un coup (pas de reveal
* noeud-par-noeud : la fiabilite du layout prime sur l'effet progressif pour ce type precis). */
export async function renderMermaid(title: string, mermaidCode: string): Promise<void> {
const { window, ready } = ensureWindow()
window.show()
window.moveTop()
window.focus()
await ready
return new Promise((resolve, reject) => {
const requestId = randomUUID()
function handler(_event: IpcMainEvent, id: string, ok: boolean, error?: string): void {
if (id !== requestId) return
ipcMain.removeListener('mermaid-render-done', handler)
if (ok) resolve()
else reject(new Error(error ?? 'Echec du rendu Mermaid'))
}
ipcMain.on('mermaid-render-done', handler)
window.webContents.send('render-mermaid', { requestId, title, mermaidCode })
})
}
/** Joue une legende audio seule (sans forme associee), pour la narration sequencee de Mermaid. */
export function playCaption(audio: ArrayBuffer, timeoutMs = 20000): Promise<void> {
if (!win || win.isDestroyed()) return Promise.resolve()
const window = win
return waitForAck(window, 'caption-done', timeoutMs, (requestId) => {
window.webContents.send('play-caption', { requestId, audio })
})
}