Skip to content

Commit f78d449

Browse files
committedFeb 16, 2023
Adding multi display support
1 parent efcfeae commit f78d449

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed
 

‎manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifest_version": 3,
33
"name": "Centralizer",
4-
"version": "1.1",
4+
"version": "1.3",
55
"description": "Move and resize your window to be central in your monitor",
66
"icons": {
77
"16": "images/icon.png",

‎scripts/background.js

+28-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ function centralize() {
44
function(currentWindow) {
55
chrome.system.display.getInfo(function(info) {
66
if (info && info.length > 0) {
7-
const display = info[0];
7+
const display = getClosestDisplayForWindowPosition(info, currentWindow);
88
const displayWidth = display.workArea.width;
99
const displayHeight = display.workArea.height;
1010

1111
const toBeWidth = Math.round((displayWidth / 4) * 3);
12-
const leftPos = Math.round((displayWidth - toBeWidth) / 2);
12+
const leftPos = display.workArea.left + Math.round((displayWidth - toBeWidth) / 2);
1313

1414
const toBeHeight = displayHeight - 20;
1515
const topPos = display.workArea.top + 10;
@@ -29,6 +29,32 @@ function centralize() {
2929
);
3030
}
3131

32+
function getClosestDisplayForWindowPosition(displays, window) {
33+
if (displays.length == 1) {
34+
return displays[0];
35+
}
36+
37+
const windowPosition = { x: window.top, y: window.left };
38+
39+
let points = [];
40+
41+
for (let i = 0; i < displays.length; i++) {
42+
points.push({
43+
x: displays[i].workArea.top,
44+
y: displays[i].workArea.left,
45+
display: displays[i]
46+
});
47+
}
48+
49+
closest = points.reduce((a, b) => distance(a, windowPosition) < distance(b, windowPosition) ? a : b);
50+
51+
return closest.display;
52+
}
53+
54+
function distance(p, point) {
55+
return Math.sqrt(Math.pow(point.x - p.x, 2) + Math.pow(point.y - p.y, 2))
56+
}
57+
3258
chrome.commands.onCommand.addListener((command) => {
3359
if (command === "centralize") {
3460
centralize();

0 commit comments

Comments
 (0)