From b19dd38dbb218eb1f1237c3d7f1e9a22c7162de0 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Fri, 13 Jan 2023 22:11:09 +0000 Subject: [PATCH] widlockunlock: handle widget tap to lock --- apps/widlockunlock/ChangeLog | 1 + apps/widlockunlock/metadata.json | 4 +-- apps/widlockunlock/widget.js | 45 ++++++++++++++++++++++++++++---- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/apps/widlockunlock/ChangeLog b/apps/widlockunlock/ChangeLog index b4d1ae593..b5efcaa86 100644 --- a/apps/widlockunlock/ChangeLog +++ b/apps/widlockunlock/ChangeLog @@ -1 +1,2 @@ 0.01: First commit +0.02: Add tap-to-lock functionality diff --git a/apps/widlockunlock/metadata.json b/apps/widlockunlock/metadata.json index d701279b9..cc4fa76cd 100644 --- a/apps/widlockunlock/metadata.json +++ b/apps/widlockunlock/metadata.json @@ -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", diff --git a/apps/widlockunlock/widget.js b/apps/widlockunlock/widget.js index 0716a9edf..21461b4a5 100644 --- a/apps/widlockunlock/widget.js +++ b/apps/widlockunlock/widget.js @@ -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); -}};