diff --git a/.gitignore b/.gitignore index d2001bf41..47233d1f5 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ appdates.csv .vscode .idea/ _config.yml - +tests/Layout/bin/tmp.* +tests/Layout/testresult.bmp diff --git a/modules/Layout.js b/modules/Layout.js index caf63f0c4..521b191b6 100644 --- a/modules/Layout.js +++ b/modules/Layout.js @@ -237,14 +237,13 @@ Layout.prototype.layout = function (l) { l.c.forEach(c => { c.w = c._w + ((0|c.fillx)*(l.w-l._w)/(fillx||1)); c.h = c.filly ? l.h : c._h; - c.x = x; - c.y = l.y + (1+(0|c.valign))*(l.h-c.h)/2; - x += c.w; if (c.pad) { - x += c.pad*2; c.w += c.pad*2; c.h += c.pad*2; } + c.x = x; + c.y = l.y + (1+(0|c.valign))*(l.h-c.h)/2; + x += c.w; if (c.c) this.layout(c); }); break; @@ -256,14 +255,13 @@ Layout.prototype.layout = function (l) { l.c.forEach(c => { c.w = c.fillx ? l.w : c._w; c.h = c._h + ((0|c.filly)*(l.h-l._h)/(filly||1)); - c.x = l.x + (1+(0|c.halign))*(l.w-c.w)/2; - c.y = y; - y += c.h; if (c.pad) { - y += c.pad*2; c.w += c.pad*2; c.h += c.pad*2; } + c.y = y; + c.x = l.x + (1+(0|c.halign))*(l.w-c.w)/2; + y += c.h; if (c.c) this.layout(c); }); break; @@ -274,6 +272,8 @@ Layout.prototype.debug = function(l,c) { if (!l) l = this._l; c=c||1; g.setColor(c&1,c&2,c&4).drawRect(l.x+c-1, l.y+c-1, l.x+l.w-c, l.y+l.h-c); + if (l.pad) + g.drawRect(l.x+l.pad-1, l.y+l.pad-1, l.x+l.w-l.pad, l.y+l.h-l.pad); c++; if (l.c) l.c.forEach(n => this.debug(n,c)); }; diff --git a/tests/Layout/bin/espruino b/tests/Layout/bin/espruino new file mode 100755 index 000000000..29d44789b Binary files /dev/null and b/tests/Layout/bin/espruino differ diff --git a/tests/Layout/bin/runalltests.sh b/tests/Layout/bin/runalltests.sh new file mode 100755 index 000000000..99a834d90 --- /dev/null +++ b/tests/Layout/bin/runalltests.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +cd `dirname $0` +cd ../tests +ls *.js | xargs ../bin/runtest.sh diff --git a/tests/Layout/bin/runtest.sh b/tests/Layout/bin/runtest.sh new file mode 100755 index 000000000..72071a63b --- /dev/null +++ b/tests/Layout/bin/runtest.sh @@ -0,0 +1,40 @@ +#!/bin/bash +# Requires Linux x64 (for ./espruino) +# Also imagemagick for display + +cd `dirname $0` +if [ "$#" -ne 1 ]; then + echo "USAGE:" + echo " bin/runtest.sh testxyz.js" + exit 1 +fi + +# temporary test files +TESTJS=tmp.js +TESTBMP=tmp.bmp +# actual source files +SRCDIR=../tests/ +SRCJS=$1 +SRCBMP=`basename $SRCJS .js`.bmp +echo "TEST $SRCJS ($SRCBMP)" + +cat ../../../modules/Layout.js > $TESTJS +echo 'g = Graphics.createArrayBuffer(176,176,4);' >> $TESTJS +cat $SRCDIR/$SRCJS >> $TESTJS || exit 1 +echo 'layout.render()' >> $TESTJS +echo 'layout.debug()' >> $TESTJS +echo 'require("fs").writeFileSync("'$TESTBMP'",g.asBMP())' >> $TESTJS + +./espruino $TESTJS || exit 1 +if ! cmp $TESTBMP $SRCDIR/$SRCBMP >/dev/null 2>&1 +then + echo Files differ + convert "+append" $TESTBMP $SRCDIR/$SRCBMP ../testresult.bmp + display ../testresult.bmp + exit 1 +else + echo Files are the same + exit 0 +fi + + diff --git a/tests/Layout/tests/padding_issue819.bmp b/tests/Layout/tests/padding_issue819.bmp new file mode 100644 index 000000000..82bcb9acb Binary files /dev/null and b/tests/Layout/tests/padding_issue819.bmp differ diff --git a/tests/Layout/tests/padding_issue819.js b/tests/Layout/tests/padding_issue819.js new file mode 100644 index 000000000..54641d3f9 --- /dev/null +++ b/tests/Layout/tests/padding_issue819.js @@ -0,0 +1,3 @@ +var layout = new Layout({type:"v", fillx: true, c: [ + {type: "txt", font: "10%", halign:1, pad: 20, label: "0123456789"}, +]});