Created
June 24, 2020 16:45
-
-
Save narze/f1636a8d8ee885cee76fe886c76745a9 to your computer and use it in GitHub Desktop.
Trolling Window Manager (Proof of concept) with Phoenix.js
This file contains 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
Event.on("mouseDidMove", (point) => { | |
// Phoenix.log(point.x, point.y) | |
const space = Space.active() | |
const windows = space.windows({ visible: true }) | |
// TODO: Find nearest window which does not enclose the point | |
const distances = windows.map((w) => { | |
const f = w.frame() | |
// Phoenix.log(w.app().name(), f.x, f.y, f.width, f.height) | |
const centerPoint = { | |
x: f.x + f.width / 2, | |
y: f.y + f.height / 2, | |
} | |
const distance = Math.sqrt( | |
Math.abs(point.x - centerPoint.x) ** 2 + | |
Math.abs(point.y - centerPoint.y) ** 2 | |
) | |
return [w, distance] | |
}) | |
if (distances.length) { | |
const sortedDistances = distances.sort((a, b) => a[1] - b[1]) | |
Phoenix.log(sortedDistances.map(a => a[1])) | |
const nearestWindow = sortedDistances[0][0] | |
Phoenix.log(nearestWindow.app().name()) | |
const focusedWindow = Window.focused() | |
const windowOriginX = focusedWindow.frame().x + focusedWindow.frame().width / 2 | |
const windowOriginY = focusedWindow.frame().y + focusedWindow.frame().height / 2 | |
const mouseIsOnTheRight = point.x > windowOriginX | |
const mouseIsBelow = point.y > windowOriginY | |
focusedWindow.setTopLeft({ | |
x: focusedWindow.frame().x + (Math.random() * 30) * (mouseIsOnTheRight ? -1 : 1), | |
y: focusedWindow.frame().y + (Math.random() * 30) * (mouseIsBelow ? -1 : 1), | |
}) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment