diff --git a/src/main/index.ts b/src/main/index.ts index 38b5c84..e81e33d 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -3,7 +3,7 @@ import { join } from 'path' import { setMainWindow } from './events' import { IPC, type UserSettings, type PlayOptions } from '../shared/ipc' import { login, logout, restoreSession, getCurrent } from './auth' -import { play, stopGame } from './play' +import { play, stopGame, isGameRunning } from './play' import { getPackMetaCached } from './modpack' import { getSettings, setSettings } from './settings' import { paths } from './paths' @@ -35,6 +35,13 @@ function createWindow(): BrowserWindow { win.on('ready-to-show', () => win.show()) + win.on('close', (e) => { + if (isGameRunning()) { + e.preventDefault() + win.hide() + } + }) + // Liens externes -> navigateur système. win.webContents.setWindowOpenHandler(({ url }) => { void shell.openExternal(url) diff --git a/src/main/play.ts b/src/main/play.ts index 9c11310..b2fa0ef 100644 --- a/src/main/play.ts +++ b/src/main/play.ts @@ -1,4 +1,5 @@ import type { ChildProcess } from 'child_process' +import { app, BrowserWindow } from 'electron' import { getCurrent } from './auth' import { fetchPackMeta } from './modpack' import { ensureJava } from './java' @@ -47,6 +48,10 @@ export async function play(opts?: PlayOptions): Promise { gameProcess = proc proc.on('close', () => { gameProcess = null + const allHidden = BrowserWindow.getAllWindows().every((w) => !w.isVisible()) + if (allHidden) { + app.quit() + } }) } catch (e) { emit.progress({ phase: 'error', message: (e as Error).message, progress: undefined }) @@ -60,3 +65,7 @@ export function stopGame(): boolean { gameProcess.kill() return true } + +export function isGameRunning(): boolean { + return gameProcess !== null +}