]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/ui.factor
Delete empty unit tests files, remove 1- and 1+, reorder IN: lines in a lot of places...
[factor.git] / basis / ui / ui.factor
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 words continuations init
5 combinators combinators.short-circuit hashtables concurrency.flags
6 sets accessors calendar fry destructors ui.gadgets ui.gadgets.private
7 ui.gadgets.worlds ui.gadgets.tracks ui.gestures ui.backend ui.render
8 strings ;
9 IN: ui
10
11 <PRIVATE
12
13 ! Assoc mapping aliens to gadgets
14 SYMBOL: windows
15
16 : window ( handle -- world ) windows get-global at ;
17
18 : window-focus ( handle -- gadget ) window world-focus ;
19
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,
26     #! etc.
27     swap 2array windows get-global push
28     windows get-global dup length 1 >
29     [ [ length 1 - dup 1 - ] keep exchange ] [ drop ] if ;
30
31 : unregister-window ( handle -- )
32     windows [ [ first = not ] with filter ] change-global ;
33
34 : raised-window ( world -- )
35     windows get-global
36     [ [ second eq? ] with find drop ] keep
37     [ nth ] [ delete-nth ] [ nip ] 2tri push ;
38
39 : focus-gestures ( new old -- )
40     drop-prefix <reversed>
41     lose-focus swap each-gesture
42     gain-focus swap each-gesture ;
43
44 : ?grab-input ( world -- )
45     dup grab-input?>> [ handle>> (grab-input) ] [ drop ] if ;
46
47 : ?ungrab-input ( world -- )
48     dup grab-input?>> [ handle>> (ungrab-input) ] [ drop ] if ;
49
50 : focus-world ( world -- )
51     t >>focused?
52     [ ?grab-input ] [
53         dup raised-window
54         focus-path f focus-gestures
55     ] bi ;
56
57 : unfocus-world ( world -- )
58     f >>focused?
59     [ ?ungrab-input ]
60     [ focus-path f swap focus-gestures ] bi ;
61
62 : set-up-window ( world -- )
63     {
64         [ set-gl-context ]
65         [ [ title>> ] keep set-title ]
66         [ begin-world ]
67         [ resize-world ]
68         [ t >>active? drop ]
69         [ request-focus ]
70     } cleave ;
71
72 : clean-up-broken-window ( world -- )
73     [
74         dup { [ focused?>> ] [ grab-input?>> ] } 1&&
75         [ handle>> (ungrab-input) ] [ drop ] if
76     ] [ handle>> (close-window) ] bi ;
77
78 M: world graft*
79     [ (open-window) ]
80     [
81         [ set-up-window ]
82         [ [ clean-up-broken-window ] [ ui-error ] bi* ] recover
83     ] bi ;
84
85 : reset-world ( world -- )
86     #! This is used when a window is being closed, but also
87     #! when restoring saved worlds on image startup.
88     f >>handle unfocus-world ;
89
90 : (ungraft-world) ( world -- )
91     {
92         [ set-gl-context ]
93         [ text-handle>> [ dispose ] when* ]
94         [ images>> [ dispose ] when* ]
95         [ hand-clicked close-global ]
96         [ hand-gadget close-global ]
97         [ end-world ]
98         [ [ <reversed> [ [ dispose ] when* ] each V{ } clone ] change-window-resources drop ]
99     } cleave ;
100
101 M: world ungraft*
102     [ (ungraft-world) ]
103     [ handle>> (close-window) ]
104     [ reset-world ] tri ;
105
106 : init-ui ( -- )
107     <dlist> \ graft-queue set-global
108     <dlist> \ layout-queue set-global
109     <dlist> \ gesture-queue set-global
110     V{ } clone windows set-global ;
111
112 : restore-gadget-later ( gadget -- )
113     dup graft-state>> {
114         { { f f } [ ] }
115         { { f t } [ ] }
116         { { t t } [ { f f } >>graft-state ] }
117         { { t f } [ dup unqueue-graft { f f } >>graft-state ] }
118     } case graft-later ;
119
120 : restore-gadget ( gadget -- )
121     dup restore-gadget-later
122     children>> [ restore-gadget ] each ;
123
124 : restore-world ( world -- )
125     {
126         [ reset-world ]
127         [ f >>text-handle f >>images drop ]
128         [ restore-gadget ]
129     } cleave ;
130
131 : update-hand ( world -- )
132     dup hand-world get-global eq?
133     [ hand-loc get-global swap move-hand ] [ drop ] if ;
134
135 : layout-queued ( -- seq )
136     [
137         in-layout? on
138         layout-queue [
139             dup layout find-world [ , ] when*
140         ] slurp-deque
141     ] { } make prune ;
142
143 : redraw-worlds ( seq -- )
144     [ dup update-hand draw-world ] each ;
145
146 : send-queued-gestures ( -- )
147     gesture-queue [ send-queued-gesture notify-queued ] slurp-deque ;
148
149 : update-ui ( -- )
150     notify-queued
151     layout-queued
152     redraw-worlds
153     send-queued-gestures ;
154
155 SYMBOL: ui-thread
156
157 : ui-running ( quot -- )
158     t \ ui-running set-global
159     [ f \ ui-running set-global ] [ ] cleanup ; inline
160
161 PRIVATE>
162
163 : find-window ( quot -- world )
164     [ windows get values ] dip
165     '[ dup children>> [ ] [ nip first ] if-empty @ ]
166     find-last nip ; inline
167
168 : ui-running? ( -- ? )
169     \ ui-running get-global ;
170
171 <PRIVATE
172
173 : update-ui-loop ( -- )
174     #! Note the logic: if update-ui fails, we open an error window
175     #! and run one iteration of update-ui. If that also fails, well,
176     #! the whole UI subsystem is broken so we exit out of the
177     #! update-ui-loop.
178     [ { [ ui-running? ] [ ui-thread get-global self eq? ] } 0&& ]
179     [
180         ui-notify-flag get lower-flag
181         [ update-ui ] [ ui-error update-ui ] recover
182     ] while ;
183
184 : start-ui-thread ( -- )
185     [ self ui-thread set-global update-ui-loop ]
186     "UI update" spawn drop ;
187
188 : start-ui ( quot -- )
189     call( -- ) notify-ui-thread start-ui-thread ;
190
191 : restore-windows ( -- )
192     [
193         windows get [ values ] [ delete-all ] bi
194         [ restore-world ] each
195         forget-rollover
196     ] (with-ui) ;
197
198 : restore-windows? ( -- ? )
199     windows get empty? not ;
200
201 : ?attributes ( gadget title/attributes -- attributes )
202     dup string? [ world-attributes new swap >>title ] [ clone ] if
203     swap [ [ [ 1array ] [ f ] if* ] curry unless* ] curry change-gadgets ;
204
205 PRIVATE>
206
207 : open-world-window ( world -- )
208     dup pref-dim >>dim dup relayout graft ;
209
210 : open-window* ( gadget title/attributes -- window )
211     ?attributes <world> [ open-world-window ] keep ;
212
213 : open-window ( gadget title/attributes -- )
214     open-window* drop ;
215
216 : set-fullscreen ( gadget ? -- )
217     [ find-world ] dip (set-fullscreen) ;
218
219 : fullscreen? ( gadget -- ? )
220     find-world (fullscreen?) ;
221
222 : toggle-fullscreen ( gadget -- )
223     dup fullscreen? not set-fullscreen ;
224
225 : raise-window ( gadget -- )
226     find-world raise-window* ;
227
228 : topmost-window ( -- world )
229     windows get last second ;
230
231 HOOK: close-window ui-backend ( gadget -- )
232
233 M: object close-window
234     find-world [ ungraft ] when* ;
235
236 [
237     f \ ui-running set-global
238     <flag> ui-notify-flag set-global
239 ] "ui" add-init-hook
240
241 : with-ui ( quot -- )
242     ui-running? [ call( -- ) ] [ '[ init-ui @ ] (with-ui) ] if ;
243
244 HOOK: beep ui-backend ( -- )