New widget: bt2

master
Rob Pilling 2022-12-29 21:59:23 +00:00
parent d2d41e1404
commit 326afaf72e
3 changed files with 79 additions and 0 deletions

14
apps/widbt2/metadata.json Normal file
View File

@ -0,0 +1,14 @@
{
"id": "widbt2",
"name": "Bluetooth Widget 2",
"version": "0.01",
"description": "If active, shows a white bluetooth icon, if connected, a blue one",
"icon": "widget.png",
"type": "widget",
"tags": "widget,bluetooth,clkinfo",
"provides_widgets" : ["bluetooth"],
"supports": ["BANGLEJS","BANGLEJS2"],
"storage": [
{"name":"widbt2.wid.js","url":"widget.js"}
]
}

65
apps/widbt2/widget.js Normal file
View File

@ -0,0 +1,65 @@
(function(){
"ram";
// 0: asleep, 1: active, 2: connected
let state = NRF.getSecurityStatus().connected
? 2
: 0; // could be active, assuming not here
const width = () => state > 0 ? 15 : 0;
const update = (newState) => {
state = newState;
WIDGETS.bluetooth.width = width();
setTimeout(Bangle.drawWidgets, 50); // no need for .bind()
};
// { {key: State]: { boolean: string } }
// ^ dark theme
const colours = {
1: { // active: white
false: "#fff",
true: "#fff",
},
2: { // connected: blue
false: "#0ff",
true: "#00f",
},
};
WIDGETS.bluetooth = {
area: "tl",
sortorder: -1,
draw: function() {
if (state == 0)
return;
g.reset();
g.setColor(colours[state][g.theme.dark]);
g.drawImage(
atob("CxQBBgDgFgJgR4jZMawfAcA4D4NYybEYIwTAsBwDAA=="),
this.x + 2,
this.y + 2
);
},
width: width(),
};
NRF.on("connect", update.bind(null, 2));
NRF.on("disconnect", update.bind(null, 1));
let origWake = NRF.wake;
let origSleep = NRF.sleep;
NRF.wake = function() {
update(1);
return origWake.apply(this, arguments);
};
NRF.sleep = function() {
update(0);
return origSleep.apply(this, arguments);
};
})();

BIN
apps/widbt2/widget.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB