1
0
Fork 0
BangleApps/apps/barclock/clock-bar.js

126 lines
4.1 KiB
JavaScript
Raw Normal View History

2020-03-30 19:34:44 +00:00
/* jshint esversion: 6 */
/**
* A simple digital clock showing seconds as a bar
2020-03-30 19:34:44 +00:00
**/
// Check settings for what type our clock should be
let locale = require("locale");
{ // add some more info to locale
let date = new Date();
date.setFullYear(1111);
date.setMonth(1, 3); // februari: months are zero-indexed
const localized = locale.date(date, true);
locale.dayFirst = /3.*2/.test(localized);
2022-05-15 16:25:18 +00:00
locale.hasMeridian = (locale.meridian(date)!=="");
}
2022-05-15 16:25:18 +00:00
2022-06-26 12:15:46 +00:00
let barW = 0,prevX = 0;
2021-10-02 14:52:36 +00:00
function renderBar(l) {
"ram";
2022-06-26 12:15:46 +00:00
if (l) prevX = 0; // called from Layout: drawing area was cleared
else l = clock.layout.bar;
let x2 = l.x+barW;
if (clock.powerSave && Bangle.isLocked()) x2 = 0; // hide bar
if (x2===prevX) return; // nothing to do
if (x2===0) x2--; // don't leave 1px line
if (x2<Math.max(0, prevX)) g.setBgColor(l.bgCol || g.theme.bg).clearRect(x2+1, l.y, prevX, l.y2);
else g.setColor(l.col||g.theme.fg).fillRect(prevX+1, l.y, x2, l.y2);
prevX = x2;
2021-10-02 14:52:36 +00:00
}
2020-03-30 19:34:44 +00:00
2021-10-02 14:52:36 +00:00
function timeText(date) {
if (!clock.is12Hour) {
return locale.time(date, true);
2020-04-01 12:16:35 +00:00
}
const date12 = new Date(date.getTime());
const hours = date12.getHours();
if (hours===0) {
date12.setHours(12);
} else if (hours>12) {
date12.setHours(hours-12);
2020-03-30 19:34:44 +00:00
}
return locale.time(date12, true);
2021-10-02 14:52:36 +00:00
}
function ampmText(date) {
return (clock.is12Hour && locale.hasMeridian) ? locale.meridian(date) : "";
2021-10-02 14:52:36 +00:00
}
function dateText(date) {
const dayName = locale.dow(date, true),
month = locale.month(date, true),
day = date.getDate();
const dayMonth = locale.dayFirst ? `${day} ${month}` : `${month} ${day}`;
return `${dayName} ${dayMonth}`;
2021-10-02 14:52:36 +00:00
}
2020-03-30 19:34:44 +00:00
2022-05-15 16:25:18 +00:00
const ClockFace = require("ClockFace"),
clock = new ClockFace({
precision:1,
settingsFile:'barclock.settings.json',
2022-05-15 16:25:18 +00:00
init: function() {
const Layout = require("Layout");
this.layout = new Layout({
type: "v", c: [
{
type: "h", c: [
2022-05-27 15:20:13 +00:00
{id: "time", label: "88:88", type: "txt", font: "6x8:5", col:g.theme.fg, bgCol: g.theme.bg}, // updated below
2022-05-15 16:25:18 +00:00
{id: "ampm", label: " ", type: "txt", font: "6x8:2", col:g.theme.fg, bgCol: g.theme.bg},
],
},
2022-06-26 12:15:46 +00:00
{id: "bar", type: "custom", fillx: 1, height: 6, col: g.theme.fg2, render: renderBar},
this.showDate ? {height: 40} : {},
this.showDate ? {id: "date", type: "txt", font: "10%", valign: 1} : {},
2022-05-15 16:25:18 +00:00
],
}, {lazy: true});
// adjustments based on screen size and whether we display am/pm
let thickness; // bar thickness, same as time font "pixel block" size
2022-05-27 15:20:13 +00:00
if (this.is12Hour && locale.hasMeridian) {
2022-05-15 16:25:18 +00:00
// Maximum font size = (<screen width> - <ampm: 2chars * (2*6)px>) / (5chars * 6px)
thickness = Math.floor((Bangle.appRect.w-24)/(5*6));
} else {
this.layout.ampm.label = "";
thickness = Math.floor(Bangle.appRect.w/(5*6));
}
2022-06-26 12:15:46 +00:00
let bar = this.layout.bar;
bar.height = thickness+1;
2022-05-27 15:20:13 +00:00
if (this.font===1) { // vector
const B2 = process.env.HWVERSION>1;
2022-05-27 15:20:13 +00:00
if (this.is12Hour && locale.hasMeridian) {
this.layout.time.font = "Vector:"+(B2 ? 50 : 60);
this.layout.ampm.font = "Vector:"+(B2 ? 20 : 40);
2022-05-27 15:20:13 +00:00
} else {
this.layout.time.font = "Vector:"+(B2 ? 60 : 80);
2022-05-27 15:20:13 +00:00
}
} else {
this.layout.time.font = "6x8:"+thickness;
}
2022-05-15 16:25:18 +00:00
this.layout.update();
2022-06-26 12:15:46 +00:00
bar.y2 = bar.y+bar.height-1;
2022-05-15 16:25:18 +00:00
},
update: function(date, c) {
"ram";
2022-05-15 16:25:18 +00:00
if (c.m) this.layout.time.label = timeText(date);
if (c.h) this.layout.ampm.label = ampmText(date);
if (c.d && this.showDate) this.layout.date.label = dateText(date);
2022-06-26 12:15:46 +00:00
if (c.m) this.layout.render();
if (c.s) {
barW = Math.round(date.getSeconds()/60*this.layout.bar.w);
renderBar();
}
2022-05-15 16:25:18 +00:00
},
resume: function() {
2022-06-26 12:15:46 +00:00
prevX = 0; // force redraw of bar
2022-05-15 16:25:18 +00:00
this.layout.forgetLazyState();
},
});
2022-06-26 12:15:46 +00:00
// power saving: only update once a minute while locked, hide bar
if (clock.powerSave) {
2022-06-26 12:15:46 +00:00
Bangle.on("lock", lock => {
clock.precision = lock ? 60 : 1;
clock.tick();
renderBar(); // hide/redraw bar right away
});
}
2022-05-15 16:25:18 +00:00
clock.start();