Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stopw: hardcode format and show milliseconds just for the first minute #2697

Merged
merged 6 commits into from Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/clkinfostopw/ChangeLog
@@ -1,2 +1,3 @@
0.01: New clkinfo!
0.02: Added format option, reduced battery usage
0.03: Hardcode colon-format, show milliseconds for the first minute
28 changes: 18 additions & 10 deletions apps/clkinfostopw/clkinfo.js
Expand Up @@ -2,7 +2,8 @@
var durationOnPause = "---";
var redrawInterval;
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 () {
if (redrawInterval)
clearInterval(redrawInterval);
Expand All @@ -11,24 +12,31 @@
var queueRedraw = function () {
var _this = this;
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 duration = function (start) {
var seconds = (Date.now() - start) / 1000;
if (seconds < 60)
if (seconds < milliTime)
return seconds.toFixed(1);
var mins = seconds / 60;
seconds %= 60;
if (mins < 60)
return format === 0
? "".concat(pad2(mins), "m").concat(pad2(seconds), "s")
: "".concat(mins.toFixed(0), ":").concat(pad2(seconds));
return "".concat(mins.toFixed(0), ":").concat(pad2(seconds));
var hours = mins / 60;
mins %= 60;
return format === 0
? "".concat(hours.toFixed(0), "h").concat(pad2(mins), "m").concat(pad2(seconds), "s")
: "".concat(hours.toFixed(0), ":").concat(pad2(mins), ":").concat(pad2(seconds));
return "".concat(hours.toFixed(0), ":").concat(pad2(mins), ":").concat(pad2(seconds));
};
var img = function () { return atob("GBiBAAAAAAB+AAB+AAAAAAB+AAH/sAOB8AcA4A4YcAwYMBgYGBgYGBg8GBg8GBgYGBgAGAwAMA4AcAcA4AOBwAH/gAB+AAAAAAAAAA=="); };
return {
Expand Down Expand Up @@ -56,10 +64,10 @@
if (startTime) {
durationOnPause = duration(startTime);
startTime = undefined;
unqueueRedraw();
}
else {
queueRedraw.call(this);
showMillis = true;
startTime = Date.now();
}
}
Expand Down
30 changes: 18 additions & 12 deletions apps/clkinfostopw/clkinfo.ts
Expand Up @@ -2,8 +2,8 @@
let durationOnPause = "---";
let redrawInterval: number | undefined;
let startTime: number | undefined;
let { format = StopWatchFormat.HMS }: StopWatchSettings
= require("Storage").readJSON("clkinfostopw.setting.json", true) || {};
let showMillis = true;
const milliTime = 60;

const unqueueRedraw = () => {
if (redrawInterval) clearInterval(redrawInterval);
Expand All @@ -12,31 +12,37 @@

const queueRedraw = function(this: ClockInfo.MenuItem) {
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 duration = (start: number) => {
let seconds = (Date.now() - start) / 1000;

if (seconds < 60)
if (seconds < milliTime)
return seconds.toFixed(1);

let mins = seconds / 60;
seconds %= 60;

if (mins < 60)
return format === StopWatchFormat.HMS
? `${pad2(mins)}m${pad2(seconds)}s`
: `${mins.toFixed(0)}:${pad2(seconds)}`;
return `${mins.toFixed(0)}:${pad2(seconds)}`;

let hours = mins / 60;
mins %= 60;

return format === StopWatchFormat.HMS
? `${hours.toFixed(0)}h${pad2(mins)}m${pad2(seconds)}s`
: `${hours.toFixed(0)}:${pad2(mins)}:${pad2(seconds)}`;
return `${hours.toFixed(0)}:${pad2(mins)}:${pad2(seconds)}`;
};

const img = () => atob("GBiBAAAAAAB+AAB+AAAAAAB+AAH/sAOB8AcA4A4YcAwYMBgYGBgYGBg8GBg8GBgYGBgAGAwAMA4AcAcA4AOBwAH/gAB+AAAAAAAAAA==");
Expand Down Expand Up @@ -64,10 +70,10 @@
run: function() { // tapped
if (startTime) {
durationOnPause = duration(startTime);
startTime = undefined;
unqueueRedraw();
startTime = undefined; // this also unqueues the redraw
} else {
queueRedraw.call(this);
showMillis = true;
startTime = Date.now();
}
}
Expand Down
5 changes: 2 additions & 3 deletions apps/clkinfostopw/metadata.json
@@ -1,7 +1,7 @@
{
"id": "clkinfostopw",
"name": "Stop Watch Clockinfo",
"version":"0.02",
"version":"0.03",
"description": "A simple stopwatch, shown via clockinfo",
"icon": "app.png",
"type": "clkinfo",
Expand All @@ -10,7 +10,6 @@
"readme":"README.md",
"allow_emulator": true,
"storage": [
{"name":"stopw.clkinfo.js","url":"clkinfo.js"},
{"name":"stopw.settings.js","url":"settings.js"}
{"name":"stopw.clkinfo.js","url":"clkinfo.js"}
]
}
24 changes: 0 additions & 24 deletions apps/clkinfostopw/settings.js

This file was deleted.