1
0
Fork 0

wid_edit: only allow top row, don't "disable" widgets

master
Richard de Boer 2022-01-17 19:36:41 +01:00
parent 0be14efaec
commit 66120e31e4
No known key found for this signature in database
GPG Key ID: 8721727971871937
4 changed files with 35 additions and 114 deletions

View File

@ -1,18 +1,15 @@
# Widget Editor
This adds a setting menu which allows you to change the location of all widgets.
This adds a setting menu which allows you to change the location of widgets.
## Settings
There is no app icon in the launcher; you can find the settings under
`Settings`->`App/Widget Settings`->`Widget Editor`.
`Apps`->`Widget Editor`.
For every widget, you have these options:
* **Area**: In which corner to draw the widget.
* **Note**: Not all apps handle widgets in the bottom corners well.
* **Sort Order**: Changes the order if several widgets use the same corner.
* **Enabled**: We do our best to hide disabled widgets, but they are still
loaded and use RAM.
* **Side**: On which side to draw the widget.
* **Sort Order**: Changes the order if several widgets use the same side.
## Creator

View File

@ -7,39 +7,19 @@ Bangle.loadWidgets = function() {
});
const o = require("Storage").readJSON("wid_edit.json", 1) || {},
c = o.custom || {};
let _W;
for (const w in c){
if (!(w in WIDGETS)) continue;
_W= {};
if (c[w].hide) {
// disabled: move widget to _WIDGETS, and place it way offscreen (in case it tries to draw itself anyway)
_W = WIDGETS[w]; _W.x = 1000; _W.y = 1000;
WIDGETS[w] = {draw:()=>{}}; // in case it tries to call itself
}
else {
// enabled: store default area/sortorder in _WIDGETS (only if changed)
if (c[w].area) _W.area = WIDGETS[w].area;
if ('sortorder' in c[w]) _W.sortorder = WIDGETS[w].sortorder;
Object.assign(WIDGETS[w], c[w]);
}
if (Object.keys(_W).length) _WIDGETS[w] = _W;
let _W = {};
// store default area/sortorder in _WIDGETS
if (c[w].area) _W.area = WIDGETS[w].area;
if ('sortorder' in c[w]) _W.sortorder = WIDGETS[w].sortorder;
Object.assign(WIDGETS[w], c[w]);
_WIDGETS[w] = _W;
}
if (!Object.keys(_WIDGETS).length) delete _WIDGETS; // no need for this
const W = WIDGETS;
WIDGETS = {};
let a, t=0, b=0;
Object.keys(W)
.sort((a, b) => (0|W[b].sortorder)-(0|W[a].sortorder))
.forEach(k => {
WIDGETS[k] = W[k];
if (a=W[k].area) {
t = t || (a[0]=="t");
b = b || (a[0]=="b");
}
});
Bangle.appRect = {
x: 0, y: t*24,
w: g.getWidth(), h: g.getHeight()-1-(t+b)*24,
x2: g.getWidth()-1, y2: g.getHeight()-1-b*24
}
.forEach(k => WIDGETS[k] = W[k]);
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 8.1 KiB

View File

