mirror of https://github.com/espruino/BangleApps
imageclock - Initial implementation of remove UI
parent
a818496f45
commit
8b2fac1a71
|
@ -1,3 +1,7 @@
|
|||
let unlockedDrawInterval = [];
|
||||
let lockedDrawInterval = [];
|
||||
|
||||
{
|
||||
let watchface = require("Storage").readJSON("imageclock.face.json");
|
||||
let watchfaceResources = require("Storage").readJSON("imageclock.resources.json");
|
||||
let precompiledJs = eval(require("Storage").read("imageclock.draw.js"));
|
||||
|
@ -38,7 +42,7 @@ let palette = new Uint16Array([
|
|||
0xffff, //white
|
||||
0xffff, //white
|
||||
0xffff, //white
|
||||
])
|
||||
]);
|
||||
|
||||
let p0 = g;
|
||||
let p1;
|
||||
|
@ -75,12 +79,21 @@ if (settings.perflog){
|
|||
};
|
||||
}
|
||||
|
||||
let delayTimeouts = [];
|
||||
|
||||
function delay(t) {
|
||||
return new Promise(function (resolve) {
|
||||
setTimeout(resolve, t);
|
||||
delayTimeouts.push(setTimeout(resolve, t));
|
||||
});
|
||||
}
|
||||
|
||||
function cleanupDelays(){
|
||||
for (let t of delayTimeouts){
|
||||
clearTimeout(t);
|
||||
}
|
||||
delayTimeouts = [];
|
||||
}
|
||||
|
||||
function prepareImg(resource){
|
||||
startPerfLog("prepareImg");
|
||||
//print("prepareImg: ", resource);
|
||||
|
@ -553,10 +566,13 @@ let drawingTime;
|
|||
|
||||
let start;
|
||||
|
||||
let deferredTimout;
|
||||
|
||||
function initialDraw(resources, face){
|
||||
//print("Free memory", process.memory(false).free);
|
||||
requestedDraws++;
|
||||
if (!isDrawing){
|
||||
cleanupDelays();
|
||||
//print(new Date().toISOString(), "Can draw,", requestedDraws, "draws requested so far");
|
||||
isDrawing = true;
|
||||
resetPerfLog();
|
||||
|
@ -570,7 +586,7 @@ function initialDraw(resources, face){
|
|||
|
||||
promise.then(()=>{
|
||||
let currentDrawingTime = Date.now();
|
||||
if (showWidgets){
|
||||
if (showWidgets && global.WIDGETS){
|
||||
//print("Draw widgets");
|
||||
Bangle.drawWidgets();
|
||||
g.setColor(g.theme.fg);
|
||||
|
@ -590,7 +606,7 @@ function initialDraw(resources, face){
|
|||
if (requestedDraws > 0){
|
||||
//print(new Date().toISOString(), "Had deferred drawing left, drawing again");
|
||||
requestedDraws = 0;
|
||||
setTimeout(()=>{initialDraw(resources, face);}, 10);
|
||||
deferredTimout = setTimeout(()=>{initialDraw(resources, face);}, 10);
|
||||
}
|
||||
} //else {
|
||||
//print("queued draw");
|
||||
|
@ -601,7 +617,7 @@ function handleHrm(e){
|
|||
if (e.confidence > 70){
|
||||
pulse = e.bpm;
|
||||
if (!redrawEvents || redrawEvents.includes("HRM") && !Bangle.isLocked()){
|
||||
//print("Redrawing on HRM");
|
||||
print("Redrawing on HRM");
|
||||
initialDraw(watchfaceResources, watchface);
|
||||
}
|
||||
}
|
||||
|
@ -631,20 +647,16 @@ function getMatchedWaitingTime(time){
|
|||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
function setMatchedInterval(callable, time, intervalHandler, delay){
|
||||
//print("Setting matched interval for", time);
|
||||
//print("Setting matched interval for", time, intervalHandler);
|
||||
let matchedTime = getMatchedWaitingTime(time + delay);
|
||||
setTimeout(()=>{
|
||||
return setTimeout(()=>{
|
||||
let interval = setInterval(callable, time);
|
||||
if (intervalHandler) intervalHandler(interval);
|
||||
callable();
|
||||
}, matchedTime);
|
||||
}
|
||||
|
||||
let unlockedDrawInterval;
|
||||
let lockedDrawInterval;
|
||||
|
||||
let lastDrawTime = 0;
|
||||
let firstDraw = true;
|
||||
|
@ -659,20 +671,33 @@ let events = getByPath(watchface, ["Properties","Events"]);
|
|||
//print("events", events);
|
||||
//print("redrawEvents", redrawEvents);
|
||||
|
||||
let initialDrawTimeoutUnlocked;
|
||||
let initialDrawTimeoutLocked;
|
||||
|
||||
function handleLock(isLocked, forceRedraw){
|
||||
//print("isLocked", Bangle.isLocked());
|
||||
if (lockedDrawInterval) clearInterval(lockedDrawInterval);
|
||||
if (unlockedDrawInterval) clearInterval(unlockedDrawInterval);
|
||||
for (let i of unlockedDrawInterval){
|
||||
//print("Clearing unlocked", i);
|
||||
clearInterval(i);
|
||||
}
|
||||
for (let i of lockedDrawInterval){
|
||||
//print("Clearing locked", i);
|
||||
clearInterval(i);
|
||||
}
|
||||
unlockedDrawInterval = [];
|
||||
lockedDrawInterval = [];
|
||||
|
||||
if (!isLocked){
|
||||
if (forceRedraw || !redrawEvents || (redrawEvents.includes("unlock"))){
|
||||
//print("Redrawing on unlock", isLocked);
|
||||
initialDraw(watchfaceResources, watchface);
|
||||
}
|
||||
setMatchedInterval(()=>{
|
||||
initialDrawTimeoutUnlocked = setMatchedInterval(()=>{
|
||||
//print("Redrawing on unlocked interval");
|
||||
initialDraw(watchfaceResources, watchface);
|
||||
},unlockedRedraw, (v)=>{
|
||||
unlockedDrawInterval = v;
|
||||
//print("New matched unlocked interval", v);
|
||||
unlockedDrawInterval.push(v);
|
||||
}, lastDrawTime);
|
||||
if (!events || events.includes("HRM")) Bangle.setHRMPower(1, "imageclock");
|
||||
if (!events || events.includes("pressure")) Bangle.setBarometerPower(1, 'imageclock');
|
||||
|
@ -681,11 +706,12 @@ function handleLock(isLocked, forceRedraw){
|
|||
//print("Redrawing on lock", isLocked);
|
||||
initialDraw(watchfaceResources, watchface);
|
||||
}
|
||||
setMatchedInterval(()=>{
|
||||
initialDrawTimeoutLocked = setMatchedInterval(()=>{
|
||||
//print("Redrawing on locked interval");
|
||||
initialDraw(watchfaceResources, watchface);
|
||||
},lockedRedraw, (v)=>{
|
||||
lockedDrawInterval = v;
|
||||
//print("New matched locked interval", v);
|
||||
lockedDrawInterval.push(v);
|
||||
}, lastDrawTime);
|
||||
Bangle.setHRMPower(0, "imageclock");
|
||||
Bangle.setBarometerPower(0, 'imageclock');
|
||||
|
@ -697,21 +723,25 @@ let showWidgets = false;
|
|||
let showWidgetsChanged = false;
|
||||
let currentDragDistance = 0;
|
||||
|
||||
Bangle.setUI("clock");
|
||||
Bangle.on('drag', (e)=>{
|
||||
function restoreWidgetDraw(){
|
||||
if (global.WIDGETS) {
|
||||
for (let w in global.WIDGETS) {
|
||||
let wd = global.WIDGETS[w];
|
||||
wd.draw = originalWidgetDraw[w];
|
||||
wd.area = originalWidgetArea[w];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function handleDrag(e){
|
||||
//print("handleDrag");
|
||||
currentDragDistance += e.dy;
|
||||
if (Math.abs(currentDragDistance) < 10) return;
|
||||
dragDown = currentDragDistance > 0;
|
||||
currentDragDistance = 0;
|
||||
if (!showWidgets && dragDown){
|
||||
//print("Enable widgets");
|
||||
if (WIDGETS && typeof WIDGETS === "object") {
|
||||
for (let w in WIDGETS) {
|
||||
let wd = WIDGETS[w];
|
||||
wd.draw = originalWidgetDraw[w];
|
||||
wd.area = originalWidgetArea[w];
|
||||
}
|
||||
}
|
||||
restoreWidgetDraw();
|
||||
showWidgetsChanged = true;
|
||||
}
|
||||
if (showWidgets && !dragDown){
|
||||
|
@ -727,7 +757,8 @@ Bangle.on('drag', (e)=>{
|
|||
initialDraw();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Bangle.on('drag', handleDrag);
|
||||
|
||||
if (!events || events.includes("pressure")){
|
||||
Bangle.on('pressure', handlePressure);
|
||||
|
@ -753,11 +784,11 @@ let originalWidgetArea = {};
|
|||
|
||||
function clearWidgetsDraw(){
|
||||
//print("Clear widget draw calls");
|
||||
if (WIDGETS && typeof WIDGETS === "object") {
|
||||
if (global.WIDGETS) {
|
||||
originalWidgetDraw = {};
|
||||
originalWidgetArea = {};
|
||||
for (let w in WIDGETS) {
|
||||
let wd = WIDGETS[w];
|
||||
for (let w in global.WIDGETS) {
|
||||
let wd = global.WIDGETS[w];
|
||||
originalWidgetDraw[w] = wd.draw;
|
||||
originalWidgetArea[w] = wd.area;
|
||||
wd.draw = () => {};
|
||||
|
@ -766,9 +797,42 @@ function clearWidgetsDraw(){
|
|||
}
|
||||
}
|
||||
|
||||
setTimeout(()=>{
|
||||
Bangle.loadWidgets();
|
||||
clearWidgetsDraw();
|
||||
}, 0);
|
||||
if (!global.WIDGETS) Bangle.loadWidgets();
|
||||
if (!showWidgets) clearWidgetsDraw();
|
||||
|
||||
handleLock(Bangle.isLocked());
|
||||
|
||||
Bangle.setUI({
|
||||
mode : "clock",
|
||||
remove : function() {
|
||||
//print("remove calls");
|
||||
// Called to unload all of the clock app
|
||||
Bangle.setHRMPower(0, "imageclock");
|
||||
Bangle.setBarometerPower(0, 'imageclock');
|
||||
|
||||
Bangle.removeListener('drag', handleDrag);
|
||||
Bangle.removeListener('lock', handleLock);
|
||||
Bangle.removeListener('charging', handleCharging);
|
||||
Bangle.removeListener('HRM', handleHrm);
|
||||
Bangle.removeListener('pressure', handlePressure);
|
||||
|
||||
if (deferredTimout) clearTimeout(deferredTimout);
|
||||
if (initialDrawTimeoutUnlocked) clearTimeout(initialDrawTimeoutUnlocked);
|
||||
if (initialDrawTimeoutLocked) clearTimeout(initialDrawTimeoutLocked);
|
||||
|
||||
for (let i of unlockedDrawInterval){
|
||||
//print("Clearing unlocked", i);
|
||||
clearInterval(i);
|
||||
}
|
||||
delete unlockedDrawInterval;
|
||||
for (let i of lockedDrawInterval){
|
||||
//print("Clearing locked", i);
|
||||
clearInterval(i);
|
||||
}
|
||||
delete lockedDrawInterval;
|
||||
|
||||
cleanupDelays();
|
||||
restoreWidgetDraw();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue