1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays binary-search combinators concurrency.flags
4 deques fry kernel locals make math math.order math.rectangles
5 math.vectors models namespaces sequences threads vectors ;
8 ! Values for orientation slot
9 CONSTANT: horizontal { 1 0 }
10 CONSTANT: vertical { 0 1 }
16 { orientation initial: { 0 1 } }
18 { visible? initial: t }
22 { graft-state initial: { f f } }
28 M: gadget equal? 2drop f ;
30 M: gadget hashcode* nip identity-hashcode ;
32 M: gadget model-changed 2drop ;
34 : gadget-child ( gadget -- child ) children>> first ; inline
36 : nth-gadget ( n gadget -- child ) children>> nth ; inline
38 : <gadget> ( -- gadget )
41 : control-value ( control -- value )
44 : set-control-value ( value control -- )
47 : relative-loc ( fromgadget togadget -- loc )
51 [ [ parent>> ] dip relative-loc ] [ drop loc>> ] 2bi v+
54 GENERIC: user-input* ( str gadget -- ? )
56 M: gadget user-input* 2drop t ;
58 GENERIC: temp-im-input ( str gadget -- ? )
60 M: gadget temp-im-input 2drop t ;
62 GENERIC: children-on ( rect gadget -- seq )
64 M: gadget children-on nip children>> ;
68 :: (fast-children-on) ( point axis children quot -- i )
71 quot call( value -- loc ) v-
73 ] search drop ; inline
77 :: fast-children-on ( rect axis children quot -- slice )
78 rect loc>> axis children quot (fast-children-on) 0 or
79 rect rect-bounds v+ axis children quot (fast-children-on) ?1+
80 children <slice> ; inline
82 M: gadget contains-rect? ( bounds gadget -- ? )
83 dup visible?>> [ call-next-method ] [ 2drop f ] if ;
85 M: gadget contains-point? ( loc gadget -- ? )
86 dup visible?>> [ call-next-method ] [ 2drop f ] if ;
88 : pick-up ( point gadget -- child/f )
89 2dup [ dup point>rect ] dip children-on
90 [ contains-point? ] with find-last nip
91 [ [ loc>> v- ] [ pick-up ] bi ] [ nip ] ?if ;
93 : max-dims ( seq -- dim )
94 [ 0 0 ] dip [ first2 swapd [ max ] 2bi@ ] each 2array ;
96 : sum-dims ( seq -- dim )
97 [ 0 0 ] dip [ first2 swapd [ + ] 2bi@ ] each 2array ;
99 : each-child ( gadget quot -- )
100 [ children>> ] dip each ; inline
103 GENERIC: gadget-selection? ( gadget -- ? )
105 M: gadget gadget-selection? drop f ;
107 GENERIC: gadget-selection ( gadget -- string/f )
109 M: gadget gadget-selection drop f ;
112 GENERIC: gadget-text* ( gadget -- )
114 GENERIC: gadget-text-separator ( gadget -- str )
116 M: gadget gadget-text-separator
117 orientation>> vertical = "\n" "" ? ;
119 : gadget-seq-text ( seq gadget -- )
120 gadget-text-separator '[ _ % ] [ gadget-text* ] interleave ;
122 M: gadget gadget-text*
123 [ children>> ] keep gadget-seq-text ;
125 M: array gadget-text*
126 [ gadget-text* ] each ;
128 : gadget-text ( gadget -- string ) [ gadget-text* ] "" make ;
134 SYMBOL: ui-notify-flag
136 : notify-ui-thread ( -- ) ui-notify-flag get-global raise-flag ;
138 : invalidate ( gadget -- )
139 \ invalidate >>layout-state drop ;
141 : forget-pref-dim ( gadget -- ) f >>pref-dim drop ;
143 : layout-queue ( -- queue )
146 CONSTANT: layout-queue-limit 8000
148 : layout-later ( gadget -- )
150 [ push notify-ui-thread ]
151 [ length layout-queue-limit > [ yield ] when ] bi
154 : invalidate* ( gadget -- )
155 \ invalidate* >>layout-state
158 [ layout-later ] [ parent>> [ relayout ] when* ] if ;
162 : relayout ( gadget -- )
163 dup layout-state>> \ invalidate* eq?
164 [ drop ] [ invalidate* ] if ;
166 : relayout-1 ( gadget -- )
168 [ drop ] [ dup invalidate layout-later ] if ;
170 : show-gadget ( gadget -- ) t >>visible? drop ;
172 : hide-gadget ( gadget -- ) f >>visible? drop ;
178 GENERIC: dim-changed ( gadget -- )
180 M: gadget dim-changed
181 in-layout? get [ invalidate ] [ invalidate* ] if ;
185 M: gadget dim<< ( dim gadget -- )
188 [ [ nip ] [ call-next-method ] 2bi dim-changed ] if ;
190 GENERIC: pref-dim* ( gadget -- dim )
192 : pref-dim ( gadget -- dim )
194 [ pref-dim* ] [ ] [ layout-state>> ] tri
195 [ drop ] [ dupd pref-dim<< ] if
198 : pref-dims ( gadgets -- seq ) [ pref-dim ] map ; inline
200 M: gadget pref-dim* dim>> ;
202 GENERIC: layout* ( gadget -- )
204 M: gadget layout* drop ;
206 : prefer ( gadget -- ) dup pref-dim >>dim drop ;
208 : layout ( gadget -- )
212 dup [ layout ] each-child
215 GENERIC: graft* ( gadget -- )
217 M: gadget graft* drop ;
219 GENERIC: ungraft* ( gadget -- )
221 M: gadget ungraft* drop ;
225 : graft-queue ( -- dlist )
226 \ graft-queue get [ "UI not running" throw ] unless* ;
228 : unqueue-graft ( gadget -- )
229 [ graft-node>> graft-queue delete-node ]
230 [ [ first { t t } { f f } ? ] change-graft-state drop ] bi ;
232 : (queue-graft) ( gadget flags -- )
234 dup graft-queue push-front* >>graft-node drop
237 : queue-graft ( gadget -- )
238 { f t } (queue-graft) ;
240 : queue-ungraft ( gadget -- )
241 { t f } (queue-graft) ;
243 : graft-later ( gadget -- )
247 { { t f } [ unqueue-graft ] }
248 { { f f } [ queue-graft ] }
251 : graft ( gadget -- )
252 dup graft-later [ graft ] each-child ;
254 : ungraft-later ( gadget -- )
258 { { f t } [ unqueue-graft ] }
259 { { t t } [ queue-ungraft ] }
262 : ungraft ( gadget -- )
263 dup [ ungraft ] each-child ungraft-later ;
265 : activate-control ( gadget -- )
273 : deactivate-control ( gadget -- )
274 dup model>> dup [ 2dup remove-connection ] when 2drop ;
276 : notify ( gadget -- )
278 [ first { f f } { t t } ? >>graft-state ] [ ] bi
280 { { f t } [ dup activate-control graft* ] }
281 { { t f } [ dup deactivate-control ungraft* ] }
284 : notify-queued ( -- )
285 graft-queue [ notify ] slurp-deque ;
287 : (unparent) ( gadget -- )
292 : (clear-gadget) ( gadget -- )
293 dup [ (unparent) ] each-child
294 f >>focus f >>children drop ;
296 : unfocus-gadget ( child gadget -- )
297 [ nip ] [ focus>> eq? ] 2bi [ f >>focus ] when drop ;
301 : not-in-layout ( -- )
303 [ "Cannot add/remove gadgets in layout*" throw ] when ;
305 GENERIC: remove-gadget ( gadget parent -- )
307 M: gadget remove-gadget 2drop ;
309 : unparent ( gadget -- )
317 [ children>> remove! drop ]
324 : clear-gadget ( gadget -- )
326 [ (clear-gadget) ] [ relayout ] bi ;
330 : (add-gadget) ( child parent -- )
334 [ [ ?push ] change-children drop ]
335 [ graft-state>> second [ graft ] [ drop ] if ]
340 : add-gadget ( parent child -- parent )
345 : add-gadgets ( parent children -- parent )
347 over '[ _ (add-gadget) ] each
350 : parents ( gadget -- seq )
351 [ parent>> ] follow ;
353 : each-parent ( gadget quot -- ? )
354 [ parents ] dip all? ; inline
356 : find-parent ( gadget quot -- parent )
357 [ parents ] dip find nip ; inline
359 : screen-loc ( gadget -- loc )
360 parents { 0 0 } [ loc>> v+ ] reduce ;
364 : (screen-rect) ( gadget -- loc ext )
366 [ rect-extent ] dip (screen-rect)
367 [ [ nip ] [ v+ ] 2bi ] dip [ v+ ] [ vmin ] 2bi*
374 : screen-rect ( gadget -- rect )
375 (screen-rect) <extent-rect> ;
377 : child? ( parent child -- ? )
379 { [ 2dup eq? ] [ 2drop t ] }
380 { [ dup not ] [ 2drop f ] }
384 GENERIC: focusable-child* ( gadget -- child/t )
386 M: gadget focusable-child* drop t ;
388 M: f focusable-child* drop f ;
390 : focusable-child ( gadget -- child )
392 dup t eq? [ drop ] [ nip focusable-child ] if ;
394 GENERIC: request-focus-on ( child gadget -- )
396 M: gadget request-focus-on parent>> request-focus-on ;
398 M: f request-focus-on 2drop ;
400 : request-focus ( gadget -- )
401 [ focusable-child ] keep request-focus-on ;
403 : focus-path ( gadget -- seq )
406 GENERIC: preedit? ( gadget -- ? )
408 M: gadget preedit? drop f ;
412 { "ui.gadgets" "prettyprint" } "ui.gadgets.prettyprint" require-when