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
10 FROM: namespaces => change-global ;
15 ! Assoc mapping aliens to gadgets
18 : window ( handle -- world ) windows get-global at ;
20 : register-window ( world handle -- )
21 #! Add the new window just below the topmost window. Why?
22 #! So that if the new window doesn't actually receive focus
23 #! (eg, we're using focus follows mouse and the mouse is not
24 #! in the new window when it appears) Factor doesn't get
25 #! confused and send workspace operations to the new window,
27 swap 2array windows get-global push
28 windows get-global dup length 1 >
29 [ [ length 1 - dup 1 - ] keep exchange ] [ drop ] if ;
31 : unregister-window ( handle -- )
32 windows [ [ first = ] with reject ] change-global ;
34 : raised-window ( world -- )
36 [ [ second eq? ] with find drop ] keep
37 [ nth ] [ remove-nth! drop ] [ nip ] 2tri push ;
39 : focus-gestures ( new old -- )
40 drop-prefix <reversed>
41 lose-focus swap each-gesture
42 gain-focus swap each-gesture ;
44 : ?grab-input ( world -- )
45 dup grab-input?>> [ handle>> (grab-input) ] [ drop ] if ;
47 : ?ungrab-input ( world -- )
48 dup grab-input?>> [ handle>> (ungrab-input) ] [ drop ] if ;
50 : focus-world ( world -- )
54 focus-path f focus-gestures
57 : unfocus-world ( world -- )
60 [ focus-path f swap focus-gestures ] bi ;
62 : set-up-window ( world -- )
65 [ [ title>> ] keep set-title ]
72 : clean-up-broken-window ( world -- )
74 dup { [ focused?>> ] [ grab-input?>> ] } 1&&
75 [ handle>> (ungrab-input) ] [ drop ] if
76 ] [ handle>> (close-window) ] bi ;
82 [ [ clean-up-broken-window ] [ ui-error ] bi* ] recover
85 : dispose-window-resources ( world -- )
86 [ <reversed> [ [ dispose ] when* ] each V{ } clone ] change-window-resources drop ;
91 [ text-handle>> [ dispose ] when* ]
92 [ images>> [ dispose ] when* ]
93 [ hand-clicked close-global ]
94 [ hand-gadget close-global ]
96 [ dispose-window-resources ]
98 [ [ (close-window) f ] change-handle drop ]
99 [ promise>> t swap fulfill ]
103 <box> drag-timer set-global
104 f hand-gadget set-global
105 f hand-clicked set-global
106 f hand-world set-global
108 <dlist> \ graft-queue set-global
109 <dlist> \ layout-queue set-global
110 <dlist> \ gesture-queue set-global
111 V{ } clone windows set-global ;
113 : update-hand ( world -- )
114 dup hand-world get-global eq?
115 [ hand-loc get-global swap move-hand ] [ drop ] if ;
117 : (layout-queued) ( deque -- seq )
121 dup layout find-world [ , ] when*
123 ] { } make members ; inline
125 : layout-queued ( -- seq )
126 layout-queue dup deque-empty?
127 [ drop { } ] [ (layout-queued) ] if ;
129 : redraw-worlds ( seq -- )
130 [ dup update-hand draw-world ] each ;
132 : send-queued-gestures ( -- )
133 gesture-queue [ send-queued-gesture notify-queued ] slurp-deque ;
139 send-queued-gestures ;
143 : ui-running ( quot -- )
144 t \ ui-running set-global
145 [ f \ ui-running set-global ] [ ] cleanup ; inline
149 : find-window ( quot: ( world -- ? ) -- world )
150 [ windows get-global values ] dip
151 '[ dup children>> [ ] [ nip first ] if-empty @ ]
152 find-last nip ; inline
154 : ui-running? ( -- ? )
155 \ ui-running get-global ;
159 : update-ui-loop ( -- )
160 #! Note the logic: if update-ui fails, we open an error window
161 #! and run one iteration of update-ui. If that also fails, well,
162 #! the whole UI subsystem is broken so we exit out of the
164 [ { [ ui-running? ] [ ui-thread get-global self eq? ] } 0&& ]
166 ui-notify-flag get lower-flag
167 [ update-ui ] [ ui-error update-ui ] recover
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 >>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 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-main-window-attributes ( class -- attributes )
232 "{" expect dup all-slots parse-tuple-literal-slots ;
234 : define-main-window ( word attributes quot -- )
236 '[ [ f _ clone @ open-window ] with-ui ] ( -- ) define-declared
237 ] [ 2drop current-vocab main<< ] 3bi ;
241 world-attributes parse-main-window-attributes