Espruino/libs/js/graphical_menu.min.js
Gordon Williams 529fb0779e Bangle.js: setUI now has a 'redraw' argument to allow apps to provide a way to redraw themselves
Also fixes for scroller.isActive (and redraw checks) that break if there is a back icon
2023-06-20 11:00:23 +01:00

4 lines
1.9 KiB
JavaScript

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