From ecd5868821bd8ad4334c21fd2f0f492c21985f32 Mon Sep 17 00:00:00 2001 From: Richard de Boer Date: Fri, 27 May 2022 17:08:50 +0200 Subject: [PATCH] ClockFace: add settings support --- modules/ClockFace.js | 20 +++++++++++++++---- modules/ClockFace.md | 41 +++++++++++++++++++++++++++++++++++++++ modules/ClockFace_menu.js | 10 ++++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 modules/ClockFace_menu.js diff --git a/modules/ClockFace.js b/modules/ClockFace.js index d6c3a2e66..f8dc33287 100644 --- a/modules/ClockFace.js +++ b/modules/ClockFace.js @@ -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"); diff --git a/modules/ClockFace.md b/modules/ClockFace.md index e760c3e74..6b59e68e7 100644 --- a/modules/ClockFace.md +++ b/modules/ClockFace.md @@ -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`: diff --git a/modules/ClockFace_menu.js b/modules/ClockFace_menu.js new file mode 100644 index 000000000..cd99ea39f --- /dev/null +++ b/modules/ClockFace_menu.js @@ -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), + }; + };