1
0
Fork 0

Ensure padding works correctly (fix #819), also add Layout test harness basics

master
Gordon Williams 2021-09-23 12:47:12 +01:00
parent 3b8c365b0f
commit 0109a8114b
7 changed files with 58 additions and 9 deletions

3
.gitignore vendored
View File

@ -7,4 +7,5 @@ appdates.csv
.vscode .vscode
.idea/ .idea/
_config.yml _config.yml
tests/Layout/bin/tmp.*
tests/Layout/testresult.bmp

View File

@ -237,14 +237,13 @@ Layout.prototype.layout = function (l) {
l.c.forEach(c => { l.c.forEach(c => {
c.w = c._w + ((0|c.fillx)*(l.w-l._w)/(fillx||1)); c.w = c._w + ((0|c.fillx)*(l.w-l._w)/(fillx||1));
c.h = c.filly ? l.h : c._h; 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) { if (c.pad) {
x += c.pad*2;
c.w += c.pad*2; c.w += c.pad*2;
c.h += 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); if (c.c) this.layout(c);
}); });
break; break;
@ -256,14 +255,13 @@ Layout.prototype.layout = function (l) {
l.c.forEach(c => { l.c.forEach(c => {
c.w = c.fillx ? l.w : c._w; c.w = c.fillx ? l.w : c._w;
c.h = c._h + ((0|c.filly)*(l.h-l._h)/(filly||1)); 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) { if (c.pad) {
y += c.pad*2;
c.w += c.pad*2; c.w += c.pad*2;
c.h += 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); if (c.c) this.layout(c);
}); });
break; break;
@ -274,6 +272,8 @@ Layout.prototype.debug = function(l,c) {
if (!l) l = this._l; if (!l) l = this._l;
c=c||1; 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); 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++; c++;
if (l.c) l.c.forEach(n => this.debug(n,c)); if (l.c) l.c.forEach(n => this.debug(n,c));
}; };

BIN
tests/Layout/bin/espruino Executable file

Binary file not shown.

View File

@ -0,0 +1,5 @@
#!/bin/bash
cd `dirname $0`
cd ../tests
ls *.js | xargs ../bin/runtest.sh

40
tests/Layout/bin/runtest.sh Executable file
View File

@ -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

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -0,0 +1,3 @@
var layout = new Layout({type:"v", fillx: true, c: [
{type: "txt", font: "10%", halign:1, pad: 20, label: "0123456789"},
]});