From a36493209db5aa05a762ba8d57ca839fd793a48a Mon Sep 17 00:00:00 2001 From: bordun Date: Sun, 26 Jul 2020 20:37:43 +0200 Subject: [PATCH] electron-window: use current monitor's dimensions; on win32, store/retreive last used screen --- src/server/electron-window.js | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/server/electron-window.js b/src/server/electron-window.js index 65c790b6..7e2c56f6 100644 --- a/src/server/electron-window.js +++ b/src/server/electron-window.js @@ -3,17 +3,27 @@ var path = require('path'), shortcut = require('electron-localshortcut'), app = require('./electron-app'), settings = require('./settings'), - theme = require('./theme'), - screenSize = screen.getPrimaryDisplay().size - + theme = require('./theme') module.exports = function(options={}) { - var window + var window, + currentScreen = screen.getDisplayNearestPoint(screen.getCursorScreenPoint()) + + if (process.platform === 'win32' && settings.read('last-used-screen')) { + // on windows, retreive last used screen + var lastScreen = screen.getAllDisplays().find(x=>x.id === settings.read('last-used-screen')) + if (lastScreen) currentScreen = lastScreen + else settings.write('last-used-screen', null) + } + + var screenSize = currentScreen.size, + width = options.width || screenSize.width, + height = options.height || screenSize.height window = new BrowserWindow({ - width: options.width || screenSize.width, - height: options.height || screenSize.height, + width: width, + height: height, title: options.title || settings.infos.appName, icon: path.resolve(__dirname + '/../assets/logo.png'), backgroundColor: options.color || theme.backgroundColor, @@ -27,6 +37,12 @@ module.exports = function(options={}) { show: false, }) + var bounds = currentScreen.bounds, + x = Math.ceil(bounds.x + ((bounds.width - width) / 2)), + y = Math.ceil(bounds.y + ((bounds.height - height) / 2)) + + window.setBounds({x: x, y: y}) + window.once('ready-to-show', ()=>{ window.show() }) @@ -106,6 +122,13 @@ module.exports = function(options={}) { }) } + if (process.platform === 'win32') { + // on windows, store last used screen when closing + window.on('close', function() { + settings.write('last-used-screen', screen.getDisplayMatching(window.getBounds()).id) + }) + } + if (options.shortcuts) { shortcut.register(window,'CmdOrCtrl+R',function(){