forked from FOSS/BangleApps
barclock: optimize drawing
parent
91e80cc3b2
commit
86e3ee489b
|
@ -13,11 +13,18 @@ let locale = require("locale");
|
|||
locale.hasMeridian = (locale.meridian(date)!=="");
|
||||
}
|
||||
|
||||
let barW = 0,prevX = 0;
|
||||
function renderBar(l) {
|
||||
"ram";
|
||||
if (!this.fraction) return; // zero-size fillRect stills draws one line of pixels, we don't want that
|
||||
if (this.powerSave && Bangle.isLocked()) return;
|
||||
g.fillRect(l.x, l.y, l.x+this.fraction*l.w-1, l.y+l.height-1);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
@ -45,7 +52,6 @@ function dateText(date) {
|
|||
return `${dayName} ${dayMonth}`;
|
||||
}
|
||||
|
||||
|
||||
const ClockFace = require("ClockFace"),
|
||||
clock = new ClockFace({
|
||||
precision:1,
|
||||
|
@ -60,7 +66,7 @@ const ClockFace = require("ClockFace"),
|
|||
{id: "ampm", label: " ", type: "txt", font: "6x8:2", col:g.theme.fg, bgCol: g.theme.bg},
|
||||
],
|
||||
},
|
||||
{id: "bar", type: "custom", fraction: 0, fillx: 1, height: 6, col: g.theme.fg2, render: renderBar},
|
||||
{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} : {},
|
||||
],
|
||||
|
@ -74,7 +80,8 @@ const ClockFace = require("ClockFace"),
|
|||
this.layout.ampm.label = "";
|
||||
thickness = Math.floor(Bangle.appRect.w/(5*6));
|
||||
}
|
||||
this.layout.bar.height = thickness+1;
|
||||
let bar = this.layout.bar;
|
||||
bar.height = thickness+1;
|
||||
if (this.font===1) { // vector
|
||||
const B2 = process.env.HWVERSION>1;
|
||||
if (this.is12Hour && locale.hasMeridian) {
|
||||
|
@ -87,31 +94,31 @@ const ClockFace = require("ClockFace"),
|
|||
this.layout.time.font = "6x8:"+thickness;
|
||||
}
|
||||
this.layout.update();
|
||||
bar.y2 = bar.y+bar.height-1;
|
||||
},
|
||||
update: function(date, c) {
|
||||
"ram";
|
||||
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);
|
||||
const SECONDS_PER_MINUTE = 60;
|
||||
if (c.s) this.layout.bar.fraction = date.getSeconds()/SECONDS_PER_MINUTE;
|
||||
this.layout.render();
|
||||
if (c.m) this.layout.render();
|
||||
if (c.s) {
|
||||
barW = Math.round(date.getSeconds()/60*this.layout.bar.w);
|
||||
renderBar();
|
||||
}
|
||||
},
|
||||
resume: function() {
|
||||
prevX = 0; // force redraw of bar
|
||||
this.layout.forgetLazyState();
|
||||
},
|
||||
});
|
||||
|
||||
// power saving: only update once a minute while locked, hide bar
|
||||
if (clock.powerSave) {
|
||||
Bangle.on("lock", l => {
|
||||
if (l) {
|
||||
clock.precision = 60;
|
||||
clock.tick();
|
||||
const l = clock.layout.bar;
|
||||
setTimeout(() => g.clearRect(l.x, l.y, l.x+l.w-1, l.y+l.height-1), 100);
|
||||
} else {
|
||||
clock.precision = 1;
|
||||
clock.tick();
|
||||
}
|
||||
Bangle.on("lock", lock => {
|
||||
clock.precision = lock ? 60 : 1;
|
||||
clock.tick();
|
||||
renderBar(); // hide/redraw bar right away
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue