/
ui.ts
233 lines (219 loc) · 4.96 KB
/
ui.ts
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
const loc = require('locale');
/**
* Returns a promise that is a page with a title, question or statement and buttons
* @param title
* @param question
* @param buttons
* @param selected
* @param dont
* @return {*}
*/
import { convertStringToPage, getFixedStringLines } from './string';
/**
* Create a selection page
* @param title
* @param question
* @param buttons
* @param selected
* @param dontFormatText
*/
export function createSelectPage(
title: string,
question: string,
buttons: Record<string, any>,
selected?: number,
dontFormatText?: boolean
) {
selected = selected || 0;
if (!dontFormatText) {
question = convertStringToPage(question)[0];
}
return E.showPrompt(question, {
title: title,
buttons: buttons,
selected: selected,
});
}
/**
* Create a menu
* @param title
* @param subtitle
* @param menuItems
* @param backFn
* @param exitFn
*/
export function createMenu(
title: string,
subtitle: string,
menuItems: Record<string, any>,
backFn?: () => void,
exitFn?: () => void
) {
E.showMenu();
const menu = {
'': { title: title },
};
if (subtitle) {
menu[' '] = { value: subtitle };
}
Object.keys(menuItems).forEach((key) => {
menu[key] = menuItems[key];
});
if (backFn) {
menu['< Back'] = backFn;
}
if (exitFn) {
menu['<< Exit'] = exitFn;
}
return E.showMenu(menu);
}
/**
* Create an alert and call then passed callback
* @param title
* @param text
* @param dontFormat
*/
export function createAlert(title: string, text: string, dontFormat?: boolean) {
if (!dontFormat) {
text = convertStringToPage(text)[0];
}
return E.showAlert(text, title);
}
/**
* Show a message for a specified time before resolving, if no time then immediately return
* @param title
* @param text
* @param time
* @param dontFormat
*/
export function createMessage(
title: string,
text: string,
time?: number,
dontFormat?: boolean
) {
time = time || 0;
if (!dontFormat) {
text = convertStringToPage(text)[0];
}
return new Promise((resolve) => {
E.showMessage(text, title);
setTimeout(() => resolve(), time);
});
}
/**
* Creates a prompt where the answers can be scrolled
* @param title
* @param message
* @param buttons
* @param selected
*/
export function createScrollPrompt(
title: string,
message: string,
buttons?: Record<string, any>,
selected?: number
) {
if (!buttons) {
buttons = { Yes: true, No: false };
}
selected = selected || 0;
let buttonKeys = Object.keys(buttons);
const msg = getFixedStringLines(message);
function draw(currentBtn: number) {
g.clear(1); // clear screen
g.reset().setFont('6x8', 2).setFontAlign(0, 0);
const W = g.getWidth();
const H = g.getHeight();
if (title) {
title = loc.translate(title);
g.drawString(title, W / 2, 34);
const w = (g.stringWidth(title) + 16) / 2;
g.fillRect(W / 2 - w, 44, W / 2 + w, 44);
}
const offset = (H - msg.length * 16) / 2;
msg.forEach((line, y) =>
g.drawString(loc.translate(line), W / 2, offset + y * 16)
);
let buttonWidths = 0;
const buttonPadding = 16;
let btn = loc.translate(buttonKeys[currentBtn]);
buttonWidths = buttonPadding + g.stringWidth(btn);
//buttonKeys.forEach(btn => buttonWidths += buttonPadding + g.stringWidth(loc.translate(btn)));
let x = (W - buttonWidths) / 2;
let y = H - 40;
const w = g.stringWidth(btn);
x += (w + buttonPadding) / 2;
const bw = 2 + w / 2;
// Polygon for button
const poly = [
x - bw,
y - 12,
x + bw,
y - 12,
x + bw + 4,
y - 8,
x + bw + 4,
y + 8,
x + bw,
y + 12,
x - bw,
y + 12,
x - bw - 4,
y + 8,
x - bw - 4,
y - 8,
x - bw,
y - 12,
];
g.setColor(0x02f7)
.fillPoly(poly)
.setColor(-1)
.drawPoly(poly)
.drawString(btn, x, y + 1);
g.setColor(!currentBtn ? 0 : -1);
g.fillPoly([120, y - 29, 106, y - 15, 134, y - 15]);
g.setColor(buttonKeys.length - 1 === currentBtn ? 0 : -1);
g.fillPoly([120, y + 12 + 4 + 14, 106, y + 12 + 4, 134, y + 12 + 4]);
g.setColor(-1).flip(); // turn screen on
}
if (Bangle.btnWatches) {
Bangle.btnWatches.forEach(clearWatch);
Bangle.btnWatches = undefined;
}
g.clear(1); // clear screen
Bangle.drawWidgets(); // redraw widgets
draw(selected);
return new Promise((resolve) => {
Bangle.btnWatches = [
setWatch(
() => {
if (selected > 0) {
selected--;
draw(selected);
}
},
BTN1,
{ repeat: 1 }
),
setWatch(
() => {
if (selected < buttonKeys.length - 1) {
selected++;
draw(selected);
}
},
BTN3,
{ repeat: 1 }
),
setWatch(
() => {
g.clear(1);
resolve(buttons[buttonKeys[selected]]);
},
BTN2,
{ repeat: 1 }
),
];
});
}