129 lines
3.3 KiB
TypeScript
129 lines
3.3 KiB
TypeScript
import { app, BrowserWindow, Tray, Menu, nativeImage } from 'electron';
|
|
import * as path from 'path';
|
|
import * as dotenv from 'dotenv';
|
|
import { setupIpcHandlers, cleanupIpcHandlers } from './ipc-handlers';
|
|
import { initDatabase, closeDatabase } from './database';
|
|
|
|
// Load environment variables from .env file
|
|
// In dev: __dirname = dist/main, so go up to electron-app root
|
|
// In prod: __dirname = resources/app.asar/dist/main, .env should be in resources
|
|
const envPath = process.env.NODE_ENV === 'development'
|
|
? path.join(__dirname, '../../.env')
|
|
: path.join(process.resourcesPath, '.env');
|
|
dotenv.config({ path: envPath });
|
|
|
|
console.log('Loading .env from:', envPath);
|
|
console.log('WS_URL:', process.env.WS_URL);
|
|
console.log('API_URL:', process.env.API_URL);
|
|
|
|
let mainWindow: BrowserWindow | null = null;
|
|
let tray: Tray | null = null;
|
|
|
|
function createWindow(): void {
|
|
// In dev: __dirname = dist/main, logo is at root
|
|
// In prod: __dirname = resources/app.asar/dist/main
|
|
const iconPath = process.env.NODE_ENV === 'development'
|
|
? path.join(__dirname, '../../logo.png')
|
|
: path.join(__dirname, '../assets/logo.png');
|
|
|
|
console.log('Window icon path:', iconPath);
|
|
|
|
mainWindow = new BrowserWindow({
|
|
width: 1400,
|
|
height: 900,
|
|
minWidth: 1000,
|
|
minHeight: 700,
|
|
frame: false,
|
|
backgroundColor: '#0a0e27',
|
|
icon: iconPath,
|
|
webPreferences: {
|
|
preload: path.join(__dirname, 'preload.js'),
|
|
nodeIntegration: false,
|
|
contextIsolation: true,
|
|
sandbox: true,
|
|
},
|
|
title: 'rmtPocketWatcher',
|
|
});
|
|
|
|
// Setup IPC handlers for WebSocket communication
|
|
setupIpcHandlers(mainWindow);
|
|
|
|
// Load the app
|
|
if (process.env.NODE_ENV === 'development') {
|
|
mainWindow.loadURL('http://localhost:5173');
|
|
mainWindow.webContents.openDevTools();
|
|
} else {
|
|
mainWindow.loadFile(path.join(__dirname, '../../renderer/index.html'));
|
|
}
|
|
|
|
mainWindow.on('closed', () => {
|
|
cleanupIpcHandlers();
|
|
mainWindow = null;
|
|
});
|
|
}
|
|
|
|
function createTray(): void {
|
|
// In dev: __dirname = dist/main, logo is at root
|
|
// In prod: __dirname = resources/app.asar/dist/main
|
|
const iconPath = process.env.NODE_ENV === 'development'
|
|
? path.join(__dirname, '../../logo.png')
|
|
: path.join(__dirname, '../assets/logo.png');
|
|
|
|
console.log('Tray icon path:', iconPath);
|
|
|
|
const icon = nativeImage.createFromPath(iconPath).resize({ width: 16, height: 16 });
|
|
|
|
tray = new Tray(icon);
|
|
|
|
const contextMenu = Menu.buildFromTemplate([
|
|
{
|
|
label: 'Show rmtPocketWatcher',
|
|
click: () => {
|
|
if (mainWindow) {
|
|
mainWindow.show();
|
|
mainWindow.focus();
|
|
}
|
|
}
|
|
},
|
|
{
|
|
label: 'Quit',
|
|
click: () => {
|
|
app.quit();
|
|
}
|
|
}
|
|
]);
|
|
|
|
tray.setToolTip('rmtPocketWatcher');
|
|
tray.setContextMenu(contextMenu);
|
|
|
|
// Double-click to show window
|
|
tray.on('double-click', () => {
|
|
if (mainWindow) {
|
|
mainWindow.show();
|
|
mainWindow.focus();
|
|
}
|
|
});
|
|
}
|
|
|
|
app.whenReady().then(() => {
|
|
initDatabase();
|
|
createTray();
|
|
createWindow();
|
|
});
|
|
|
|
app.on('window-all-closed', () => {
|
|
// Quit the app when all windows are closed
|
|
app.quit();
|
|
});
|
|
|
|
app.on('activate', () => {
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
|
createWindow();
|
|
}
|
|
});
|
|
|
|
app.on('before-quit', () => {
|
|
cleanupIpcHandlers();
|
|
closeDatabase();
|
|
});
|