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