1
0
Fork 0
BangleApps/apps/wid_edit/settings.js

198 lines
6.2 KiB
JavaScript
Raw Permalink Normal View History

2022-01-15 17:16:48 +00:00
/**
* @param {function} back Use back() to return to settings menu
*/
(function(back) {
const names = {};
const settings = require("Storage").readJSON("wid_edit.json", 1) || {};
if (!('custom' in settings)) settings.custom = {};
global._WIDGETS = global._WIDGETS || {};
let cleanup = false;
for (const id in settings.custom) {
if (!(id in global.WIDGETS)) {
// widget which no longer exists
cleanup = true;
delete settings.custom[id];
}
}
if (cleanup) {
if (!Object.keys(settings.custom).length) delete settings.custom;
require("Storage").writeJSON("wid_edit.json", settings);
}
2022-01-15 17:16:48 +00:00
/**
* Sort & redraw all widgets
*/
function redrawWidgets() {
let W = global.WIDGETS;
2022-01-15 17:16:48 +00:00
global.WIDGETS = {};
Object.keys(W)
2023-07-31 21:04:20 +00:00
.sort() // see comment in boot.js
2022-01-15 17:16:48 +00:00
.sort((a, b) => (0|W[b].sortorder)-(0|W[a].sortorder))
.forEach(k => {global.WIDGETS[k] = W[k];});
2022-01-15 17:16:48 +00:00
Bangle.drawWidgets();
}
/**
* Try to find app name for widget
* @param {string} widget WIDGETS key
* @return {string} widget name
*/
function name(widget) {
if (!(widget in names)) {
let infoFile = widget+".info";
// widget names don't always correspond to appid :-(
// so we try both with and without 'wid'-prefix
if (!require("Storage").list(new RegExp(`^${infoFile}$`)).length) {
infoFile = (widget.substr(0, 3)==="wid") ? infoFile.substr(3) : ("wid"+infoFile);
}
names[widget] = (require("Storage").readJSON(infoFile, 1) || {}).name || widget;
}
return names[widget];
}
function edit(id) {
let WIDGET = global.WIDGETS[id],
def = {area: WIDGET.area, sortorder: WIDGET.sortorder|0}; // default values
Object.assign(def, global._WIDGETS[id]||{}); // defaults were saved in _WIDGETS
2022-01-15 17:16:48 +00:00
settings.custom = settings.custom||{};
let saved = settings.custom[id] || {},
area = saved.area || def.area,
sortorder = ("sortorder" in saved) ? saved.sortorder : def.sortorder;
2022-01-15 17:16:48 +00:00
/**
* Draw highlighted widget
2022-01-15 17:16:48 +00:00
*/
function highlight() {
if (WIDGET.width > 0) {
// draw widget, then draw a highlighted border on top
WIDGET.draw(WIDGET);
2022-01-15 17:16:48 +00:00
g.setColor(g.theme.fgH)
.drawRect(WIDGET.x, WIDGET.y, WIDGET.x+WIDGET.width-1, WIDGET.y+23);
} else {
// hidden widget: fake a width and provide our own draw()
const draw = WIDGET.draw, width = WIDGET.width;
WIDGET.width = 24;
WIDGET.draw = function() {
g.setColor(g.theme.bgH).setColor(g.theme.fgH)
.clearRect(this.x, this.y, this.x+23, this.y+23)
.drawRect(this.x, this.y, this.x+23, this.y+23)
.drawLine(this.x, this.y, this.x+23, this.y+23)
.drawLine(this.x, this.y+23, this.x+23, this.y);
};
// re-layout+draw all widgets with our placeholder in between
redrawWidgets();
// and restore original values
WIDGET.draw = draw;
WIDGET.width = width;
2022-01-15 17:16:48 +00:00
}
}
highlight();
/**
* Save widget and redraw with new settings
*/
function save() {
// we only save non-default values
saved = {};
if ((area!==def.area) || (sortorder!==def.sortorder)) {
2022-01-15 17:16:48 +00:00
if (area!==def.area) saved.area = area;
if (sortorder!==def.sortorder) saved.sortorder = sortorder;
settings.custom = settings.custom || {};
settings.custom[id] = saved;
} else if (settings.custom) {
delete settings.custom[id];
2022-01-15 17:16:48 +00:00
}
if (!Object.keys(settings.custom).length) delete settings.custom;
require("Storage").writeJSON("wid_edit.json", settings);
Object.assign(WIDGET, def, saved);
if (WIDGET.sortorder === undefined) delete WIDGET.sortorder; // default can be undefined, but don't put that in the widget
// if we assigned custom values, store defaults in _WIDGETS
let _W = {};
if (saved.area) _W.area = def.area;
if ('sortorder' in saved) _W.sortorder = def.sortorder;
if (Object.keys(_W).length) global._WIDGETS[id] = _W;
else delete global._WIDGETS[id];
2022-01-15 17:16:48 +00:00
// drawWidgets won't clear e.g. bottom bar if we just disabled the last bottom widget
redrawWidgets();
highlight();
m.draw();
}
2022-11-06 18:37:25 +00:00
const AREA_NAMES = [ "Top left", "Top right", "Bottom left", "Bottom right" ];
const AREAS = [ "tl", "tr", "bl", "br" ];
2022-01-15 17:16:48 +00:00
const menu = {
"": {"title": name(id),
back: () => {
redrawWidgets();
mainMenu();
} },
2022-11-06 18:37:25 +00:00
/*LANG*/"Position": {
value: AREAS.indexOf(area),
format: v => AREA_NAMES[v],
min: 0,
max: AREAS.length - 1,
onchange: v => {
print("v", v);
area = AREAS[v];
2022-01-15 17:16:48 +00:00
save();
}
},
/*LANG*/"Sort Order": {
value: sortorder,
onchange: o => {
sortorder = o;
save();
}
},
/*LANG*/"Reset": () => {
area = def.area;
sortorder = def.sortorder;
save();
mainMenu(); // changing multiple values made the rest of the menu wrong, so take the easy out
}
};
2022-01-15 17:16:48 +00:00
let m = E.showMenu(menu);
}
function mainMenu() {
let menu = {
"": {
"title": /*LANG*/"Widgets",
back: ()=>{
if (!Object.keys(global._WIDGETS).length) delete global._WIDGETS; // no defaults to remember
back();
}},
2022-01-15 17:16:48 +00:00
};
Object.keys(global.WIDGETS).forEach(id=>{
// mark customized widgets with asterisk
menu[name(id)+((id in global._WIDGETS) ? " *" : "")] = () => edit(id);
2022-01-15 17:16:48 +00:00
});
if (Object.keys(global._WIDGETS).length) { // only show reset if there is anything to reset
2022-01-15 17:16:48 +00:00
menu[/*LANG*/"Reset All"] = () => {
E.showPrompt(/*LANG*/"Reset all widgets?").then(confirm => {
if (confirm) {
delete settings.custom;
require("Storage").writeJSON("wid_edit.json", settings);
for(let id in global._WIDGETS) {
Object.assign(global.WIDGETS[id], global._WIDGETS[id]); // restore defaults
2022-01-15 17:16:48 +00:00
}
global._WIDGETS = {};
redrawWidgets();
}
mainMenu(); // reload with reset widgets
});
};
2022-01-15 17:16:48 +00:00
}
E.showMenu(menu);
}
mainMenu();
})