configurable drag gestures

master
Micha 2022-03-23 14:37:44 +01:00 committed by GitHub
parent cde9f59ba3
commit 6158d76f17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 95 additions and 77 deletions

View File

@ -4,12 +4,13 @@ var s = Object.assign({
CAL_ROWS: 4, //number of calendar rows.(weeks) Shouldn't exceed 5 when using widgets. CAL_ROWS: 4, //number of calendar rows.(weeks) Shouldn't exceed 5 when using widgets.
BUZZ_ON_BT: true, //2x slow buzz on disconnect, 2x fast buzz on connect. Will be extra widget eventually BUZZ_ON_BT: true, //2x slow buzz on disconnect, 2x fast buzz on connect. Will be extra widget eventually
MODE24: true, //24h mode vs 12h mode MODE24: true, //24h mode vs 12h mode
FIRSTDAYOFFSET: 6, //First day of the week: 0-6: Sun, Sat, Fri, Thu, Wed, Tue, Mon FIRSTDAY: 6, //First day of the week: mo, tu, we, th, fr, sa, su
REDSUN: true, // Use red color for sunday? REDSUN: true, // Use red color for sunday?
REDSAT: true, // Use red color for saturday? REDSAT: true, // Use red color for saturday?
DRAGENABLED: true, DRAGDOWN: "[AI:messg]",
DRAGMUSIC: true, DRAGRIGHT: "[AI:music]",
DRAGMESSAGES: true DRAGLEFT: "[ignore]",
DRAGUP: "[calend.]"
}, require('Storage').readJSON("clockcal.json", true) || {}); }, require('Storage').readJSON("clockcal.json", true) || {});
const h = g.getHeight(); const h = g.getHeight();
@ -45,22 +46,22 @@ function drawFullCalendar(monthOffset) {
if (typeof minuteInterval !== "undefined") clearTimeout(minuteInterval); if (typeof minuteInterval !== "undefined") clearTimeout(minuteInterval);
d = addMonths(Date(), monthOffset); d = addMonths(Date(), monthOffset);
tdy = Date().getDate() + "." + Date().getMonth(); tdy = Date().getDate() + "." + Date().getMonth();
newmonth=false; newmonth = false;
c_y = 0; c_y = 0;
g.reset(); g.reset();
g.setBgColor(0); g.setBgColor(0);
g.clear(); g.clear();
var prevmonth = addMonths(d, -1) var prevmonth = addMonths(d, -1);
const today = prevmonth.getDate(); const today = prevmonth.getDate();
var rD = new Date(prevmonth.getTime()); var rD = new Date(prevmonth.getTime());
rD.setDate(rD.getDate() - (today - 1)); rD.setDate(rD.getDate() - (today - 1));
const dow = (s.FIRSTDAYOFFSET + rD.getDay()) % 7; const dow = (s.FIRSTDAY + rD.getDay()) % 7;
rD.setDate(rD.getDate() - dow); rD.setDate(rD.getDate() - dow);
var rDate = rD.getDate(); var rDate = rD.getDate();
bottomrightY = c_y - 3; bottomrightY = c_y - 3;
clrsun=s.REDSUN?'#f00':'#fff'; clrsun = s.REDSUN ? '#f00' : '#fff';
clrsat=s.REDSUN?'#f00':'#fff'; clrsat = s.REDSUN ? '#f00' : '#fff';
var fg=[clrsun,'#fff','#fff','#fff','#fff','#fff',clrsat]; var fg = [clrsun, '#fff', '#fff', '#fff', '#fff', '#fff', clrsat];
for (var y = 1; y <= 11; y++) { for (var y = 1; y <= 11; y++) {
bottomrightY += CELL_H; bottomrightY += CELL_H;
bottomrightX = -2; bottomrightX = -2;
@ -73,10 +74,10 @@ function drawFullCalendar(monthOffset) {
} else if (rDate == 1) { } else if (rDate == 1) {
caldrawFirst(rDate); caldrawFirst(rDate);
} else { } else {
caldrawNormal(rDate,fg[rD.getDay()]); caldrawNormal(rDate, fg[rD.getDay()]);
} }
if (newmonth && x == 7) { if (newmonth && x == 7) {
caldrawMonth(rDate,monthclr[rMonth % 6],months[rMonth],rD); caldrawMonth(rDate, monthclr[rMonth % 6], months[rMonth], rD);
} }
rD.setDate(rDate + 1); rD.setDate(rDate + 1);
} }
@ -84,7 +85,7 @@ function drawFullCalendar(monthOffset) {
delete addMonths; delete addMonths;
if (DEBUG) console.log("Calendar performance (ms):" + (Date().getTime() - start)); if (DEBUG) console.log("Calendar performance (ms):" + (Date().getTime() - start));
} }
function caldrawMonth(rDate,c,m,rD) { function caldrawMonth(rDate, c, m, rD) {
g.setColor(c); g.setColor(c);
g.setFont("Vector", 18); g.setFont("Vector", 18);
g.setFontAlign(-1, 1, 1); g.setFontAlign(-1, 1, 1);
@ -111,7 +112,7 @@ function caldrawFirst(rDate) {
g.setColor('#000'); g.setColor('#000');
g.drawString(rDate, bottomrightX, bottomrightY); g.drawString(rDate, bottomrightX, bottomrightY);
} }
function caldrawNormal(rDate,c) { function caldrawNormal(rDate, c) {
g.setFont("Vector", 16); g.setFont("Vector", 16);
g.setFontAlign(1, 1); g.setFontAlign(1, 1);
g.setColor(c); g.setColor(c);
@ -163,7 +164,7 @@ function drawWatch() {
g.clear(); g.clear();
drawMinutes(); drawMinutes();
if (!dimSeconds) drawSeconds(); if (!dimSeconds) drawSeconds();
const dow = (s.FIRSTDAYOFFSET + d.getDay()) % 7; //MO=0, SU=6 const dow = (s.FIRSTDAY + d.getDay()) % 7; //MO=0, SU=6
const today = d.getDate(); const today = d.getDate();
var rD = new Date(d.getTime()); var rD = new Date(d.getTime());
rD.setDate(rD.getDate() - dow); rD.setDate(rD.getDate() - dow);
@ -205,27 +206,52 @@ function BTevent() {
setTimeout(function () { Bangle.buzz(interval); }, interval * 3); setTimeout(function () { Bangle.buzz(interval); }, interval * 3);
} }
} }
function action(a) {
g.reset();
if (typeof secondInterval !== "undefined") clearTimeout(secondInterval);
if (DEBUG) console.log("action:" + a);
switch (a) {
case "[ignore]":
break;
case "[calend.]":
drawFullCalendar();
break;
case "[AI:music]":
l = require("Storage").list(RegExp("music.*app.js"));
if (l.length > 0) {
load(l[0]);
} else E.showAlert("Music app not found", "Not found").then(drawWatch);
break;
case "[AI:messg]":
l = require("Storage").list(RegExp("message.*app.js"));
if (l.length > 0) {
load(l[0]);
} else E.showAlert("Message app not found", "Not found").then(drawWatch);
break;
default:
l = require("Storage").list(RegExp(a + ".app.js"));
if (l.length > 0) {
load(l[0]);
} else E.showAlert(a + ": App not found", "Not found").then(drawWatch);
break;
}
}
function input(dir) { function input(dir) {
if (s.DRAGENABLED) { Bangle.buzz(100, 1);
Bangle.buzz(100,1); if (DEBUG) console.log("swipe:" + dir);
console.log("swipe:"+dir);
switch (dir) { switch (dir) {
case "r": case "r":
if (state == "calendar") { if (state == "calendar") {
drawWatch(); drawWatch();
} else { } else {
if (s.DRAGMUSIC) { action(s.DRAGRIGHT);
l=require("Storage").list(RegExp("music.*app"));
if (l.length > 0) {
load(l[0]);
} else Bangle.buzz(3000,1);//not found
}
} }
break; break;
case "l": case "l":
if (state == "calendar") { if (state == "calendar") {
drawWatch(); drawWatch();
} else {
action(s.DRAGLEFT);
} }
break; break;
case "d": case "d":
@ -233,21 +259,15 @@ function input(dir) {
monthOffset--; monthOffset--;
drawFullCalendar(monthOffset); drawFullCalendar(monthOffset);
} else { } else {
if (s.DRAGMESSAGES) { action(s.DRAGDOWN);
l=require("Storage").list(RegExp("message.*app"));
if (l.length > 0) {
load(l[0]);
} else Bangle.buzz(3000,1);//not found
}
} }
break; break;
case "u": case "u":
if (state == "watch") { if (state == "calendar") {
state = "calendar";
drawFullCalendar(0);
} else if (state == "calendar") {
monthOffset++; monthOffset++;
drawFullCalendar(monthOffset); drawFullCalendar(monthOffset);
} else {
action(s.DRAGUP);
} }
break; break;
default: default:
@ -255,27 +275,25 @@ function input(dir) {
drawWatch(); drawWatch();
} }
break; break;
}
} }
} }
let drag; let drag;
Bangle.on("drag", e => { Bangle.on("drag", e => {
if (s.DRAGENABLED) {
if (!drag) { if (!drag) {
drag = { x: e.x, y: e.y }; drag = { x: e.x, y: e.y };
} else if (!e.b) { } else if (!e.b) {
const dx = e.x - drag.x, dy = e.y - drag.y; const dx = e.x - drag.x, dy = e.y - drag.y;
var dir = "t"; var dir = "t";
if (Math.abs(dx) > Math.abs(dy) + 10) { if (Math.abs(dx) > Math.abs(dy) + 20) {
dir = (dx > 0) ? "r" : "l"; dir = (dx > 0) ? "r" : "l";
} else if (Math.abs(dy) > Math.abs(dx) + 10) { } else if (Math.abs(dy) > Math.abs(dx) + 20) {
dir = (dy > 0) ? "d" : "u"; dir = (dy > 0) ? "d" : "u";
} }
drag = null; drag = null;
input(dir); input(dir);
} }
}
}); });
//register events //register events