diff options
author | RaindropsSys <raindrops@equestria.dev> | 2024-06-13 15:46:03 +0200 |
---|---|---|
committer | RaindropsSys <raindrops@equestria.dev> | 2024-06-13 15:46:03 +0200 |
commit | e44e2fe070484e06d384a31ef2699c3a2d5d474e (patch) | |
tree | 2d5eb5d1b01646270d18cf1f2d94519966d6e7de /main.js | |
download | faunerie-e44e2fe070484e06d384a31ef2699c3a2d5d474e.tar.gz faunerie-e44e2fe070484e06d384a31ef2699c3a2d5d474e.tar.bz2 faunerie-e44e2fe070484e06d384a31ef2699c3a2d5d474e.zip |
GitHub migration
Diffstat (limited to 'main.js')
-rwxr-xr-x | main.js | 119 |
1 files changed, 119 insertions, 0 deletions
@@ -0,0 +1,119 @@ +/* +Developers! Don't rewrite this in TypeScript! +Only the Electron renderer code should be rewritten in TypeScript + */ + +const { app, dialog, BrowserWindow, ipcMain, protocol} = require('electron'); +const zlib = require('zlib'); +const fs = require('fs'); +const util = require('util'); +const { userInfo } = require('os'); + +protocol.registerSchemesAsPrivileged([ + { scheme: 'pbip', privileges: { bypassCSP: true, secure: true, stream: true, corsEnabled: false, supportFetchAPI: true } } +]) + +require('@electron/remote/main').initialize(); + +function open() { + global.win = new BrowserWindow({ + width: 1500, + minWidth: 800, + title: "Prisbeam", + height: 800, + minHeight: 450, + webPreferences: { + nodeIntegration: true, + contextIsolation: false, + backgroundThrottling: false + }, + titleBarStyle: "hidden", + titleBarOverlay: { + color: "transparent", + symbolColor: "#ffffff", + height: 43 + }, + backgroundColor: "#222222", + trafficLightPosition: { + x: 13, + y: 13 + }, + }); + + require("@electron/remote/main").enable(win.webContents); + + win.on('close', async (e) => { + if (await win.webContents.executeJavaScript("instance?.dataStore.unloaded;")) e.preventDefault(); + await win.webContents.executeJavaScript("instance?.safeClose();"); + }) + + win.loadFile("./dom/index.html"); + win.on('ready-to-show', () => { + win.send("path", app.getPath("userData")); + }); + + ipcMain.handle('openFolder', () => { + return dialog.showOpenDialogSync({ + title: "Select the directory where your database has been saved", + message: "Select the directory where your database has been saved", + defaultPath: userInfo().homedir, + buttonLabel: "Open", + properties: ["openDirectory", "treatPackageAsDirectory", "dontAddToRecent", "createDirectory"] + }); + }); + + ipcMain.handle('openPreprocessed', () => { + return dialog.showOpenDialogSync({ + title: "Select the file corresponding to your preprocessed database", + message: "Select the file corresponding to your preprocessed database", + defaultPath: userInfo().homedir, + buttonLabel: "Open", + properties: ["openFile", "treatPackageAsDirectory", "dontAddToRecent"] + }); + }); +} + +if (app.getName() !== "Electron") { + const gotTheLock = app.requestSingleInstanceLock(); + + if (!gotTheLock) { + app.quit(); + } +} + +app.whenReady().then(() => { + protocol.handle('pbip', async (req) => { + const { pathname, searchParams } = new URL(req.url); + let mime = searchParams.get("mime") ?? "application/octet-stream"; + + const inflateRawSync = util.promisify(zlib.inflateRaw); + + try { + let file = await fs.promises.readFile(pathname); + let data = await inflateRawSync(file); + + return new Response(data, { + status: 200, + headers: { 'content-type': mime } + }); + } catch (e) { + console.error(e); + + return new Response(e.stack, { + status: 500, + headers: { 'content-type': 'text/plain' } + }); + } + + }); + + open(); + + app.on('activate', () => { + if (BrowserWindow.getAllWindows().length === 0) open(); + }); +}); + +app.on('window-all-closed', () => { + app.quit(); +}); |