]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/ui.factor
Merge branch 'mongodb-changes' of git://github.com/x6j8x/factor
[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
7 parser sequences sets strings threads ui.backend ui.gadgets
8 ui.gadgets.private ui.gadgets.worlds ui.gestures vocabs.parser
9 words ;
10 IN: ui
11
12 <PRIVATE
13
14 ! Assoc mapping aliens to gadgets
15 SYMBOL: windows
16
17 : window ( handle -- world ) windows get-global at ;
18
19 : window-focus ( handle -- gadget ) window world-focus ;
20
21 : register-window ( world handle -- )
22     #! Add the new window just below the topmost window. Why?
23     #! So that if the new window doesn't actually receive focus
24     #! (eg, we're using focus follows mouse and the mouse is not
25     #! in the new window when it appears) Factor doesn't get
26     #! confused and send workspace operations to the new window,
27     #! etc.
28     swap 2array windows get-global push
29     windows get-global dup length 1 >
30     [ [ length 1 - dup 1 - ] keep exchange ] [ drop ] if ;
31
32 : unregister-window ( handle -- )
33     windows [ [ first = not ] with filter ] change-global ;
34
35 : raised-window ( world -- )
36     windows get-global
37     [ [ second eq? ] with find drop ] keep
38     [ nth ] [ remove-nth! drop ] [ nip ] 2tri push ;
39
40 : focus-gestures ( new old -- )
41     drop-prefix <reversed>
42     lose-focus swap each-gesture
43     gain-focus swap each-gesture ;
44
45 : ?grab-input ( world -- )
46     dup grab-input?>> [ handle>> (grab-input) ] [ drop ] if ;
47
48 : ?ungrab-input ( world -- )
49     dup grab-input?>> [ handle>> (ungrab-input) ] [ drop ] if ;
50
51 : focus-world ( world -- )
52     t >>focused?
53     [ ?grab-input ] [
54         dup raised-window
55         focus-path f focus-gestures
56     ] bi ;
57
58 : unfocus-world ( world -- )
59     f >>focused?
60     [ ?ungrab-input ]
61     [ focus-path f swap focus-gestures ] bi ;
62
63 : set-up-window ( world -- )
64     {
65         [ set-gl-context ]
66         [ [ title>> ] keep set-title ]
67         [ begin-world ]
68         [ resize-world ]
69         [ t >>active? drop ]
70         [ request-focus ]
71     } cleave ;
72
73 : clean-up-broken-window ( world -- )
74     [
75         dup { [ focused?>> ] [ grab-input?>> ] } 1&&
76         [ handle>> (ungrab-input) ] [ drop ] if
77     ] [ handle>> (close-window) ] bi ;
78
79 M: world graft*
80     [ (open-window) ]
81     [
82         [ set-up-window ]
83         [ [ clean-up-broken-window ] [ ui-error ] bi* ] recover
84     ] bi ;
85
86 M: world ungraft*
87     {
88         [ set-gl-context ]
89         [ text-handle>> [ dispose ] when* ]
90         [ images>> [ dispose ] when* ]
91         [ hand-clicked close-global ]
92         [ hand-gadget close-global ]
93         [ end-world ]
94         [ [ <reversed> [ [ dispose ] when* ] each V{ } clone ] change-window-resources drop ]
95         [ [ (close-window) f ] change-handle drop ]
96         [ unfocus-world ]
97         [ promise>> t swap fulfill ]
98     } cleave ;
99
100 : init-ui ( -- )
101     <box> drag-timer set-global
102     f hand-gadget set-global
103     f hand-clicked set-global
104     f hand-world set-global
105     f world set-global
106     <dlist> \ graft-queue set-global
107     <dlist> \ layout-queue set-global
108     <dlist> \ gesture-queue set-global
109     V{ } clone windows set-global ;
110
111 : update-hand ( world -- )
112     dup hand-world get-global eq?
113     [ hand-loc get-global swap move-hand ] [ drop ] if ;
114
115 : layout-queued ( -- seq )
116     [
117         in-layout? on
118         layout-queue [
119             dup layout find-world [ , ] when*
120         ] slurp-deque
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-thread
136
137 : 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 )
144     [ windows get 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
155     #! and run one iteration of update-ui. If that also fails, well,
156     #! the whole UI subsystem is broken so we exit out of the
157     #! update-ui-loop.
158     [ { [ ui-running? ] [ ui-thread get-global self eq? ] } 0&& ]
159     [
160         ui-notify-flag get lower-flag
161         [ update-ui ] [ ui-error update-ui ] recover
162     ] while ;
163
164 : start-ui-thread ( -- )
165     [ self ui-thread set-global update-ui-loop ]
166     "UI update" spawn drop ;
167
168 : start-ui ( quot -- )
169     call( -- ) notify-ui-thread start-ui-thread ;
170
171 : ?attributes ( gadget title/attributes -- attributes )
172     dup string? [ world-attributes new swap >>title ] [ clone ] if
173     swap [ [ [ 1array ] [ f ] if* ] curry unless* ] curry change-gadgets ;
174
175 PRIVATE>
176
177 : open-world-window ( world -- )
178     dup pref-dim >>dim dup relayout graft ;
179
180 : open-window* ( gadget title/attributes -- window )
181     ?attributes <world> [ open-world-window ] keep ;
182
183 : open-window ( gadget title/attributes -- )
184     open-window* drop ;
185
186 : set-fullscreen ( gadget ? -- )
187     [ find-world ] dip (set-fullscreen) ;
188
189 : fullscreen? ( gadget -- ? )
190     find-world (fullscreen?) ;
191
192 : toggle-fullscreen ( gadget -- )
193     dup fullscreen? not set-fullscreen ;
194
195 : raise-window ( gadget -- )
196     find-world raise-window* ;
197
198 : topmost-window ( -- world )
199     windows get last second ;
200
201 HOOK: close-window ui-backend ( gadget -- )
202
203 M: object close-window
204     find-world [ ungraft ] when* ;
205
206 [
207     f \ ui-running set-global
208     <flag> ui-notify-flag set-global
209 ] "ui" add-startup-hook
210
211 : with-ui ( quot: ( -- ) -- )
212     ui-running? [ call( -- ) ] [ '[ init-ui @ ] (with-ui) ] if ;
213
214 HOOK: beep ui-backend ( -- )
215
216 HOOK: system-alert ui-backend ( caption text -- )
217
218 : parse-main-window-attributes ( class -- attributes )
219     "{" expect dup all-slots parse-tuple-literal-slots ;
220
221 : define-main-window ( word attributes quot -- )
222     [
223         '[ [ f _ clone @ open-window ] with-ui ] (( -- )) define-declared
224     ] [ 2drop current-vocab main<< ] 3bi ;
225
226 SYNTAX: MAIN-WINDOW:
227     CREATE
228     world-attributes parse-main-window-attributes
229     parse-definition
230     define-main-window ;