200 lines
4.7 KiB
TypeScript
200 lines
4.7 KiB
TypeScript
import { app, BrowserWindow, globalShortcut, session } from "electron";
|
|
import { isDev } from "./util.js";
|
|
// import { pollResources } from "./resourceManager.js";
|
|
import { getPreloadPath, getUIPath } from "./pathResolver.js";
|
|
import { registerIPCHandlers } from "./ipc/ipcHandler.js";
|
|
|
|
import "./ipc/ipcHandler.js";
|
|
import path from "path";
|
|
import { fileURLToPath } from "url";
|
|
|
|
app.setAppUserModelId("com.tracker.app");
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
function resolveIcon() {
|
|
const isDev = !app.isPackaged;
|
|
|
|
if (isDev) {
|
|
// DEV mode → use project root path
|
|
return path.join(process.cwd(), "src", "assets", "icons", "appIcon.ico");
|
|
}
|
|
|
|
console.log("Line 24", path.join(__dirname, "..", "assets", "icons", "appIcon.ico"))
|
|
|
|
// PRODUCTION mode → inside dist-electron
|
|
return path.join(__dirname, "..", "assets", "icons", "appIcon.ico");
|
|
}
|
|
|
|
// ---------------------------
|
|
// SINGLE INSTANCE LOCK
|
|
// ---------------------------
|
|
const gotTheLock = app.requestSingleInstanceLock();
|
|
|
|
if (!gotTheLock) {
|
|
app.quit();
|
|
process.exit(0);
|
|
}
|
|
|
|
let mainWindow: BrowserWindow | null = null;
|
|
let splashWindow: BrowserWindow | null = null;
|
|
|
|
// ---------------------------
|
|
// HANDLE DOUBLE OPEN ATTEMPT
|
|
// ---------------------------
|
|
app.on("second-instance", () => {
|
|
if (mainWindow) {
|
|
if (mainWindow.isMinimized()) mainWindow.restore();
|
|
mainWindow.show();
|
|
mainWindow.focus();
|
|
}
|
|
});
|
|
|
|
// ---------------------------
|
|
// MAIN APP READY
|
|
// ---------------------------
|
|
app.on("ready", async () => {
|
|
|
|
|
|
splashWindow = new BrowserWindow({
|
|
width: 300,
|
|
height: 300,
|
|
frame: false,
|
|
transparent: false,
|
|
alwaysOnTop: false, // splash does NOT need to be on top || Yooo Don't make it onTop
|
|
resizable: false,
|
|
show: true,
|
|
skipTaskbar: true,
|
|
webPreferences: {
|
|
devTools: true,
|
|
}
|
|
});
|
|
|
|
|
|
splashWindow.loadFile(
|
|
path.join(__dirname, "../assets/splash.html")
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create the main window
|
|
const win = new BrowserWindow({
|
|
width: 500,
|
|
height: 600,
|
|
resizable: false,
|
|
fullscreenable: false,
|
|
minimizable: false,
|
|
maximizable: false,
|
|
closable: true,
|
|
alwaysOnTop: true,
|
|
skipTaskbar: true,
|
|
autoHideMenuBar: true,
|
|
frame: process.platform === "darwin" ? false : true,
|
|
show: false, // speed improvement
|
|
icon: resolveIcon(),
|
|
webPreferences: {
|
|
devTools: true,
|
|
preload: getPreloadPath(),
|
|
contextIsolation: true,
|
|
nodeIntegration: false,
|
|
},
|
|
});
|
|
|
|
mainWindow = win;
|
|
|
|
// ------------------------------------
|
|
// ONLY LOG NETWORK REQUESTS IN DEV
|
|
// ------------------------------------
|
|
if (isDev() && process.env.LOG_REQUESTS === "true") {
|
|
session.defaultSession.webRequest.onBeforeRequest((details, callback) => {
|
|
console.log("🌐 Request:", details.method, details.url);
|
|
callback({ cancel: false });
|
|
});
|
|
}
|
|
|
|
// ------------------------------------
|
|
// LOAD FRONTEND
|
|
// ------------------------------------
|
|
if (isDev()) {
|
|
win.loadURL("http://localhost:5123");
|
|
win.webContents.openDevTools(); // Optional (slows startup)
|
|
} else {
|
|
win.loadFile(getUIPath());
|
|
}
|
|
|
|
// ------------------------------------
|
|
// SHOW WINDOW WHEN READY (no white flash)
|
|
// ------------------------------------
|
|
win.once("ready-to-show", () => {
|
|
// Delay heavy work to improve startup speed
|
|
setTimeout(() => {
|
|
splashWindow?.close();
|
|
splashWindow = null;
|
|
|
|
win.show();
|
|
// pollResources(win);
|
|
registerIPCHandlers(win);
|
|
}, 500);
|
|
});
|
|
|
|
// ------------------------------------
|
|
// HANDLE CLOSE EVENTS
|
|
// ------------------------------------
|
|
handleCloseEvents(win);
|
|
});
|
|
|
|
|
|
// app.on('browser-window-focus', () => {
|
|
// globalShortcut.register('CommandOrControl+R', () => {
|
|
// // Do nothing; disables reload
|
|
// });
|
|
// globalShortcut.register('F5', () => {
|
|
// // Do nothing; disables reload
|
|
// });
|
|
// globalShortcut.register('CommandOrControl+Shift+I', () => {
|
|
// // Do nothing; disables DevTools
|
|
// });
|
|
// });
|
|
|
|
app.on('browser-window-blur', () => {
|
|
globalShortcut.unregisterAll();
|
|
});
|
|
|
|
|
|
// ---------------------------
|
|
// CUSTOM CLOSE BEHAVIOR
|
|
// ---------------------------
|
|
function handleCloseEvents(window: BrowserWindow) {
|
|
let willClose = false;
|
|
|
|
window.on("close", (e) => {
|
|
if (willClose) return;
|
|
|
|
e.preventDefault();
|
|
window.hide();
|
|
|
|
if (app.dock) {
|
|
app.dock.hide();
|
|
}
|
|
});
|
|
|
|
app.on("before-quit", () => {
|
|
willClose = true;
|
|
});
|
|
|
|
window.on("show", () => {
|
|
willClose = false;
|
|
});
|
|
}
|
|
|
|
// ---------------------------
|
|
// GET MAIN WINDOW EXPORT
|
|
// ---------------------------
|
|
export function getMainWindow() {
|
|
return mainWindow;
|
|
}
|