316 lines
8.3 KiB
TypeScript
316 lines
8.3 KiB
TypeScript
import {
|
|
app,
|
|
BrowserWindow,
|
|
ipcMain,
|
|
Menu,
|
|
shell,
|
|
utilityProcess,
|
|
} from 'electron';
|
|
import type {
|
|
BrowserWindow as BrowserWindowType,
|
|
IpcMainInvokeEvent,
|
|
MenuItemConstructorOptions,
|
|
} from 'electron';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
const appRoot = path.resolve(__dirname, '../..');
|
|
const brandName = 'TrueGrowth';
|
|
const brandIconPath = path.join(
|
|
appRoot,
|
|
'apps/electron/assets/truegrowth-dock-icon.png'
|
|
);
|
|
const localApiPort = Number(process.env.TRUEGROWTH_LOCAL_API_PORT || 48177);
|
|
const userDataPath =
|
|
process.env.TRUEGROWTH_ELECTRON_USER_DATA ||
|
|
path.join(app.getPath('appData'), brandName);
|
|
const isDev = Boolean(process.env.VITE_DEV_SERVER_URL);
|
|
let localApi: { close: () => void; listen: (...args: unknown[]) => unknown; on: (...args: unknown[]) => unknown } | null = null;
|
|
let mainWindow: BrowserWindow | null = null;
|
|
|
|
function readAppVersion(): string {
|
|
try {
|
|
const packageInfo = JSON.parse(
|
|
fs.readFileSync(path.join(appRoot, 'package.json'), 'utf8')
|
|
) as { version?: string };
|
|
return packageInfo.version || app.getVersion();
|
|
} catch {
|
|
return app.getVersion();
|
|
}
|
|
}
|
|
|
|
const appVersion = readAppVersion();
|
|
|
|
function createDesktopLocalRendererEntry(indexHtmlPath: string) {
|
|
const html = fs.readFileSync(indexHtmlPath, 'utf8');
|
|
const appAssetPattern =
|
|
/https:\/\/cdn\.jsdelivr\.net\/npm\/aitu-app@[^/"'>\s]+\/([^"'<>\s]+)/g;
|
|
|
|
return html.replace(appAssetPattern, (_url, assetPath) => `./${assetPath}`);
|
|
}
|
|
|
|
process.title = brandName;
|
|
app.setName(brandName);
|
|
app.setAppUserModelId('ai.truegrowth.desktop');
|
|
app.setPath('userData', userDataPath);
|
|
process.env.TRUEGROWTH_ELECTRON_USER_DATA = userDataPath;
|
|
process.env.TRUEGROWTH_RUNTIME_STATE_DIR =
|
|
process.env.TRUEGROWTH_RUNTIME_STATE_DIR ||
|
|
path.join(userDataPath, 'runtime');
|
|
if (!isDev) {
|
|
process.env.TRUEGROWTH_DESKTOP_PACKAGED =
|
|
process.env.TRUEGROWTH_DESKTOP_PACKAGED || '1';
|
|
}
|
|
|
|
const hasSingleInstanceLock = app.requestSingleInstanceLock({
|
|
userDataPath,
|
|
});
|
|
|
|
if (!hasSingleInstanceLock) {
|
|
app.quit();
|
|
process.exit(0);
|
|
}
|
|
|
|
app.on('second-instance', () => {
|
|
if (!mainWindow) {
|
|
return;
|
|
}
|
|
if (mainWindow.isMinimized()) {
|
|
mainWindow.restore();
|
|
}
|
|
mainWindow.focus();
|
|
mainWindow.show();
|
|
});
|
|
|
|
function getWindowState(window: BrowserWindowType | null) {
|
|
return {
|
|
isFullScreen: window?.isFullScreen?.() ?? false,
|
|
isMaximized: window?.isMaximized?.() ?? false,
|
|
platform: process.platform,
|
|
};
|
|
}
|
|
|
|
function sendWindowState(window: BrowserWindowType | null) {
|
|
if (!window || window.isDestroyed()) {
|
|
return;
|
|
}
|
|
window.webContents.send('truegrowth-window:state', getWindowState(window));
|
|
}
|
|
|
|
function getWindowFromIpcEvent(event: IpcMainInvokeEvent) {
|
|
const window = BrowserWindow.fromWebContents(event.sender);
|
|
return window && !window.isDestroyed() ? window : null;
|
|
}
|
|
|
|
function installWindowControlsIpc() {
|
|
ipcMain.handle('truegrowth-window:minimize', (event) => {
|
|
const window = getWindowFromIpcEvent(event);
|
|
window?.minimize();
|
|
return window ? getWindowState(window) : getWindowState(mainWindow);
|
|
});
|
|
|
|
ipcMain.handle('truegrowth-window:toggle-maximize', (event) => {
|
|
const window = getWindowFromIpcEvent(event);
|
|
if (!window) {
|
|
return getWindowState(mainWindow);
|
|
}
|
|
if (window.isFullScreen()) {
|
|
window.setFullScreen(false);
|
|
} else if (window.isMaximized()) {
|
|
window.unmaximize();
|
|
} else {
|
|
window.maximize();
|
|
}
|
|
return getWindowState(window);
|
|
});
|
|
|
|
ipcMain.handle('truegrowth-window:close', (event) => {
|
|
const window = getWindowFromIpcEvent(event);
|
|
window?.close();
|
|
return { closed: Boolean(window) };
|
|
});
|
|
|
|
ipcMain.handle('truegrowth-window:is-maximized', (event) => {
|
|
const window = getWindowFromIpcEvent(event);
|
|
return getWindowState(window || mainWindow);
|
|
});
|
|
}
|
|
|
|
function installApplicationMenu() {
|
|
const editMenu: MenuItemConstructorOptions[] = [
|
|
{ role: 'undo' },
|
|
{ role: 'redo' },
|
|
{ type: 'separator' },
|
|
{ role: 'cut' },
|
|
{ role: 'copy' },
|
|
{ role: 'paste' },
|
|
{ role: 'selectAll' },
|
|
];
|
|
const viewMenu: MenuItemConstructorOptions[] = [
|
|
{ role: 'reload' },
|
|
{ role: 'forceReload' },
|
|
{ role: 'toggleDevTools' },
|
|
{ type: 'separator' },
|
|
{ role: 'resetZoom' },
|
|
{ role: 'zoomIn' },
|
|
{ role: 'zoomOut' },
|
|
{ type: 'separator' },
|
|
{ role: 'togglefullscreen' },
|
|
];
|
|
const windowMenu: MenuItemConstructorOptions[] = [
|
|
{ role: 'minimize' },
|
|
{ role: 'zoom' },
|
|
...(process.platform === 'darwin'
|
|
? [{ type: 'separator' as const }, { role: 'front' as const }]
|
|
: [{ role: 'close' as const }]),
|
|
];
|
|
const template: MenuItemConstructorOptions[] = [
|
|
...(process.platform === 'darwin'
|
|
? [
|
|
{
|
|
label: brandName,
|
|
submenu: [
|
|
{ role: 'about', label: `About ${brandName}` },
|
|
{ type: 'separator' },
|
|
{ role: 'services' },
|
|
{ type: 'separator' },
|
|
{ role: 'hide', label: `Hide ${brandName}` },
|
|
{ role: 'hideOthers' },
|
|
{ role: 'unhide' },
|
|
{ type: 'separator' },
|
|
{ role: 'quit', label: `Quit ${brandName}` },
|
|
],
|
|
},
|
|
]
|
|
: []),
|
|
{
|
|
label: 'File',
|
|
submenu: [
|
|
process.platform === 'darwin' ? { role: 'close' } : { role: 'quit' },
|
|
],
|
|
},
|
|
{ label: 'Edit', submenu: editMenu },
|
|
{ label: 'View', submenu: viewMenu },
|
|
{ label: 'Window', submenu: windowMenu },
|
|
{ label: 'Help', submenu: [] },
|
|
];
|
|
|
|
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
|
|
}
|
|
|
|
function createWindow() {
|
|
mainWindow = new BrowserWindow({
|
|
width: 1440,
|
|
height: 920,
|
|
minWidth: 1180,
|
|
minHeight: 760,
|
|
title: brandName,
|
|
icon: brandIconPath,
|
|
frame: false,
|
|
autoHideMenuBar: true,
|
|
backgroundColor: '#f7f7f5',
|
|
webPreferences: {
|
|
preload: path.join(__dirname, 'preload.js'),
|
|
contextIsolation: true,
|
|
nodeIntegration: false,
|
|
sandbox: false,
|
|
},
|
|
});
|
|
|
|
if (process.platform !== 'darwin') {
|
|
mainWindow.setMenuBarVisibility(false);
|
|
}
|
|
|
|
mainWindow.on('closed', () => {
|
|
mainWindow = null;
|
|
});
|
|
|
|
[
|
|
'enter-full-screen',
|
|
'leave-full-screen',
|
|
'maximize',
|
|
'resize',
|
|
'restore',
|
|
'unmaximize',
|
|
].forEach((eventName) => {
|
|
mainWindow?.on(eventName as 'resize', () => sendWindowState(mainWindow));
|
|
});
|
|
|
|
mainWindow.webContents.once('did-finish-load', () => {
|
|
sendWindowState(mainWindow);
|
|
});
|
|
|
|
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
|
|
void shell.openExternal(url);
|
|
return { action: 'deny' };
|
|
});
|
|
|
|
if (isDev && process.env.VITE_DEV_SERVER_URL) {
|
|
void mainWindow.loadURL(process.env.VITE_DEV_SERVER_URL);
|
|
return;
|
|
}
|
|
|
|
const indexHtmlPath = path.join(__dirname, '../../dist/apps/web/index.html');
|
|
const entryHtml = createDesktopLocalRendererEntry(indexHtmlPath);
|
|
|
|
void mainWindow.loadURL(
|
|
`data:text/html;charset=utf-8,${encodeURIComponent(entryHtml)}`,
|
|
{
|
|
baseURLForDataURL: `${pathToFileURL(
|
|
path.dirname(indexHtmlPath)
|
|
).toString()}/`,
|
|
}
|
|
);
|
|
}
|
|
|
|
async function startLocalApi() {
|
|
const { createLocalApiServer } = await import(
|
|
'../../scripts/truegrowth-local-api.mjs'
|
|
);
|
|
localApi = createLocalApiServer({ utilityProcess });
|
|
localApi.on('error', (error: NodeJS.ErrnoException) => {
|
|
if (error?.code === 'EADDRINUSE') {
|
|
console.log(
|
|
`TrueGrowth local API already available at http://127.0.0.1:${localApiPort}`
|
|
);
|
|
return;
|
|
}
|
|
throw error;
|
|
});
|
|
localApi.listen(localApiPort, '127.0.0.1');
|
|
}
|
|
|
|
app.whenReady().then(async () => {
|
|
installApplicationMenu();
|
|
installWindowControlsIpc();
|
|
if (process.platform === 'darwin' && app.dock) {
|
|
app.dock.setIcon(brandIconPath);
|
|
}
|
|
app.setAboutPanelOptions({
|
|
applicationName: brandName,
|
|
applicationVersion: appVersion,
|
|
iconPath: brandIconPath,
|
|
copyright: `Copyright © ${new Date().getFullYear()} TrueGrowth`,
|
|
});
|
|
await startLocalApi();
|
|
createWindow();
|
|
|
|
app.on('activate', () => {
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
|
createWindow();
|
|
}
|
|
});
|
|
});
|
|
|
|
app.on('window-all-closed', () => {
|
|
if (process.platform !== 'darwin') {
|
|
app.quit();
|
|
}
|
|
});
|
|
|
|
app.on('before-quit', () => {
|
|
localApi?.close();
|
|
});
|