@ -6,10 +6,7 @@
const settings = require("Storage").readJSON("wid_edit.json", 1) || {};
if (!('custom' in settings)) settings.custom = {};
global._WIDGETS = global._WIDGETS || {};
// Adjust appRect to always have room for widgets while in this app
Object.assign(Bangle.appRect, {
y: 24, h: g.getHeight()-25, y2: g.getHeight()-48,
});
/**
* Sort & redraw all widgets
*/
@ -41,23 +38,20 @@
}
function edit(id) {
// disabled widgets were moved to _WIDGETS and replaced with a dummy {draw}, so don't have area set
let WIDGET = WIDGETS[id].area ? WIDGETS[id] : _WIDGETS[id],
def = {area: WIDGET.area, sortorder: WIDGET.sortorder|0}; // default values (disabled is never the default)
if (WIDGET.area && id in _WIDGETS) Object.assign(def, _WIDGETS[id]); // enabled widgets have defaults saved in _WIDGETS
let WIDGET = WIDGETS[id],
def = {area: WIDGET.area, sortorder: WIDGET.sortorder|0}; // default values
Object.assign(def, _WIDGETS[id]||{}); // defaults were saved in _WIDGETS
const areas = ['tl','tr','bl','br'];
settings.custom = settings.custom||{};
let saved = settings.custom[id] || {},
area = saved.area || def.area,
sortorder = ("sortorder" in saved) ? saved.sortorder : def.sortorder,
enabled = !saved.hide;
sortorder = ("sortorder" in saved) ? saved.sortorder : def.sortorder;
/**
* Draw highlighted widget (if enabled)
* Draw highlighted widget
*/
function highlight() {
if (WIDGET.width && enabled) {
if (WIDGET.width) {
WIDGET.draw();
g.setColor(g.theme.fgH)
.drawRect(WIDGET.x, WIDGET.y, WIDGET.x+WIDGET.width-1, WIDGET.y+23);
@ -71,10 +65,9 @@
function save() {
// we only save non-default values
saved = {};
if ((area!==def.area) || (sortorder!==def.sortorder) || !enabled) {
if ((area!==def.area) || (sortorder!==def.sortorder)) {
if (area!==def.area) saved.area = area;
if (sortorder!==def.sortorder) saved.sortorder = sortorder;
if (!enabled) saved.hide = true;
settings.custom = settings.custom || {};
settings.custom[id] = saved;
} else if (settings.custom) {
@ -82,26 +75,16 @@
}
if (!Object.keys(settings.custom).length) delete settings.custom;
require("Storage").writeJSON("wid_edit.json", settings);
delete saved.hide; // no need to save this inside the widget
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 (enabled) {
WIDGETS[id] = WIDGET;
delete _WIDGETS[id];
// if we assigned custom values, store defaults in _WIDGETS
let _W = {};
if (saved.area) _W.area = def.area;
if (def.sortorder !== undefined) {
if ('sortorder' in saved) _W.sortorder = def.sortorder;
}
if (Object.keys(_W).length) _WIDGETS[id] = _W;
} else {
// disabled: move original widget into _WIDGETS, and place it offscreen
_WIDGETS[id] = WIDGET;
WIDGETS[id] = {draw: () => {}}; // in case it tries to call itself
}
// 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) _WIDGETS[id] = _W;
else delete _WIDGETS[id];
// drawWidgets won't clear e.g. bottom bar if we just disabled the last bottom widget
if (WIDGET.width) g.reset().clearRect(WIDGET.x, WIDGET.y, WIDGET.x+WIDGET.width-1, WIDGET.y+23);
redrawWidgets();
highlight();
@ -117,15 +100,11 @@
}
mainMenu();
},
/*LANG*/"Area": {
value: areas.indexOf(area),
min: 0, max: 3, wrap: true,
format: a => [
/*LANG*/"Top Left", /*LANG*/"Top Right",
/*LANG*/"Bottom Left", /*LANG*/"Bottom Right"
][a],
onchange: a => {
area = areas[a];
/*LANG*/"Side": {
value: (area === 'tl'),
format: tl => tl ? /*LANG*/"Left" : /*LANG*/"Right",
onchange: tl => {
area = tl ? "tl" : "tr";
save();
}
},
@ -136,18 +115,9 @@
save();
}
},
/*LANG*/"Enabled": {
value: enabled,
format: e => e ?/*LANG*/"Yes" :/*LANG*/"No",
onchange: e => {
enabled = e;
save();
}
},
/*LANG*/"Reset": () => {
area = def.area;
sortorder = def.sortorder;
enabled = true;
save();
mainMenu(); // changing multiple values made the rest of the menu wrong, so take the easy out
}
@ -162,33 +132,13 @@
"": {"title": /*LANG*/"Widgets"},
};
menu[/*LANG*/"< Back"] = ()=>{
if (!Object.keys(_WIDGETS).length) delete _WIDGETS;
// adjust appRect for new widget locations
let a, t=0, b=0;
for (let WIDGET of WIDGETS) {
if (a=WIDGET.area) {
t = t || (a[0]=="t");
b = b || (a[0]=="b");
}
}
Object.assign(Bangle.appRect, {
y: t*24,
h: g.getHeight()-(t+b)*24,
y2: g.getHeight()-1-b*24
});
if (!Object.keys(_WIDGETS).length) delete _WIDGETS; // no defaults to remember
back();
};
const enabled = Object.keys(WIDGETS).filter(id => WIDGETS[id].area);
enabled.forEach(function(id) {
Object.keys(WIDGETS).forEach(id=>{
// mark customized widgets with asterisk
menu[name(id)+((id in _WIDGETS) ? " *" : "")] = () => edit(id);
});
const disabled = Object.keys(WIDGETS).filter(id => !WIDGETS[id].area);
if (disabled.length) {
menu[" == "+/*LANG*/"Disabled"+" == "] = undefined;
disabled.forEach(function(id) {
menu[name(id)+" *"] = () => edit(id);
});
}
if (Object.keys(_WIDGETS).length) { // only show reset if there is anything to reset
menu[/*LANG*/"Reset All"] = () => {
E.showPrompt(/*LANG*/"Reset all widgets?").then(confirm => {
@ -196,15 +146,9 @@
delete settings.custom;
require("Storage").writeJSON("wid_edit.json", settings);
for(let id in _WIDGETS) {
if (WIDGETS[id].area) Object.assign(WIDGETS[id], _WIDGETS[id]) // restore defaults
else WIDGETS[id] = _WIDGETS[id]; // restore widget
Object.assign(WIDGETS[id], _WIDGETS[id]) // restore defaults
}
global._WIDGETS = {};
for(let WIDGET of WIDGETS) {
// drawWidgets won't clear e.g. bottom bar if there are no bottom widgets because we just moved them away
// (area has been reset by now, but x/y not yet)
if (WIDGET.width) g.reset().clearRect(WIDGET.x, WIDGET.y, WIDGET.x+WIDGET.width-1, WIDGET.y+23);
}
redrawWidgets();
}
mainMenu(); // reload with reset widgets