From 584b24ebfe8ca50808326e71e3fd82b138b52e3c Mon Sep 17 00:00:00 2001 From: Jonas Schwabe Date: Sun, 9 Apr 2017 13:15:51 +0200 Subject: [PATCH] Add display-metrics-changed handler to place window on screen on display matrics change --- src/main.js | 49 ++++++++++++++++++++++++++++++++++++++++++ src/main/mainWindow.js | 32 ++------------------------- 2 files changed, 51 insertions(+), 30 deletions(-) diff --git a/src/main.js b/src/main.js index a1ea01fb..d0f4a644 100644 --- a/src/main.js +++ b/src/main.js @@ -213,6 +213,55 @@ app.on('window-all-closed', () => { } }); +function getValidWindowPosition(state, screen) { + // Check if the previous position is out of the viewable area + // (e.g. because the screen has been plugged off) + const displays = screen.getAllDisplays(); + let minX = 0; + let maxX = 0; + let minY = 0; + let maxY = 0; + for (let i = 0; i < displays.length; i++) { + const display = displays[i]; + maxX = Math.max(maxX, display.bounds.x + display.bounds.width); + maxY = Math.max(maxY, display.bounds.y + display.bounds.height); + minX = Math.min(minX, display.bounds.x); + minY = Math.min(minY, display.bounds.y); + } + + if (state.x > maxX || state.y > maxY || state.x < minX || state.y < minY) { + Reflect.deleteProperty(state, 'x'); + Reflect.deleteProperty(state, 'y'); + Reflect.deleteProperty(state, 'width'); + Reflect.deleteProperty(state, 'height'); + } + + return state; +} + +function handleScreenResize(screen, browserWindow) { + function handle() { + const position = browserWindow.getPosition(); + const size = browserWindow.getSize(); + const validPosition = getValidWindowPosition({ + x: position[0], + y: position[1], + width: size[0], + height: size[1] + }, screen); + browserWindow.setPosition(validPosition.x || 0, validPosition.y || 0); + } + + browserWindow.on('restore', handle); + handle(); +} + +app.on('browser-window-created', (e, newWindow) => { + // Screen cannot be required before app is ready + const {screen} = require('electron'); // eslint-disable-line global-require + handleScreenResize(screen, newWindow); +}); + // For OSX, show hidden mainWindow when clicking dock icon. app.on('activate', () => { mainWindow.show(); diff --git a/src/main/mainWindow.js b/src/main/mainWindow.js index 5b0dd42d..83175758 100644 --- a/src/main/mainWindow.js +++ b/src/main/mainWindow.js @@ -14,35 +14,6 @@ function saveWindowState(file, window) { } } -function getValidWindowPosition(state) { - // Screen cannot be required before app is ready - const {screen} = require('electron'); // eslint-disable-line global-require - - // Check if the previous position is out of the viewable area - // (e.g. because the screen has been plugged off) - const displays = screen.getAllDisplays(); - let minX = 0; - let maxX = 0; - let minY = 0; - let maxY = 0; - for (let i = 0; i < displays.length; i++) { - const display = displays[i]; - maxX = Math.max(maxX, display.bounds.x + display.bounds.width); - maxY = Math.max(maxY, display.bounds.y + display.bounds.height); - minX = Math.min(minX, display.bounds.x); - minY = Math.min(minY, display.bounds.y); - } - - if (state.x > maxX || state.y > maxY || state.x < minX || state.y < minY) { - Reflect.deleteProperty(state, 'x'); - Reflect.deleteProperty(state, 'y'); - Reflect.deleteProperty(state, 'width'); - Reflect.deleteProperty(state, 'height'); - } - - return state; -} - function createMainWindow(config, options) { const defaultWindowWidth = 1000; const defaultWindowHeight = 700; @@ -53,11 +24,12 @@ function createMainWindow(config, options) { const boundsInfoPath = path.join(app.getPath('userData'), 'bounds-info.json'); var windowOptions; try { - windowOptions = getValidWindowPosition(JSON.parse(fs.readFileSync(boundsInfoPath, 'utf-8'))); + windowOptions = JSON.parse(fs.readFileSync(boundsInfoPath, 'utf-8')); } catch (e) { // Follow Electron's defaults, except for window dimensions which targets 1024x768 screen resolution. windowOptions = {width: defaultWindowWidth, height: defaultWindowHeight}; } + if (process.platform === 'linux') { windowOptions.icon = options.linuxAppIcon; }