fix desktop initial window size being bigger than current display size on smaller Linux/Windows screens

This commit is contained in:
undergroundwires
2021-04-05 14:31:31 +01:00
parent 5c43965f0b
commit 02bdc4cf04

View File

@@ -4,7 +4,7 @@
// This script is running through entire life of the application. // This script is running through entire life of the application.
// It doesn't have any windows which you can see on screen, opens the main window from here. // It doesn't have any windows which you can see on screen, opens the main window from here.
import { app, protocol, BrowserWindow, shell } from 'electron'; import { app, protocol, BrowserWindow, shell, screen } from 'electron';
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'; import { createProtocol } from 'vue-cli-plugin-electron-builder/lib';
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'; import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer';
import path from 'path'; import path from 'path';
@@ -34,9 +34,10 @@ if (!process.env.IS_TEST) {
function createWindow() { function createWindow() {
// Create the browser window. // Create the browser window.
const size = getWindowSize(1350, 955);
win = new BrowserWindow({ win = new BrowserWindow({
width: 1350, width: size.width,
height: 955, height: size.height,
webPreferences: { webPreferences: {
contextIsolation: false, // To reach node https://github.com/nklayman/vue-cli-plugin-electron-builder/issues/1285 contextIsolation: false, // To reach node https://github.com/nklayman/vue-cli-plugin-electron-builder/issues/1285
// Use pluginOptions.nodeIntegration, leave this alone // Use pluginOptions.nodeIntegration, leave this alone
@@ -143,3 +144,12 @@ function loadUrlWithNodeWorkaround(window: BrowserWindow, url: string) {
window.loadURL(url); window.loadURL(url);
}, 10); }, 10);
} }
function getWindowSize(idealWidth: number, idealHeight: number) {
let { width, height } = screen.getPrimaryDisplay().workAreaSize;
// To ensure not creating a screen bigger than current screen size
// Not using "enableLargerThanScreen" as it's macOS only (see https://www.electronjs.org/docs/api/browser-window)
width = Math.min(width, idealWidth);
height = Math.min(height, idealHeight);
return { width, height };
}