diff --git a/apps/widbt2/metadata.json b/apps/widbt2/metadata.json new file mode 100644 index 000000000..af7728958 --- /dev/null +++ b/apps/widbt2/metadata.json @@ -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"} + ] +} diff --git a/apps/widbt2/widget.js b/apps/widbt2/widget.js new file mode 100644 index 000000000..8b1fc3ede --- /dev/null +++ b/apps/widbt2/widget.js @@ -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); + }; +})(); diff --git a/apps/widbt2/widget.png b/apps/widbt2/widget.png new file mode 100644 index 000000000..1a884a62c Binary files /dev/null and b/apps/widbt2/widget.png differ