1 ! Copyright (C) 2006, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays assocs io kernel math models namespaces make
4 dlists deques sequences threads sequences words ui.gadgets
5 ui.gadgets.worlds ui.gadgets.tracks ui.gestures ui.backend
6 ui.render continuations init combinators hashtables
7 concurrency.flags sets accessors calendar ;
10 ! Assoc mapping aliens to gadgets
13 SYMBOL: stop-after-last-window?
15 : event-loop? ( -- ? )
17 { [ stop-after-last-window? get not ] [ t ] }
18 { [ graft-queue deque-empty? not ] [ t ] }
19 { [ windows get-global empty? not ] [ t ] }
23 : event-loop ( -- ) [ event-loop? ] [ do-events ] [ ] while ;
25 : window ( handle -- world ) windows get-global at ;
27 : window-focus ( handle -- gadget ) window world-focus ;
29 : register-window ( world handle -- )
30 #! Add the new window just below the topmost window. Why?
31 #! So that if the new window doesn't actually receive focus
32 #! (eg, we're using focus follows mouse and the mouse is not
33 #! in the new window when it appears) Factor doesn't get
34 #! confused and send workspace operations to the new window,
36 swap 2array windows get-global push
37 windows get-global dup length 1 >
38 [ [ length 1- dup 1- ] keep exchange ] [ drop ] if ;
40 : unregister-window ( handle -- )
41 windows global [ [ first = not ] with filter ] change-at ;
43 : raised-window ( world -- )
45 [ [ second eq? ] with find drop ] keep
46 [ nth ] [ delete-nth ] [ nip ] 2tri push ;
48 : focus-gestures ( new old -- )
49 drop-prefix <reversed>
50 T{ lose-focus } swap each-gesture
51 T{ gain-focus } swap each-gesture ;
53 : focus-world ( world -- )
56 focus-path f focus-gestures ;
58 : unfocus-world ( world -- )
60 focus-path f swap focus-gestures ;
64 [ [ title>> ] keep set-title ]
65 [ request-focus ] tri ;
67 : reset-world ( world -- )
68 #! This is used when a window is being closed, but also
69 #! when restoring saved worlds on image startup.
70 [ fonts>> clear-assoc ]
72 [ f >>handle drop ] tri ;
74 : (ungraft-world) ( world -- )
76 [ hand-clicked close-global ]
77 [ hand-gadget close-global ] tri ;
81 [ handle>> (close-window) ]
84 : find-window ( quot -- world )
86 [ gadget-child swap call ] with find-last nip ; inline
91 <dlist> \ graft-queue set-global
92 <dlist> \ layout-queue set-global
93 <dlist> \ gesture-queue set-global
94 V{ } clone windows set-global ;
96 : restore-gadget-later ( gadget -- )
100 { { t t } [ { f f } >>graft-state ] }
101 { { t f } [ dup unqueue-graft { f f } >>graft-state ] }
104 : restore-gadget ( gadget -- )
105 dup restore-gadget-later
106 children>> [ restore-gadget ] each ;
108 : restore-world ( world -- )
109 dup reset-world restore-gadget ;
111 : restore-windows ( -- )
112 windows get [ values ] keep delete-all
113 [ restore-world ] each
116 : restore-windows? ( -- ? )
117 windows get empty? not ;
119 : update-hand ( world -- )
120 dup hand-world get-global eq?
121 [ hand-loc get-global swap move-hand ] [ drop ] if ;
123 : layout-queued ( -- seq )
127 dup layout find-world [ , ] when*
131 : redraw-worlds ( seq -- )
132 [ dup update-hand draw-world ] each ;
134 : notify ( gadget -- )
136 [ first { f f } { t t } ? >>graft-state ] keep
138 { { f t } [ dup activate-control graft* ] }
139 { { t f } [ dup deactivate-control ungraft* ] }
142 : notify-queued ( -- )
143 graft-queue [ notify ] slurp-deque ;
145 : send-queued-gestures ( -- )
146 gesture-queue [ send-queued-gesture ] slurp-deque ;
156 ] [ ui-error ] recover ;
159 10 milliseconds sleep ;
163 : ui-running ( quot -- )
164 t \ ui-running set-global
165 [ f \ ui-running set-global ] [ ] cleanup ; inline
167 : ui-running? ( -- ? )
168 \ ui-running get-global ;
170 : update-ui-loop ( -- )
171 [ ui-running? ui-thread get-global self eq? and ]
172 [ ui-notify-flag get lower-flag update-ui ]
175 : start-ui-thread ( -- )
176 [ self ui-thread set-global update-ui-loop ]
177 "UI update" spawn drop ;
179 : open-world-window ( world -- )
180 dup pref-dim >>dim dup relayout graft ;
182 : open-window ( gadget title -- )
183 f <world> open-world-window ;
185 : set-fullscreen? ( ? gadget -- )
186 find-world set-fullscreen* ;
188 : fullscreen? ( gadget -- ? )
189 find-world fullscreen* ;
191 : raise-window ( gadget -- )
192 find-world raise-window* ;
194 HOOK: close-window ui-backend ( gadget -- )
196 M: object close-window
197 find-world [ ungraft ] when* ;
203 init-ui ui-hook get call
205 notify-ui-thread start-ui-thread ;
208 f \ ui-running set-global
209 <flag> ui-notify-flag set-global
212 HOOK: ui ui-backend ( -- )
216 : with-ui ( quot -- )
223 stop-after-last-window? on