-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdesktop.js
More file actions
242 lines (184 loc) · 7.16 KB
/
desktop.js
File metadata and controls
242 lines (184 loc) · 7.16 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* ICON MANAGEMENT */
class DesktopManager {
constructor() {
window.addEventListener('load', this.resizeSelectionCanvas.bind(this))
window.addEventListener('resize', this.resizeSelectionCanvas.bind(this))
window.addEventListener('orientationchange', this.resizeSelectionCanvas.bind(this))
this.desktop = document.querySelector('.desktop')
this.desktop_container = document.querySelector('.desktop-container')
// attach icon behaviors
this.desktop
.querySelectorAll('.desktop-icon')
.forEach(icon => {
this.attachIconSelection(icon)
})
document.querySelector('.desktop')
.addEventListener('click', this.handleClick.bind(this))
this.canvas = document.querySelector('canvas#selections')
this.sel_ctx = this.canvas.getContext('2d')
this.drawing_selection = false
this.skip_click = false
this.resizeSelectionCanvas()
// attach selection events
window.mm.addMouseupListener(() => this.handleSelectionEnd())
if (isMobileBrowser()) {
this.desktop_container
.addEventListener('touchstart', this.handleSelectionStart.bind(this))
this.desktop_container
.addEventListener('touchend', this.handleSelectionEnd.bind(this))
} else {
this.desktop_container
.addEventListener('pointerdown', this.handleSelectionStart.bind(this))
this.desktop_container
.addEventListener('pointerup', this.handleSelectionEnd.bind(this))
}
// taskbar quick
document.querySelectorAll('.task-bar__quick[data-launch]')
.forEach(el => {
el.addEventListener('click', () => window.pm.createInstance(el.dataset.launch))
})
}
attachIconSelection(icon) {
icon.addEventListener('click', () => {
document.querySelector('.desktop')
.querySelectorAll('.desktop-icon[data-active="true"]')
.forEach(el => el.dataset.active = false)
icon.dataset.active = true
})
const openProgram = e => {
e.preventDefault()
if ('launch' in icon.dataset) {
window.pm.createInstance(icon.dataset.launch)
} else {
window.wm.openWindow({title: 'Sorry!'}, `
<p>It doesn't work.</p>
`)
}
}
icon.addEventListener('dblclick', openProgram)
if (isMobileBrowser()) {
// on mobile, fire this on a single click
icon.addEventListener('click', openProgram)
}
}
clearIconSelections() {
document.querySelector('.desktop')
.querySelectorAll('.desktop-icon[data-active="true"]')
.forEach(el => el.dataset.active = false)
}
handleClick(e) {
// ensure that we're not firing a click after a selection event
if (this.skip_click === true) {
this.skip_click = false
return
}
let assertNotIcon = target => {
if (target === document.body) return true
if (target.classList.contains('desktop-icon')) return false
return assertNotIcon(target.parentNode)
}
if (!assertNotIcon(e.target)) return
this.clearIconSelections()
}
checkIconOverlap(icon, x1, y1, x2, y2) {
let rect = icon.getBoundingClientRect()
/*
* Conditions for missing an overlap:
* - top edge of sel box is below bottom edge of icon
* - left edge of sel box is past right edge of icon
* - right edge of sel box is past left edge of icon
* - bottom edge of sel box is above top edge of icon
*/
let i_x1 = rect.x * window.devicePixelRatio
let i_y1 = rect.y * window.devicePixelRatio
let i_x2 = (rect.x + rect.width ) * window.devicePixelRatio
let i_y2 = (rect.y + rect.height) * window.devicePixelRatio
if (!(Math.min(y1,y2) > i_y2 ||
Math.max(y1,y2) < i_y1) &&
!(Math.min(x1,x2) > i_x2 ||
Math.max(x1,x2) < i_x1)
) {
return true
}
return false
}
resizeSelectionCanvas() {
const canvas = document.querySelector('canvas#selections')
const taskbarHeight = document
.querySelector('.task-bar')
.getBoundingClientRect()
.height
// get the naive sizing
let canvas_width = window.innerWidth
let canvas_height = window.innerHeight - taskbarHeight
// first set the canvas dom element size
canvas.style.height = `${canvas_height}px`
canvas.style.width = `${canvas_width}px`
// fix the dpi scaling
let dpi = window.devicePixelRatio
canvas.setAttribute('width', canvas_width * dpi)
canvas.setAttribute('height', canvas_height * dpi)
this.canvas_size = {
width: canvas_width * dpi,
height: canvas_height * dpi
}
}
clearSelectionRect() {
// clear a rectangle the size of the canvas
this.sel_ctx.clearRect(0,0,this.canvas_size.width,this.canvas_size.height)
}
drawSelectionRect(x1, y1, x2, y2) {
// scale dpi to match screen pixels to canvas pixels
let dpi = window.devicePixelRatio
x1 *= dpi
y1 *= dpi
x2 *= dpi
y2 *= dpi
// clear the previous one (if any)
this.clearSelectionRect()
// begin draw
this.sel_ctx.beginPath()
this.sel_ctx.lineWidth = '1px'
this.sel_ctx.strokeStyle = 'red'
this.sel_ctx.setLineDash([1])
// x, y, width, height
this.sel_ctx.rect(x1, y1, x2 - x1, y2 - y1)
this.sel_ctx.stroke()
for (let icon of this.desktop.querySelectorAll('.desktop-icon')) {
if (this.checkIconOverlap(icon, x1, y1, x2, y2)) {
icon.dataset.active = true
} else {
icon.dataset.active = false
}
}
}
handleSelectionStart(e) {
window.mm.updateMousePos(e)
this.drawing_selection = true
this.initial_selection_start = {
x: window.mouse.x,
y: window.mouse.y
}
const updateSelection = () => {
// conditional hoisted to not break clicking/dblclicking
if (this.drawing_selection) {
this.skip_click = true
this.drawSelectionRect(
this.initial_selection_start.x,
this.initial_selection_start.y,
window.mouse.x,
window.mouse.y
)
window.requestAnimationFrame(updateSelection)
}
}
setTimeout(updateSelection, 250)
}
handleSelectionEnd(e) {
this.drawing_selection = false
this.clearSelectionRect()
// hacky, but this keeps the selections from instantly disappearing
setTimeout(() => this.skip_click = false, 100)
}
}
window.dm = new DesktopManager()