Skip to content

Commit c7e573c

Browse files
committed
New clockbg: add more default colors and .unload/load to allow the design to change each time you flick to launcher and back
1 parent 0616959 commit c7e573c

File tree

26 files changed

+188
-64
lines changed

26 files changed

+188
-64
lines changed

apps/analogquadclk/ChangeLog

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
0.01: New Clock!
22
0.02: Fix fastloading memory leak and clockinfo overwritten by hands
3-
0.03: Use new clockinfo lib with function to render images wirh borders
3+
0.03: Use new clockinfo lib with function to render images wirh borders
4+
0.04: Use new clockbg to allow backgrounds to change when fast loading

apps/analogquadclk/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const W = g.getWidth();
33
const H = g.getHeight();
44
const background = require("clockbg"); // image backgrounds
5+
background.load(); // reload if we fast loaded into here
56
let drawTimeout; // timeout used to update every minute
67
let date = new Date(); // date at last draw
78
let lastModified = {x1:0,y1:0,x2:W-1,y2:H-1,first:true}; // rect that was covered by hands
@@ -106,6 +107,7 @@
106107
remove: function() {
107108
if (drawTimeout) clearTimeout(drawTimeout);
108109
drawTimeout = undefined;
110+
background.unload(); // free memory from background
109111
if (clockInfoMenuA) clockInfoMenuA.remove();
110112
if (clockInfoMenuB) clockInfoMenuB.remove();
111113
if (clockInfoMenuC) clockInfoMenuC.remove();

apps/analogquadclk/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{ "id": "analogquadclk",
22
"name": "Analog Quad Clock",
33
"shortName":"Quad Clock",
4-
"version":"0.03",
4+
"version":"0.04",
55
"description": "An analog clock with clockinfos in each of the 4 corners, allowing 4 different data types to be rendered at once",
66
"icon": "icon.png",
77
"screenshots" : [ { "url":"screenshot.png" }, { "url":"screenshot2.png" } ],

apps/boxclk/ChangeLog

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
0.05: Fixes step count not resetting after a new day starts
66
0.06: Added clockbackground app functionality
77
0.07: Allow custom backgrounds per boxclk config and from the clockbg module
8-
0.08: Improves performance, responsiveness, and bug fixes
8+
0.08: Improves performance, responsiveness, and bug fixes
99
- [+] Added box size caching to reduce calculations
1010
- [+] Improved step count with real-time updates
1111
- [+] Improved battery level update logic to reduce unnecessary refreshes
1212
- [+] Fixed optional seconds not displaying in time
1313
- [+] Fixed drag handler by adding E.stopEventPropagation()
1414
- [+] General code optimization and cleanup
1515
0.09: Revised event handler code
16-
0.10: Revised suffix formatting in getDate function
16+
0.10: Revised suffix formatting in getDate function
17+
0.11: Use new clockbg to allow backgrounds to change when fast loading

apps/boxclk/app.js

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
// 1. Module dependencies and initial configurations
33
let background = require("clockbg");
4+
background.load(); // reload if we fast loaded into here
45
let storage = require("Storage");
56
let locale = require("locale");
67
let widgets = require("widget_utils");
@@ -27,22 +28,22 @@
2728
let touchHandler = function(zone, e) {
2829
let boxTouched = false;
2930
let touchedBox = null;
30-
31+
3132
for (let boxKey in boxes) {
3233
if (touchInText(e, boxes[boxKey])) {
3334
touchedBox = boxKey;
3435
boxTouched = true;
3536
break;
3637
}
3738
}
38-
39+
3940
if (boxTouched) {
4041
// Toggle the selected state of the touched box
4142
boxes[touchedBox].selected = !boxes[touchedBox].selected;
42-
43+
4344
// Update isDragging based on whether any box is selected
4445
isDragging = Object.values(boxes).some(box => box.selected);
45-
46+
4647
if (isDragging) {
4748
widgets.hide();
4849
} else {
@@ -52,10 +53,10 @@
5253
// If tapped outside any box, deselect all boxes
5354
deselectAllBoxes();
5455
}
55-
56+
5657
// Always redraw after a touch event
5758
draw();
58-
59+
5960
// Handle double tap for saving
6061
if (!boxTouched && !isDragging) {
6162
if (doubleTapTimer) {
@@ -69,26 +70,26 @@
6970
displaySaveIcon();
7071
return;
7172
}
72-
73+
7374
doubleTapTimer = setTimeout(() => {
7475
doubleTapTimer = null;
7576
}, 500);
7677
}
7778
};
78-
79+
7980
let dragHandler = function(e) {
8081
if (!isDragging) return;
81-
82+
8283
// Stop propagation of the drag event to prevent other handlers
8384
E.stopEventPropagation();
84-
85+
8586
for (let key in boxes) {
8687
if (boxes[key].selected) {
8788
let boxItem = boxes[key];
8889
calcBoxSize(boxItem);
8990
let newX = boxItem.pos.x + e.dx;
9091
let newY = boxItem.pos.y + e.dy;
91-
92+
9293
if (newX - boxItem.cachedSize.width / 2 >= 0 &&
9394
newX + boxItem.cachedSize.width / 2 <= w &&
9495
newY - boxItem.cachedSize.height / 2 >= 0 &&
@@ -98,18 +99,18 @@
9899
}
99100
}
100101
}
101-
102+
102103
draw();
103104
};
104-
105+
105106
let stepHandler = function(up) {
106107
if (boxes.step && !isDragging) {
107108
boxes.step.string = formatStr(boxes.step, Bangle.getHealthStatus("day").steps);
108109
boxes.step.cachedSize = null;
109110
draw();
110111
}
111112
};
112-
113+
113114
let lockHandler = function(isLocked) {
114115
if (isLocked) {
115116
deselectAllBoxes();
@@ -211,7 +212,7 @@
211212
const day = date.getDate();
212213
const month = shortMonth ? locale.month(date, 1) : locale.month(date, 0);
213214
const year = date.getFullYear();
214-
215+
215216
const getSuffix = (day) => {
216217
if (day >= 11 && day <= 13) return 'th';
217218
const lastDigit = day % 10;
@@ -222,7 +223,7 @@
222223
default: return 'th';
223224
}
224225
};
225-
226+
226227
const dayStr = disableSuffix ? day : `${day}${getSuffix(day)}`;
227228
return `${month} ${dayStr}${short ? '' : `, ${year}`}`; // not including year for short version
228229
};
@@ -323,42 +324,42 @@
323324

324325
let draw = function() {
325326
g.clear();
326-
327+
327328
// Always draw backgrounds full screen
328329
if (bgImage) { // Check for bg in boxclk config
329330
g.drawImage(bgImage, 0, 0);
330331
} else { // Otherwise use clockbg module
331332
background.fillRect(0, 0, g.getWidth(), g.getHeight());
332333
}
333-
334+
334335
if (!isDragging) {
335336
updateBoxData();
336337
}
337-
338+
338339
for (let boxKey in boxes) {
339340
let boxItem = boxes[boxKey];
340-
341+
341342
// Set font and alignment for each box individually
342343
g.setFont(boxItem.font, boxItem.fontSize);
343344
g.setFontAlign(0, 0);
344-
345+
345346
calcBoxSize(boxItem);
346-
347+
347348
const pos = calcBoxPos(boxItem);
348-
349+
349350
if (boxItem.selected) {
350351
g.setColor(boxItem.border);
351352
g.drawRect(pos.x1, pos.y1, pos.x2, pos.y2);
352353
}
353-
354+
354355
g.drawString(
355356
boxItem,
356357
boxItem.string,
357358
boxItem.pos.x + boxItem.xOffset,
358359
boxItem.pos.y + boxItem.yOffset
359360
);
360361
}
361-
362+
362363
if (!isDragging) {
363364
if (drawTimeout) clearTimeout(drawTimeout);
364365
let updateInterval = boxes.time && !isBool(boxes.time.short, true) ? 1000 : 60000 - (Date.now() % 60000);
@@ -382,20 +383,20 @@
382383
if (boxItem.cachedSize) {
383384
return boxItem.cachedSize;
384385
}
385-
386+
386387
g.setFont(boxItem.font, boxItem.fontSize);
387388
g.setFontAlign(0, 0);
388-
389+
389390
let strWidth = g.stringWidth(boxItem.string) + 2 * boxItem.outline;
390391
let fontHeight = g.getFontHeight() + 2 * boxItem.outline;
391392
let totalWidth = strWidth + 2 * boxItem.xPadding;
392393
let totalHeight = fontHeight + 2 * boxItem.yPadding;
393-
394+
394395
boxItem.cachedSize = {
395396
width: totalWidth,
396397
height: totalHeight
397398
};
398-
399+
399400
return boxItem.cachedSize;
400401
};
401402

@@ -424,18 +425,18 @@
424425
Bangle.on('lock', lockHandler);
425426
Bangle.on('touch', touchHandler);
426427
Bangle.on('drag', dragHandler);
427-
428+
428429
if (boxes.step) {
429430
boxes.step.string = formatStr(boxes.step, Bangle.getHealthStatus("day").steps);
430431
Bangle.on('step', stepHandler);
431432
}
432-
433+
433434
if (boxes.batt) {
434435
boxes.batt.lastLevel = E.getBattery();
435436
boxes.batt.string = formatStr(boxes.batt, boxes.batt.lastLevel);
436437
boxes.batt.lastUpdate = Date.now();
437438
}
438-
439+
439440
Bangle.setUI({
440441
mode: "clock",
441442
remove: function() {
@@ -448,14 +449,15 @@
448449
}
449450
if (drawTimeout) clearTimeout(drawTimeout);
450451
drawTimeout = undefined;
452+
background.unload(); // free memory from background
451453
delete Graphics.prototype.setFontBrunoAce;
452454
// Restore original drawString function (no outlines)
453455
g.drawString = g_drawString;
454456
restoreSetColor();
455457
widgets.show();
456458
}
457459
});
458-
460+
459461
loadCustomFont();
460462
draw();
461463
};

apps/boxclk/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "boxclk",
33
"name": "Box Clock",
4-
"version": "0.10",
4+
"version": "0.11",
55
"description": "A customizable clock with configurable text boxes that can be positioned to show your favorite background",
66
"icon": "app.png",
77
"dependencies" : { "clockbg":"module" },

apps/clockbg/ChangeLog

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@
66
Add a 'view' option in settings menu to view the current background
77
0.05: Random square+plasma speed improvements (~2x faster)
88
0.06: 25% speed improvement if Math.randInt exists (2v25 fw)
9-
0.07: Added 'rings'and 'tris' background styles
9+
0.07: Added 'rings'and 'tris' background styles
10+
0.08: Allow backgrounds with a random choice of colour schemes
11+
Default (solid colour) now uses a variety of shades
12+
Added .load/.unload to allow changes when fast loading clocks

apps/clockbg/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ background.fillRect(x1, y1, x2, y2);
3636

3737
// if you ever need to reload to a new background (this could take ~100ms)
3838
background.reload();
39+
40+
// Call this to unload (free memory - eg in .remove when fast loading)
41+
background.unload();
42+
43+
// If .unload has been called and you might have fast-loaded back, call .load to ensure everything is loaded again!
44+
// It won't reload if it's already been loaded
45+
background.load();
3946
```
4047

4148
You should also add `"dependencies" : { "clockbg":"module" },` to your app's metadata to

apps/clockbg/interface.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<option value="error">Diffusion Dither</option>
4242
<option value="random1">Random Dither</option>
4343
<option value="comic">'Comic' Dither</option>
44+
<option value="floyd">Floyd-Steinberg</option>
4445
</select>
4546
</div>
4647

@@ -105,7 +106,7 @@
105106
var img = IMAGES.find(img => img.path == imgPath) || {"dither":true}; // No IMAGES entry for custom images
106107
var zoom = document.getElementById("box_zoom").value;
107108
var dither = document.getElementById("box_dither").value;
108-
if (dither=="" && img.dither) dither="bayer2";
109+
if (dither=="" && img.dither) dither="floyd";
109110
if (dither=="no" || dither=="") dither=undefined;
110111
var mirror = document.getElementById("box_mirror").checked;
111112
var brightness = 0|document.getElementById("box_bright").value;

apps/clockbg/lib.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ exports.reload = function() {
44
//let t = Date.now();
55
settings = Object.assign({
66
style : "randomcolor",
7-
colors : ["#F00","#0F0","#00F"]
7+
colors : ["#00f","#0bf","#0f7","#3f0","#ff0","#f30","#f07","#b0f"]
88
},require("Storage").readJSON("clockbg.json",1)||{});
9+
// if an array of arrays then we select one at random
10+
if (settings.colors && settings.colors[0] instanceof Array)
11+
settings.colors = settings.colors[Math.randInt(settings.colors.length)];
912
if (settings.style=="image")
1013
settings.img = require("Storage").read(settings.fn);
1114
else if (settings.style=="randomcolor") {
@@ -68,10 +71,22 @@ exports.reload = function() {
6871
//console.log("bg",Date.now()-t);
6972
};
7073

74+
/// Will load settings if they haven't already been loaded
75+
exports.load = function() {
76+
if (settings===undefined)
77+
exports.reload();
78+
}
79+
80+
/// Remove settings and free memory - .load() must be called before drawing again
81+
exports.unload = function() {
82+
settings = undefined;
83+
}
84+
7185
// Fill a rectangle with the current background style, rect = {x,y,w,h}
7286
// eg require("clockbg").fillRect({x:10,y:10,w:50,h:50})
7387
// require("clockbg").fillRect(Bangle.appRect)
7488
exports.fillRect = function(rect,y,x2,y2) {
89+
if (!settings) return;
7590
if ("object"!=typeof rect) rect = {x:rect,y:y,w:1+x2-rect,h:1+y2-y};
7691
if (settings.img) {
7792
g.setClipRect(rect.x, rect.y, rect.x+rect.w-1, rect.y+rect.h-1).drawImage(settings.img,0,0,settings.imgOpt).setClipRect(0,0,g.getWidth()-1,g.getHeight()-1);

0 commit comments

Comments
 (0)