ClockFace_menu: use addItems for settings
parent
a3c82d5bab
commit
66bf7f3b12
|
|
@ -174,4 +174,29 @@ Bangle.on('step', function(steps) {
|
||||||
if (clock.paused === false) // draw step count
|
if (clock.paused === false) // draw step count
|
||||||
});
|
});
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
@ -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) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue