feat(windows): add separate movable chat and stepper Electron windows
Real-usage feedback after visually testing the previous in-window overlay approach: both the chat panel and the pipeline stepper must be their own independently movable windows, like the existing login window, not absolutely-positioned divs sharing the avatar window. - src/main/chatWindow.ts, src/main/stepperWindow.ts: singleton BrowserWindow pattern (same as auth/loginWindow.ts), frameless, transparent, always-on-top, loading the renderer bundle on their own #chat/#stepper hash route. - The chat window toggles via a new "toggle-chat-window" IPC (replacing the previous in-window chatOpen-only toggle); its "closed" event is relayed back to the avatar window so the context menu's checkmark stays in sync if it's closed externally (Alt+F4). - The stepper window is opened/closed by the main process itself, right where it already knows a session was created (sessions:create) or ended (new "session-ended" IPC) - not by the renderer polling session state, since the stepper has no business logic of its own. - New "session-status-changed" broadcast: the stepper window runs in its own isolated renderer process with no access to the chat window's Zustand store, so the main process relays session.status to every window whenever sessions:create/confirm-objective actually change it server-side. - Removed the previous window-resize-to-make-room approach entirely (set-pipeline-panel-visible is gone) - each panel now lives in its own right-sized window instead. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
6e8a9d2f0d
commit
4da3254d0a
@@ -6,7 +6,7 @@ import { SECURE_WEB_PREFERENCES, blockExternalNavigation } from '../utils/window
|
|||||||
let win: BrowserWindow | null = null
|
let win: BrowserWindow | null = null
|
||||||
|
|
||||||
// Fenetre normale et independante de l'avatar, meme pattern singleton que les autres fenetres
|
// 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 {
|
export function openLoginWindow(): void {
|
||||||
if (win && !win.isDestroyed()) {
|
if (win && !win.isDestroyed()) {
|
||||||
win.show()
|
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 { loginAndLoadKeys } from './auth/authClient'
|
||||||
import { openLoginWindow, closeLoginWindow } from './auth/loginWindow'
|
import { openLoginWindow, closeLoginWindow } from './auth/loginWindow'
|
||||||
import { clearSession, getCurrentUserEmail, isLoggedIn, requireLoggedIn } from './auth/session'
|
import { clearSession, getCurrentUserEmail, isLoggedIn, requireLoggedIn } from './auth/session'
|
||||||
|
import { closeChatWindow, isChatWindowOpen, onChatWindowClosed, openChatWindow } from './chatWindow'
|
||||||
|
import { closeStepperWindow, openStepperWindow } from './stepperWindow'
|
||||||
import {
|
import {
|
||||||
confirmObjective,
|
confirmObjective,
|
||||||
createSession,
|
createSession,
|
||||||
@@ -20,7 +22,8 @@ import type { AuthStatus, LoginResult } from '../shared/auth'
|
|||||||
import type {
|
import type {
|
||||||
ObjectiveMessageResponse,
|
ObjectiveMessageResponse,
|
||||||
SessionObjectiveResponse,
|
SessionObjectiveResponse,
|
||||||
SessionResponse
|
SessionResponse,
|
||||||
|
SessionStatus
|
||||||
} from '../shared/session'
|
} from '../shared/session'
|
||||||
import { logger } from './utils/logger'
|
import { logger } from './utils/logger'
|
||||||
import { SECURE_WEB_PREFERENCES, blockExternalNavigation } from './utils/windowSecurity'
|
import { SECURE_WEB_PREFERENCES, blockExternalNavigation } from './utils/windowSecurity'
|
||||||
@@ -146,22 +149,34 @@ app
|
|||||||
|
|
||||||
ipcMain.on('logout', (event) => {
|
ipcMain.on('logout', (event) => {
|
||||||
clearSession()
|
clearSession()
|
||||||
|
closeChatWindow()
|
||||||
|
closeStepperWindow()
|
||||||
event.sender.send('toggle-listening', false)
|
event.sender.send('toggle-listening', false)
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.on('quit-app', () => app.quit())
|
ipcMain.on('quit-app', () => app.quit())
|
||||||
|
|
||||||
const BASE_WINDOW_WIDTH = 420
|
ipcMain.on('toggle-chat-window', () => {
|
||||||
const PIPELINE_PANEL_WIDTH = 200
|
if (isChatWindowOpen()) closeChatWindow()
|
||||||
|
else openChatWindow()
|
||||||
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 })
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 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(
|
ipcMain.handle(
|
||||||
'voice-turn',
|
'voice-turn',
|
||||||
async (_event, inputAudio: ArrayBuffer, mimeType: string): Promise<VoiceTurnResult> => {
|
async (_event, inputAudio: ArrayBuffer, mimeType: string): Promise<VoiceTurnResult> => {
|
||||||
@@ -217,9 +232,14 @@ app
|
|||||||
email: getCurrentUserEmail()
|
email: getCurrentUserEmail()
|
||||||
}))
|
}))
|
||||||
|
|
||||||
ipcMain.handle('sessions:create', (): Promise<SessionResponse> => {
|
ipcMain.handle('sessions:create', async (): Promise<SessionResponse> => {
|
||||||
requireLoggedIn()
|
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> => {
|
ipcMain.handle('sessions:get', (_event, id: unknown): Promise<SessionResponse> => {
|
||||||
@@ -247,12 +267,15 @@ app
|
|||||||
|
|
||||||
ipcMain.handle(
|
ipcMain.handle(
|
||||||
'sessions:confirm-objective',
|
'sessions:confirm-objective',
|
||||||
(_event, id: unknown): Promise<SessionObjectiveResponse> => {
|
async (_event, id: unknown): Promise<SessionObjectiveResponse> => {
|
||||||
requireLoggedIn()
|
requireLoggedIn()
|
||||||
if (typeof id !== 'number' || !Number.isInteger(id) || id <= 0) {
|
if (typeof id !== 'number' || !Number.isInteger(id) || id <= 0) {
|
||||||
throw new Error('id de session invalide')
|
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 {
|
import type {
|
||||||
ObjectiveMessageResponse,
|
ObjectiveMessageResponse,
|
||||||
SessionObjectiveResponse,
|
SessionObjectiveResponse,
|
||||||
SessionResponse
|
SessionResponse,
|
||||||
|
SessionStatus
|
||||||
} from '../shared/session'
|
} from '../shared/session'
|
||||||
|
|
||||||
export interface SessionsApi {
|
export interface SessionsApi {
|
||||||
@@ -22,9 +23,12 @@ export interface ForgeAvatarApi {
|
|||||||
openLoginWindow: () => void
|
openLoginWindow: () => void
|
||||||
logout: () => void
|
logout: () => void
|
||||||
quitApp: () => void
|
quitApp: () => void
|
||||||
setPipelinePanelVisible: (visible: boolean) => void
|
toggleChatWindow: () => void
|
||||||
|
notifySessionEnded: () => void
|
||||||
onToggleListening: (callback: (listening: boolean) => void) => () => void
|
onToggleListening: (callback: (listening: boolean) => void) => () => void
|
||||||
onProactiveSpeech: (callback: (audio: ArrayBuffer) => void) => () => void
|
onProactiveSpeech: (callback: (audio: ArrayBuffer) => void) => () => void
|
||||||
|
onChatWindowClosed: (callback: () => void) => () => void
|
||||||
|
onSessionStatusChanged: (callback: (status: SessionStatus) => void) => () => void
|
||||||
onUpdateAvailable: (callback: () => void) => () => void
|
onUpdateAvailable: (callback: () => void) => () => void
|
||||||
onUpdateReady: (callback: () => void) => () => void
|
onUpdateReady: (callback: () => void) => () => void
|
||||||
login: (email: string, password: string) => Promise<LoginResult>
|
login: (email: string, password: string) => Promise<LoginResult>
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ import type { AuthStatus, LoginResult } from '../shared/auth'
|
|||||||
import type {
|
import type {
|
||||||
ObjectiveMessageResponse,
|
ObjectiveMessageResponse,
|
||||||
SessionObjectiveResponse,
|
SessionObjectiveResponse,
|
||||||
SessionResponse
|
SessionResponse,
|
||||||
|
SessionStatus
|
||||||
} from '../shared/session'
|
} from '../shared/session'
|
||||||
|
|
||||||
const api = {
|
const api = {
|
||||||
@@ -28,8 +29,11 @@ const api = {
|
|||||||
quitApp: (): void => {
|
quitApp: (): void => {
|
||||||
ipcRenderer.send('quit-app')
|
ipcRenderer.send('quit-app')
|
||||||
},
|
},
|
||||||
setPipelinePanelVisible: (visible: boolean): void => {
|
toggleChatWindow: (): void => {
|
||||||
ipcRenderer.send('set-pipeline-panel-visible', visible)
|
ipcRenderer.send('toggle-chat-window')
|
||||||
|
},
|
||||||
|
notifySessionEnded: (): void => {
|
||||||
|
ipcRenderer.send('session-ended')
|
||||||
},
|
},
|
||||||
onToggleListening: (callback: (listening: boolean) => void): (() => void) => {
|
onToggleListening: (callback: (listening: boolean) => void): (() => void) => {
|
||||||
const handler = (_event: Electron.IpcRendererEvent, listening: boolean): void =>
|
const handler = (_event: Electron.IpcRendererEvent, listening: boolean): void =>
|
||||||
@@ -42,6 +46,17 @@ const api = {
|
|||||||
ipcRenderer.on('proactive-speech', handler)
|
ipcRenderer.on('proactive-speech', handler)
|
||||||
return () => ipcRenderer.removeListener('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) => {
|
onUpdateAvailable: (callback: () => void): (() => void) => {
|
||||||
const handler = (): void => callback()
|
const handler = (): void => callback()
|
||||||
ipcRenderer.on('update:available', handler)
|
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