widlockunlock: handle widget tap to lock

pull/2606/head
Rob Pilling 2023-01-13 22:11:09 +00:00
parent 8bc9c5de25
commit b19dd38dbb
3 changed files with 43 additions and 7 deletions

View File

@ -1 +1,2 @@
0.01: First commit
0.02: Add tap-to-lock functionality

View File

@ -1,8 +1,8 @@
{
"id": "widlockunlock",
"name": "Lock/Unlock Widget",
"version": "0.01",
"description": "On devices with always-on display (Bangle.js 2) this displays lock icon whenever the display is locked, or an unlock icon otherwise",
"version": "0.02",
"description": "On devices with always-on display (Bangle.js 2) this displays lock icon whenever the display is locked, or an unlock icon otherwise. Tap to lock the lcd",
"icon": "widget.png",
"type": "widget",
"tags": "widget,lock",

View File

@ -1,6 +1,41 @@
Bangle.on("lockunlock", function() {
Bangle.drawWidgets();
WIDGETS["lockunlock"] = {
area: "tl",
sortorder: 10,
width: 14,
draw: w => {
g.reset()
.drawImage(
atob(Bangle.isLocked()
? "DBGBAAAA8DnDDCBCBP////////n/n/n//////z/A"
: "DBGBAAAA8BnDDCBABP///8A8A8Y8Y8Y8A8A//z/A"),
w.x + 1,
w.y + 3
);
},
};
Bangle.on("lock", () => Bangle.drawWidgets());
Bangle.on('touch', (_btn, xy) => {
const oversize = 5;
const w = WIDGETS.lockunlock;
const x = xy.x;
const y = xy.y;
if(w.x - oversize <= x && x < w.x + 14 + oversize
&& w.y - oversize <= y && y < w.y + 24 + oversize)
{
Bangle.setLocked(true);
const backlightTimeout = Bangle.getOptions().backlightTimeout; // ms
// seems to be a race/if we don't give the firmware enough time,
// it won't timeout the backlight and we'll restore it in our setTimeout below
Bangle.setOptions({ backlightTimeout: 100 });
setTimeout(() => {
Bangle.setOptions({ backlightTimeout });
}, 300);
}
});
WIDGETS["lockunlock"]={area:"tl",sortorder:10,width:14,draw:function(w) {
g.reset().drawImage(atob(Bangle.isLocked() ? "DBGBAAAA8DnDDCBCBP////////n/n/n//////z/A" : "DBGBAAAA8BnDDCBABP///8A8A8Y8Y8Y8A8A//z/A"), w.x+1, w.y+3);
}};