-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHyprWindow.js
More file actions
107 lines (98 loc) · 2.95 KB
/
Copy pathHyprWindow.js
File metadata and controls
107 lines (98 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
function parseJson(raw, fallback) {
try {
return JSON.parse(String(raw || ""))
} catch (e) {
return fallback
}
}
function isWindow(win) {
return !!(win && typeof win === "object" && Array.isArray(win.at) && Array.isArray(win.size))
}
function focusedMonitor(monitors) {
if (!Array.isArray(monitors) || monitors.length === 0) return null
for (var i = 0; i < monitors.length; i++) {
if (monitors[i] && monitors[i].focused) return monitors[i]
}
return monitors[0]
}
function monitorForWindow(win, monitors) {
if (!Array.isArray(monitors) || monitors.length === 0) return null
if (win && win.monitor !== undefined && win.monitor !== null) {
for (var i = 0; i < monitors.length; i++) {
if (Number(monitors[i].id) === Number(win.monitor)) return monitors[i]
}
}
return focusedMonitor(monitors)
}
function overlayRect(win, monitors) {
if (!isWindow(win)) return null
var mon = monitorForWindow(win, monitors)
if (!mon) {
return {
x: Number(win.at[0]) || 0,
y: Number(win.at[1]) || 0,
width: Number(win.size[0]) || 0,
height: Number(win.size[1]) || 0,
screenWidth: Number(win.size[0]) || 0,
screenHeight: Number(win.size[1]) || 0,
monitorName: "",
address: String(win.address || ""),
title: String(win.title || win.class || ""),
floating: !!win.floating,
fullscreen: Number(win.fullscreen) || 0
}
}
var scale = Number(mon.scale)
if (!isFinite(scale) || scale <= 0) scale = 1
var mx = Number(mon.x) || 0
var my = Number(mon.y) || 0
var mw = Number(mon.width) || 0
var mh = Number(mon.height) || 0
var logicalW = mw / scale
var logicalH = mh / scale
var x = Number(win.at[0]) - mx
var y = Number(win.at[1]) - my
var w = Number(win.size[0])
var h = Number(win.size[1])
if (!isFinite(x)) x = 0
if (!isFinite(y)) y = 0
if (!isFinite(w)) w = 0
if (!isFinite(h)) h = 0
var overflowLogical = (x + w) > (logicalW + 2) || (y + h) > (logicalH + 2)
var fitsPhysical = mw > 0 && (x + w) <= (mw + 2) && (y + h) <= (mh + 2)
if (overflowLogical && fitsPhysical && scale !== 1) {
x = x / scale
y = y / scale
w = w / scale
h = h / scale
}
return {
x: x,
y: y,
width: w,
height: h,
screenWidth: logicalW,
screenHeight: logicalH,
monitorName: String(mon.name || ""),
address: String(win.address || ""),
title: String(win.title || win.class || ""),
floating: !!win.floating,
fullscreen: Number(win.fullscreen) || 0
}
}
function parseSnapshot(windowRaw, monitorsRaw) {
var win = parseJson(windowRaw, null)
var monitors = parseJson(monitorsRaw, [])
if (!Array.isArray(monitors)) monitors = []
return overlayRect(win, monitors)
}
if (typeof module !== "undefined" && module.exports) {
module.exports = {
parseJson: parseJson,
isWindow: isWindow,
focusedMonitor: focusedMonitor,
monitorForWindow: monitorForWindow,
overlayRect: overlayRect,
parseSnapshot: parseSnapshot
}
}