mirror of
https://github.com/espruino/Espruino.git
synced 2025-12-08 19:06:15 +00:00
4 lines
1.5 KiB
JavaScript
4 lines
1.5 KiB
JavaScript
exports.list=function(a,m){var b=m[""],e=Object.keys(m);b&&e.splice(e.indexOf(""),1);b instanceof Object||(b={});void 0===b.selected&&(b.selected=0);b.fontHeight||(b.fontHeight=6);var k=0|b.x,n=b.x2||a.getWidth()-1,g=0|b.y,q=b.y2||a.getHeight()-1;b.title&&(g+=b.fontHeight+2);var d={draw:function(){a.clear();a.setColor(-1);a.setFontAlign(0,-1);b.predraw&&b.predraw(a);b.title&&(a.drawString(b.title,(k+n)/2,g-b.fontHeight-2),a.drawLine(k,g-2,n,g-2));for(var p=0|Math.min((q-g)/b.fontHeight,
|
|
e.length),c=E.clip(b.selected-(p>>1),0,e.length-p),h=g;p--;){var f=k;c==b.selected&&(a.fillRect(k,h,n,h+b.fontHeight-1),a.setColor(0),d.selectEdit&&(a.drawImage({width:12,height:5,buffer:" \u0007\x00\u00f9\u00f0\u000e\x00@",transparent:0},k,h),f+=15));a.setFontAlign(-1,-1);var l=e[c++];a.drawString(l,f,h);f=m[l];"object"==typeof f&&(a.setFontAlign(1,-1),l=f.value,a.drawString(f.format?f.format(l):l,n,h));a.setColor(-1);h+=b.fontHeight}a.setFontAlign(-1,-1);b.preflip&&b.preflip(a);a.flip&&a.flip()},
|
|
select:function(a){a=m[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=0|E.clip(b.selected+a,
|
|
0,e.length-1);d.draw()}};d.draw();return d} |