Files
desktop/lib/main/app.ts
rosetta 83f38dc63f 'init'
2026-01-30 05:01:05 +02:00

228 lines
5.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { BrowserWindow, shell, app, ipcMain, nativeTheme, screen, powerMonitor } from 'electron'
import { join } from 'path'
import fs from 'fs'
import { WORKING_DIR } from './constants';
import { boot } from './boot/bootloader';
export async function startApplication() {
let preloaderWindow = createPreloaderWindow();
await fs.promises.mkdir(WORKING_DIR, { recursive: true });
createAppWindow(preloaderWindow);
}
export function createPreloaderWindow() {
let preloaderWindow = new BrowserWindow({
width: 150,
height: 150,
frame: false,
transparent: true,
center: true,
resizable: false,
alwaysOnTop: true
});
preloaderWindow.loadFile(join(__dirname, '../../resources/preload.html'));
return preloaderWindow;
}
export function createAppWindow(preloaderWindow?: BrowserWindow): void {
const mainWindow = new BrowserWindow({
width: 900,
height: 670,
minWidth: 385,
minHeight: 555,
show: false,
title: 'Rosetta Messager',
icon: join(__dirname, '../../resources/R.png'),
frame: false,
autoHideMenuBar: true,
backgroundColor: '#000',
webPreferences: {
preload: join(__dirname, '../preload/preload.js'),
sandbox: false,
nodeIntegration: true,
nodeIntegrationInSubFrames: true,
nodeIntegrationInWorker: true,
webSecurity: false,
allowRunningInsecureContent: true
}
});
powerMonitor.on('lock-screen', () => {
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.reload();
}
});
foundationIpcRegistration(mainWindow);
mainWindow.webContents.on('did-finish-load', () => {
if (preloaderWindow && !preloaderWindow.isDestroyed()) {
preloaderWindow.close();
}
mainWindow.show();
});
mainWindow.webContents.setWindowOpenHandler((details) => {
shell.openExternal(details.url)
return { action: 'deny' }
});
boot(mainWindow);
}
export function foundationIpcRegistration(mainWindow: BrowserWindow) {
ipcMain.removeAllListeners('window-resize');
ipcMain.removeAllListeners('window-resizeble');
ipcMain.removeAllListeners('window-theme');
ipcMain.removeAllListeners("write-file");
ipcMain.removeAllListeners("read-file");
ipcMain.removeAllListeners("mkdir");
ipcMain.removeHandler("get-core-version");
ipcMain.removeHandler("get-arch");
ipcMain.removeAllListeners("get-user-dir");
ipcMain.removeHandler("get-downloads-path")
ipcMain.removeHandler("get-app-path");
ipcMain.removeHandler('open-dev-tools');
ipcMain.removeHandler('window-state');
ipcMain.removeHandler('window-toggle');
ipcMain.removeHandler('window-close');
ipcMain.removeHandler('window-minimize');
ipcMain.removeHandler('showItemInFolder');
ipcMain.removeHandler('openExternal');
ipcMain.handle('showItemInFolder', (_, fullPath: string) => {
shell.showItemInFolder(fullPath);
});
ipcMain.handle('openExternal', (_, url: string) => {
shell.openExternal(url);
});
ipcMain.handle('open-dev-tools', () => {
if (mainWindow.webContents.isDevToolsOpened()) {
return;
}
mainWindow.webContents.openDevTools({ mode: 'detach' });
});
ipcMain.on('window-resize', (_, { width, height }) => {
const { x: currentX, y: currentY, width: currentWidth, height: currentHeight } = mainWindow.getBounds();
const newX = currentX + (currentWidth - width) / 2;
const newY = currentY + (currentHeight - height) / 2;
const { width: screenWidth, height: screenHeight } = screen.getPrimaryDisplay().workAreaSize;
const clampedX = Math.max(0, Math.min(newX, screenWidth - width));
//const clampedY = Math.max(0, Math.min(newY, screenHeight - height));
mainWindow.setBounds({
x: Math.round(clampedX),
//y: Math.round(clampedY),
width,
height,
});
});
ipcMain.on('window-resizeble', (_, isResizeble) => {
mainWindow.setResizable(isResizeble);
mainWindow.webContents.send('window-state-changed');
});
ipcMain.on('window-theme', (_, theme) => {
nativeTheme.themeSource = theme;
})
ipcMain.on("write-file", (_, filePath, content) => {
fs.writeFile(filePath, content, (err) => {
if (err) {
console.error(err);
return;
}
});
});
ipcMain.handle('window-state', () => {
return {
isMinimized: mainWindow.isMinimized(),
isMaximized: mainWindow.isMaximized(),
isFullScreen: mainWindow.isFullScreen(),
isVisible: mainWindow.isVisible(),
isFocused: mainWindow.isFocused(),
isResizable: mainWindow.isResizable(),
isClosable: mainWindow.isClosable(),
isDestroyed: mainWindow.isDestroyed(),
bounds: mainWindow.getBounds()
};
});
ipcMain.handle('window-toggle', () => {
if (mainWindow.isMaximized() || mainWindow.isFullScreen()) {
mainWindow.setFullScreen(false);
mainWindow.unmaximize();
} else {
mainWindow.setFullScreen(true);
}
setTimeout(() => {
/**
* Разобраться с этой хуйней
*/
mainWindow.webContents.send('window-state-changed');
}, 700);
});
ipcMain.handle('window-minimize', () => {
mainWindow.minimize();
mainWindow.webContents.send('window-state-changed');
});
ipcMain.handle('window-close', () => {
mainWindow.hide();
mainWindow.webContents.send('window-state-changed');
});
ipcMain.on("read-file", (_, filePath) => {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
mainWindow.webContents.send("read-file-reply", data);
});
});
ipcMain.on("mkdir", (_, dirPath) => {
fs.mkdir(dirPath, { recursive: true }, (err) => {
if (err) {
console.error(err);
return;
}
mainWindow.webContents.send("mkdir-reply");
});
});
/**
* Change to get-core-version
*/
ipcMain.handle("get-core-version", () => {
return app.getVersion();
});
ipcMain.handle("get-arch", () => {
return process.arch;
})
ipcMain.on("get-user-dir", () => {
const userDir = app.getPath("userData");
mainWindow.webContents.send("get-user-dir-reply", userDir);
});
ipcMain.handle("get-app-path", () => {
return app.getAppPath();
});
ipcMain.handle("get-downloads-path", () => {
return app.getPath("downloads");
});
}