va-tracker/src/electron/main.ts

98 lines
2.4 KiB
TypeScript

import { app, BrowserWindow, Menu, desktopCapturer } from 'electron';
import { ipcMainHandle, ipcMainOn, isDev } from './util.js';
import { getStaticData, pollResources } from './resourceManager.js';
import { getPreloadPath, getUIPath } from './pathResolver.js';
import { createTray } from './tray.js';
import { createMenu } from './menu.js';
import fs from "fs";
import path from "path";
app.on('ready', async () => {
const mainWindow = new BrowserWindow({
webPreferences: {
preload: getPreloadPath(),
},
// disables default system frame (dont do this if you want a proper working menu bar)
frame: process.platform === 'darwin' ? false : true,
});
if (isDev()) {
mainWindow.loadURL('http://localhost:5123');
} else {
mainWindow.loadFile(getUIPath());
}
pollResources(mainWindow);
ipcMainHandle('getStaticData', () => {
return getStaticData();
});
ipcMainOn('sendFrameAction', (payload) => {
switch (payload) {
case 'CLOSE':
mainWindow.close();
break;
case 'MAXIMIZE':
mainWindow.maximize();
break;
case 'MINIMIZE':
mainWindow.minimize();
break;
}
});
createTray(mainWindow);
handleCloseEvents(mainWindow);
createMenu(mainWindow);
await takeScreenshot();
});
async function takeScreenshot() {
// const sources = await desktopCapturer.getSources({ types: ["screen"] });
const sources = await desktopCapturer.getSources({
types: ['window'], thumbnailSize: {
height: 768,
width: 1366
}
});
// Capture the first screen (you can choose others by filtering sources)
const screen = sources[0];
const image = screen.thumbnail.toPNG();
const folderPath = "C:\\Users\\Public\\vatracker\\screenshots";
// Ensure directory exists
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath, { recursive: true });
}
const filePath = path.join(folderPath, `screenshot-${Date.now()}.png`);
fs.writeFileSync(filePath, image);
console.log(`Screenshot saved at: ${filePath}`);
}
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;
});
}