[MM-38607] Ensure window is inside one of the screen bounds before rendering (#1769)

This commit is contained in:
Devin Binnie 2021-09-29 12:40:27 -04:00 committed by GitHub
parent a48c469005
commit a3d7f0518e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,7 +6,7 @@ import fs from 'fs';
import path from 'path';
import os from 'os';
import {app, BrowserWindow, BrowserWindowConstructorOptions, globalShortcut, ipcMain} from 'electron';
import {app, BrowserWindow, BrowserWindowConstructorOptions, globalShortcut, ipcMain, screen} from 'electron';
import log from 'electron-log';
import {CombinedConfig} from 'types/config';
@ -32,6 +32,10 @@ function saveWindowState(file: string, window: BrowserWindow) {
}
}
function isInsideRectangle(container: Electron.Rectangle, rect: Electron.Rectangle) {
return container.x <= rect.x && container.y <= rect.y && container.width >= rect.width && container.height >= rect.height;
}
function isFramelessWindow() {
return os.platform() === 'darwin' || (os.platform() === 'win32' && os.release().startsWith('10'));
}
@ -46,6 +50,10 @@ function createMainWindow(config: CombinedConfig, options: {linuxAppIcon: string
if (!savedWindowState) {
throw new Error('Provided bounds info file does not validate, using defaults instead.');
}
const matchingScreen = screen.getDisplayMatching(savedWindowState);
if (!(matchingScreen && (isInsideRectangle(matchingScreen.bounds, savedWindowState) || savedWindowState.maximized))) {
throw new Error('Provided bounds info are outside the bounds of your screen, using defaults instead.');
}
} catch (e) {
// Follow Electron's defaults, except for window dimensions which targets 1024x768 screen resolution.
savedWindowState = {width: DEFAULT_WINDOW_WIDTH, height: DEFAULT_WINDOW_HEIGHT};