Skip to content

Commit

Permalink
[feat] (macos) Added shortcut for moving windows between displays
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferllings committed Sep 28, 2023
1 parent 43677c1 commit 7b953a6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
51 changes: 47 additions & 4 deletions src/browsers/main.js
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,4 @@ function sendEventToAll(event, ...params) {
Object.keys(controllers).map(function(key, index) {
controllers[key].sendEvent(event, ...params)
})
}
}

0 comments on commit 7b953a6

Please sign in to comment.