Add support for Joe's "graphics baseline" idea
parent
eba330f1b1
commit
4d69475cac
|
@ -25,7 +25,8 @@ M: border pref-dim*
|
||||||
[ min-dim>> ] bi vmax ;
|
[ min-dim>> ] bi vmax ;
|
||||||
|
|
||||||
M: border baseline
|
M: border baseline
|
||||||
[ size>> second ] [ gadget-child baseline ] bi + ;
|
[ size>> second ] [ gadget-child baseline ] bi
|
||||||
|
dup [ + ] [ nip ] if ;
|
||||||
|
|
||||||
: border-major-dim ( border -- dim )
|
: border-major-dim ( border -- dim )
|
||||||
[ dim>> ] [ size>> 2 v*n ] bi v- ;
|
[ dim>> ] [ size>> 2 v*n ] bi v- ;
|
||||||
|
|
|
@ -164,3 +164,12 @@ M: mock-gadget ungraft*
|
||||||
|
|
||||||
\ graft* must-infer
|
\ graft* must-infer
|
||||||
\ ungraft* must-infer
|
\ ungraft* must-infer
|
||||||
|
|
||||||
|
! Test baseline calculations
|
||||||
|
[ 10 ] [ 0 10 0 combine-baseline-metrics + ] unit-test
|
||||||
|
[ 15 ] [ 0 10 5 combine-baseline-metrics + ] unit-test
|
||||||
|
[ 30 ] [ 30 0 0 combine-baseline-metrics + ] unit-test
|
||||||
|
[ 35 ] [ 10 0 30 combine-baseline-metrics + ] unit-test
|
||||||
|
[ 20 ] [ 5 10 10 combine-baseline-metrics + ] unit-test
|
||||||
|
[ 20 ] [ 20 10 0 combine-baseline-metrics + ] unit-test
|
||||||
|
[ 55 ] [ 20 10 40 combine-baseline-metrics + ] unit-test
|
|
@ -3,7 +3,7 @@
|
||||||
USING: accessors arrays hashtables kernel models math namespaces
|
USING: accessors arrays hashtables kernel models math namespaces
|
||||||
make sequences quotations math.vectors combinators sorting
|
make sequences quotations math.vectors combinators sorting
|
||||||
binary-search vectors dlists deques models threads
|
binary-search vectors dlists deques models threads
|
||||||
concurrency.flags math.order math.rectangles fry ;
|
concurrency.flags math.order math.rectangles fry locals ;
|
||||||
IN: ui.gadgets
|
IN: ui.gadgets
|
||||||
|
|
||||||
! Values for orientation slot
|
! Values for orientation slot
|
||||||
|
@ -197,19 +197,49 @@ SYMBOL: +baseline+
|
||||||
|
|
||||||
GENERIC: baseline ( gadget -- y )
|
GENERIC: baseline ( gadget -- y )
|
||||||
|
|
||||||
M: gadget baseline pref-dim second ;
|
M: gadget baseline drop f ;
|
||||||
|
|
||||||
: (baseline-align) ( baselines -- ys )
|
: (max-ascent-and-descent) ( accum baseline height -- accum' )
|
||||||
[ { } ] [ [ supremum ] keep [ - ] with map ] if-empty ;
|
over [ over - 2array vmax ] [ 2drop ] if ;
|
||||||
|
|
||||||
: baseline-align ( gadgets -- ys )
|
: max-ascent-and-descent ( baselines heights -- ascent descent )
|
||||||
[ baseline ] map (baseline-align) ;
|
{ 0 0 } [ (max-ascent-and-descent) ] 2reduce first2 ;
|
||||||
|
|
||||||
: (baseline-metrics) ( baselines sizes -- ascent descent )
|
: max-height-with-baseline ( baselines heights -- y )
|
||||||
{ 0 0 } [ second over - 2array vmax ] 2reduce first2 ;
|
0 [ swap [ max ] [ drop ] if ] 2reduce ;
|
||||||
|
|
||||||
|
: max-height-without-baseline ( baselines heights -- y )
|
||||||
|
0 [ swap [ drop ] [ max ] if ] 2reduce ;
|
||||||
|
|
||||||
|
:: baseline-align ( gadgets -- ys )
|
||||||
|
gadgets [ [ baseline ] map ] [ [ pref-dim second ] map ] bi
|
||||||
|
over 0 [ [ max ] when* ] reduce :> max-baseline
|
||||||
|
2dup max-height-without-baseline :> max-height-without-baseline
|
||||||
|
max-height-without-baseline max-baseline [-] 2 /i :> offset-with-baseline
|
||||||
|
max-baseline max-height-without-baseline [-] 2 /i :> offset-without-baseline
|
||||||
|
[
|
||||||
|
over [
|
||||||
|
drop
|
||||||
|
max-baseline
|
||||||
|
offset-with-baseline
|
||||||
|
] [
|
||||||
|
nip
|
||||||
|
max-height-without-baseline
|
||||||
|
offset-without-baseline
|
||||||
|
] if [ swap - ] dip +
|
||||||
|
] 2map ;
|
||||||
|
|
||||||
|
: combine-baseline-metrics ( height ascent descent -- ascent' descent' )
|
||||||
|
[ [ [-] 2 /i ] keep ] dip [ + ] [ max ] bi-curry* bi ;
|
||||||
|
|
||||||
: baseline-metrics ( children sizes -- ascent descent )
|
: baseline-metrics ( children sizes -- ascent descent )
|
||||||
[ [ baseline ] map ] dip (baseline-metrics) ;
|
#! Consider gadgets with a baseline and those without separately.
|
||||||
|
[ [ baseline ] map ] [ [ second ] map ] bi*
|
||||||
|
[ max-height-without-baseline ] [ max-ascent-and-descent ] 2bi
|
||||||
|
combine-baseline-metrics ;
|
||||||
|
|
||||||
|
: baseline-height ( children sizes -- height )
|
||||||
|
baseline-metrics + ;
|
||||||
|
|
||||||
GENERIC: layout* ( gadget -- )
|
GENERIC: layout* ( gadget -- )
|
||||||
|
|
||||||
|
|
|
@ -50,7 +50,7 @@ TUPLE: grid-layout grid gap fill? row-heights column-widths ;
|
||||||
: row-heights ( grid-layout -- heights )
|
: row-heights ( grid-layout -- heights )
|
||||||
[ grid>> ] [ fill?>> ] bi
|
[ grid>> ] [ fill?>> ] bi
|
||||||
[ [ second ] iterate-cell-dims ]
|
[ [ second ] iterate-cell-dims ]
|
||||||
[ [ dup [ pref-dim>> ] map baseline-metrics + ] map ]
|
[ [ dup [ pref-dim>> ] map baseline-height ] map ]
|
||||||
if ;
|
if ;
|
||||||
|
|
||||||
: column-widths ( grid-layout -- widths )
|
: column-widths ( grid-layout -- widths )
|
||||||
|
|
|
@ -20,6 +20,7 @@ IN: ui.gadgets.packs.tests
|
||||||
orient
|
orient
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
|
! Test baseline alignment
|
||||||
<shelf> +baseline+ >>align
|
<shelf> +baseline+ >>align
|
||||||
5 { 10 10 } <baseline-gadget> add-gadget
|
5 { 10 10 } <baseline-gadget> add-gadget
|
||||||
10 { 10 10 } <baseline-gadget> add-gadget
|
10 { 10 10 } <baseline-gadget> add-gadget
|
||||||
|
@ -33,4 +34,44 @@ IN: ui.gadgets.packs.tests
|
||||||
"g" get
|
"g" get
|
||||||
dup layout
|
dup layout
|
||||||
children>> [ loc>> ] map
|
children>> [ loc>> ] map
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
! Test mixed baseline and ordinary alignment
|
||||||
|
<shelf> +baseline+ >>align
|
||||||
|
<gadget> { 20 20 } >>dim add-gadget
|
||||||
|
10 { 10 10 } <baseline-gadget> add-gadget
|
||||||
|
"g" set
|
||||||
|
|
||||||
|
[ { 30 20 } ] [ "g" get pref-dim ] unit-test
|
||||||
|
|
||||||
|
[ ] [ "g" get layout ] unit-test
|
||||||
|
|
||||||
|
[ V{ { 0 0 } { 20 5 } } ] [
|
||||||
|
"g" get children>> [ loc>> ] map
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
<shelf> +baseline+ >>align
|
||||||
|
<gadget> { 15 15 } >>dim add-gadget
|
||||||
|
5 { 10 10 } <baseline-gadget> add-gadget
|
||||||
|
"g" set
|
||||||
|
|
||||||
|
[ { 25 15 } ] [ "g" get pref-dim ] unit-test
|
||||||
|
|
||||||
|
[ ] [ "g" get layout ] unit-test
|
||||||
|
|
||||||
|
[ V{ { 0 0 } { 15 5 } } ] [
|
||||||
|
"g" get children>> [ loc>> ] map
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
<shelf> +baseline+ >>align
|
||||||
|
<gadget> { 20 20 } >>dim add-gadget
|
||||||
|
30 { 10 50 } <baseline-gadget> add-gadget
|
||||||
|
"g" set
|
||||||
|
|
||||||
|
[ { 30 50 } ] [ "g" get pref-dim ] unit-test
|
||||||
|
|
||||||
|
[ ] [ "g" get layout ] unit-test
|
||||||
|
|
||||||
|
[ V{ { 0 5 } { 20 0 } } ] [
|
||||||
|
"g" get children>> [ loc>> ] map
|
||||||
] unit-test
|
] unit-test
|
|
@ -66,7 +66,7 @@ PRIVATE>
|
||||||
|
|
||||||
: max-pack-dim ( pack sizes -- dim )
|
: max-pack-dim ( pack sizes -- dim )
|
||||||
over align>> +baseline+ eq?
|
over align>> +baseline+ eq?
|
||||||
[ [ children>> ] dip baseline-metrics + 0 swap 2array ] [ nip max-dim ] if ;
|
[ [ children>> ] dip baseline-height 0 swap 2array ] [ nip max-dim ] if ;
|
||||||
|
|
||||||
: pack-pref-dim ( pack sizes -- dim )
|
: pack-pref-dim ( pack sizes -- dim )
|
||||||
[ max-pack-dim ]
|
[ max-pack-dim ]
|
||||||
|
@ -81,7 +81,7 @@ M: pack pref-dim*
|
||||||
children>> [ 0 ] [ first baseline ] if-empty ;
|
children>> [ 0 ] [ first baseline ] if-empty ;
|
||||||
|
|
||||||
: horizontal-baseline ( pack -- y )
|
: horizontal-baseline ( pack -- y )
|
||||||
children>> [ baseline ] [ max ] map-reduce ;
|
children>> dup pref-dims baseline-metrics drop ;
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
|
|
|
@ -29,10 +29,10 @@ TUPLE: paragraph < gadget margin ;
|
||||||
: gadget>word ( gadget -- word )
|
: gadget>word ( gadget -- word )
|
||||||
[ ] [ pref-dim first ] [ word-break? ] tri <word> ;
|
[ ] [ pref-dim first ] [ word-break? ] tri <word> ;
|
||||||
|
|
||||||
TUPLE: line words ascent descent ;
|
TUPLE: line words height ;
|
||||||
|
|
||||||
: <line> ( words -- line )
|
: <line> ( words -- line )
|
||||||
dup [ key>> ] map dup pref-dims baseline-metrics line boa ;
|
dup [ key>> ] map dup pref-dims baseline-height line boa ;
|
||||||
|
|
||||||
: wrap-paragraph ( paragraph -- wrapped-paragraph )
|
: wrap-paragraph ( paragraph -- wrapped-paragraph )
|
||||||
[ children>> [ gadget>word ] map ] [ margin>> ] bi
|
[ children>> [ gadget>word ] map ] [ margin>> ] bi
|
||||||
|
@ -44,17 +44,14 @@ TUPLE: line words ascent descent ;
|
||||||
: max-line-width ( wrapped-paragraph -- x )
|
: max-line-width ( wrapped-paragraph -- x )
|
||||||
[ words>> line-width ] [ max ] map-reduce ;
|
[ words>> line-width ] [ max ] map-reduce ;
|
||||||
|
|
||||||
: line-height ( wrapped-line -- ys )
|
|
||||||
[ ascent>> ] [ descent>> ] bi + ;
|
|
||||||
|
|
||||||
: sum-line-heights ( wrapped-paragraph -- y )
|
: sum-line-heights ( wrapped-paragraph -- y )
|
||||||
[ line-height ] sigma ;
|
[ height>> ] sigma ;
|
||||||
|
|
||||||
M: paragraph pref-dim*
|
M: paragraph pref-dim*
|
||||||
wrap-paragraph [ max-line-width ] [ sum-line-heights ] bi 2array ;
|
wrap-paragraph [ max-line-width ] [ sum-line-heights ] bi 2array ;
|
||||||
|
|
||||||
: line-y-coordinates ( wrapped-paragraph -- ys )
|
: line-y-coordinates ( wrapped-paragraph -- ys )
|
||||||
0 [ line-height + ] accumulate nip ;
|
0 [ height>> + ] accumulate nip ;
|
||||||
|
|
||||||
: word-x-coordinates ( wrapped-line -- xs )
|
: word-x-coordinates ( wrapped-line -- xs )
|
||||||
0 [ width>> + ] accumulate nip ;
|
0 [ width>> + ] accumulate nip ;
|
||||||
|
@ -75,8 +72,11 @@ M: paragraph layout*
|
||||||
[ layout-line ] 2each ;
|
[ layout-line ] 2each ;
|
||||||
|
|
||||||
M: paragraph baseline
|
M: paragraph baseline
|
||||||
children>> [ 0 ] [
|
wrap-paragraph [ f ] [
|
||||||
first [ loc>> second ] [ baseline ] bi +
|
first words>>
|
||||||
|
[ key>> ] map
|
||||||
|
dup [ pref-dim ] map
|
||||||
|
baseline-metrics drop
|
||||||
] if-empty ;
|
] if-empty ;
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
|
@ -146,7 +146,7 @@ GENERIC# accept-completion-hook 1 ( item popup -- )
|
||||||
monospace-font >>font
|
monospace-font >>font
|
||||||
t >>selection-required?
|
t >>selection-required?
|
||||||
transparent >>column-line-color
|
transparent >>column-line-color
|
||||||
0 >>gap
|
2 >>gap
|
||||||
dup '[ _ accept-completion ] >>action ;
|
dup '[ _ accept-completion ] >>action ;
|
||||||
|
|
||||||
: <completion-scroller> ( completion-popup -- scroller )
|
: <completion-scroller> ( completion-popup -- scroller )
|
||||||
|
|
Loading…
Reference in New Issue