fix object desctory when oneClick window close

This commit is contained in:
MathieuG-P
2022-12-19 03:38:06 +01:00
parent 802d0533c8
commit 605a383963
9 changed files with 44 additions and 26 deletions
+8 -5
View File
@@ -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<string>) => {
@@ -34,7 +37,7 @@ ipcMain.on('choose-folder', async (event, request: IpcRequest<void>) => {
ipcMain.on("window.progression", async (event, request: IpcRequest<number>) => {
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[]}>) => {
+5
View File
@@ -14,4 +14,9 @@ ipcMain.on("open-window-then-close-all", async (event, request: IpcRequest<AppWi
ipcMain.on("close-all-windows", async (event, request: IpcRequest<AppWindow>) => {
const windowManager = WindowManagerService.getInstance();
windowManager.closeAllWindows(request.args);
});
ipcMain.on("close-windows", async (event, request: IpcRequest<AppWindow[]>) => {
const windowManager = WindowManagerService.getInstance();
windowManager.close(...request.args);
});
+5 -4
View File
@@ -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<AppWindow, BrowserWindow> = new Map<AppWindow, BrowserWindow>();
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<AppWindow, BrowserWindow>){ 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<T = any>(channel: string, response: IpcResponse<T>): 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);
}
+7 -5
View File
@@ -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();
});
}
}
@@ -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 (
<header id="titlebar" className="min-h-[22px] bg-light-main-color-1 shrink-0 dark:bg-main-color-1 w-screen h-[22px] flex content-center items-center justify-start z-10">
@@ -54,14 +57,14 @@ export default function TitleBar({template = "main"} : {template?: "update"|"mai
);
}
if(template === "update"){
if(template === "launcher.html"){
return (
<header id="titlebar" className="min-h-[22px] bg-transparent w-screen h-[22px] z-10">
<div id="drag-region" className='grow basis-0 h-full'></div>
</header>
)
}
if(template === "oneclick"){
if(template === "oneclick-download-map.html"){
return (
<header id="titlebar" className="min-h-[22px] bg-transparent w-screen h-[22px] flex content-center items-center justify-start z-10">
<div id="drag-region" className='grow h-full'>
@@ -22,6 +22,10 @@ export class WindowManagerService{
public closeAll(except?: AppWindow){
this.ipcService.sendLazy<AppWindow>("close-all-windows", {args: except});
}
}
public close(...win: AppWindow[]){
this.ipcService.sendLazy<AppWindow[]>("close-windows", {args: win});
}
}
+1 -1
View File
@@ -42,7 +42,7 @@ export default function App() {
<NotificationOverlay/>
<BsmIframeView/>
<div className="relative flex flex-col grow max-w-full min-w-0">
<TitleBar/>
<TitleBar template="index.html"/>
<div className="bg-light-main-color-2 dark:bg-main-color-2 relative rounded-tl-lg grow overflow-hidden max-w-full">
<Routes>
<Route path="/bs-version/:versionNumber" element={<VersionViewer/>}/>
+1 -1
View File
@@ -42,7 +42,7 @@ export default function Launcher() {
return (
<div className="w-full h-full">
<TitleBar template="update"/>
<TitleBar template="launcher.html"/>
<div className="relative flex flex-col items-center justify-center pt-10">
<motion.div ref={constraintsRef}>
<motion.div drag dragConstraints={constraintsRef} animate={{rotate: [0, 10, 0]}} transition={{ duration: .6, repeat: Infinity, repeatDelay: 1.6 }}>
@@ -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() {
<div className="relative w-screen h-screen overflow-hidden">
{cover && <BsmImage className="absolute top-0 left-0 w-full h-full object-cover" image={cover}/>}
<div className="w-full h-full backdrop-brightness-50 backdrop-blur-md flex flex-col justify-start items-center gap-10">
<TitleBar template="oneclick"/>
<TitleBar template="oneclick-download-map.html"/>
<BsmImage className="aspect-square w-1/2 object-cover rounded-md shadow-black shadow-lg" placeholder={defaultImage} image={cover} errorImage={defaultImage}/>
<h1 className="overflow-hidden font-bold italic text-xl text-gray-200 tracking-wide w-full text-center whitespace-nowrap text-ellipsis px-2">{mapInfo?.name ?? "Chargement de la map..."}</h1>
</div>