1 ! Copyright (C) 2006, 2009 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors fonts kernel locals math math.order
4 math.rectangles math.vectors models namespaces opengl sequences
5 ui.commands ui.gadgets ui.gadgets.labels ui.gadgets.packs
6 ui.gadgets.presentations ui.gadgets.scrollers
7 ui.gadgets.viewports ui.gestures ui.render ui.theme ;
10 TUPLE: list < pack index presenter color hook ;
12 : list-theme ( list -- list )
13 selection-color >>color ; inline
15 : <list> ( hook presenter model -- gadget )
25 : calc-bounded-index ( n list -- m )
26 control-value length 1 - min 0 max ;
28 : bound-index ( list -- )
29 dup index>> over calc-bounded-index >>index drop ;
31 : list-presentation-hook ( list -- quot )
32 hook>> [ [ list? ] find-parent ] prepend ;
34 : <list-presentation> ( hook elt presenter -- gadget )
35 [ call( elt -- obj ) ] [ drop ] 2bi
36 [ >label monospace-font >>font ] dip
40 : <list-items> ( list -- seq )
41 [ list-presentation-hook ]
45 [ 2dup ] dip swap <list-presentation>
51 dup <list-items> add-gadgets
54 : selected-rect ( list -- rect )
55 dup index>> swap children>> ?nth ;
61 rect-bounds gl-fill-rect
65 M: list focusable-child* drop t ;
67 : list-value ( list -- object )
68 dup index>> swap control-value ?nth ;
70 : scroll>selected ( list -- )
71 ! We change the rectangle's width to zero to avoid
73 [ selected-rect rect-bounds { 0 1 } v* <rect> ] keep
76 : list-empty? ( list -- ? ) control-value empty? ;
78 : select-index ( n list -- )
82 [ control-value length rem ] [ index<< ] [ ] tri
83 [ relayout-1 ] [ scroll>selected ] bi
86 : select-previous ( list -- )
87 [ index>> 1 - ] keep select-index ;
89 : select-next ( list -- )
90 [ index>> 1 + ] keep select-index ;
92 : invoke-value-action ( list -- )
94 dup hook>> call( list -- )
96 [ index>> ] keep nth-gadget invoke-secondary
99 :: select-gadget ( gadget list -- )
100 gadget list children>> index
101 [ list select-index ] when* ;
103 : clamp-loc ( point max -- point )
104 { 0 0 } swap vclamp ;
106 : select-at ( point list -- )
107 [ dim>> clamp-loc ] keep
111 : list-page ( list vec -- )
112 [ dup selected-rect rect-bounds 2 v/n v+ over visible-dim ] dip
113 v* v+ swap select-at ;
115 : list-page-up ( list -- ) { 0 -1 } list-page ;
117 : list-page-down ( list -- ) { 0 1 } list-page ;
119 list "keyboard-navigation" "Lists can be navigated from the keyboard." {
120 { T{ button-down } request-focus }
121 { T{ key-down f f "UP" } select-previous }
122 { T{ key-down f f "DOWN" } select-next }
123 { T{ key-down f f "PAGE_UP" } list-page-up }
124 { T{ key-down f f "PAGE_DOWN" } list-page-down }
125 { T{ key-down f f "RET" } invoke-value-action }