va-tracker/src/electron/main.ts

84 lines
1.9 KiB
TypeScript

import { app, BrowserWindow, session } from 'electron';
import { ipcMainHandle, ipcMainOn, isDev } from './util.js';
import { getStaticData, pollResources } from './resourceManager.js';
import { getPreloadPath, getUIPath } from './pathResolver.js';
import "./ipc/ipcHandler.js";
app.on('ready', async () => {
console.log("ready");
const mainWindow = new BrowserWindow({
width: 550, // fixed width
height: 750, // fixed height
resizable: false, // prevents resizing
fullscreenable: false, // disables fullscreen toggle (F11 or double-click title bar)
maximizable: false, // prevents maximize
autoHideMenuBar: true,
frame: process.platform === 'darwin' ? false : true,
webPreferences: {
preload: getPreloadPath(),
},
});
session.defaultSession.webRequest.onBeforeRequest((details, callback) => {
console.log('🌐 Request:', details.method, details.url);
callback({ cancel: false });
});
if (isDev()) {
mainWindow.loadURL('http://localhost:5123');
mainWindow.webContents.openDevTools();
} else {
mainWindow.loadFile(getUIPath());
}
pollResources(mainWindow);
// IPC handlers
ipcMainHandle('getStaticData', () => getStaticData());
ipcMainOn('sendFrameAction', (payload) => {
switch (payload) {
case 'CLOSE':
mainWindow.close();
break;
case 'MAXIMIZE':
// prevent maximize
break;
case 'MINIMIZE':
mainWindow.minimize();
break;
}
});
handleCloseEvents(mainWindow);
});
function handleCloseEvents(mainWindow: BrowserWindow) {
let willClose = false;
mainWindow.on('close', (e) => {
if (willClose) return;
e.preventDefault();
mainWindow.hide();
if (app.dock) {
app.dock.hide();
}
});
app.on('before-quit', () => {
willClose = true;
});
mainWindow.on('show', () => {
willClose = false;
});
}