Created
July 10, 2018 06:17
-
-
Save sean0x72/44a9fdd504b832714c24f899a11c0b42 to your computer and use it in GitHub Desktop.
simulating windows areosnap feature when dragging a frameless window without using a draggable region
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
There is no api for windows areosnap feature that I could find or not way to trigger it with the windows hotkeys. | |
*/ | |
const titleBar = document.getElementById('title-bar'); | |
const { remote, screen } = require('electron'); | |
let activate = false; | |
const win = { | |
offsetX: 0, | |
offsetY: 0, | |
target: null | |
}; | |
titleBar.addEventListener('mousedown', e => { | |
activate = true; | |
win.target = remote.getCurrentWindow(); | |
const [winX, winY] = win.target.getPosition(); | |
const { screenX, screenY } = e; | |
win.offsetX = screenX - winX; | |
win.offsetY = screenY - winY; | |
}); | |
titleBar.addEventListener('dblclick', e => { | |
if (win.target.isMaximized()) win.target.restore(); | |
else win.target.maximize(); | |
}); | |
window.addEventListener('mouseup', e => { | |
if (!activate) return; | |
activate = false; | |
const { screenX, screenY } = e; | |
const currentScreen = screen.getDisplayNearestPoint({ x: screenX, y: screenY }); | |
const { x, y, width, height } = currentScreen.workArea; | |
if (screenY < 3) { | |
win.target.maximize(); | |
} | |
else if (screenY > height - 3) { | |
const h = height / 2 | |
win.target.setSize(width, h); | |
win.target.setPosition(x, h); | |
} else if (screenX < 3) { | |
const w = width / 2 | |
win.target.setSize(w, height); | |
win.target.setPosition(x, y); | |
} else if (screenX > width - 3) { | |
const w = width / 2 | |
win.target.setSize(w, height); | |
win.target.setPosition(w, y); | |
} | |
}) | |
window.addEventListener('mousemove', e => { | |
if (!activate) return; | |
const { screenX, screenY } = e; | |
const moveX = screenX - win.offsetX; | |
const moveY = screenY - win.offsetY; | |
win.target.setPosition(moveX, moveY); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment