1 ! Copyright (C) 2006, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs boxes classes.tuple
4 classes.tuple.parser combinators combinators.short-circuit
5 concurrency.flags concurrency.promises continuations deques
6 destructors dlists fry init kernel lexer make math namespaces
7 parser sequences sets strings threads ui.backend ui.gadgets
8 ui.gadgets.private ui.gadgets.worlds ui.gestures vocabs.parser
14 ! Assoc mapping aliens to gadgets
17 : window ( handle -- world ) ui-windows get-global at ;
19 : register-window ( world handle -- )
20 ! Add the new window just below the topmost window. Why?
21 ! So that if the new window doesn't actually receive focus
22 ! (eg, we're using focus follows mouse and the mouse is not
23 ! in the new window when it appears) Factor doesn't get
24 ! confused and send workspace operations to the new window,
26 swap 2array ui-windows get-global push
27 ui-windows get-global dup length 1 >
28 [ [ length 1 - dup 1 - ] keep exchange ] [ drop ] if ;
30 : unregister-window ( handle -- )
31 ui-windows [ [ first = ] with reject ] change-global ;
33 : raised-window ( world -- )
35 [ [ second eq? ] with find drop ] keep
36 [ nth ] [ remove-nth! drop ] [ nip ] 2tri push ;
38 : focus-gestures ( new old -- )
39 drop-prefix <reversed>
40 lose-focus swap each-gesture
41 gain-focus swap each-gesture ;
43 : ?grab-input ( world -- )
44 dup grab-input?>> [ handle>> (grab-input) ] [ drop ] if ;
46 : ?ungrab-input ( world -- )
47 dup grab-input?>> [ handle>> (ungrab-input) ] [ drop ] if ;
49 : focus-world ( world -- )
53 focus-path f focus-gestures
56 : unfocus-world ( world -- )
59 [ focus-path f swap focus-gestures ] bi ;
61 : set-up-window ( world -- )
64 [ [ title>> ] keep set-title ]
71 : clean-up-broken-window ( world -- )
73 dup { [ focused?>> ] [ grab-input?>> ] } 1&&
74 [ handle>> (ungrab-input) ] [ drop ] if
75 ] [ handle>> (close-window) ] bi ;
81 [ [ clean-up-broken-window ] [ ui-error ] bi* ] recover
84 : dispose-window-resources ( world -- )
85 [ <reversed> [ [ dispose ] when* ] each V{ } clone ] change-window-resources drop ;
90 [ text-handle>> [ dispose ] when* ]
91 [ images>> [ dispose ] when* ]
92 [ hand-clicked close-global ]
93 [ hand-gadget close-global ]
95 [ dispose-window-resources ]
97 [ [ (close-window) f ] change-handle drop ]
98 [ promise>> t swap fulfill ]
102 <box> drag-timer set-global
103 f hand-gadget set-global
104 f hand-clicked set-global
105 f hand-world set-global
107 <dlist> \ graft-queue set-global
108 <dlist> \ layout-queue set-global
109 <dlist> \ gesture-queue set-global
110 V{ } clone ui-windows set-global ;
112 : update-hand ( world -- )
113 dup hand-world get-global eq?
114 [ hand-loc get-global swap move-hand ] [ drop ] if ;
116 : (layout-queued) ( deque -- seq )
120 dup layout find-world [ , ] when*
122 ] { } make members ; inline
124 : layout-queued ( -- seq )
125 layout-queue dup deque-empty?
126 [ drop { } ] [ (layout-queued) ] if ;
128 : redraw-worlds ( seq -- )
129 [ dup update-hand draw-world ] each ;
131 : send-queued-gestures ( -- )
132 gesture-queue [ send-queued-gesture notify-queued ] slurp-deque ;
138 send-queued-gestures ;
142 : ui-running ( quot -- )
143 t \ ui-running set-global
144 [ f \ ui-running set-global ] [ ] cleanup ; inline
148 : find-window ( quot: ( world -- ? ) -- world )
149 [ ui-windows get-global values ] dip
150 '[ dup children>> [ ] [ nip first ] if-empty @ ]
151 find-last nip ; inline
153 : ui-running? ( -- ? )
154 \ ui-running get-global ;
158 : update-ui-loop ( -- )
159 ! Note the logic: if update-ui fails, we open an error window
160 ! and run one iteration of update-ui. If that also fails, well,
161 ! the whole UI subsystem is broken so we exit out of the
163 [ { [ ui-running? ] [ ui-thread get-global self eq? ] } 0&& ]
165 ui-notify-flag get lower-flag
166 [ update-ui ] [ ui-error update-ui ] recover
169 : start-ui-thread ( -- )
170 [ self ui-thread set-global update-ui-loop ]
171 "UI update" spawn drop ;
173 : start-ui ( quot -- )
174 call( -- ) notify-ui-thread start-ui-thread ;
176 : ?attributes ( gadget title/attributes -- attributes )
177 dup string? [ <world-attributes> swap >>title ] [ clone ] if
178 swap [ [ [ 1array ] [ f ] if* ] curry unless* ] curry change-gadgets ;
182 : open-world-window ( world -- )
183 dup pref-dim >>dim dup relayout graft ;
185 : open-window* ( gadget title/attributes -- window )
186 ?attributes <world> [ open-world-window ] keep ;
188 : open-window ( gadget title/attributes -- )
191 : set-fullscreen ( gadget ? -- )
192 [ find-world ] dip (set-fullscreen) ;
194 : fullscreen? ( gadget -- ? )
195 find-world (fullscreen?) ;
197 : toggle-fullscreen ( gadget -- )
198 dup fullscreen? not set-fullscreen ;
200 : raise-window ( gadget -- )
201 find-world raise-window* ;
203 : topmost-window ( -- world )
204 ui-windows get-global last second ;
206 HOOK: close-window ui-backend ( gadget -- )
208 M: object close-window
209 find-world [ ungraft ] when* ;
212 f \ ui-running set-global
213 <flag> ui-notify-flag set-global
214 ] "ui" add-startup-hook
216 HOOK: resize-window ui-backend ( world dim -- )
217 M: object resize-window 2drop ;
219 : relayout-window ( gadget -- )
221 [ find-world [ dup pref-dim resize-window ] when* ] bi ;
223 : with-ui ( quot: ( -- ) -- )
224 ui-running? [ call( -- ) ] [ '[ init-ui @ ] (with-ui) ] if ;
226 HOOK: beep ui-backend ( -- )
228 HOOK: system-alert ui-backend ( caption text -- )
230 : parse-main-window-attributes ( class -- attributes )
231 "{" expect dup all-slots parse-tuple-literal-slots ;
233 : define-main-window ( word attributes quot -- )
235 '[ [ f _ clone @ open-window ] with-ui ] ( -- ) define-declared
236 ] [ 2drop current-vocab main<< ] 3bi ;
240 world-attributes parse-main-window-attributes