]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/ui.factor
ui.*: lets rename ui-running to with-ui-running
[factor.git] / basis / ui / ui.factor
1 ! Copyright (C) 2006, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs boxes classes.tuple
4 classes.tuple.parser combinators combinators.short-circuit
5 concurrency.flags concurrency.promises continuations deques
6 destructors dlists fry init kernel lexer make math
7 math.functions namespaces parser sequences sets strings threads
8 ui.backend ui.gadgets ui.gadgets.private ui.gadgets.worlds
9 ui.gestures ui.render vectors vocabs.parser words ;
10 IN: ui
11
12 <PRIVATE
13
14 ! Assoc mapping aliens to gadgets
15 SYMBOL: ui-windows
16
17 : window ( handle -- world ) ui-windows get-global at ;
18
19 : register-window ( world handle -- )
20     ! Add the new window just below the topmost window. Why?
21     ! So that if the new window doesn't actually receive focus
22     ! (eg, we're using focus follows mouse and the mouse is not
23     ! in the new window when it appears) Factor doesn't get
24     ! confused and send workspace operations to the new window,
25     ! etc.
26     swap 2array ui-windows get-global push
27     ui-windows get-global dup length 1 >
28     [ [ length 1 - dup 1 - ] keep exchange ] [ drop ] if ;
29
30 : unregister-window ( handle -- )
31     ui-windows [ [ first = ] with reject ] change-global ;
32
33 : raised-window ( world -- )
34     ui-windows get-global
35     [ [ second eq? ] with find drop ] keep
36     [ nth ] [ remove-nth! drop ] [ nip ] 2tri push ;
37
38 : focus-gestures ( new old -- )
39     drop-prefix <reversed>
40     lose-focus swap each-gesture
41     gain-focus swap each-gesture ;
42
43 : ?grab-input ( world -- )
44     dup grab-input?>> [ handle>> (grab-input) ] [ drop ] if ;
45
46 : ?ungrab-input ( world -- )
47     dup grab-input?>> [ handle>> (ungrab-input) ] [ drop ] if ;
48
49 : focus-world ( world -- )
50     t >>focused?
51     [ ?grab-input ] [
52         dup raised-window
53         focus-path f focus-gestures
54     ] bi ;
55
56 : unfocus-world ( world -- )
57     f >>focused?
58     [ ?ungrab-input ]
59     [ focus-path f swap focus-gestures ] bi ;
60
61 : set-up-window ( world -- )
62     {
63         [ set-gl-context ]
64         [ [ title>> ] keep set-title ]
65         [ begin-world ]
66         [ resize-world ]
67         [ request-focus ]
68     } cleave gl-init ;
69
70 : clean-up-broken-window ( world -- )
71     [
72         dup { [ focused?>> ] [ grab-input?>> ] } 1&&
73         [ handle>> (ungrab-input) ] [ drop ] if
74     ] [ handle>> (close-window) ] bi ;
75
76 M: world graft*
77     [ (open-window) ]
78     [
79         [ set-up-window ] [ ] [ clean-up-broken-window ] cleanup
80     ] bi ;
81
82 : dispose-window-resources ( world -- )
83     [ <reversed> [ [ dispose ] when* ] each V{ } clone ] change-window-resources drop ;
84
85 M: world ungraft*
86     {
87         [ set-gl-context ]
88         [ text-handle>> [ dispose ] when* ]
89         [ images>> [ dispose ] when* ]
90         [ hand-clicked close-global ]
91         [ hand-gadget close-global ]
92         [ end-world ]
93         [ dispose-window-resources ]
94         [ unfocus-world ]
95         [ [ (close-window) f ] change-handle drop ]
96         [ promise>> t swap fulfill ]
97     } cleave ;
98
99 : init-ui ( -- )
100     <box> drag-timer set-global
101     f hand-gadget set-global
102     f hand-clicked set-global
103     f hand-world set-global
104     f world set-global
105     <dlist> \ graft-queue set-global
106     100 <vector> \ layout-queue set-global
107     <dlist> \ gesture-queue set-global
108     V{ } clone ui-windows set-global ;
109
110 : update-hand ( world -- )
111     dup hand-world get-global eq?
112     [ hand-loc get-global swap move-hand ] [ drop ] if ;
113
114 : slurp-vector ( .. seq quot: ( ... elt -- .. ) -- )
115     over '[ _ empty? not ] -rot '[ _ pop @ ] while ; inline
116
117 : layout-queued ( -- seq )
118     layout-queue [
119         in-layout? on
120         [ dup layout find-world [ , ] when* ] slurp-vector
121     ] { } make members ;
122
123 : redraw-worlds ( seq -- )
124     [ dup update-hand draw-world ] each ;
125
126 : send-queued-gestures ( -- )
127     gesture-queue [ send-queued-gesture notify-queued ] slurp-deque ;
128
129 : update-ui ( -- )
130     notify-queued
131     layout-queued
132     redraw-worlds
133     send-queued-gestures ;
134
135 SYMBOL: ui-running
136
137 : with-ui-running ( quot -- )
138     t ui-running set-global
139     [ f ui-running set-global ] [ ] cleanup ; inline
140
141 PRIVATE>
142
143 : find-window ( quot: ( world -- ? ) -- world/f )
144     [ ui-windows get-global values ] dip
145     '[ dup children>> [ ] [ nip first ] if-empty @ ]
146     find-last nip ; inline
147
148 : ui-running? ( -- ? )
149     ui-running get-global ;
150
151 <PRIVATE
152
153 : update-ui-loop ( -- )
154     ! Note the logic: if update-ui fails, we open an error window and
155     ! run one iteration of update-ui. If that also fails, well, the
156     ! whole UI subsystem is broken so we throw the error to terminate
157     ! the update-ui-loop.
158     [ ui-running? ]
159     [
160         ui-notify-flag get lower-flag
161         [ update-ui ] [
162             [ ui-error update-ui ] [
163                 stop-event-loop nip rethrow
164             ] recover
165         ] recover
166     ] while ;
167
168 : start-ui-thread ( -- )
169     [ update-ui-loop ] "UI update" spawn drop ;
170
171 : start-ui ( quot -- )
172     call( -- ) notify-ui-thread start-ui-thread ;
173
174 : ?attributes ( gadget title/attributes -- attributes )
175     dup string? [ <world-attributes> swap >>title ] [ clone ] if
176     swap [ [ [ 1array ] [ f ] if* ] curry unless* ] curry change-gadgets ;
177
178 PRIVATE>
179
180 : open-world-window ( world -- )
181     dup pref-dim [ ceiling ] map >>dim dup relayout graft ;
182
183 : open-window* ( gadget title/attributes -- window )
184     ?attributes <world> [ open-world-window ] keep ;
185
186 : open-window ( gadget title/attributes -- )
187     open-window* drop ;
188
189 : set-fullscreen ( gadget ? -- )
190     [ find-world ] dip (set-fullscreen) ;
191
192 : fullscreen? ( gadget -- ? )
193     find-world (fullscreen?) ;
194
195 : toggle-fullscreen ( gadget -- )
196     dup fullscreen? not set-fullscreen ;
197
198 : raise-window ( gadget -- )
199     find-world raise-window* ;
200
201 : topmost-window ( -- world )
202     ui-windows get-global last second ;
203
204 HOOK: close-window ui-backend ( gadget -- )
205
206 M: object close-window
207     find-world [ ungraft ] when* ;
208
209 [
210     f ui-running set-global
211     <flag> ui-notify-flag set-global
212 ] "ui" add-startup-hook
213
214 HOOK: resize-window ui-backend ( world dim -- )
215 M: object resize-window 2drop ;
216
217 : relayout-window ( gadget -- )
218     [ relayout ]
219     [ find-world [ dup pref-dim resize-window ] when* ] bi ;
220
221 : with-ui ( quot: ( -- ) -- )
222     ui-running? [ call( -- ) ] [ '[ init-ui @ ] (with-ui) ] if ;
223
224 HOOK: beep ui-backend ( -- )
225
226 HOOK: system-alert ui-backend ( caption text -- )
227
228 : parse-window-attributes ( class -- attributes )
229     "{" expect dup all-slots parse-tuple-literal-slots ;
230
231 : define-window ( word attributes quot -- )
232     '[ [ f _ clone @ open-window ] with-ui ] ( -- ) define-declared ;
233
234 SYNTAX: WINDOW:
235     scan-new-word
236     world-attributes parse-window-attributes
237     parse-definition
238     define-window ;
239
240 SYNTAX: MAIN-WINDOW:
241     scan-new-word
242     world-attributes parse-window-attributes
243     parse-definition
244     [ define-window ] [ 2drop current-vocab main<< ] 3bi ;