From 7b953a64bac2a98474ef09f5ed741f2ad688162f Mon Sep 17 00:00:00 2001 From: Ferllings Date: Thu, 28 Sep 2023 10:58:45 +0200 Subject: [PATCH] [feat] (macos) Added shortcut for moving windows between displays --- src/browsers/main.js | 51 ++++++++++++++++++++++++++++++++++++++++---- src/main.js | 2 +- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/browsers/main.js b/src/browsers/main.js index d063e1d..183e899 100644 --- a/src/browsers/main.js +++ b/src/browsers/main.js @@ -1,4 +1,4 @@ -const {app, BrowserWindow, shell, Menu, MenuItem, globalShortcut} = require('electron') +const {app, BrowserWindow, shell, Menu, MenuItem, globalShortcut, screen} = require('electron') const path = require('path') const url = require('url') @@ -22,7 +22,7 @@ module.exports = (dirname, store) => { const x = store.get('position.x') const y = store.get('position.y') const alwaysOnTop = store.get('alwaysOnTop') - + // Create the browser window. mainWindow = new BrowserWindow({ show: false, // Hide the application until the page has loaded @@ -49,7 +49,31 @@ module.exports = (dirname, store) => { // Open the DevTools. // mainWindow.webContents.openDevTools() + if (process.platform === 'darwin') { // MacOS only, native on Window + // Move the window to the previous monitor + globalShortcut.register('Command+Shift+Left', () => { + const pos = mainWindow.getBounds() + const currentScreen = screen.getDisplayNearestPoint(pos) + const allScreens = screen.getAllDisplays() + const monitorIndex = allScreens.findIndex(monitor => monitor.id === currentScreen.id) + + const nextIndex = (monitorIndex==0)?allScreens.length-1:monitorIndex-1 + const nextMonitor = allScreens[nextIndex] + displayOnScreen(mainWindow, nextMonitor) + }); + // Move the window to the next monitor + globalShortcut.register('Command+Shift+Right', () => { + const pos = mainWindow.getBounds() + const currentScreen = screen.getDisplayNearestPoint(pos) + const allScreens = screen.getAllDisplays() + const monitorIndex = allScreens.findIndex(monitor => monitor.id === currentScreen.id) + + const nextIndex = (monitorIndex==allScreens.length-1)?0:monitorIndex+1 + const nextMonitor = allScreens[nextIndex] + displayOnScreen(mainWindow, nextMonitor) + }); + } mainWindow.on('close', function () { if (mainWindow) { @@ -93,14 +117,14 @@ module.exports = (dirname, store) => { ctxMenu.popup(mainWindow, params.x, params.y) }) } - + let getWindow = () => mainWindow let currentHeight = 0 let changeZoom = () => { changeSize(null, currentHeight) } - + let changeSize = (width, height) => { zoomLevel = mainWindow.webContents.getZoomLevel() currentHeight = height @@ -117,6 +141,25 @@ module.exports = (dirname, store) => { mainWindow.setContentSize(width, height) } + /** Move the selected window to the center of the screen + * @param {BrowserWindow} win - The window we want to move + * @param {Display} monitor - The selected monitor + * @see https://www.electronjs.org/docs/latest/api/structures/display + */ + let displayOnScreen = (win, monitor) => { + // Get the screen dimensions + const { x, y, height, width } = monitor.bounds + // Calculate the center of the screen + const centerX = x + width / 2 + const centerY = y + height / 2 + // Get the window width and height + const winSize = win.getSize() + // Position the window on the center of the screen. + const winX = Math.round(centerX - winSize[0] / 2) + const winY = Math.round(centerY - winSize[1] / 2) + win.setPosition(winX, winY) + } + return { init: init, getWindow: getWindow, diff --git a/src/main.js b/src/main.js index 5557dcc..64c3a32 100644 --- a/src/main.js +++ b/src/main.js @@ -260,4 +260,4 @@ function sendEventToAll(event, ...params) { Object.keys(controllers).map(function(key, index) { controllers[key].sendEvent(event, ...params) }) -} \ No newline at end of file +}