Документированный программный код

Ссылка на Github репозиторий проекта

Сниппет кода

let mainWindow: BrowserWindow;

/**
 * Create Application Window
 * @returns {BrowserWindow} Application Window Instance
 */
export const createMainWindow = (): BrowserWindow => {
    // Create new window instance
    mainWindow = new BrowserWindow({
        width: 1024,
        height: 728,
        minHeight: 500,
        minWidth: 500,
        backgroundColor: "#202020",
        show: false,
        autoHideMenuBar: true,
        frame: false,
        titleBarStyle: "hidden",
        icon: path.resolve("assets/icons/platforms/bookord-circle@4x.png"),
        webPreferences: {
            nodeIntegration: false,
            nodeIntegrationInWorker: false,
            nodeIntegrationInSubFrames: false,
            contextIsolation: true,
            preload: APP_WINDOW_PRELOAD_WEBPACK_ENTRY,
            // TODO process sandboxing
            // https://www.electronjs.org/docs/latest/tutorial/sandbox
            sandbox: false,
        },
    });

    // Enable pinch-to-zoom
    mainWindow.webContents.setVisualZoomLevelLimits(1, 3);

    // Permissions API
    const partition = "default";
    session
        .fromPartition(partition) /* eng-disable PERMISSION_REQUEST_HANDLER_JS_CHECK */
        .setPermissionRequestHandler((webContents, permission, permCallback) => {
            const allowedPermissions: string[] = []; // Full list here: https://developer.chrome.com/extensions/declare_permissions#manifest

            if (allowedPermissions.includes(permission)) {
                permCallback(true); // Approve permission request
            } else {
                console.error(
                    `The application tried to request permission for '${permission}'. This permission was not whitelisted and has been blocked.`
                );

                permCallback(false); // Deny
            }
        });
    // TODO i18nextMainBackend
    // TODO electronegativity

    // Load the index.html of the app window.
    mainWindow.loadURL(APP_WINDOW_WEBPACK_ENTRY);

    // Show window when its ready to
    mainWindow.on("ready-to-show", () => {
        mainWindow.maximize();
        mainWindow.focus();
        !isDev() && mainWindow.show();
    });

    // Only do these things when in development
    if (isDev()) {
        // Errors are thrown if the dev tools are opened
        // before the DOM is ready
        mainWindow.webContents.once("dom-ready", async () => {
            // Provides dev tools shortcuts
            // TODO supress dev tools hmr errors
            electronDebug();
            mainWindow.webContents.openDevTools();
        });
    }

    // Register Inter Process Communication for main process
    registerAllIpc();

    const watcher = io.initWatcher(mainWindow, validateSender);

    // Close all windows when main window is closed
    mainWindow.on("close", async () => {
        (await watcher).close().then(() => {
            console.info("[watcher]: closed");

            mainWindow = null;
            app.quit();
        });
    });

    return mainWindow;
};