]> gitweb.factorcode.org Git - factor.git/blob - extra/ui/gadgets/lists/lists.factor
Switch to https urls
[factor.git] / extra / ui / gadgets / lists / lists.factor
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 ;
8 IN: ui.gadgets.lists
9
10 TUPLE: list < pack index presenter color hook ;
11
12 : list-theme ( list -- list )
13     selection-color >>color ; inline
14
15 : <list> ( hook presenter model -- gadget )
16     list new
17         { 0 1 } >>orientation
18         1 >>fill
19         0 >>index
20         swap >>model
21         swap >>presenter
22         swap >>hook
23         list-theme ;
24
25 : calc-bounded-index ( n list -- m )
26     control-value length 1 - min 0 max ;
27
28 : bound-index ( list -- )
29     dup index>> over calc-bounded-index >>index drop ;
30
31 : list-presentation-hook ( list -- quot )
32     hook>> [ [ list? ] find-parent ] prepend ;
33
34 : <list-presentation> ( hook elt presenter -- gadget )
35     [ call( elt -- obj ) ] [ drop ] 2bi
36     [ >label monospace-font >>font ] dip
37     <presentation>
38     swap >>hook ; inline
39
40 : <list-items> ( list -- seq )
41     [ list-presentation-hook ]
42     [ presenter>> ]
43     [ control-value ]
44     tri [
45         [ 2dup ] dip swap <list-presentation>
46     ] map 2nip ;
47
48 M: list model-changed
49     nip
50     dup clear-gadget
51     dup <list-items> add-gadgets
52     bound-index ;
53
54 : selected-rect ( list -- rect )
55     dup index>> swap children>> ?nth ;
56
57 M: list draw-gadget*
58     origin get [
59         dup color>> gl-color
60         selected-rect [
61             rect-bounds gl-fill-rect
62         ] when*
63     ] with-translation ;
64
65 M: list focusable-child* drop t ;
66
67 : list-value ( list -- object )
68     dup index>> swap control-value ?nth ;
69
70 : scroll>selected ( list -- )
71     ! We change the rectangle's width to zero to avoid
72     ! scrolling right.
73     [ selected-rect rect-bounds { 0 1 } v* <rect> ] keep
74     scroll>rect ;
75
76 : list-empty? ( list -- ? ) control-value empty? ;
77
78 : select-index ( n list -- )
79     dup list-empty? [
80         2drop
81     ] [
82         [ control-value length rem ] [ index<< ] [ ] tri
83         [ relayout-1 ] [ scroll>selected ] bi
84     ] if ;
85
86 : select-previous ( list -- )
87     [ index>> 1 - ] keep select-index ;
88
89 : select-next ( list -- )
90     [ index>> 1 + ] keep select-index ;
91
92 : invoke-value-action ( list -- )
93     dup list-empty? [
94         dup hook>> call( list -- )
95     ] [
96         [ index>> ] keep nth-gadget invoke-secondary
97     ] if ;
98
99 :: select-gadget ( gadget list -- )
100     gadget list children>> index
101     [ list select-index ] when* ;
102
103 : clamp-loc ( point max -- point )
104     { 0 0 } swap vclamp ;
105
106 : select-at ( point list -- )
107     [ dim>> clamp-loc ] keep
108     [ pick-up ] keep
109     select-gadget ;
110
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 ;
114
115 : list-page-up ( list -- ) { 0 -1 } list-page ;
116
117 : list-page-down ( list -- ) { 0 1 } list-page ;
118
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 }
126 } define-command-map