ClockFace_menu: use addItems for settings

master
Richard de Boer 2022-06-07 21:23:12 +02:00
parent a3c82d5bab
commit 66bf7f3b12
No known key found for this signature in database
GPG Key ID: 8721727971871937
2 changed files with 53 additions and 1 deletions

View File

@ -175,3 +175,28 @@ Bangle.on('step', function(steps) {
}); });
``` ```
ClockFace_menu
==============
If your clock comes with a settings menu, you can use this library to easily add
some common options:
```js
let settings = require("Storage").readJSON("<appid>.settings.json", true)||{};
function save(key, value) {
settings[key] = value;
require("Storage").writeJSON("<appid>.settings.json", settings);
}
let menu = {
"": {"title": /*LANG*/"<clock name> Settings"},
};
require("ClockFace_menu").addItems(menu, save, {
showDate: settings.showDate,
loadWidgets: settings.loadWidgets,
});
E.showMenu(menu);
```

View File

@ -1,4 +1,31 @@
// boolean options, which default to true /**
* Add setting items to a menu
*
* @param {object} menu Menu to add items to
* @param {function} callback Callback when value changes
* @param {object} items Menu items to add, with their current value
*/
exports.addItems = function(menu, callback, items) {
Object.keys(items).forEach(key => {
let value = items[key];
const label = {
showDate:/*LANG*/"Show date",
loadWidgets:/*LANG*/"Load widgets",
}[key];
switch(key) {
case "showDate":
case "loadWidgets":
// boolean options, which default to true
if (value===undefined) value = true;
menu[label] = {
value: !!value,
onchange: v => callback(key, v),
};
}
});
};
// legacy boolean options
exports.showDate = exports.showDate =
exports.loadWidgets = exports.loadWidgets =
function(value, callback) { function(value, callback) {