]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/ui.factor
ui: the layout-queue is more efficient as a vector than a dlist
[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 namespaces parser
7 sequences sets strings threads ui.backend ui.gadgets
8 ui.gadgets.private ui.gadgets.worlds ui.gestures vectors vocabs.parser
9 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         [ t >>active? drop ]
68         [ request-focus ]
69     } cleave ;
70
71 : clean-up-broken-window ( world -- )
72     [
73         dup { [ focused?>> ] [ grab-input?>> ] } 1&&
74         [ handle>> (ungrab-input) ] [ drop ] if
75     ] [ handle>> (close-window) ] bi ;
76
77 M: world graft*
78     [ (open-window) ]
79     [
80         [ set-up-window ]
81         [ [ clean-up-broken-window ] [ ui-error ] bi* ] recover
82     ] bi ;
83
84 : dispose-window-resources ( world -- )
85     [ <reversed> [ [ dispose ] when* ] each V{ } clone ] change-window-resources drop ;
86
87 M: world ungraft*
88     {
89         [ set-gl-context ]
90         [ text-handle>> [ dispose ] when* ]
91         [ images>> [ dispose ] when* ]
92         [ hand-clicked close-global ]
93         [ hand-gadget close-global ]
94         [ end-world ]
95         [ dispose-window-resources ]
96         [ unfocus-world ]
97         [ [ (close-window) f ] change-handle drop ]
98         [ promise>> t swap fulfill ]
99     } cleave ;
100
101 : init-ui ( -- )
102     <box> drag-timer set-global
103     f hand-gadget set-global
104     f hand-clicked set-global
105     f hand-world set-global
106     f world set-global
107     <dlist> \ graft-queue set-global
108     100 <vector> \ layout-queue set-global
109     <dlist> \ gesture-queue set-global
110     V{ } clone ui-windows set-global ;
111
112 : update-hand ( world -- )
113     dup hand-world get-global eq?
114     [ hand-loc get-global swap move-hand ] [ drop ] if ;
115
116 : slurp-vector ( .. seq quot: ( ... elt -- .. ) -- )
117     over '[ _ empty? not ] -rot '[ _ pop @ ] while ; inline
118
119 : layout-queued ( -- seq )
120     layout-queue [
121         in-layout? on
122         [ dup layout find-world [ , ] when* ] slurp-vector
123     ] { } make members ;
124
125 : redraw-worlds ( seq -- )
126     [ dup update-hand draw-world ] each ;
127
128 : send-queued-gestures ( -- )
129     gesture-queue [ send-queued-gesture notify-queued ] slurp-deque ;
130
131 : update-ui ( -- )
132     notify-queued
133     layout-queued
134     redraw-worlds
135     send-queued-gestures ;
136
137 SYMBOL: ui-thread
138
139 : ui-running ( quot -- )
140     t \ ui-running set-global
141     [ f \ ui-running set-global ] [ ] cleanup ; inline
142
143 PRIVATE>
144
145 : find-window ( quot: ( world -- ? ) -- world )
146     [ ui-windows get-global values ] dip
147     '[ dup children>> [ ] [ nip first ] if-empty @ ]
148     find-last nip ; inline
149
150 : ui-running? ( -- ? )
151     \ ui-running get-global ;
152
153 <PRIVATE
154
155 : update-ui-loop ( -- )
156     ! Note the logic: if update-ui fails, we open an error window
157     ! and run one iteration of update-ui. If that also fails, well,
158     ! the whole UI subsystem is broken so we exit out of the
159     ! update-ui-loop.
160     [ { [ ui-running? ] [ ui-thread get-global self eq? ] } 0&& ]
161     [
162         ui-notify-flag get lower-flag
163         [ update-ui ] [ ui-error update-ui ] recover
164     ] while ;
165
166 : start-ui-thread ( -- )
167     [ self ui-thread set-global update-ui-loop ]
168     "UI update" spawn drop ;
169
170 : start-ui ( quot -- )
171     call( -- ) notify-ui-thread start-ui-thread ;
172
173 : ?attributes ( gadget title/attributes -- attributes )
174     dup string? [ <world-attributes> swap >>title ] [ clone ] if
175     swap [ [ [ 1array ] [ f ] if* ] curry unless* ] curry change-gadgets ;
176
177 PRIVATE>
178
179 : open-world-window ( world -- )
180     dup pref-dim >>dim dup relayout graft ;
181
182 : open-window* ( gadget title/attributes -- window )
183     ?attributes <world> [ open-world-window ] keep ;
184
185 : open-window ( gadget title/attributes -- )
186     open-window* drop ;
187
188 : set-fullscreen ( gadget ? -- )
189     [ find-world ] dip (set-fullscreen) ;
190
191 : fullscreen? ( gadget -- ? )
192     find-world (fullscreen?) ;
193
194 : toggle-fullscreen ( gadget -- )
195     dup fullscreen? not set-fullscreen ;
196
197 : raise-window ( gadget -- )
198     find-world raise-window* ;
199
200 : topmost-window ( -- world )
201     ui-windows get-global last second ;
202
203 HOOK: close-window ui-backend ( gadget -- )
204
205 M: object close-window
206     find-world [ ungraft ] when* ;
207
208 [
209     f \ ui-running set-global
210     <flag> ui-notify-flag set-global
211 ] "ui" add-startup-hook
212
213 HOOK: resize-window ui-backend ( world dim -- )
214 M: object resize-window 2drop ;
215
216 : relayout-window ( gadget -- )
217     [ relayout ]
218     [ find-world [ dup pref-dim resize-window ] when* ] bi ;
219
220 : with-ui ( quot: ( -- ) -- )
221     ui-running? [ call( -- ) ] [ '[ init-ui @ ] (with-ui) ] if ;
222
223 HOOK: beep ui-backend ( -- )
224
225 HOOK: system-alert ui-backend ( caption text -- )
226
227 : parse-main-window-attributes ( class -- attributes )
228     "{" expect dup all-slots parse-tuple-literal-slots ;
229
230 : define-main-window ( word attributes quot -- )
231     [
232         '[ [ f _ clone @ open-window ] with-ui ] ( -- ) define-declared
233     ] [ 2drop current-vocab main<< ] 3bi ;
234
235 SYNTAX: MAIN-WINDOW:
236     scan-new-word
237     world-attributes parse-main-window-attributes
238     parse-definition
239     define-main-window ;