-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprograms.js
223 lines (177 loc) · 5.76 KB
/
programs.js
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
class ProgramManager {
constructor() {
this.prototypes = {}
this.instances = {}
}
registerPrototype(name, class_prototype) {
this.prototypes[name] = class_prototype
}
hasPrototype(name) {
return (name in this.prototypes)
}
createID() {
// honestly this probably won't collide (16 ^ 10 = ~ 1 trilion)
// if you get a collision you should probably buy a lottery ticket
const alphabet = 'abcdef0123456789'
let str = ''
for (let i = 0; i < 10; i++) {
str += alphabet[Math.floor(Math.random() * alphabet.length)]
}
return str
}
hasInstance(id) {
return (id in this.instances)
}
async createInstance(name) {
// slight hack, but allow web: links to be opened in new tabs
if (name.length > 4 && name.slice(0,4) === 'web:') {
console.log('opening web link')
document.querySelector('#dummy')
.setAttribute('href', name.slice(4))
document.querySelector('#dummy').click()
return
}
if (name === 'nop') {
return
}
// argument handling for general programs
let arg = null;
if (name.indexOf(':') !== -1) {
[name, arg] = name.split(/:(.+)/)
}
console.log('launching ', name, ' with arg ', arg)
if (!this.hasPrototype(name)) {
console.warn(`attempted to open unregistered program ${name}, ignoring...`)
return
}
const id = this.createID()
// this feels real hacky
const handle = this.instances[id] = new (this.prototypes[name])(id)
const [wminfo, body] = await handle.createWindow(arg)
window.wm.openWindow(wminfo, body, win => {
win.dataset._pm_id = id
// make sure the thing knows about the window
handle.setWindowHandle(win)
// dispatch handler
handle.onAttach(win)
})
}
getInstance(id) {
if (!this.hasInstance(id)) {
console.warn(`attempted to get unknown instance ${id}, ignoring...`)
return
}
return this.instances[id]
}
closeInstance(id) {
if (!this.hasInstance(id)) {
console.warn(`attempted to close unknown instance ${id}, ignoring...`)
return
}
this.instances[id].onClose()
delete this.instances[id]
}
}
// base class for further programs
class Program {
constructor(id) {
this._pm_id = id
}
async createWindow(arg) {
let body = `
<p> Default window </p>
`
let wminfo = {
title: 'Hello!',
name: 'Default Program',
icon: 'img/desktop/EXE.png'
}
return [wminfo, body]
}
initializeMenuHandler() {
const menus = this
.getBodyHandle()
.querySelectorAll('.menu-bar__item')
// add function for closing stuff
const closeMenu = () => {
document.querySelectorAll('.menu-bar__submenu-bg')
.forEach(el => {
el.classList.remove('menu-bar__submenu-bg--active')
})
document.querySelectorAll('.menu-bar__submenu')
.forEach(el => {
el.classList.remove('menu-bar__submenu--active')
})
document.querySelectorAll('.menu-bar__item')
.forEach(el => el.classList.remove('menu-bar__item--active'))
}
let bg = this.getBodyHandle()
.parentElement
.querySelector('.menu-bar__submenu-bg')
if (bg === null) {
// create the click catcher bg
bg = document.createElement('div')
bg.classList.add('menu-bar__submenu-bg')
bg.addEventListener('click', () => {
closeMenu()
})
// put the background down
this
.getBodyHandle()
.parentElement
.appendChild(bg)
}
for (let menu of menus) {
let submenu = menu.querySelector('.menu-bar__submenu')
if (submenu !== null) {
menu.addEventListener('click', e => {
if (e.target !== menu) {return}
bg.classList.add('menu-bar__submenu-bg--active')
menu.classList.add('menu-bar__item--active')
submenu.classList.add('menu-bar__submenu--active')
})
// add event listeners for item clicking
submenu
.querySelectorAll('[data-name]')
.forEach(el => {
el.addEventListener('click', e => {
this.handleMenuClick(e.target.dataset.name)
closeMenu()
})
})
}
}
}
handleMenuClick(name) {}
setWindowHandle(win) {
this.handle = win
}
getBodyHandle() {
if (this.handle === null) {
return null
}
return this.handle.querySelector('.window-body')
}
setWindowTitle(title) {
this.handle
.querySelector('.title-bar-text')
.innerText = title
}
setWindowIcon(icon) {
this.handle.dataset.icon = icon
window.wm.redrawTaskbarMain()
}
setWindowName(name) {
this.handle.dataset.name = name
window.wm.redrawTaskbarMain()
}
onAttach() {}
onClose() {}
onResize() {}
onResizeEnd() {}
closeWindow() {
// this is a hack...
this.handle.querySelector('button[aria-label="Close"]').click()
}
}
window.pm = new ProgramManager()