-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappload.js
More file actions
243 lines (185 loc) · 6.42 KB
/
appload.js
File metadata and controls
243 lines (185 loc) · 6.42 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
243
/*
NanoPlay OS
Copyright (C) Subnodal Technologies. All Rights Reserved.
https://nanoplay.subnodal.com
Licenced by the Subnodal Open-Source Licence, which can be found at LICENCE.md.
*/
/*
appload.js is designed to expose the NanoPlay API so that third-party and
user-made apps can interact with NanoPlay OS. Scripts stored in the apps/
directory of NanoPlay OS are internal apps and do not make use of the
NanoPlay API.
*/
var uiScreen = require("ui").Screen;
function debounceButtons() {
// Wait for all buttons to be released
while (BTN1.read() || BTN2.read() || BTN3.read() || BTN4.read()) {}
// Then poll them all to pop their status buffers
require("ui").buttons.tl.poll();
require("ui").buttons.tr.poll();
require("ui").buttons.bl.poll();
require("ui").buttons.br.poll();
}
class ErrorScreen extends uiScreen {
constructor(error) {
super();
this.showStatusBar = false;
this.errorMessage = error.toString();
this.scrollPosition = 0;
}
start() {
print("<error type=\"app\">" + this.errorMessage + "</error>");
g.setBgColor(0);
g.setColor(1);
}
tick(event) {
if (event.buttons.tl == 1) {
this.close();
}
var unwrappedText = this.errorMessage.split(" ");
var wrappedText = [""];
while (unwrappedText.length > 0) {
var nextUnwrap = unwrappedText.shift();
if (nextUnwrap.length > 14) {
nextUnwrap += " ";
while (nextUnwrap.length > 0) {
wrappedText[wrappedText.length - 1] += nextUnwrap[0];
if (wrappedText[wrappedText.length - 1].length >= 14) {
wrappedText.push("");
}
nextUnwrap = nextUnwrap.substring(1);
}
continue;
}
if (nextUnwrap.length > 14 - wrappedText[wrappedText.length - 1].length - 1) {
wrappedText.push("");
}
wrappedText[wrappedText.length - 1] += nextUnwrap + " ";
}
if (event.buttons.bl == 1) {
this.scrollPosition -= 1;
} else if (event.buttons.bl == 2) {
this.scrollPosition -= 4;
}
if (event.buttons.br == 1) {
this.scrollPosition += 1;
} else if (event.buttons.br == 2) {
this.scrollPosition += 4;
}
if (this.scrollPosition < 0) {
this.scrollPosition = wrappedText.length - 4;
}
if (this.scrollPosition > wrappedText.length - 4) {
this.scrollPosition = 0;
}
for (var i = 0; i < 4; i++) {
if (this.scrollPosition + i < wrappedText.length) {
require("display").drawCharsFromCell(wrappedText[this.scrollPosition + i], 1, i);
}
}
require("ui").drawButtonIcons("back", " ", "up", "down");
require("ui").drawStatusBar({
pageUp: this.scrollPosition,
pageDown: this.scrollPosition + 4 < wrappedText.length
});
}
}
class AppScreen extends uiScreen {
constructor(programFile) {
super();
this.showStatusBar = false;
this.idleRefreshInterval = 100;
this.error = null;
this.errorShown = false;
var exposedGlobals = {
Math: Math,
String: String,
Object: Object,
Date: Date
};
Object.assign(exposedGlobals, require("api"));
this.programGlobal = {};
var _communicators = require("api")._communicators;
try {
this.programGlobal = (function() {
eval(_communicators);
var __objects = eval(require("Storage").read(programFile));
return {
start: __objects[0] || function() {},
loop: __objects[1] || function() {},
_status: __objects[2] || function() {return {_shouldClose: false, _showStatusBar: false};}
};
}).call(exposedGlobals);
} catch (e) {
this.error = e;
}
}
start() {
if (this.programGlobal["start"] != undefined) {
this.programGlobal.start();
}
}
tick(event) {
if (BTN1.read() && BTN2.read() && BTN3.read() && BTN4.read()) {
debounceButtons();
this.close();
return;
}
if (this.errorShown) {
this.close();
return;
}
if (this.error != null) {
this.open(new ErrorScreen(this.error));
this.errorShown = true;
return;
}
this.showStatusBar = this.programGlobal._status()._showStatusBar;
try {
this.programGlobal.loop(event);
} catch (e) {
this.error = e;
}
if (this.programGlobal._status()._shouldClose) {
debounceButtons();
this.close();
}
}
close() {
Modules.removeCached("api");
super.close();
}
}
exports.getApps = function() {
return require("Storage").list().filter((a) => a.endsWith(".np"));
};
exports.getHomeScreenIcons = function(homeScreen) {
var apps = exports.getApps();
var iconData = [];
for (var i = 0; i < apps.length; i++) {
(function(i) {
var manifest = require("Storage").readJSON(apps[i].split(".")[0] + ".npm", true) || {};
if (typeof(manifest["name"]) == "string") {
var nonL10nName = manifest["name"];
manifest["name"] = {};
manifest["name"][require("l10n").getLocaleCode()] = nonL10nName;
} else if (typeof(manifest["name"]) != "object") {
apps[i].split(".")[0];
}
iconData.push({
text: manifest["name"][require("l10n").getLocaleCode()] || apps[i].split(".")[0],
icon: typeof(manifest["icon"]) == "string" ? {width: 44, height: 17, buffer: atob(manifest["icon"])} : null,
action: function() {
try {
homeScreen.open(new AppScreen(apps[i]));
g.setBgColor(0);
g.setColor(1);
} catch (e) {
print(e);
}
}
});
})(i);
}
return iconData;
};