Merge pull request #2697 from bobrippling/stopw-ms-format

stopw: hardcode format and show milliseconds just for the first minute
pull/2707/head
Gordon Williams 2023-04-24 10:00:25 +01:00 committed by GitHub
commit 3940403167
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 49 deletions

View File

@ -1,2 +1,3 @@
0.01: New clkinfo! 0.01: New clkinfo!
0.02: Added format option, reduced battery usage 0.02: Added format option, reduced battery usage
0.03: Hardcode colon-format, show milliseconds for the first minute

View File

@ -2,7 +2,8 @@
var durationOnPause = "---"; var durationOnPause = "---";
var redrawInterval; var redrawInterval;
var startTime; var startTime;
var _a = (require("Storage").readJSON("clkinfostopw.setting.json", true) || {}).format, format = _a === void 0 ? 0 : _a; var showMillis = true;
var milliTime = 60;
var unqueueRedraw = function () { var unqueueRedraw = function () {
if (redrawInterval) if (redrawInterval)
clearInterval(redrawInterval); clearInterval(redrawInterval);
@ -11,24 +12,31 @@
var queueRedraw = function () { var queueRedraw = function () {
var _this = this; var _this = this;
unqueueRedraw(); unqueueRedraw();
redrawInterval = setInterval(function () { return _this.emit('redraw'); }, 1000); redrawInterval = setInterval(function () {
if (startTime) {
if (showMillis && Date.now() - startTime > milliTime * 1000) {
showMillis = false;
changeInterval(redrawInterval, 1000);
}
}
else {
unqueueRedraw();
}
_this.emit('redraw');
}, 100);
}; };
var pad2 = function (s) { return ('0' + s.toFixed(0)).slice(-2); }; var pad2 = function (s) { return ('0' + s.toFixed(0)).slice(-2); };
var duration = function (start) { var duration = function (start) {
var seconds = (Date.now() - start) / 1000; var seconds = (Date.now() - start) / 1000;
if (seconds < 60) if (seconds < milliTime)
return seconds.toFixed(1); return seconds.toFixed(1);
var mins = seconds / 60; var mins = seconds / 60;
seconds %= 60; seconds %= 60;
if (mins < 60) if (mins < 60)
return format === 0 return "".concat(mins.toFixed(0), ":").concat(pad2(seconds));
? "".concat(pad2(mins), "m").concat(pad2(seconds), "s")
: "".concat(mins.toFixed(0), ":").concat(pad2(seconds));
var hours = mins / 60; var hours = mins / 60;
mins %= 60; mins %= 60;
return format === 0 return "".concat(hours.toFixed(0), ":").concat(pad2(mins), ":").concat(pad2(seconds));
? "".concat(hours.toFixed(0), "h").concat(pad2(mins), "m").concat(pad2(seconds), "s")
: "".concat(hours.toFixed(0), ":").concat(pad2(mins), ":").concat(pad2(seconds));
}; };
var img = function () { return atob("GBiBAAAAAAB+AAB+AAAAAAB+AAH/sAOB8AcA4A4YcAwYMBgYGBgYGBg8GBg8GBgYGBgAGAwAMA4AcAcA4AOBwAH/gAB+AAAAAAAAAA=="); }; var img = function () { return atob("GBiBAAAAAAB+AAB+AAAAAAB+AAH/sAOB8AcA4A4YcAwYMBgYGBgYGBg8GBg8GBgYGBgAGAwAMA4AcAcA4AOBwAH/gAB+AAAAAAAAAA=="); };
return { return {
@ -56,10 +64,10 @@
if (startTime) { if (startTime) {
durationOnPause = duration(startTime); durationOnPause = duration(startTime);
startTime = undefined; startTime = undefined;
unqueueRedraw();
} }
else { else {
queueRedraw.call(this); queueRedraw.call(this);
showMillis = true;
startTime = Date.now(); startTime = Date.now();
} }
} }

View File

@ -2,8 +2,8 @@
let durationOnPause = "---"; let durationOnPause = "---";
let redrawInterval: number | undefined; let redrawInterval: number | undefined;
let startTime: number | undefined; let startTime: number | undefined;
let { format = StopWatchFormat.HMS }: StopWatchSettings let showMillis = true;
= require("Storage").readJSON("clkinfostopw.setting.json", true) || {}; const milliTime = 60;
const unqueueRedraw = () => { const unqueueRedraw = () => {
if (redrawInterval) clearInterval(redrawInterval); if (redrawInterval) clearInterval(redrawInterval);
@ -12,7 +12,17 @@
const queueRedraw = function(this: ClockInfo.MenuItem) { const queueRedraw = function(this: ClockInfo.MenuItem) {
unqueueRedraw(); unqueueRedraw();
redrawInterval = setInterval(() => this.emit('redraw'), 1000); redrawInterval = setInterval(() => {
if (startTime) {
if (showMillis && Date.now() - startTime > milliTime * 1000) {
showMillis = false;
changeInterval(redrawInterval, 1000);
}
} else {
unqueueRedraw();
}
this.emit('redraw')
}, 100);
}; };
const pad2 = (s: number) => ('0' + s.toFixed(0)).slice(-2); const pad2 = (s: number) => ('0' + s.toFixed(0)).slice(-2);
@ -20,23 +30,19 @@
const duration = (start: number) => { const duration = (start: number) => {
let seconds = (Date.now() - start) / 1000; let seconds = (Date.now() - start) / 1000;
if (seconds < 60) if (seconds < milliTime)
return seconds.toFixed(1); return seconds.toFixed(1);
let mins = seconds / 60; let mins = seconds / 60;
seconds %= 60; seconds %= 60;
if (mins < 60) if (mins < 60)
return format === StopWatchFormat.HMS return `${mins.toFixed(0)}:${pad2(seconds)}`;
? `${pad2(mins)}m${pad2(seconds)}s`
: `${mins.toFixed(0)}:${pad2(seconds)}`;
let hours = mins / 60; let hours = mins / 60;
mins %= 60; mins %= 60;
return format === StopWatchFormat.HMS return `${hours.toFixed(0)}:${pad2(mins)}:${pad2(seconds)}`;
? `${hours.toFixed(0)}h${pad2(mins)}m${pad2(seconds)}s`
: `${hours.toFixed(0)}:${pad2(mins)}:${pad2(seconds)}`;
}; };
const img = () => atob("GBiBAAAAAAB+AAB+AAAAAAB+AAH/sAOB8AcA4A4YcAwYMBgYGBgYGBg8GBg8GBgYGBgAGAwAMA4AcAcA4AOBwAH/gAB+AAAAAAAAAA=="); const img = () => atob("GBiBAAAAAAB+AAB+AAAAAAB+AAH/sAOB8AcA4A4YcAwYMBgYGBgYGBg8GBg8GBgYGBgAGAwAMA4AcAcA4AOBwAH/gAB+AAAAAAAAAA==");
@ -64,10 +70,10 @@
run: function() { // tapped run: function() { // tapped
if (startTime) { if (startTime) {
durationOnPause = duration(startTime); durationOnPause = duration(startTime);
startTime = undefined; startTime = undefined; // this also unqueues the redraw
unqueueRedraw();
} else { } else {
queueRedraw.call(this); queueRedraw.call(this);
showMillis = true;
startTime = Date.now(); startTime = Date.now();
} }
} }

View File

@ -1,7 +1,7 @@
{ {
"id": "clkinfostopw", "id": "clkinfostopw",
"name": "Stop Watch Clockinfo", "name": "Stop Watch Clockinfo",
"version":"0.02", "version":"0.03",
"description": "A simple stopwatch, shown via clockinfo", "description": "A simple stopwatch, shown via clockinfo",
"icon": "app.png", "icon": "app.png",
"type": "clkinfo", "type": "clkinfo",
@ -10,7 +10,6 @@
"readme":"README.md", "readme":"README.md",
"allow_emulator": true, "allow_emulator": true,
"storage": [ "storage": [
{"name":"stopw.clkinfo.js","url":"clkinfo.js"}, {"name":"stopw.clkinfo.js","url":"clkinfo.js"}
{"name":"stopw.settings.js","url":"settings.js"}
] ]
} }

View File

@ -1,24 +0,0 @@
(function (back) {
var _a;
var SETTINGS_FILE = "clkinfostopw.setting.json";
var storage = require("Storage");
var settings = storage.readJSON(SETTINGS_FILE, true) || {};
(_a = settings.format) !== null && _a !== void 0 ? _a : (settings.format = 0);
var save = function () {
storage.writeJSON(SETTINGS_FILE, settings);
};
E.showMenu({
"": { "title": "stopwatch" },
"< Back": back,
"Format": {
value: settings.format,
min: 0,
max: 1,
format: function (v) { return v === 0 ? "12m34s" : "12:34"; },
onchange: function (v) {
settings.format = v;
save();
},
},
});
});