Skip to content

Commit 88e4625

Browse files
authored
Merge pull request espruino#3637 from thyttan/setui-proposal-preview
setuichange: Bangle.js 1 support
2 parents ebd2d5b + a736c66 commit 88e4625

File tree

4 files changed

+125
-7
lines changed

4 files changed

+125
-7
lines changed

apps/setuichange/ChangeLog

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
0.01: New App!
2-
0.02: Fix case where we tried to push to Bangle.btnWatches but it wasn't
3-
defined.
4-
0.03: Throw exception if trying to add custom drag handler on mode updown and
5-
leftright.
2+
0.02: Fix case where we tried to push to Bangle.btnWatches but it wasn't defined.
3+
0.03: Throw exception if trying to add custom drag handler on mode updown and leftright.
4+
0.04: Bangle.js 1 support. No change to Bangle.js 2.

apps/setuichange/boot-b1.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
Bangle.setUI = (function(mode, cb) {
2+
var options = {};
3+
if ("object"==typeof mode) {
4+
options = mode;
5+
mode = options.mode;
6+
if (!mode) throw new Error("Missing mode in setUI({...})");
7+
}
8+
var redraw = true;
9+
if (global.WIDGETS && WIDGETS.back) {
10+
redraw = false;
11+
WIDGETS.back.remove(mode && options.back);
12+
}
13+
if (Bangle.btnWatches) {
14+
Bangle.btnWatches.forEach(clearWatch);
15+
delete Bangle.btnWatches;
16+
}
17+
if (Bangle.swipeHandler) {
18+
Bangle.removeListener("swipe", Bangle.swipeHandler);
19+
delete Bangle.swipeHandler;
20+
}
21+
if (Bangle.touchHandler) {
22+
Bangle.removeListener("touch", Bangle.touchHandler);
23+
delete Bangle.touchHandler;
24+
}
25+
delete Bangle.uiRedraw;
26+
delete Bangle.CLOCK;
27+
if (Bangle.uiRemove) {
28+
let r = Bangle.uiRemove;
29+
delete Bangle.uiRemove; // stop recursion if setUI is called inside uiRemove
30+
r();
31+
}
32+
g.reset();// reset graphics state, just in case
33+
if (!mode) return;
34+
if (mode=="updown") {
35+
Bangle.btnWatches = [
36+
setWatch(function() { cb(-1); }, BTN1, {repeat:1,edge:"rising"}),
37+
setWatch(function() { cb(1); }, BTN3, {repeat:1,edge:"rising"}),
38+
setWatch(function() { cb(); }, BTN2, {repeat:1,edge:"rising"})
39+
];
40+
} else if (mode=="leftright") {
41+
Bangle.btnWatches = [
42+
setWatch(function() { cb(-1); }, BTN1, {repeat:1,edge:"rising"}),
43+
setWatch(function() { cb(1); }, BTN3, {repeat:1,edge:"rising"}),
44+
setWatch(function() { cb(); }, BTN2, {repeat:1,edge:"rising"})
45+
];
46+
Bangle.swipeHandler = d => {cb(d);};
47+
Bangle.on("swipe", Bangle.swipeHandler);
48+
Bangle.touchHandler = d => {cb();};
49+
Bangle.on("touch", Bangle.touchHandler);
50+
} else if (mode=="clock") {
51+
Bangle.CLOCK=1;
52+
Bangle.btnWatches = [
53+
setWatch(Bangle.showLauncher, BTN2, {repeat:1,edge:"rising"})
54+
];
55+
} else if (mode=="clockupdown") {
56+
Bangle.CLOCK=1;
57+
Bangle.btnWatches = [
58+
setWatch(function() { cb(-1); }, BTN1, {repeat:1,edge:"rising"}),
59+
setWatch(function() { cb(1); }, BTN3, {repeat:1,edge:"rising"}),
60+
setWatch(Bangle.showLauncher, BTN2, {repeat:1,edge:"rising"})
61+
];
62+
} else if (mode=="custom") {
63+
if (options.clock) {
64+
Bangle.btnWatches = [
65+
setWatch(Bangle.showLauncher, BTN2, {repeat:1,edge:"rising"})
66+
];
67+
}
68+
} else
69+
throw new Error("Unknown UI mode "+E.toJS(mode));
70+
if (options.clock) Bangle.CLOCK=1;
71+
if (options.touch) {
72+
Bangle.touchHandler = options.touch;
73+
Bangle.on("touch", Bangle.touchHandler);
74+
}
75+
if (options.swipe) {
76+
Bangle.swipeHandler = options.swipe;
77+
Bangle.on("swipe", Bangle.swipeHandler);
78+
}
79+
if ((options.btn || options.btnRelease) && !Bangle.btnWatches) Bangle.btnWatches = [];
80+
if (options.btn) Bangle.btnWatches.push(
81+
setWatch(function() { options.btn(1); }, BTN1, {repeat:1,edge:"rising"}),
82+
setWatch(function() { options.btn(2); }, BTN2, {repeat:1,edge:"rising"}),
83+
setWatch(function() { options.btn(3); }, BTN3, {repeat:1,edge:"rising"})
84+
);
85+
if (options.btnRelease) Bangle.btnWatches.push(
86+
setWatch(function() { options.btn(1); }, BTN1, {repeat:1,edge:"falling"}),
87+
setWatch(function() { options.btn(2); }, BTN2, {repeat:1,edge:"falling"}),
88+
setWatch(function() { options.btn(3); }, BTN3, {repeat:1,edge:"falling"})
89+
);
90+
if (options.remove) // handler for removing the UI (intervals/etc)
91+
Bangle.uiRemove = options.remove;
92+
if (options.redraw) // handler for redrawing the UI
93+
Bangle.uiRedraw = options.redraw;
94+
if (options.back) {
95+
var touchHandler = (z) => {
96+
if (z==1) options.back();
97+
};
98+
Bangle.on("touch", touchHandler);
99+
var btnWatch;
100+
if (Bangle.btnWatches===undefined) // only add back button handler if there's no existing watch on BTN1
101+
btnWatch = setWatch(function() {
102+
btnWatch = undefined;
103+
options.back();
104+
}, BTN3, {edge:"rising"});
105+
WIDGETS = Object.assign({back:{
106+
area:"tl", width:24,
107+
draw:e=>g.reset().setColor("#f00").drawImage(atob("GBiBAAAYAAH/gAf/4A//8B//+D///D///H/P/n+H/n8P/n4f/vwAP/wAP34f/n8P/n+H/n/P/j///D///B//+A//8Af/4AH/gAAYAA=="),e.x,e.y),
108+
remove:(noclear)=>{
109+
if (btnWatch) clearWatch(btnWatch);
110+
Bangle.removeListener("touch", touchHandler);
111+
if (!noclear) g.reset().clearRect({x:WIDGETS.back.x, y:WIDGETS.back.y, w:24,h:24});
112+
delete WIDGETS.back;
113+
if (!noclear) Bangle.drawWidgets();
114+
}
115+
}},global.WIDGETS);
116+
if (redraw) Bangle.drawWidgets();
117+
}
118+
})

apps/setuichange/metadata.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{ "id": "setuichange",
22
"name": "SetUI Proposals preview",
3-
"version":"0.03",
3+
"version":"0.04",
44
"description": "Try out potential future changes to `Bangle.setUI`. Makes hardware button interaction snappier. Makes it possible to set custom event handlers on any type/mode, not just `\"custom\"`. Please provide feedback - see `Read more...` below.",
55
"icon": "app.png",
66
"tags": "",
77
"type": "bootloader",
8-
"supports" : ["BANGLEJS2"],
8+
"supports" : ["BANGLEJS","BANGLEJS2"],
99
"readme": "README.md",
1010
"storage": [
11-
{"name":"setuichange.0.boot.js","url":"boot.js"}
11+
{"name":"setuichange.0.boot.js","url":"boot-b1.js", "supports": ["BANGLEJS"]},
12+
{"name":"setuichange.0.boot.js","url":"boot-b2.js", "supports": ["BANGLEJS2"]}
1213
]
1314
}

0 commit comments

Comments
 (0)