1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays hashtables kernel math namespaces
4 make sequences quotations math.vectors combinators sorting
5 binary-search vectors dlists deques models threads
6 concurrency.flags math.order math.rectangles fry locals ;
9 ! Values for orientation slot
10 CONSTANT: horizontal { 1 0 }
11 CONSTANT: vertical { 0 1 }
17 { orientation initial: { 0 1 } }
19 { visible? initial: t }
23 { graft-state initial: { f f } }
29 M: gadget equal? 2drop f ;
31 M: gadget hashcode* nip identity-hashcode ;
33 M: gadget model-changed 2drop ;
35 : gadget-child ( gadget -- child ) children>> first ; inline
37 : nth-gadget ( n gadget -- child ) children>> nth ; inline
39 : <gadget> ( -- gadget )
42 : control-value ( control -- value )
45 : set-control-value ( value control -- )
48 : relative-loc ( fromgadget togadget -- loc )
52 [ [ parent>> ] dip relative-loc ] [ drop loc>> ] 2bi v+
55 GENERIC: user-input* ( str gadget -- ? )
57 M: gadget user-input* 2drop t ;
59 GENERIC: children-on ( rect gadget -- seq )
61 M: gadget children-on nip children>> ;
65 :: (fast-children-on) ( point axis children quot -- i )
68 quot call( value -- loc ) v-
70 ] search drop ; inline
74 :: fast-children-on ( rect axis children quot -- slice )
75 rect loc>> axis children quot (fast-children-on) 0 or
76 rect rect-bounds v+ axis children quot (fast-children-on) ?1+
77 children <slice> ; inline
79 M: gadget contains-rect? ( bounds gadget -- ? )
80 dup visible?>> [ call-next-method ] [ 2drop f ] if ;
82 M: gadget contains-point? ( loc gadget -- ? )
83 dup visible?>> [ call-next-method ] [ 2drop f ] if ;
85 : pick-up ( point gadget -- child/f )
86 2dup [ dup point>rect ] dip children-on
87 [ contains-point? ] with find-last nip
88 [ [ loc>> v- ] [ pick-up ] bi ] [ nip ] ?if ;
90 : max-dims ( seq -- dim )
91 [ 0 0 ] dip [ first2 swapd [ max ] 2bi@ ] each 2array ;
93 : sum-dims ( seq -- dim )
94 [ 0 0 ] dip [ first2 swapd [ + ] 2bi@ ] each 2array ;
96 : each-child ( gadget quot -- )
97 [ children>> ] dip each ; inline
100 GENERIC: gadget-selection? ( gadget -- ? )
102 M: gadget gadget-selection? drop f ;
104 GENERIC: gadget-selection ( gadget -- string/f )
106 M: gadget gadget-selection drop f ;
109 GENERIC: gadget-text* ( gadget -- )
111 GENERIC: gadget-text-separator ( gadget -- str )
113 M: gadget gadget-text-separator
114 orientation>> vertical = "\n" "" ? ;
116 : gadget-seq-text ( seq gadget -- )
117 gadget-text-separator '[ _ % ] [ gadget-text* ] interleave ;
119 M: gadget gadget-text*
120 [ children>> ] keep gadget-seq-text ;
122 M: array gadget-text*
123 [ gadget-text* ] each ;
125 : gadget-text ( gadget -- string ) [ gadget-text* ] "" make ;
131 SYMBOL: ui-notify-flag
133 : notify-ui-thread ( -- ) ui-notify-flag get-global raise-flag ;
135 : invalidate ( gadget -- )
136 \ invalidate >>layout-state drop ;
138 : forget-pref-dim ( gadget -- ) f >>pref-dim drop ;
140 : layout-queue ( -- queue ) \ layout-queue get ;
142 : layout-later ( gadget -- )
143 ! When unit testing gadgets without the UI running, the
144 ! invalid queue is not initialized and we simply ignore
145 ! invalidation requests.
146 layout-queue [ push-front notify-ui-thread ] [ drop ] if* ;
148 : invalidate* ( gadget -- )
149 \ invalidate* >>layout-state
152 [ layout-later ] [ parent>> [ relayout ] when* ] if ;
156 : relayout ( gadget -- )
157 dup layout-state>> \ invalidate* eq?
158 [ drop ] [ invalidate* ] if ;
160 : relayout-1 ( gadget -- )
162 [ drop ] [ dup invalidate layout-later ] if ;
164 : show-gadget ( gadget -- ) t >>visible? drop ;
166 : hide-gadget ( gadget -- ) f >>visible? drop ;
172 GENERIC: dim-changed ( gadget -- )
174 M: gadget dim-changed
175 in-layout? get [ invalidate ] [ invalidate* ] if ;
179 M: gadget dim<< ( dim gadget -- )
182 [ [ nip ] [ call-next-method ] 2bi dim-changed ] if ;
184 GENERIC: pref-dim* ( gadget -- dim )
186 : pref-dim ( gadget -- dim )
188 [ pref-dim* ] [ ] [ layout-state>> ] tri
189 [ drop ] [ dupd pref-dim<< ] if
192 : pref-dims ( gadgets -- seq ) [ pref-dim ] map ; inline
194 M: gadget pref-dim* dim>> ;
196 GENERIC: layout* ( gadget -- )
198 M: gadget layout* drop ;
200 : prefer ( gadget -- ) dup pref-dim >>dim drop ;
202 : layout ( gadget -- )
206 dup [ layout ] each-child
209 GENERIC: graft* ( gadget -- )
211 M: gadget graft* drop ;
213 GENERIC: ungraft* ( gadget -- )
215 M: gadget ungraft* drop ;
219 : graft-queue ( -- dlist )
220 \ graft-queue get [ "UI not running" throw ] unless* ;
222 : unqueue-graft ( gadget -- )
223 [ graft-node>> graft-queue delete-node ]
224 [ [ first { t t } { f f } ? ] change-graft-state drop ] bi ;
226 : (queue-graft) ( gadget flags -- )
228 dup graft-queue push-front* >>graft-node drop
231 : queue-graft ( gadget -- )
232 { f t } (queue-graft) ;
234 : queue-ungraft ( gadget -- )
235 { t f } (queue-graft) ;
237 : graft-later ( gadget -- )
241 { { t f } [ unqueue-graft ] }
242 { { f f } [ queue-graft ] }
245 : graft ( gadget -- )
246 dup graft-later [ graft ] each-child ;
248 : ungraft-later ( gadget -- )
252 { { f t } [ unqueue-graft ] }
253 { { t t } [ queue-ungraft ] }
256 : ungraft ( gadget -- )
257 dup [ ungraft ] each-child ungraft-later ;
259 : activate-control ( gadget -- )
267 : deactivate-control ( gadget -- )
268 dup model>> dup [ 2dup remove-connection ] when 2drop ;
270 : notify ( gadget -- )
272 [ first { f f } { t t } ? >>graft-state ] [ ] bi
274 { { f t } [ dup activate-control graft* ] }
275 { { t f } [ dup deactivate-control ungraft* ] }
278 : notify-queued ( -- )
279 graft-queue [ notify ] slurp-deque ;
281 : (unparent) ( gadget -- )
286 : (clear-gadget) ( gadget -- )
287 dup [ (unparent) ] each-child
288 f >>focus f >>children drop ;
290 : unfocus-gadget ( child gadget -- )
291 [ nip ] [ focus>> eq? ] 2bi [ f >>focus ] when drop ;
295 : not-in-layout ( -- )
297 [ "Cannot add/remove gadgets in layout*" throw ] when ;
299 GENERIC: remove-gadget ( gadget parent -- )
301 M: gadget remove-gadget 2drop ;
303 : unparent ( gadget -- )
311 [ children>> remove! drop ]
318 : clear-gadget ( gadget -- )
320 [ (clear-gadget) ] [ relayout ] bi ;
324 : (add-gadget) ( child parent -- )
328 [ [ ?push ] change-children drop ]
329 [ graft-state>> second [ graft ] [ drop ] if ]
334 : add-gadget ( parent child -- parent )
339 : add-gadgets ( parent children -- parent )
341 over '[ _ (add-gadget) ] each
344 : parents ( gadget -- seq )
345 [ parent>> ] follow ;
347 : each-parent ( gadget quot -- ? )
348 [ parents ] dip all? ; inline
350 : find-parent ( gadget quot -- parent )
351 [ parents ] dip find nip ; inline
353 : screen-loc ( gadget -- loc )
354 parents { 0 0 } [ loc>> v+ ] reduce ;
358 : (screen-rect) ( gadget -- loc ext )
360 [ rect-extent ] dip (screen-rect)
361 [ [ nip ] [ v+ ] 2bi ] dip [ v+ ] [ vmin ] 2bi*
368 : screen-rect ( gadget -- rect )
369 (screen-rect) <extent-rect> ;
371 : child? ( parent child -- ? )
373 { [ 2dup eq? ] [ 2drop t ] }
374 { [ dup not ] [ 2drop f ] }
378 GENERIC: focusable-child* ( gadget -- child/t )
380 M: gadget focusable-child* drop t ;
382 : focusable-child ( gadget -- child )
384 dup t eq? [ drop ] [ nip focusable-child ] if ;
386 GENERIC: request-focus-on ( child gadget -- )
388 M: gadget request-focus-on parent>> request-focus-on ;
390 M: f request-focus-on 2drop ;
392 : request-focus ( gadget -- )
393 [ focusable-child ] keep request-focus-on ;
395 : focus-path ( gadget -- seq )
400 { "ui.gadgets" "prettyprint" } "ui.gadgets.prettyprint" require-when