Develop #2
@@ -6,7 +6,7 @@ import { SECURE_WEB_PREFERENCES, blockExternalNavigation } from '../utils/window
|
||||
let win: BrowserWindow | null = null
|
||||
|
||||
// Fenetre normale et independante de l'avatar, meme pattern singleton que les autres fenetres
|
||||
// secondaires (drawingWindow.ts, exerciseWindow.ts, progressWindow.ts).
|
||||
// secondaires (../chatWindow.ts, ../stepperWindow.ts).
|
||||
export function openLoginWindow(): void {
|
||||
if (win && !win.isDestroyed()) {
|
||||
win.show()
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import { BrowserWindow } from 'electron'
|
||||
import { join } from 'path'
|
||||
import { is } from '@electron-toolkit/utils'
|
||||
import { SECURE_WEB_PREFERENCES, blockExternalNavigation } from './utils/windowSecurity'
|
||||
|
||||
let win: BrowserWindow | null = null
|
||||
let onClosedCallback: (() => void) | null = null
|
||||
|
||||
// Fenetre separee et deplacable (retour utilisateur direct : le chat et le stepper doivent etre
|
||||
// des vraies fenetres Electron, pas des panneaux ancres dans la fenetre avatar), meme pattern
|
||||
// singleton que src/main/auth/loginWindow.ts et stepperWindow.ts.
|
||||
export function isChatWindowOpen(): boolean {
|
||||
return win !== null && !win.isDestroyed()
|
||||
}
|
||||
|
||||
export function openChatWindow(): void {
|
||||
if (isChatWindowOpen()) {
|
||||
win?.show()
|
||||
win?.focus()
|
||||
return
|
||||
}
|
||||
|
||||
win = new BrowserWindow({
|
||||
width: 380,
|
||||
height: 560,
|
||||
title: 'Max — conversation',
|
||||
frame: false,
|
||||
transparent: true,
|
||||
backgroundColor: '#00000000',
|
||||
hasShadow: false,
|
||||
alwaysOnTop: true,
|
||||
skipTaskbar: true,
|
||||
webPreferences: {
|
||||
preload: join(__dirname, '../preload/index.js'),
|
||||
...SECURE_WEB_PREFERENCES
|
||||
}
|
||||
})
|
||||
blockExternalNavigation(win)
|
||||
win.setAlwaysOnTop(true, 'floating')
|
||||
win.on('closed', () => {
|
||||
win = null
|
||||
onClosedCallback?.()
|
||||
})
|
||||
|
||||
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
|
||||
void win.loadURL(`${process.env['ELECTRON_RENDERER_URL']}#chat`)
|
||||
} else {
|
||||
void win.loadFile(join(__dirname, '../renderer/index.html'), { hash: 'chat' })
|
||||
}
|
||||
}
|
||||
|
||||
export function closeChatWindow(): void {
|
||||
if (isChatWindowOpen()) win?.close()
|
||||
}
|
||||
|
||||
/** Permet au main process de resynchroniser l'etat "ouvert" cote fenetre avatar (menu contextuel)
|
||||
* quand la fenetre de chat est fermee autrement que via le menu (ex: Alt+F4). */
|
||||
export function onChatWindowClosed(callback: () => void): void {
|
||||
onClosedCallback = callback
|
||||
}
|
||||
@@ -9,6 +9,8 @@ import { stripWakeWord } from './wakeWord'
|
||||
import { loginAndLoadKeys } from './auth/authClient'
|
||||
import { openLoginWindow, closeLoginWindow } from './auth/loginWindow'
|
||||
import { clearSession, getCurrentUserEmail, isLoggedIn, requireLoggedIn } from './auth/session'
|
||||
import { closeChatWindow, isChatWindowOpen, onChatWindowClosed, openChatWindow } from './chatWindow'
|
||||
import { closeStepperWindow, openStepperWindow } from './stepperWindow'
|
||||
import {
|
||||
confirmObjective,
|
||||
createSession,
|
||||
@@ -20,7 +22,8 @@ import type { AuthStatus, LoginResult } from '../shared/auth'
|
||||
import type {
|
||||
ObjectiveMessageResponse,
|
||||
SessionObjectiveResponse,
|
||||
SessionResponse
|
||||
SessionResponse,
|
||||
SessionStatus
|
||||
} from '../shared/session'
|
||||
import { logger } from './utils/logger'
|
||||
import { SECURE_WEB_PREFERENCES, blockExternalNavigation } from './utils/windowSecurity'
|
||||
@@ -146,22 +149,34 @@ app
|
||||
|
||||
ipcMain.on('logout', (event) => {
|
||||
clearSession()
|
||||
closeChatWindow()
|
||||
closeStepperWindow()
|
||||
event.sender.send('toggle-listening', false)
|
||||
})
|
||||
|
||||
ipcMain.on('quit-app', () => app.quit())
|
||||
|
||||
const BASE_WINDOW_WIDTH = 420
|
||||
const PIPELINE_PANEL_WIDTH = 200
|
||||
|
||||
ipcMain.on('set-pipeline-panel-visible', (_event, visible: unknown) => {
|
||||
if (typeof visible !== 'boolean' || !mainWindow) return
|
||||
const bounds = mainWindow.getBounds()
|
||||
const targetWidth = visible ? BASE_WINDOW_WIDTH + PIPELINE_PANEL_WIDTH : BASE_WINDOW_WIDTH
|
||||
if (bounds.width === targetWidth) return
|
||||
mainWindow.setBounds({ x: bounds.x, y: bounds.y, width: targetWidth, height: bounds.height })
|
||||
ipcMain.on('toggle-chat-window', () => {
|
||||
if (isChatWindowOpen()) closeChatWindow()
|
||||
else openChatWindow()
|
||||
})
|
||||
|
||||
// Resynchronise la coche "Ecrire un message" du menu contextuel (fenetre avatar) quand la
|
||||
// fenetre de chat est fermee autrement que par ce menu (ex: Alt+F4).
|
||||
onChatWindowClosed(() => mainWindow?.webContents.send('chat-window-closed'))
|
||||
|
||||
ipcMain.on('session-ended', () => closeStepperWindow())
|
||||
|
||||
// Relais inter-fenetres : la fenetre du stepper vit dans son propre process renderer isole
|
||||
// (voir src/main/stepperWindow.ts) et n'a donc pas acces a l'etat de la fenetre de chat qui
|
||||
// fait reellement les appels a Max API. Le main process, qui gere ces appels, diffuse le
|
||||
// nouveau statut a toutes les fenetres des qu'il change reellement cote serveur.
|
||||
function broadcastSessionStatus(status: SessionStatus): void {
|
||||
for (const window of BrowserWindow.getAllWindows()) {
|
||||
window.webContents.send('session-status-changed', status)
|
||||
}
|
||||
}
|
||||
|
||||
ipcMain.handle(
|
||||
'voice-turn',
|
||||
async (_event, inputAudio: ArrayBuffer, mimeType: string): Promise<VoiceTurnResult> => {
|
||||
@@ -217,9 +232,14 @@ app
|
||||
email: getCurrentUserEmail()
|
||||
}))
|
||||
|
||||
ipcMain.handle('sessions:create', (): Promise<SessionResponse> => {
|
||||
ipcMain.handle('sessions:create', async (): Promise<SessionResponse> => {
|
||||
requireLoggedIn()
|
||||
return createSession()
|
||||
const result = await createSession()
|
||||
// L'utilisateur a mentionne vouloir apprendre quelque chose (une session existe
|
||||
// reellement cote serveur) : c'est le seul moment ou le stepper doit apparaitre.
|
||||
openStepperWindow()
|
||||
broadcastSessionStatus(result.status)
|
||||
return result
|
||||
})
|
||||
|
||||
ipcMain.handle('sessions:get', (_event, id: unknown): Promise<SessionResponse> => {
|
||||
@@ -247,12 +267,15 @@ app
|
||||
|
||||
ipcMain.handle(
|
||||
'sessions:confirm-objective',
|
||||
(_event, id: unknown): Promise<SessionObjectiveResponse> => {
|
||||
async (_event, id: unknown): Promise<SessionObjectiveResponse> => {
|
||||
requireLoggedIn()
|
||||
if (typeof id !== 'number' || !Number.isInteger(id) || id <= 0) {
|
||||
throw new Error('id de session invalide')
|
||||
}
|
||||
return confirmObjective(id)
|
||||
const result = await confirmObjective(id)
|
||||
const session = await getSession(id)
|
||||
broadcastSessionStatus(session.status)
|
||||
return result
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import { BrowserWindow } from 'electron'
|
||||
import { join } from 'path'
|
||||
import { is } from '@electron-toolkit/utils'
|
||||
import { SECURE_WEB_PREFERENCES, blockExternalNavigation } from './utils/windowSecurity'
|
||||
|
||||
let win: BrowserWindow | null = null
|
||||
|
||||
// Fenetre separee et deplacable, ouverte par le main process des qu'une session existe reellement
|
||||
// (voir sessions:create dans src/main/index.ts) - jamais par le renderer, et jamais juste parce que
|
||||
// l'utilisateur est connecte. Meme pattern singleton que chatWindow.ts/auth/loginWindow.ts.
|
||||
export function isStepperWindowOpen(): boolean {
|
||||
return win !== null && !win.isDestroyed()
|
||||
}
|
||||
|
||||
export function openStepperWindow(): void {
|
||||
if (isStepperWindowOpen()) return
|
||||
|
||||
win = new BrowserWindow({
|
||||
width: 220,
|
||||
height: 460,
|
||||
title: 'Max — progression',
|
||||
frame: false,
|
||||
transparent: true,
|
||||
backgroundColor: '#00000000',
|
||||
hasShadow: false,
|
||||
alwaysOnTop: true,
|
||||
resizable: false,
|
||||
skipTaskbar: true,
|
||||
webPreferences: {
|
||||
preload: join(__dirname, '../preload/index.js'),
|
||||
...SECURE_WEB_PREFERENCES
|
||||
}
|
||||
})
|
||||
blockExternalNavigation(win)
|
||||
win.setAlwaysOnTop(true, 'floating')
|
||||
win.on('closed', () => {
|
||||
win = null
|
||||
})
|
||||
|
||||
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
|
||||
void win.loadURL(`${process.env['ELECTRON_RENDERER_URL']}#stepper`)
|
||||
} else {
|
||||
void win.loadFile(join(__dirname, '../renderer/index.html'), { hash: 'stepper' })
|
||||
}
|
||||
}
|
||||
|
||||
export function closeStepperWindow(): void {
|
||||
if (isStepperWindowOpen()) win?.close()
|
||||
}
|
||||
Vendored
+6
-2
@@ -4,7 +4,8 @@ import type { AuthStatus, LoginResult } from '../shared/auth'
|
||||
import type {
|
||||
ObjectiveMessageResponse,
|
||||
SessionObjectiveResponse,
|
||||
SessionResponse
|
||||
SessionResponse,
|
||||
SessionStatus
|
||||
} from '../shared/session'
|
||||
|
||||
export interface SessionsApi {
|
||||
@@ -22,9 +23,12 @@ export interface ForgeAvatarApi {
|
||||
openLoginWindow: () => void
|
||||
logout: () => void
|
||||
quitApp: () => void
|
||||
setPipelinePanelVisible: (visible: boolean) => void
|
||||
toggleChatWindow: () => void
|
||||
notifySessionEnded: () => void
|
||||
onToggleListening: (callback: (listening: boolean) => void) => () => void
|
||||
onProactiveSpeech: (callback: (audio: ArrayBuffer) => void) => () => void
|
||||
onChatWindowClosed: (callback: () => void) => () => void
|
||||
onSessionStatusChanged: (callback: (status: SessionStatus) => void) => () => void
|
||||
onUpdateAvailable: (callback: () => void) => () => void
|
||||
onUpdateReady: (callback: () => void) => () => void
|
||||
login: (email: string, password: string) => Promise<LoginResult>
|
||||
|
||||
@@ -5,7 +5,8 @@ import type { AuthStatus, LoginResult } from '../shared/auth'
|
||||
import type {
|
||||
ObjectiveMessageResponse,
|
||||
SessionObjectiveResponse,
|
||||
SessionResponse
|
||||
SessionResponse,
|
||||
SessionStatus
|
||||
} from '../shared/session'
|
||||
|
||||
const api = {
|
||||
@@ -28,8 +29,11 @@ const api = {
|
||||
quitApp: (): void => {
|
||||
ipcRenderer.send('quit-app')
|
||||
},
|
||||
setPipelinePanelVisible: (visible: boolean): void => {
|
||||
ipcRenderer.send('set-pipeline-panel-visible', visible)
|
||||
toggleChatWindow: (): void => {
|
||||
ipcRenderer.send('toggle-chat-window')
|
||||
},
|
||||
notifySessionEnded: (): void => {
|
||||
ipcRenderer.send('session-ended')
|
||||
},
|
||||
onToggleListening: (callback: (listening: boolean) => void): (() => void) => {
|
||||
const handler = (_event: Electron.IpcRendererEvent, listening: boolean): void =>
|
||||
@@ -42,6 +46,17 @@ const api = {
|
||||
ipcRenderer.on('proactive-speech', handler)
|
||||
return () => ipcRenderer.removeListener('proactive-speech', handler)
|
||||
},
|
||||
onChatWindowClosed: (callback: () => void): (() => void) => {
|
||||
const handler = (): void => callback()
|
||||
ipcRenderer.on('chat-window-closed', handler)
|
||||
return () => ipcRenderer.removeListener('chat-window-closed', handler)
|
||||
},
|
||||
onSessionStatusChanged: (callback: (status: SessionStatus) => void): (() => void) => {
|
||||
const handler = (_event: Electron.IpcRendererEvent, status: SessionStatus): void =>
|
||||
callback(status)
|
||||
ipcRenderer.on('session-status-changed', handler)
|
||||
return () => ipcRenderer.removeListener('session-status-changed', handler)
|
||||
},
|
||||
onUpdateAvailable: (callback: () => void): (() => void) => {
|
||||
const handler = (): void => callback()
|
||||
ipcRenderer.on('update:available', handler)
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { EventEmitter } from 'node:events'
|
||||
|
||||
vi.mock('@electron-toolkit/utils', () => ({ is: { dev: false } }))
|
||||
|
||||
class FakeWebContents extends EventEmitter {}
|
||||
|
||||
class FakeBrowserWindow extends EventEmitter {
|
||||
webContents = new FakeWebContents()
|
||||
show = vi.fn()
|
||||
focus = vi.fn()
|
||||
setAlwaysOnTop = vi.fn()
|
||||
loadURL = vi.fn()
|
||||
loadFile = vi.fn()
|
||||
close = vi.fn()
|
||||
destroyed = false
|
||||
isDestroyed(): boolean {
|
||||
return this.destroyed
|
||||
}
|
||||
}
|
||||
|
||||
let lastWindow: FakeBrowserWindow | null = null
|
||||
function createBrowserWindow(): FakeBrowserWindow {
|
||||
const window = new FakeBrowserWindow()
|
||||
lastWindow = window
|
||||
return window
|
||||
}
|
||||
|
||||
vi.mock('electron', () => ({
|
||||
BrowserWindow: vi.fn(createBrowserWindow)
|
||||
}))
|
||||
|
||||
const { openChatWindow, closeChatWindow, isChatWindowOpen, onChatWindowClosed } =
|
||||
await import('../../src/main/chatWindow')
|
||||
|
||||
describe('chatWindow', () => {
|
||||
beforeEach(() => {
|
||||
lastWindow?.emit('closed')
|
||||
vi.clearAllMocks()
|
||||
lastWindow = null
|
||||
})
|
||||
|
||||
describe('openChatWindow()', () => {
|
||||
it('should create and load a window on the #chat hash', () => {
|
||||
openChatWindow()
|
||||
|
||||
expect(lastWindow?.loadFile).toHaveBeenCalledWith(
|
||||
expect.stringContaining('index.html'),
|
||||
expect.objectContaining({ hash: 'chat' })
|
||||
)
|
||||
expect(isChatWindowOpen()).toBe(true)
|
||||
})
|
||||
|
||||
it('should reuse the existing window and bring it to front on a second call', () => {
|
||||
openChatWindow()
|
||||
const firstWindow = lastWindow
|
||||
openChatWindow()
|
||||
|
||||
expect(lastWindow).toBe(firstWindow)
|
||||
expect(firstWindow?.show).toHaveBeenCalled()
|
||||
expect(firstWindow?.focus).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('closeChatWindow()', () => {
|
||||
it('should close the window when one is open', () => {
|
||||
openChatWindow()
|
||||
closeChatWindow()
|
||||
|
||||
expect(lastWindow?.close).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should do nothing when no window is open', () => {
|
||||
expect(() => closeChatWindow()).not.toThrow()
|
||||
expect(isChatWindowOpen()).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('onChatWindowClosed()', () => {
|
||||
it('should notify the registered callback when the window closes', () => {
|
||||
const callback = vi.fn()
|
||||
onChatWindowClosed(callback)
|
||||
|
||||
openChatWindow()
|
||||
lastWindow?.emit('closed')
|
||||
|
||||
expect(callback).toHaveBeenCalledOnce()
|
||||
expect(isChatWindowOpen()).toBe(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,87 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { EventEmitter } from 'node:events'
|
||||
|
||||
vi.mock('@electron-toolkit/utils', () => ({ is: { dev: false } }))
|
||||
|
||||
class FakeWebContents extends EventEmitter {}
|
||||
|
||||
class FakeBrowserWindow extends EventEmitter {
|
||||
webContents = new FakeWebContents()
|
||||
show = vi.fn()
|
||||
focus = vi.fn()
|
||||
setAlwaysOnTop = vi.fn()
|
||||
loadURL = vi.fn()
|
||||
loadFile = vi.fn()
|
||||
close = vi.fn()
|
||||
destroyed = false
|
||||
isDestroyed(): boolean {
|
||||
return this.destroyed
|
||||
}
|
||||
}
|
||||
|
||||
let lastWindow: FakeBrowserWindow | null = null
|
||||
function createBrowserWindow(): FakeBrowserWindow {
|
||||
const window = new FakeBrowserWindow()
|
||||
lastWindow = window
|
||||
return window
|
||||
}
|
||||
|
||||
vi.mock('electron', () => ({
|
||||
BrowserWindow: vi.fn(createBrowserWindow)
|
||||
}))
|
||||
|
||||
const { openStepperWindow, closeStepperWindow, isStepperWindowOpen } =
|
||||
await import('../../src/main/stepperWindow')
|
||||
|
||||
describe('stepperWindow', () => {
|
||||
beforeEach(() => {
|
||||
lastWindow?.emit('closed')
|
||||
vi.clearAllMocks()
|
||||
lastWindow = null
|
||||
})
|
||||
|
||||
describe('openStepperWindow()', () => {
|
||||
it('should create and load a window on the #stepper hash', () => {
|
||||
openStepperWindow()
|
||||
|
||||
expect(lastWindow?.loadFile).toHaveBeenCalledWith(
|
||||
expect.stringContaining('index.html'),
|
||||
expect.objectContaining({ hash: 'stepper' })
|
||||
)
|
||||
expect(isStepperWindowOpen()).toBe(true)
|
||||
})
|
||||
|
||||
it('should do nothing (not reopen/focus) when one is already open', () => {
|
||||
openStepperWindow()
|
||||
const firstWindow = lastWindow
|
||||
openStepperWindow()
|
||||
|
||||
expect(lastWindow).toBe(firstWindow)
|
||||
expect(firstWindow?.show).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should open a new window once the previous one was closed', () => {
|
||||
openStepperWindow()
|
||||
const firstWindow = lastWindow
|
||||
firstWindow?.emit('closed')
|
||||
|
||||
openStepperWindow()
|
||||
|
||||
expect(lastWindow).not.toBe(firstWindow)
|
||||
})
|
||||
})
|
||||
|
||||
describe('closeStepperWindow()', () => {
|
||||
it('should close the window when one is open', () => {
|
||||
openStepperWindow()
|
||||
closeStepperWindow()
|
||||
|
||||
expect(lastWindow?.close).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should do nothing when no window is open', () => {
|
||||
expect(() => closeStepperWindow()).not.toThrow()
|
||||
expect(isStepperWindowOpen()).toBe(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user