1
0
Fork 0

Merge pull request #1893 from rigrig/clockface-settings

ClockFace: add settings support
master
Gordon Williams 2022-06-06 11:30:29 +01:00 committed by GitHub
commit 1a2d7a6e25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 4 deletions

View File

@ -10,7 +10,8 @@ function ClockFace(options) {
"precision",
"init", "draw", "update",
"pause", "resume",
"up", "down", "upDown"
"up", "down", "upDown",
"settingsFile",
].includes(k)) throw `Invalid ClockFace option: ${k}`;
});
if (!options.draw && !options.update) throw "ClockFace needs at least one of draw() or update() functions";
@ -33,7 +34,18 @@ function ClockFace(options) {
};
if (options.upDown) this._upDown = options.upDown;
this.is12Hour = !!(require("Storage").readJSON("setting.json", 1) || {})["12hour"];
if (options.settingsFile) {
const settings = (require("Storage").readJSON(options.settingsFile, true) || {});
Object.keys(settings).forEach(k => {
this[k] = settings[k];
});
}
// these default to true
["showDate", "loadWidgets"].forEach(k => {
if (this[k]===undefined) this[k] = true;
});
// use global 24/12-hour setting if not set by clock-settings
if (!('is12Hour' in this)) this.is12Hour = !!(require("Storage").readJSON("setting.json", true) || {})["12hour"];
}
ClockFace.prototype.tick = function() {
@ -46,7 +58,7 @@ ClockFace.prototype.tick = function() {
};
if (!this._last) {
g.clear(true);
Bangle.drawWidgets();
if (global.WIDGETS) Bangle.drawWidgets();
g.reset();
this.draw.apply(this, [time, {d: true, h: true, m: true, s: true}]);
} else {
@ -70,7 +82,7 @@ ClockFace.prototype.start = function() {
.CLOCK is set by Bangle.setUI('clock') but we want to load widgets so we can check appRect and *then*
call setUI. see #1864 */
Bangle.CLOCK = 1;
Bangle.loadWidgets();
if (this.loadWidgets) Bangle.loadWidgets();
if (this.init) this.init.apply(this);
if (this._upDown) Bangle.setUI("clockupdown", d=>this._upDown.apply(this,[d]));
else Bangle.setUI("clock");

View File

@ -85,6 +85,7 @@ var clock = new ClockFace({
if (dir === -1) // Up
else // (dir === 1): Down
},
settingsFile: 'appid.settings.json', // optional, values from file will be applied to `this`
});
clock.start();
@ -110,11 +111,51 @@ clock.start();
```
SettingsFile
------------
If you use the `settingsFile` option, values from that file are loaded and set
directly on the clock.
For example:
```json
// example.settings.json:
{
"showDate": false,
"foo": 123
}
```
```js
var ClockFace = require("ClockFace");
var clock = new ClockFace({
draw: function(){/*...*/},
settingsFile: "example.settings.json",
});
// now
clock.showDate === false;
clock.foo === 123;
clock.loadWidgets === true; // default when not in settings file
clock.is12Hour === ??; // not in settings file: uses global setting
clock.start();
```
Properties
----------
The following properties are automatically set on the clock:
* `is12Hour`: `true` if the "Time Format" setting is set to "12h", `false` for "24h".
* `paused`: `true` while the clock is paused. (You don't need to check this inside your `draw()` code)
* `showDate`: `true` (if not overridden through the settings file.)
* `loadWidgets`: `true` (if not overridden through the settings file.)
If set to `false` before calling `start()`, the clock won't call `Bangle.loadWidgets();` for you.
Best is to add a setting for this, but if you never want to load widgets, you could do this:
```js
var ClockFace = require("ClockFace");
var clock = new ClockFace({draw: function(){/*...*/}});
clock.loadWidgets = false; // prevent loading of widgets
clock.start();
```
Inside the `draw()`/`update()` function you can access these using `this`:

10
modules/ClockFace_menu.js Normal file
View File

@ -0,0 +1,10 @@
// boolean options, which default to true
exports.showDate =
exports.loadWidgets =
function(value, callback) {
if (value === undefined) value = true;
return {
value: !!value,
onchange: v=>callback(v),
};
};