From e1260031b6195442247ea73fa906fb07eb87bfda Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 4 Feb 2009 00:50:04 -0600 Subject: [PATCH] Grid layout now respects baselines when fill attribute is off Paragraph gadgets now compute a baseline --- basis/ui/gadgets/grids/grids-tests.factor | 44 ++++++++- basis/ui/gadgets/grids/grids.factor | 99 ++++++++++++------- basis/ui/gadgets/paragraphs/paragraphs.factor | 5 + 3 files changed, 113 insertions(+), 35 deletions(-) diff --git a/basis/ui/gadgets/grids/grids-tests.factor b/basis/ui/gadgets/grids/grids-tests.factor index 9015b7ec1b..0b73de39c1 100644 --- a/basis/ui/gadgets/grids/grids-tests.factor +++ b/basis/ui/gadgets/grids/grids-tests.factor @@ -1,10 +1,14 @@ USING: ui.gadgets ui.gadgets.grids tools.test kernel arrays -namespaces math.geometry.rect accessors ; +namespaces math.geometry.rect accessors ui.gadgets.grids.private +ui.gadgets.debug sequences ; IN: ui.gadgets.grids.tests +[ { { { 1 "a" } { 1 "b" } } { { 2 "a" } { 2 "b" } } } ] +[ { 1 2 } { "a" "b" } cross-zip ] unit-test + [ { 0 0 } ] [ { } pref-dim ] unit-test -: 100x100 { 100 100 } >>dim ; +: 100x100 ( -- gadget ) { 100 100 } >>dim ; [ { 100 100 } ] [ 100x100 @@ -45,3 +49,39 @@ IN: ui.gadgets.grids.tests "a" get rect-dim "b" get rect-dim ] unit-test + +[ ] [ + 100x100 dup "a" set + 100x100 dup "b" set + 100x100 dup "c" set + [ 1array ] tri@ 3array + + { 10 10 } >>gap "g" set +] unit-test + +[ ] [ "g" get prefer ] unit-test +[ ] [ "g" get layout ] unit-test + +[ { 10 10 } ] [ "a" get loc>> ] unit-test +[ { 100 100 } ] [ "a" get dim>> ] unit-test + +[ { 10 120 } ] [ "b" get loc>> ] unit-test +[ { 100 100 } ] [ "b" get dim>> ] unit-test + +[ { 10 230 } ] [ "c" get loc>> ] unit-test +[ { 100 100 } ] [ "c" get dim>> ] unit-test + +5 { 10 10 } +10 { 10 10 } 2array +1array f >>fill? +"g" set + +[ ] [ "g" get prefer ] unit-test + +[ { 20 15 } ] [ "g" get dim>> ] unit-test + +[ V{ { 0 5 } { 10 0 } } ] [ + "g" get + dup layout + children>> [ loc>> ] map +] unit-test \ No newline at end of file diff --git a/basis/ui/gadgets/grids/grids.factor b/basis/ui/gadgets/grids/grids.factor index 4d8d053a20..bc1c24aaed 100644 --- a/basis/ui/gadgets/grids/grids.factor +++ b/basis/ui/gadgets/grids/grids.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2006, 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: arrays kernel math namespaces make sequences words io +USING: arrays kernel math math.order namespaces make sequences words io math.vectors ui.gadgets columns accessors strings.tables math.geometry.rect locals fry ; IN: ui.gadgets.grids @@ -8,8 +8,7 @@ IN: ui.gadgets.grids TUPLE: grid < gadget grid { gap initial: { 0 0 } } -{ fill? initial: t } -align ; +{ fill? initial: t } ; : new-grid ( children class -- grid ) new-gadget @@ -27,50 +26,82 @@ align ; : grid-remove ( grid i j -- grid ) [ ] 2dip grid-add ; -: pref-dim-grid ( grid -- dims ) - grid>> [ [ pref-dim ] map ] map ; + ( gadget -- cell ) [ pref-dim ] [ baseline ] bi cell boa ; -: pair-up ( horiz vert -- dims ) - [ [ (pair-up) ] curry map ] with map ; +M: cell baseline baseline>> ; -: add-gaps ( gap seq -- newseq ) - [ v+ ] with map ; +TUPLE: grid-layout grid gap fill? row-heights column-widths ; -: gap-sum ( gap seq -- newseq ) - dupd add-gaps dim-sum v+ ; +: iterate-cell-dims ( cells quot -- seq ) + '[ [ pref-dim>> @ ] [ max ] map-reduce ] map ; inline + +: row-heights ( grid-layout -- heights ) + [ grid>> ] [ fill?>> ] bi + [ [ second ] iterate-cell-dims ] + [ [ dup [ pref-dim>> ] map baseline-metrics + ] map ] + if ; + +: column-widths ( grid-layout -- widths ) + grid>> flip [ first ] iterate-cell-dims ; + +: ( grid -- grid-layout ) + grid-layout new + swap + [ grid>> [ [ ] map ] map >>grid ] + [ fill?>> >>fill? ] + [ gap>> >>gap ] + tri + dup row-heights >>row-heights + dup column-widths >>column-widths ; + +: accumulate-cell-dims ( seq gap -- n ns ) + dup '[ + _ + ] accumulate ; + +: accumulate-cell-xs ( grid-layout -- x xs ) + [ column-widths>> ] [ gap>> first ] bi + accumulate-cell-dims ; + +: accumulate-cell-ys ( grid-layout -- y ys ) + [ row-heights>> ] [ gap>> second ] bi + accumulate-cell-dims ; M: grid pref-dim* - [ gap>> ] [ compute-grid [ gap-sum ] bi-curry@ ] bi bi (pair-up) ; + + [ accumulate-cell-xs drop ] + [ accumulate-cell-ys drop ] + bi 2array ; -: do-grid ( dims grid quot -- ) - [ grid>> ] dip '[ _ 2each ] 2each ; inline +: (compute-cell-locs) ( grid-layout -- locs ) + [ accumulate-cell-xs nip ] + [ accumulate-cell-ys nip ] + bi cross-zip flip ; -: grid-positions ( grid dims -- locs ) - [ gap>> dup ] dip add-gaps swap [ v+ ] accumulate nip ; +: adjust-for-baseline ( row-locs row-cells -- row-locs' ) + baseline-align [ 0 swap 2array v+ ] 2map ; -: position-grid ( grid horiz vert -- ) - pick [ [ grid-positions ] bi-curry@ bi pair-up ] dip - [ (>>loc) ] do-grid ; - -: resize-grid ( grid horiz vert -- ) - pick fill?>> [ - pair-up swap [ (>>dim) ] do-grid - ] [ - 2drop grid>> [ [ prefer ] each ] each +: cell-locs ( grid-layout -- locs ) + dup fill?>> + [ (compute-cell-locs) ] [ + [ (compute-cell-locs) ] [ grid>> ] bi + [ adjust-for-baseline ] 2map ] if ; -: grid-layout ( grid horiz vert -- ) - [ position-grid ] [ resize-grid ] 3bi ; +: cell-dims ( grid-layout -- dims ) + dup fill?>> + [ [ column-widths>> ] [ row-heights>> ] bi cross-zip flip ] + [ grid>> [ [ pref-dim>> ] map ] map ] + if ; -M: grid layout* dup compute-grid grid-layout ; +M: grid layout* + [ grid>> ] [ [ cell-locs ] [ cell-dims ] bi ] bi + [ [ [ >>loc ] [ >>dim ] bi* drop ] 3each ] 3each ; M: grid children-on ( rect gadget -- seq ) dup children>> empty? [ 2drop f ] [ @@ -83,3 +114,5 @@ M: grid gadget-text* grid>> [ [ gadget-text ] map ] map format-table [ CHAR: \n , ] [ % ] interleave ; + +PRIVATE> \ No newline at end of file diff --git a/basis/ui/gadgets/paragraphs/paragraphs.factor b/basis/ui/gadgets/paragraphs/paragraphs.factor index deea011b4f..e8e07d798b 100644 --- a/basis/ui/gadgets/paragraphs/paragraphs.factor +++ b/basis/ui/gadgets/paragraphs/paragraphs.factor @@ -74,4 +74,9 @@ M: paragraph layout* wrap-paragraph dup line-y-coordinates [ layout-line ] 2each ; +M: paragraph baseline + children>> [ 0 ] [ + first [ loc>> second ] [ baseline ] bi + + ] if-empty ; + PRIVATE> \ No newline at end of file