1 ! Copyright (C) 2006, 2009 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 continuations
5 init combinators hashtables concurrency.flags sets accessors
6 calendar fry ui.gadgets ui.gadgets.worlds ui.gadgets.tracks
7 ui.gestures ui.backend ui.render ui.text ui.text.private ;
10 ! Assoc mapping aliens to gadgets
13 : window ( handle -- world ) windows get-global at ;
15 : window-focus ( handle -- gadget ) window world-focus ;
17 : register-window ( world handle -- )
18 #! Add the new window just below the topmost window. Why?
19 #! So that if the new window doesn't actually receive focus
20 #! (eg, we're using focus follows mouse and the mouse is not
21 #! in the new window when it appears) Factor doesn't get
22 #! confused and send workspace operations to the new window,
24 swap 2array windows get-global push
25 windows get-global dup length 1 >
26 [ [ length 1- dup 1- ] keep exchange ] [ drop ] if ;
28 : unregister-window ( handle -- )
29 windows global [ [ first = not ] with filter ] change-at ;
31 : raised-window ( world -- )
33 [ [ second eq? ] with find drop ] keep
34 [ nth ] [ delete-nth ] [ nip ] 2tri push ;
36 : focus-gestures ( new old -- )
37 drop-prefix <reversed>
38 lose-focus swap each-gesture
39 gain-focus swap each-gesture ;
41 : focus-world ( world -- )
44 focus-path f focus-gestures ;
46 : unfocus-world ( world -- )
48 focus-path f swap focus-gestures ;
52 [ [ title>> ] keep set-title ]
53 [ request-focus ] tri ;
55 : reset-world ( world -- )
56 #! This is used when a window is being closed, but also
57 #! when restoring saved worlds on image startup.
58 [ fonts>> clear-assoc ]
60 [ f >>handle drop ] tri ;
62 : (ungraft-world) ( world -- )
64 [ handle>> select-gl-context ]
65 [ fonts>> free-fonts ]
66 [ hand-clicked close-global ]
67 [ hand-gadget close-global ]
72 [ handle>> (close-window) ]
75 : find-window ( quot -- world )
77 [ gadget-child swap call ] with find-last nip ; inline
80 <dlist> \ graft-queue set-global
81 <dlist> \ layout-queue set-global
82 <dlist> \ gesture-queue set-global
83 V{ } clone windows set-global ;
85 : restore-gadget-later ( gadget -- )
89 { { t t } [ { f f } >>graft-state ] }
90 { { t f } [ dup unqueue-graft { f f } >>graft-state ] }
93 : restore-gadget ( gadget -- )
94 dup restore-gadget-later
95 children>> [ restore-gadget ] each ;
97 : restore-world ( world -- )
98 dup reset-world restore-gadget ;
100 : update-hand ( world -- )
101 dup hand-world get-global eq?
102 [ hand-loc get-global swap move-hand ] [ drop ] if ;
104 : layout-queued ( -- seq )
108 dup layout find-world [ , ] when*
112 : redraw-worlds ( seq -- )
113 [ dup update-hand draw-world ] each ;
115 : notify ( gadget -- )
117 [ first { f f } { t t } ? >>graft-state ] keep
119 { { f t } [ dup activate-control graft* ] }
120 { { t f } [ dup deactivate-control ungraft* ] }
123 : notify-queued ( -- )
124 graft-queue [ notify ] slurp-deque ;
126 : send-queued-gestures ( -- )
127 gesture-queue [ send-queued-gesture notify-queued ] slurp-deque ;
137 ] [ ui-error ] recover ;
141 : ui-running ( quot -- )
142 t \ ui-running set-global
143 [ f \ ui-running set-global ] [ ] cleanup ; inline
145 : ui-running? ( -- ? )
146 \ ui-running get-global ;
148 : update-ui-loop ( -- )
149 [ ui-running? ui-thread get-global self eq? and ]
150 [ ui-notify-flag get lower-flag update-ui ]
153 : start-ui-thread ( -- )
154 [ self ui-thread set-global update-ui-loop ]
155 "UI update" spawn drop ;
157 : open-world-window ( world -- )
158 dup pref-dim >>dim dup relayout graft ;
160 : open-window ( gadget title -- )
161 f <world> open-world-window ;
163 : set-fullscreen? ( ? gadget -- )
164 find-world set-fullscreen* ;
166 : fullscreen? ( gadget -- ? )
167 find-world fullscreen* ;
169 : raise-window ( gadget -- )
170 find-world raise-window* ;
172 HOOK: close-window ui-backend ( gadget -- )
174 M: object close-window
175 find-world [ ungraft ] when* ;
177 : start-ui ( quot -- )
178 call notify-ui-thread start-ui-thread ;
181 f \ ui-running set-global
182 <flag> ui-notify-flag set-global
185 HOOK: (with-ui) ui-backend ( quot -- )
187 : restore-windows ( -- )
189 windows get [ values ] [ delete-all ] bi
190 [ restore-world ] each
194 : restore-windows? ( -- ? )
195 windows get empty? not ;
197 : with-ui ( quot -- )
198 ui-running? [ call ] [ '[ init-ui @ ] (with-ui) ] if ;
200 HOOK: beep ui-backend ( -- )