Espruino/libs/js/graphical_menu.min.js
Gordon Williams 6652295b4f Bangle.js/Pixl.js: Updated E.showMenu
Bangle.js: E.showMenu now has some colour, bigger, with next/prev indicators, and works with widgets
2020-02-10 15:14:18 +00:00

4 lines
1.8 KiB
JavaScript

exports.list=function(a,p){var b=p[""],e=Object.keys(p);b&&e.splice(e.indexOf(""),1);b instanceof Object||(b={});void 0===b.selected&&(b.selected=0);b.fontHeight||(b.fontHeight=6);var q=0|b.x,m=b.x2||a.getWidth()-1,l=0|b.y,v=b.y2||a.getHeight()-1;b.title&&(l+=b.fontHeight+2);var w=b.cB||0,n=b.cF;void 0===n&&(n=-1);var r=b.cHB;void 0===r&&(r=-1);var u=b.cHF||0,d={draw:function(){a.setColor(n);b.predraw&&b.predraw(a);a.setFontAlign(0,-1);b.title&&(a.drawString(b.title,(q+m)/2,l-b.fontHeight-
2),a.drawLine(q,l-2,m,l-2));for(var t=0|Math.min((v-l)/b.fontHeight,e.length),c=E.clip(b.selected-(t>>1),0,e.length-t),f=l,x=0<c;t--;){var h=e[c],g=p[h],k=c==b.selected&&!d.selectEdit;a.setColor(k?r:w);a.fillRect(q,f,m,f+b.fontHeight-1);a.setColor(k?u:n);a.setFontAlign(-1,-1);a.drawString(h,q,f);"object"==typeof g&&(h=m,k=g.value,g.format&&(k=g.format(k)),d.selectEdit&&c==b.selected&&(g=10<b.fontHeight?2:1,h-=12*g+1,a.setColor(r),a.fillRect(h-(a.stringWidth(k)+4),f,m,f+b.fontHeight-1),a.setColor(u),
a.drawImage({width:12,height:5,buffer:" \u0007\x00\u00f9\u00f0\u000e\x00@",transparent:0},h,f+(b.fontHeight-5*g)/2,{scale:g})),a.setFontAlign(1,-1),a.drawString(k,h-2,f));a.setColor(n);f+=b.fontHeight;c++}a.setFontAlign(-1,-1);b.preflip&&b.preflip(a,x,c<e.length);a.flip&&a.flip()},select:function(a){a=p[e[b.selected]];if("function"==typeof a)a(d);else if("object"==typeof a){if("number"==typeof a.value)d.selectEdit=d.selectEdit?void 0:a;else if("boolean"==typeof a.value&&(a.value=!a.value),a.onchange)a.onchange(a.value);
d.draw()}},move:function(a){if(d.selectEdit){var c=d.selectEdit;c.value-=(a||1)*(c.step||1);void 0!==c.min&&c.value<c.min&&(c.value=c.min);void 0!==c.max&&c.value>c.max&&(c.value=c.max);if(c.onchange)c.onchange(c.value)}else b.selected=(a+b.selected)%e.length,0>b.selected&&(b.selected+=e.length);d.draw()}};d.draw();return d}