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
7 math.functions namespaces parser sequences sets strings threads
8 ui.backend ui.gadgets ui.gadgets.private ui.gadgets.worlds
9 ui.gestures ui.render vectors vocabs.parser words ;
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 ]
70 : clean-up-broken-window ( world -- )
72 dup { [ focused?>> ] [ grab-input?>> ] } 1&&
73 [ handle>> (ungrab-input) ] [ drop ] if
74 ] [ handle>> (close-window) ] bi ;
79 [ set-up-window ] [ ] [ clean-up-broken-window ] cleanup
82 : dispose-window-resources ( world -- )
83 [ <reversed> [ [ dispose ] when* ] each V{ } clone ] change-window-resources drop ;
88 [ text-handle>> [ dispose ] when* ]
89 [ images>> [ dispose ] when* ]
90 [ hand-clicked close-global ]
91 [ hand-gadget close-global ]
93 [ dispose-window-resources ]
95 [ [ (close-window) f ] change-handle drop ]
96 [ promise>> t swap fulfill ]
100 <box> drag-timer set-global
101 f hand-gadget set-global
102 f hand-clicked set-global
103 f hand-world set-global
105 <dlist> \ graft-queue set-global
106 100 <vector> \ layout-queue set-global
107 <dlist> \ gesture-queue set-global
108 V{ } clone ui-windows set-global ;
110 : update-hand ( world -- )
111 dup hand-world get-global eq?
112 [ hand-loc get-global swap move-hand ] [ drop ] if ;
114 : slurp-vector ( .. seq quot: ( ... elt -- .. ) -- )
115 over '[ _ empty? not ] -rot '[ _ pop @ ] while ; inline
117 : layout-queued ( -- seq )
120 [ dup layout find-world [ , ] when* ] slurp-vector
123 : redraw-worlds ( seq -- )
124 [ dup update-hand draw-world ] each ;
126 : send-queued-gestures ( -- )
127 gesture-queue [ send-queued-gesture notify-queued ] slurp-deque ;
133 send-queued-gestures ;
137 : with-ui-running ( quot -- )
138 t ui-running set-global
139 [ f ui-running set-global ] [ ] cleanup ; inline
143 : find-window ( quot: ( world -- ? ) -- world/f )
144 [ ui-windows get-global values ] dip
145 '[ dup children>> [ ] [ nip first ] if-empty @ ]
146 find-last nip ; inline
148 : ui-running? ( -- ? )
149 ui-running get-global ;
155 : update-ui-loop ( -- )
156 ! Note the logic: if update-ui fails, we open an error window and
157 ! run one iteration of update-ui. If that also fails, well, the
158 ! whole UI subsystem is broken so we throw the error to terminate
159 ! the update-ui-loop.
160 [ { [ ui-running? ] [ ui-thread get-global self eq? ] } 0&& ]
162 ui-notify-flag get lower-flag
164 [ ui-error update-ui ] [
165 stop-event-loop nip rethrow
170 : start-ui-thread ( -- )
171 [ self ui-thread set-global update-ui-loop ]
172 "UI update" spawn drop ;
174 : start-ui ( quot -- )
175 call( -- ) notify-ui-thread start-ui-thread ;
177 : ?attributes ( gadget title/attributes -- attributes )
178 dup string? [ <world-attributes> swap >>title ] [ clone ] if
179 swap [ [ [ 1array ] [ f ] if* ] curry unless* ] curry change-gadgets ;
183 : open-world-window ( world -- )
184 dup pref-dim [ ceiling ] map >>dim dup relayout graft ;
186 : open-window* ( gadget title/attributes -- window )
187 ?attributes <world> [ open-world-window ] keep ;
189 : open-window ( gadget title/attributes -- )
192 : set-fullscreen ( gadget ? -- )
193 [ find-world ] dip (set-fullscreen) ;
195 : fullscreen? ( gadget -- ? )
196 find-world (fullscreen?) ;
198 : toggle-fullscreen ( gadget -- )
199 dup fullscreen? not set-fullscreen ;
201 : raise-window ( gadget -- )
202 find-world raise-window* ;
204 : topmost-window ( -- world )
205 ui-windows get-global last second ;
207 HOOK: close-window ui-backend ( gadget -- )
209 M: object close-window
210 find-world [ ungraft ] when* ;
213 f ui-running set-global
214 <flag> ui-notify-flag set-global
215 ] "ui" add-startup-hook
217 HOOK: resize-window ui-backend ( world dim -- )
218 M: object resize-window 2drop ;
220 : relayout-window ( gadget -- )
222 [ find-world [ dup pref-dim resize-window ] when* ] bi ;
224 : with-ui ( quot: ( -- ) -- )
225 ui-running? [ call( -- ) ] [ '[ init-ui @ ] (with-ui) ] if ;
227 HOOK: beep ui-backend ( -- )
229 HOOK: system-alert ui-backend ( caption text -- )
231 : parse-window-attributes ( class -- attributes )
232 "{" expect dup all-slots parse-tuple-literal-slots ;
234 : define-window ( word attributes quot -- )
235 '[ [ f _ clone @ open-window ] with-ui ] ( -- ) define-declared ;
239 world-attributes parse-window-attributes
245 world-attributes parse-window-attributes
247 [ define-window ] [ 2drop current-vocab main<< ] 3bi ;