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 ) windows get-global at ;
19 : window-focus ( handle -- gadget ) window world-focus ;
21 : register-window ( world handle -- )
22 #! Add the new window just below the topmost window. Why?
23 #! So that if the new window doesn't actually receive focus
24 #! (eg, we're using focus follows mouse and the mouse is not
25 #! in the new window when it appears) Factor doesn't get
26 #! confused and send workspace operations to the new window,
28 swap 2array windows get-global push
29 windows get-global dup length 1 >
30 [ [ length 1 - dup 1 - ] keep exchange ] [ drop ] if ;
32 : unregister-window ( handle -- )
33 windows [ [ first = not ] with filter ] change-global ;
35 : raised-window ( world -- )
37 [ [ second eq? ] with find drop ] keep
38 [ nth ] [ remove-nth! drop ] [ nip ] 2tri push ;
40 : focus-gestures ( new old -- )
41 drop-prefix <reversed>
42 lose-focus swap each-gesture
43 gain-focus swap each-gesture ;
45 : ?grab-input ( world -- )
46 dup grab-input?>> [ handle>> (grab-input) ] [ drop ] if ;
48 : ?ungrab-input ( world -- )
49 dup grab-input?>> [ handle>> (ungrab-input) ] [ drop ] if ;
51 : focus-world ( world -- )
55 focus-path f focus-gestures
58 : unfocus-world ( world -- )
61 [ focus-path f swap focus-gestures ] bi ;
63 : set-up-window ( world -- )
66 [ [ title>> ] keep set-title ]
73 : clean-up-broken-window ( world -- )
75 dup { [ focused?>> ] [ grab-input?>> ] } 1&&
76 [ handle>> (ungrab-input) ] [ drop ] if
77 ] [ handle>> (close-window) ] bi ;
83 [ [ clean-up-broken-window ] [ ui-error ] bi* ] recover
89 [ text-handle>> [ dispose ] when* ]
90 [ images>> [ dispose ] when* ]
91 [ hand-clicked close-global ]
92 [ hand-gadget close-global ]
94 [ [ <reversed> [ [ dispose ] when* ] each V{ } clone ] change-window-resources drop ]
95 [ [ (close-window) f ] change-handle drop ]
97 [ promise>> t swap fulfill ]
101 <box> drag-timer set-global
102 f hand-gadget set-global
103 f hand-clicked set-global
104 f hand-world set-global
106 <dlist> \ graft-queue set-global
107 <dlist> \ layout-queue set-global
108 <dlist> \ gesture-queue set-global
109 V{ } clone windows set-global ;
111 : update-hand ( world -- )
112 dup hand-world get-global eq?
113 [ hand-loc get-global swap move-hand ] [ drop ] if ;
115 : layout-queued ( -- seq )
119 dup layout find-world [ , ] when*
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 : ui-running ( quot -- )
138 t \ ui-running set-global
139 [ f \ ui-running set-global ] [ ] cleanup ; inline
143 : find-window ( quot -- world )
144 [ windows get values ] dip
145 '[ dup children>> [ ] [ nip first ] if-empty @ ]
146 find-last nip ; inline
148 : ui-running? ( -- ? )
149 \ ui-running get-global ;
153 : update-ui-loop ( -- )
154 #! Note the logic: if update-ui fails, we open an error window
155 #! and run one iteration of update-ui. If that also fails, well,
156 #! the whole UI subsystem is broken so we exit out of the
158 [ { [ ui-running? ] [ ui-thread get-global self eq? ] } 0&& ]
160 ui-notify-flag get lower-flag
161 [ update-ui ] [ ui-error update-ui ] recover
164 : start-ui-thread ( -- )
165 [ self ui-thread set-global update-ui-loop ]
166 "UI update" spawn drop ;
168 : start-ui ( quot -- )
169 call( -- ) notify-ui-thread start-ui-thread ;
171 : ?attributes ( gadget title/attributes -- attributes )
172 dup string? [ world-attributes new swap >>title ] [ clone ] if
173 swap [ [ [ 1array ] [ f ] if* ] curry unless* ] curry change-gadgets ;
177 : open-world-window ( world -- )
178 dup pref-dim >>dim dup relayout graft ;
180 : open-window* ( gadget title/attributes -- window )
181 ?attributes <world> [ open-world-window ] keep ;
183 : open-window ( gadget title/attributes -- )
186 : set-fullscreen ( gadget ? -- )
187 [ find-world ] dip (set-fullscreen) ;
189 : fullscreen? ( gadget -- ? )
190 find-world (fullscreen?) ;
192 : toggle-fullscreen ( gadget -- )
193 dup fullscreen? not set-fullscreen ;
195 : raise-window ( gadget -- )
196 find-world raise-window* ;
198 : topmost-window ( -- world )
199 windows get last second ;
201 HOOK: close-window ui-backend ( gadget -- )
203 M: object close-window
204 find-world [ ungraft ] when* ;
207 f \ ui-running set-global
208 <flag> ui-notify-flag set-global
209 ] "ui" add-startup-hook
211 : with-ui ( quot: ( -- ) -- )
212 ui-running? [ call( -- ) ] [ '[ init-ui @ ] (with-ui) ] if ;
214 HOOK: beep ui-backend ( -- )
216 HOOK: system-alert ui-backend ( caption text -- )
218 : parse-main-window-attributes ( class -- attributes )
219 "{" expect dup all-slots parse-tuple-literal-slots ;
221 : define-main-window ( word attributes quot -- )
223 '[ [ f _ clone @ open-window ] with-ui ] (( -- )) define-declared
224 ] [ 2drop current-vocab main<< ] 3bi ;
228 world-attributes parse-main-window-attributes