-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHintLayout.js
More file actions
140 lines (127 loc) · 3.8 KB
/
Copy pathHintLayout.js
File metadata and controls
140 lines (127 loc) · 3.8 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
function overlaps(a, b) {
return !(a.x + a.width <= b.x || b.x + b.width <= a.x || a.y + a.height <= b.y || b.y + b.height <= a.y)
}
function clamp(value, min, max) {
if (max < min) return min
if (value < min) return min
if (value > max) return max
return value
}
function slotPoint(slot, win, badge, gap, inset) {
var x = win.x
var y = win.y
var w = win.width
var h = win.height
var bw = badge.width
var bh = badge.height
var left = x + inset
var right = x + w - inset - bw
var top = y + inset
var bottom = y + h - inset - bh
var midX = x + (w - bw) / 2
var midY = y + (h - bh) / 2
var step = bh + gap
switch (String(slot || "")) {
case "caption-close":
return { x: right, y: top }
case "caption-max":
return { x: right, y: top + step }
case "caption-float":
return { x: right, y: top + 2 * step }
case "caption-min":
return { x: right, y: top + 3 * step }
case "caption-pin":
return { x: left, y: top }
case "titlebar":
return { x: midX, y: top }
case "edge-left":
return { x: left, y: midY }
case "edge-right":
return { x: right, y: midY }
case "edge-top":
return { x: midX, y: top + step }
case "edge-bottom":
return { x: midX, y: bottom }
case "outside-left":
return { x: left, y: midY - step }
case "outside-right":
return { x: right, y: midY - step }
case "outside-top":
return { x: midX, y: top + 2 * step }
case "outside-bottom":
return { x: midX, y: bottom - step }
case "outside-left-alt":
return { x: left, y: midY + step }
case "outside-right-alt":
return { x: right, y: midY + step }
case "outside-top-alt":
return { x: midX, y: top + 3 * step }
case "outside-bottom-alt":
return { x: midX, y: bottom - 2 * step }
case "center":
return { x: midX, y: midY }
default:
return null
}
}
function layout(win, hints, options) {
var opts = options || {}
var badge = {
width: Number(opts.badgeWidth) || 92,
height: Number(opts.badgeHeight) || 28
}
var gap = opts.gap !== undefined ? Number(opts.gap) : 6
var inset = opts.inset !== undefined ? Number(opts.inset) : 6
var placed = []
if (!win || !(win.width > 0) || !(win.height > 0)) return placed
var screenW = Number(win.screenWidth)
var screenH = Number(win.screenHeight)
if (!isFinite(screenW) || screenW <= 0) screenW = win.x + win.width
if (!isFinite(screenH) || screenH <= 0) screenH = win.y + win.height
var minX = Math.max(0, win.x)
var minY = Math.max(0, win.y)
var maxX = Math.min(screenW, win.x + win.width) - badge.width
var maxY = Math.min(screenH, win.y + win.height) - badge.height
var sorted = (hints || []).slice()
sorted.sort(function (a, b) { return (b.priority || 0) - (a.priority || 0) })
for (var i = 0; i < sorted.length; i++) {
var hint = sorted[i]
var point = slotPoint(hint.slot, win, badge, gap, inset)
if (!point) continue
var rect = {
x: clamp(Math.round(point.x), minX, Math.max(minX, maxX)),
y: clamp(Math.round(point.y), minY, Math.max(minY, maxY)),
width: badge.width,
height: badge.height
}
var collided = false
for (var j = 0; j < placed.length; j++) {
if (overlaps(rect, placed[j])) {
collided = true
break
}
}
if (collided) continue
placed.push({
id: hint.id,
slot: hint.slot,
chord: hint.chord,
chordLabel: hint.chordLabel || hint.chord,
label: hint.label || "",
priority: hint.priority || 0,
x: rect.x,
y: rect.y,
width: rect.width,
height: rect.height
})
}
return placed
}
if (typeof module !== "undefined" && module.exports) {
module.exports = {
overlaps: overlaps,
clamp: clamp,
slotPoint: slotPoint,
layout: layout
}
}