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 -- )
98 [ reset-world ] [ init-text-rendering ] [ restore-gadget ] tri ;
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 : send-queued-gestures ( -- )
116 gesture-queue [ send-queued-gesture notify-queued ] slurp-deque ;
124 ] [ ui-error ] recover ;
128 : ui-running ( quot -- )
129 t \ ui-running set-global
130 [ f \ ui-running set-global ] [ ] cleanup ; inline
132 : ui-running? ( -- ? )
133 \ ui-running get-global ;
135 : update-ui-loop ( -- )
136 [ ui-running? ui-thread get-global self eq? and ]
137 [ ui-notify-flag get lower-flag update-ui ]
140 : start-ui-thread ( -- )
141 [ self ui-thread set-global update-ui-loop ]
142 "UI update" spawn drop ;
144 : open-world-window ( world -- )
145 dup pref-dim >>dim dup relayout graft ;
147 : open-window ( gadget title -- )
148 f <world> open-world-window ;
150 : set-fullscreen? ( ? gadget -- )
151 find-world set-fullscreen* ;
153 : fullscreen? ( gadget -- ? )
154 find-world fullscreen* ;
156 : raise-window ( gadget -- )
157 find-world raise-window* ;
159 HOOK: close-window ui-backend ( gadget -- )
161 M: object close-window
162 find-world [ ungraft ] when* ;
164 : start-ui ( quot -- )
165 call notify-ui-thread start-ui-thread ;
168 f \ ui-running set-global
169 <flag> ui-notify-flag set-global
172 HOOK: (with-ui) ui-backend ( quot -- )
174 : restore-windows ( -- )
176 windows get [ values ] [ delete-all ] bi
177 [ restore-world ] each
181 : restore-windows? ( -- ? )
182 windows get empty? not ;
184 : with-ui ( quot -- )
185 ui-running? [ call ] [ '[ init-ui @ ] (with-ui) ] if ;
187 HOOK: beep ui-backend ( -- )