From 605a38396389d323b15bae88a64e36ca877c61fa Mon Sep 17 00:00:00 2001 From: MathieuG-P <40181755+Zagrios@users.noreply.github.com> Date: Mon, 19 Dec 2022 03:38:06 +0100 Subject: [PATCH] fix object desctory when oneClick window close --- src/main/ipcs/os-controls-ipcs.ts | 13 ++++++++----- src/main/ipcs/window-manager-ipcs.ts | 5 +++++ src/main/services/utils.service.ts | 9 +++++---- src/main/services/window-manager.service.ts | 12 +++++++----- .../title-bar/title-bar.component.tsx | 17 ++++++++++------- src/renderer/services/window-manager.service.ts | 6 +++++- src/renderer/windows/App.tsx | 2 +- src/renderer/windows/Launcher.tsx | 2 +- .../windows/OneClick/OneClickDownloadMap.tsx | 4 ++-- 9 files changed, 44 insertions(+), 26 deletions(-) diff --git a/src/main/ipcs/os-controls-ipcs.ts b/src/main/ipcs/os-controls-ipcs.ts index fe258a84..7b92c098 100644 --- a/src/main/ipcs/os-controls-ipcs.ts +++ b/src/main/ipcs/os-controls-ipcs.ts @@ -2,24 +2,27 @@ import { ipcMain, shell, dialog, app } from 'electron'; import { UtilsService } from '../services/utils.service'; import { IpcRequest } from 'shared/models/ipc'; + +// TODO IMPROVE WINDOW CONTROL BY USING WINDOW SERVICE + ipcMain.on('window.close', async () => { const utils = UtilsService.getInstance(); - utils.getMainWindow()?.close(); + utils.getMainWindows("index.html")?.close(); }); ipcMain.on('window.maximize', async () => { const utils = UtilsService.getInstance(); - utils.getMainWindow()?.maximize(); + utils.getMainWindows("index.html")?.maximize(); }); ipcMain.on('window.minimize', async () => { const utils = UtilsService.getInstance(); - utils.getMainWindow()?.minimize(); + utils.getMainWindows("index.html")?.minimize(); }); ipcMain.on('window.reset', async () => { const utils = UtilsService.getInstance(); - utils.getMainWindow()?.restore(); + utils.getMainWindows("index.html")?.restore(); }); ipcMain.on('new-window', async (event, request: IpcRequest) => { @@ -34,7 +37,7 @@ ipcMain.on('choose-folder', async (event, request: IpcRequest) => { ipcMain.on("window.progression", async (event, request: IpcRequest) => { const utils = UtilsService.getInstance(); - utils.getMainWindow().setProgressBar(request.args / 100); + utils.getMainWindows("index.html")?.setProgressBar(request.args / 100); }); ipcMain.on('save-file', async (event, request: IpcRequest<{filename?: string, filters?: Electron.FileFilter[]}>) => { diff --git a/src/main/ipcs/window-manager-ipcs.ts b/src/main/ipcs/window-manager-ipcs.ts index c94ae72c..e0393d68 100644 --- a/src/main/ipcs/window-manager-ipcs.ts +++ b/src/main/ipcs/window-manager-ipcs.ts @@ -14,4 +14,9 @@ ipcMain.on("open-window-then-close-all", async (event, request: IpcRequest) => { const windowManager = WindowManagerService.getInstance(); windowManager.closeAllWindows(request.args); +}); + +ipcMain.on("close-windows", async (event, request: IpcRequest) => { + const windowManager = WindowManagerService.getInstance(); + windowManager.close(...request.args); }); \ No newline at end of file diff --git a/src/main/services/utils.service.ts b/src/main/services/utils.service.ts index 9f643b9e..a91f36db 100644 --- a/src/main/services/utils.service.ts +++ b/src/main/services/utils.service.ts @@ -7,6 +7,7 @@ import { app, BrowserWindow } from "electron"; import { rm, unlink } from "fs/promises"; import { IpcResponse } from "shared/models/ipc"; import log from "electron-log"; +import { AppWindow } from "shared/models/window-manager/app-window.model"; export class UtilsService{ @@ -14,7 +15,7 @@ export class UtilsService{ private assetsPath: string = ''; - private mainWindow: BrowserWindow; + private windows: Map = new Map(); private constructor(){} @@ -30,8 +31,8 @@ export class UtilsService{ public getAssestsJsonsPath(): string { return this.getAssetsPath("jsons"); } public getTempPath(): string{ return path.join(app.getPath("temp"), app.getName()) } - public setMainWindow(win: BrowserWindow){ this.mainWindow = win; } - public getMainWindow(){ return this.mainWindow; } + public setMainWindows(windows: Map){ this.windows = windows; } + public getMainWindows(win: AppWindow){ return this.windows.get(win); } public pathExist(path: string): boolean{ return existsSync(path); } @@ -95,7 +96,7 @@ export class UtilsService{ public ipcSend(channel: string, response: IpcResponse): void{ try { - this.mainWindow.webContents.send(channel, response); + Array.from(this.windows.values()).forEach(window => window.webContents.send(channel, response)); } catch (error) { log.error(error); } diff --git a/src/main/services/window-manager.service.ts b/src/main/services/window-manager.service.ts index 6bd9a855..2cc85e87 100644 --- a/src/main/services/window-manager.service.ts +++ b/src/main/services/window-manager.service.ts @@ -53,15 +53,11 @@ export class WindowManagerService{ }); this.windows.set(windowType, window); - this.utilsService.setMainWindow(window); + this.utilsService.setMainWindows(this.windows); return promise.then(() => window); } - public closeWindow(window: AppWindow){ - this.windows.get(window).close(); - } - public closeAllWindows(except?: AppWindow){ this.windows.forEach((window, key) => { if(key === except){ return; } @@ -69,4 +65,10 @@ export class WindowManagerService{ }) } + public close(...win: AppWindow[]){ + win.forEach(window => { + this.windows.get(window)?.close(); + }); + } + } \ No newline at end of file diff --git a/src/renderer/components/title-bar/title-bar.component.tsx b/src/renderer/components/title-bar/title-bar.component.tsx index 4ab20920..6bbe6121 100644 --- a/src/renderer/components/title-bar/title-bar.component.tsx +++ b/src/renderer/components/title-bar/title-bar.component.tsx @@ -1,16 +1,19 @@ import { useState } from 'react'; import { IpcService } from 'renderer/services/ipc.service'; +import { WindowManagerService } from 'renderer/services/window-manager.service'; +import { AppWindow } from 'shared/models/window-manager/app-window.model'; import './title-bar.component.css' -export default function TitleBar({template = "main"} : {template?: "update"|"main"|"oneclick"}) { +export default function TitleBar({template = "index.html"} : {template: AppWindow}) { const ipcService = IpcService.getInstance(); + const windows = WindowManagerService.getInstance(); const [maximized, setMaximized] = useState(false); - const closeWindow = () => { - ipcService.sendLazy('window.close'); - } + const closeWindow = () => { + return windows.close(template); + } const maximizeWindow = () => { ipcService.sendLazy('window.maximize'); @@ -30,7 +33,7 @@ export default function TitleBar({template = "main"} : {template?: "update"|"mai setMaximized(!maximized); } - if(template === "main"){ + if(template === "index.html"){ return (
@@ -54,14 +57,14 @@ export default function TitleBar({template = "main"} : {template?: "update"|"mai ); } - if(template === "update"){ + if(template === "launcher.html"){ return (
) } - if(template === "oneclick"){ + if(template === "oneclick-download-map.html"){ return (
diff --git a/src/renderer/services/window-manager.service.ts b/src/renderer/services/window-manager.service.ts index ca9126f3..67ce6f5c 100644 --- a/src/renderer/services/window-manager.service.ts +++ b/src/renderer/services/window-manager.service.ts @@ -22,6 +22,10 @@ export class WindowManagerService{ public closeAll(except?: AppWindow){ this.ipcService.sendLazy("close-all-windows", {args: except}); - } + } + + public close(...win: AppWindow[]){ + this.ipcService.sendLazy("close-windows", {args: win}); + } } \ No newline at end of file diff --git a/src/renderer/windows/App.tsx b/src/renderer/windows/App.tsx index c1837916..b4b95473 100644 --- a/src/renderer/windows/App.tsx +++ b/src/renderer/windows/App.tsx @@ -42,7 +42,7 @@ export default function App() {
- +
}/> diff --git a/src/renderer/windows/Launcher.tsx b/src/renderer/windows/Launcher.tsx index 30600c6f..f193c482 100644 --- a/src/renderer/windows/Launcher.tsx +++ b/src/renderer/windows/Launcher.tsx @@ -42,7 +42,7 @@ export default function Launcher() { return (
- +
diff --git a/src/renderer/windows/OneClick/OneClickDownloadMap.tsx b/src/renderer/windows/OneClick/OneClickDownloadMap.tsx index d7f515b4..4785b55b 100644 --- a/src/renderer/windows/OneClick/OneClickDownloadMap.tsx +++ b/src/renderer/windows/OneClick/OneClickDownloadMap.tsx @@ -59,7 +59,7 @@ export default function OneClickDownloadMap() { }); - promise.finally(() => windows.closeAll("index.html")); + promise.finally(() =>{}); return () => { sub.unsubscribe(); @@ -71,7 +71,7 @@ export default function OneClickDownloadMap() {
{cover && }
- +

{mapInfo?.name ?? "Chargement de la map..."}