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 dlists
4 deques sequences threads sequences words continuations init call
5 combinators hashtables concurrency.flags sets accessors calendar fry
6 destructors ui.gadgets ui.gadgets.private ui.gadgets.worlds
7 ui.gadgets.tracks ui.gestures ui.backend ui.render ui.text
11 ! Assoc mapping aliens to gadgets
14 : window ( handle -- world ) windows get-global at ;
16 : window-focus ( handle -- gadget ) window world-focus ;
18 : register-window ( world handle -- )
19 #! Add the new window just below the topmost window. Why?
20 #! So that if the new window doesn't actually receive focus
21 #! (eg, we're using focus follows mouse and the mouse is not
22 #! in the new window when it appears) Factor doesn't get
23 #! confused and send workspace operations to the new window,
25 swap 2array windows get-global push
26 windows get-global dup length 1 >
27 [ [ length 1- dup 1- ] keep exchange ] [ drop ] if ;
29 : unregister-window ( handle -- )
30 windows global [ [ first = not ] with filter ] change-at ;
32 : raised-window ( world -- )
34 [ [ second eq? ] with find drop ] keep
35 [ nth ] [ delete-nth ] [ nip ] 2tri push ;
37 : focus-gestures ( new old -- )
38 drop-prefix <reversed>
39 lose-focus swap each-gesture
40 gain-focus swap each-gesture ;
42 : focus-world ( world -- )
45 focus-path f focus-gestures ;
47 : unfocus-world ( world -- )
49 focus-path f swap focus-gestures ;
53 [ [ title>> ] keep set-title ]
54 [ request-focus ] tri ;
56 : reset-world ( world -- )
57 #! This is used when a window is being closed, but also
58 #! when restoring saved worlds on image startup.
59 f >>handle unfocus-world ;
61 : (ungraft-world) ( world -- )
63 [ handle>> select-gl-context ]
64 [ text-handle>> dispose ]
65 [ images>> [ dispose ] when* ]
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 -- )
100 [ init-text-rendering ]
105 : update-hand ( world -- )
106 dup hand-world get-global eq?
107 [ hand-loc get-global swap move-hand ] [ drop ] if ;
109 : layout-queued ( -- seq )
113 dup layout find-world [ , ] when*
117 : redraw-worlds ( seq -- )
118 [ dup update-hand draw-world ] each ;
120 : send-queued-gestures ( -- )
121 gesture-queue [ send-queued-gesture notify-queued ] slurp-deque ;
129 ] [ ui-error ] recover ;
133 : ui-running ( quot -- )
134 t \ ui-running set-global
135 [ f \ ui-running set-global ] [ ] cleanup ; inline
137 : ui-running? ( -- ? )
138 \ ui-running get-global ;
140 : update-ui-loop ( -- )
141 [ ui-running? ui-thread get-global self eq? and ]
142 [ ui-notify-flag get lower-flag update-ui ]
145 : start-ui-thread ( -- )
146 [ self ui-thread set-global update-ui-loop ]
147 "UI update" spawn drop ;
149 : open-world-window ( world -- )
150 dup pref-dim >>dim dup relayout graft ;
152 : open-window ( gadget title -- )
153 f <world> open-world-window ;
155 : set-fullscreen? ( ? gadget -- )
156 find-world set-fullscreen* ;
158 : fullscreen? ( gadget -- ? )
159 find-world fullscreen* ;
161 : raise-window ( gadget -- )
162 find-world raise-window* ;
164 HOOK: close-window ui-backend ( gadget -- )
166 M: object close-window
167 find-world [ ungraft ] when* ;
169 : start-ui ( quot -- )
170 call notify-ui-thread start-ui-thread ;
173 f \ ui-running set-global
174 <flag> ui-notify-flag set-global
177 HOOK: (with-ui) ui-backend ( quot -- )
179 : restore-windows ( -- )
181 windows get [ values ] [ delete-all ] bi
182 [ restore-world ] each
186 : restore-windows? ( -- ? )
187 windows get empty? not ;
189 : with-ui ( quot -- )
190 ui-running? [ call ] [ '[ init-ui @ ] (with-ui) ] if ;
192 HOOK: beep ui-backend ( -- )