|
| 1 | +{ |
| 2 | + // twistThreshold How much acceleration to register a twist of the watch strap? Can be negative for opposite direction. default = 800 |
| 3 | + // twistMaxY Maximum acceleration in Y to trigger a twist (low Y means watch is facing the right way up). default = -800 |
| 4 | + // twistTimeout How little time (in ms) must a twist take from low->high acceleration? default = 1000 |
| 5 | + let onTwistEmitDrag = ()=>{ |
| 6 | + Bangle.setOptions({twistThreshold:2500, twistMaxY:-800, twistTimeout:400}); |
| 7 | + let isTwistDragging = false; |
| 8 | + let twistHandler = ()=>{ |
| 9 | + if (!isTwistDragging) { |
| 10 | + isTwistDragging = true; |
| 11 | + Bangle.setLocked(false); |
| 12 | + Bangle.setLCDPower(true); |
| 13 | + let i = 25; |
| 14 | + const int = setInterval(() => { |
| 15 | + Bangle.emit("drag", {dy:-3, b:i===0?0:1}) |
| 16 | + i--; |
| 17 | + if (i<0) { |
| 18 | + clearInterval(int); |
| 19 | + isTwistDragging = false; |
| 20 | + } |
| 21 | + }, 10); |
| 22 | + } |
| 23 | + } |
| 24 | + Bangle.on("twist", twistHandler); |
| 25 | + // Give messagegui some extra time to add its remove function to |
| 26 | + // Bangle.uiRemove, then attach msgtwscr remove logic. |
| 27 | + setTimeout( |
| 28 | + ()=>{if (Bangle.uiRemove) { |
| 29 | + let showMessageUIRemove = Bangle.uiRemove; |
| 30 | + Bangle.uiRemove = function () { |
| 31 | + Bangle.removeListener("twist", twistHandler) |
| 32 | + showMessageUIRemove(); |
| 33 | + // Reset twist drag logic if we go to next message. |
| 34 | + attachAfterTimeout(); |
| 35 | + } |
| 36 | + }}, |
| 37 | + 800) |
| 38 | + } |
| 39 | + |
| 40 | + // If doing regular loads, not Bangle.load, this is used: |
| 41 | + if (global.__FILE__=="messagegui.new.js") { |
| 42 | + onTwistEmitDrag(); |
| 43 | + } |
| 44 | + |
| 45 | + let attachAfterTimeout = ()=>{ |
| 46 | + setTimeout(()=>{ |
| 47 | + if (global.__FILE__=="messagegui.new.js") { |
| 48 | + onTwistEmitDrag(); |
| 49 | + } |
| 50 | + },700) |
| 51 | + // It feels like there's a more elegant solution than checking the filename |
| 52 | + // after 700 milliseconds. But this at least seems to work w/o sometimes |
| 53 | + // activating when it shouldn't. |
| 54 | + // Maybe we could add events for when fast load and/or Bangle.uiRemove occurs? |
| 55 | + // Then that could be used similarly to boot code and/or the `kill` event. |
| 56 | + } |
| 57 | + |
| 58 | + // If Fastload Utils is installed this is used: |
| 59 | + Bangle.on("message", (_, msg)=>{if (Bangle.CLOCK && msg.new) { |
| 60 | + attachAfterTimeout(); |
| 61 | + }}); |
| 62 | + |
| 63 | +} |
0 commit comments