1 ! Copyright (C) 2006, 2007 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays assocs io kernel math models namespaces make
4 prettyprint dlists deques sequences threads sequences words
5 debugger ui.gadgets ui.gadgets.worlds ui.gadgets.tracks
6 ui.gestures ui.backend ui.render continuations init combinators
7 hashtables concurrency.flags sets accessors ;
10 ! Assoc mapping aliens to gadgets
13 SYMBOL: stop-after-last-window?
15 : event-loop? ( -- ? )
17 { [ stop-after-last-window? get not ] [ t ] }
18 { [ graft-queue deque-empty? not ] [ t ] }
19 { [ windows get-global empty? not ] [ t ] }
23 : event-loop ( -- ) [ event-loop? ] [ do-events ] [ ] while ;
25 : window ( handle -- world ) windows get-global at ;
27 : window-focus ( handle -- gadget ) window world-focus ;
29 : register-window ( world handle -- )
30 #! Add the new window just below the topmost window. Why?
31 #! So that if the new window doesn't actually receive focus
32 #! (eg, we're using focus follows mouse and the mouse is not
33 #! in the new window when it appears) Factor doesn't get
34 #! confused and send workspace operations to the new window,
36 swap 2array windows get-global push
37 windows get-global dup length 1 >
38 [ [ length 1- dup 1- ] keep exchange ] [ drop ] if ;
40 : unregister-window ( handle -- )
41 windows global [ [ first = not ] with filter ] change-at ;
43 : raised-window ( world -- )
45 [ [ second eq? ] with find drop ] keep
46 [ nth ] [ delete-nth ] [ nip ] 2tri push ;
48 : focus-gestures ( new old -- )
49 drop-prefix <reversed>
50 T{ lose-focus } swap each-gesture
51 T{ gain-focus } swap each-gesture ;
53 : focus-world ( world -- )
56 focus-path f focus-gestures ;
58 : unfocus-world ( world -- )
60 focus-path f swap focus-gestures ;
64 dup title>> over set-title
67 : reset-world ( world -- )
68 #! This is used when a window is being closed, but also
69 #! when restoring saved worlds on image startup.
70 dup fonts>> clear-assoc
76 dup hand-clicked close-global
77 dup hand-gadget close-global
78 dup handle>> (close-window)
81 : find-window ( quot -- world )
83 [ gadget-child swap call ] with find-last nip ; inline
88 <dlist> \ graft-queue set-global
89 <dlist> \ layout-queue set-global
90 V{ } clone windows set-global ;
92 : restore-gadget-later ( gadget -- )
96 { { t t } [ { f f } >>graft-state ] }
97 { { t f } [ dup unqueue-graft { f f } >>graft-state ] }
100 : restore-gadget ( gadget -- )
101 dup restore-gadget-later
102 children>> [ restore-gadget ] each ;
104 : restore-world ( world -- )
105 dup reset-world restore-gadget ;
107 : restore-windows ( -- )
108 windows get [ values ] keep delete-all
109 [ restore-world ] each
112 : restore-windows? ( -- ? )
113 windows get empty? not ;
115 : update-hand ( world -- )
116 dup hand-world get-global eq?
117 [ hand-loc get-global swap move-hand ] [ drop ] if ;
119 : layout-queued ( -- seq )
123 dup layout find-world [ , ] when*
127 : redraw-worlds ( seq -- )
128 [ dup update-hand draw-world ] each ;
130 : notify ( gadget -- )
132 dup first { f f } { t t } ?
133 pick (>>graft-state) {
134 { { f t } [ dup activate-control graft* ] }
135 { { t f } [ dup deactivate-control ungraft* ] }
138 : notify-queued ( -- )
139 graft-queue [ notify ] slurp-deque ;
142 [ notify-queued layout-queued redraw-worlds ] assert-depth ;
147 : ui-try ( quot -- ) [ ui-error ] recover ;
151 : ui-running ( quot -- )
152 t \ ui-running set-global
153 [ f \ ui-running set-global ] [ ] cleanup ; inline
155 : ui-running? ( -- ? )
156 \ ui-running get-global ;
158 : update-ui-loop ( -- )
159 ui-running? ui-thread get-global self eq? and [
160 ui-notify-flag get lower-flag
165 : start-ui-thread ( -- )
166 [ self ui-thread set-global update-ui-loop ]
167 "UI update" spawn drop ;
169 : open-world-window ( world -- )
170 dup pref-dim >>dim dup relayout graft ;
172 : open-window ( gadget title -- )
173 f <world> open-world-window ;
175 : set-fullscreen? ( ? gadget -- )
176 find-world set-fullscreen* ;
178 : fullscreen? ( gadget -- ? )
179 find-world fullscreen* ;
181 : raise-window ( gadget -- )
182 find-world raise-window* ;
184 HOOK: close-window ui-backend ( gadget -- )
186 M: object close-window
187 find-world [ ungraft ] when* ;
193 init-ui ui-hook get call
195 notify-ui-thread start-ui-thread ;
198 f \ ui-running set-global
199 <flag> ui-notify-flag set-global
202 HOOK: ui ui-backend ( -- )
206 : with-ui ( quot -- )
213 stop-after-last-window? on