Rework idle timeout.

pull/990/head
Andrew Gregory 2021-12-03 12:02:31 +08:00 committed by GitHub
parent c537d5c5f7
commit a75b58df70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 30 deletions

View File

@ -110,13 +110,6 @@ var state = {
hide:0 hide:0
}; };
function resetIdle() {
if (state.idletimer) {
clearTimeout(state.idletimer);
}
state.idletimer = setTimeout(_=>load(), 10000);
}
function drawToken(id, r) { function drawToken(id, r) {
var x1 = r.x; var x1 = r.x;
var y1 = r.y; var y1 = r.y;
@ -153,14 +146,14 @@ function drawToken(id, r) {
// counter - draw triangle as swipe hint // counter - draw triangle as swipe hint
let yc = (y1 + y2) / 2; let yc = (y1 + y2) / 2;
g.fillPoly([0, yc, 10, yc - 10, 10, yc + 10, 0, yc]); g.fillPoly([0, yc, 10, yc - 10, 10, yc + 10, 0, yc]);
adj = 5; adj = 10;
} }
// digits just below label // digits just below label
sz = 30; sz = 30;
do { do {
g.setFont("Vector", sz--); g.setFont("Vector", sz--);
} while (g.stringWidth(state.otp) > (r.w - adj)); } while (g.stringWidth(state.otp) > (r.w - adj));
g.drawString(state.otp, (x1 + x2) / 2 + adj, y1 + 16, false); g.drawString(state.otp, (x1 + adj + x2) / 2, y1 + 16, false);
} }
// shaded lines top and bottom // shaded lines top and bottom
g.setColor(0.5, 0.5, 0.5); g.setColor(0.5, 0.5, 0.5);
@ -170,6 +163,8 @@ function drawToken(id, r) {
} }
function draw() { function draw() {
var timerfn = load;
var timerdly = 10000;
var d = new Date(); var d = new Date();
resetIdle(); resetIdle();
if (state.curtoken != -1) { if (state.curtoken != -1) {
@ -216,13 +211,12 @@ function draw() {
if (state.drawtimer) { if (state.drawtimer) {
clearTimeout(state.drawtimer); clearTimeout(state.drawtimer);
} }
var dly;
if (tokens[state.curtoken].period > 0) { if (tokens[state.curtoken].period > 0) {
dly = (state.otp == calculating) ? 1 : 1000; timerdly = (state.otp == calculating) ? 1 : 1000; // timed
} else { } else {
dly = state.nexttime - d.getTime(); timerdly = state.nexttime - d.getTime(); // counter
} }
state.drawtimer = setTimeout(draw, dly); timerfn = draw;
if (tokens[state.curtoken].period <= 0) { if (tokens[state.curtoken].period <= 0) {
state.hide = 0; state.hide = 0;
} }
@ -239,10 +233,13 @@ function draw() {
g.setFontAlign(0, 0, 0); g.setFontAlign(0, 0, 0);
g.drawString(notokens, Bangle.appRect.x + Bangle.appRect.w / 2, Bangle.appRect.y + Bangle.appRect.h / 2, false); g.drawString(notokens, Bangle.appRect.x + Bangle.appRect.w / 2, Bangle.appRect.y + Bangle.appRect.h / 2, false);
} }
if (state.drawtimer) {
clearTimeout(state.drawtimer);
}
state.drawtimer = setTimeout(timerfn, timerdly);
} }
function onTouch(zone, e) { function onTouch(zone, e) {
resetIdle();
if (e) { if (e) {
var id = Math.floor((state.listy + (e.y - Bangle.appRect.y)) / tokenentryheight); var id = Math.floor((state.listy + (e.y - Bangle.appRect.y)) / tokenentryheight);
if (id == state.curtoken || tokens.length == 0 || id >= tokens.length) { if (id == state.curtoken || tokens.length == 0 || id >= tokens.length) {
@ -264,28 +261,20 @@ function onTouch(zone, e) {
state.nextTime = 0; state.nextTime = 0;
state.curtoken = id; state.curtoken = id;
state.hide = 2; state.hide = 2;
draw();
} }
} }
draw();
} }
function onDrag(e) { function onDrag(e) {
resetIdle();
if (e.x > g.getWidth() || e.y > g.getHeight()) return; if (e.x > g.getWidth() || e.y > g.getHeight()) return;
if (e.dx == 0 && e.dy == 0) return; if (e.dx == 0 && e.dy == 0) return;
var newy = Math.min(state.listy - e.dy, tokens.length * tokenentryheight - Bangle.appRect.h); var newy = Math.min(state.listy - e.dy, tokens.length * tokenentryheight - Bangle.appRect.h);
newy = Math.max(0, newy); state.listy = Math.max(0, newy);
if (newy != state.listy) { draw();
state.listy = newy;
draw();
}
} }
function onSwipe(e) { function onSwipe(e) {
resetIdle();
if (e == 1) {
Bangle.showLauncher();
}
if (e == -1 && state.curtoken != -1 && tokens[state.curtoken].period <= 0) { if (e == -1 && state.curtoken != -1 && tokens[state.curtoken].period <= 0) {
tokens[state.curtoken].period--; tokens[state.curtoken].period--;
let newsettings={tokens:tokens,misc:settings.misc}; let newsettings={tokens:tokens,misc:settings.misc};
@ -293,12 +282,11 @@ function onSwipe(e) {
state.nextTime = 0; state.nextTime = 0;
state.otp = ""; state.otp = "";
state.hide = 2; state.hide = 2;
draw();
} }
draw();
} }
function bangle1Btn(e) { function bangle1Btn(e) {
resetIdle();
if (tokens.length > 0) { if (tokens.length > 0) {
if (state.curtoken == -1) { if (state.curtoken == -1) {
state.curtoken = state.prevcur; state.curtoken = state.prevcur;
@ -315,6 +303,8 @@ function bangle1Btn(e) {
state.curtoken = -1; state.curtoken = -1;
state.nextTime = 0; state.nextTime = 0;
onTouch(0, fakee); onTouch(0, fakee);
} else {
draw(); // resets idle timer
} }
} }
@ -322,9 +312,9 @@ Bangle.on('touch', onTouch);
Bangle.on('drag' , onDrag ); Bangle.on('drag' , onDrag );
Bangle.on('swipe', onSwipe); Bangle.on('swipe', onSwipe);
if (typeof BTN2 == 'number') { if (typeof BTN2 == 'number') {
setWatch(function(){bangle1Btn(-1); }, BTN1, {edge:"rising", debounce:50, repeat:true}); setWatch(function(){bangle1Btn(-1);}, BTN1, {edge:"rising", debounce:50, repeat:true});
setWatch(function(){Bangle.showLauncher();}, BTN2, {edge:"rising", debounce:50, repeat:true}); setWatch(function(){load(); }, BTN2, {edge:"rising", debounce:50, repeat:true});
setWatch(function(){bangle1Btn( 1); }, BTN3, {edge:"rising", debounce:50, repeat:true}); setWatch(function(){bangle1Btn( 1);}, BTN3, {edge:"rising", debounce:50, repeat:true});
} }
Bangle.loadWidgets(); Bangle.loadWidgets();