]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/ui.factor
f561f3cd49164828a465c4c35d850f4e5cc3770e
[factor.git] / basis / ui / ui.factor
1 ! Copyright (C) 2006, 2007 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays assocs io kernel math models namespaces make
4 prettyprint dlists deques sequences threads sequences words
5 debugger ui.gadgets ui.gadgets.worlds ui.gadgets.tracks
6 ui.gestures ui.backend ui.render continuations init combinators
7 hashtables concurrency.flags sets accessors ;
8 IN: ui
9
10 ! Assoc mapping aliens to gadgets
11 SYMBOL: windows
12
13 SYMBOL: stop-after-last-window?
14
15 : event-loop? ( -- ? )
16     {
17         { [ stop-after-last-window? get not ] [ t ] }
18         { [ graft-queue deque-empty? not ] [ t ] }
19         { [ windows get-global empty? not ] [ t ] }
20         [ f ]
21     } cond ;
22
23 : event-loop ( -- ) [ event-loop? ] [ do-events ] [ ] while ;
24
25 : window ( handle -- world ) windows get-global at ;
26
27 : window-focus ( handle -- gadget ) window world-focus ;
28
29 : register-window ( world handle -- )
30     #! Add the new window just below the topmost window. Why?
31     #! So that if the new window doesn't actually receive focus
32     #! (eg, we're using focus follows mouse and the mouse is not
33     #! in the new window when it appears) Factor doesn't get
34     #! confused and send workspace operations to the new window,
35     #! etc.
36     swap 2array windows get-global push
37     windows get-global dup length 1 >
38     [ [ length 1- dup 1- ] keep exchange ] [ drop ] if ;
39
40 : unregister-window ( handle -- )
41     windows global [ [ first = not ] with filter ] change-at ;
42
43 : raised-window ( world -- )
44     windows get-global
45     [ [ second eq? ] with find drop ] keep
46     [ nth ] [ delete-nth ] [ nip ] 2tri push ;
47
48 : focus-gestures ( new old -- )
49     drop-prefix <reversed>
50     T{ lose-focus } swap each-gesture
51     T{ gain-focus } swap each-gesture ;
52
53 : focus-world ( world -- )
54     t >>focused?
55     dup raised-window
56     focus-path f focus-gestures ;
57
58 : unfocus-world ( world -- )
59     f >>focused?
60     focus-path f swap focus-gestures ;
61
62 M: world graft*
63     dup (open-window)
64     dup title>> over set-title
65     request-focus ;
66
67 : reset-world ( world -- )
68     #! This is used when a window is being closed, but also
69     #! when restoring saved worlds on image startup.
70     dup fonts>> clear-assoc
71     dup unfocus-world
72     f >>handle drop ;
73
74 M: world ungraft*
75     dup free-fonts
76     dup hand-clicked close-global
77     dup hand-gadget close-global
78     dup handle>> (close-window)
79     reset-world ;
80
81 : find-window ( quot -- world )
82     windows get values
83     [ gadget-child swap call ] with find-last nip ; inline
84
85 SYMBOL: ui-hook
86
87 : init-ui ( -- )
88     <dlist> \ graft-queue set-global
89     <dlist> \ layout-queue set-global
90     V{ } clone windows set-global ;
91
92 : restore-gadget-later ( gadget -- )
93     dup graft-state>> {
94         { { f f } [ ] }
95         { { f t } [ ] }
96         { { t t } [ { f f } >>graft-state ] }
97         { { t f } [ dup unqueue-graft { f f } >>graft-state ] }
98     } case graft-later ;
99
100 : restore-gadget ( gadget -- )
101     dup restore-gadget-later
102     children>> [ restore-gadget ] each ;
103
104 : restore-world ( world -- )
105     dup reset-world restore-gadget ;
106
107 : restore-windows ( -- )
108     windows get [ values ] keep delete-all
109     [ restore-world ] each
110     forget-rollover ;
111
112 : restore-windows? ( -- ? )
113     windows get empty? not ;
114
115 : update-hand ( world -- )
116     dup hand-world get-global eq?
117     [ hand-loc get-global swap move-hand ] [ drop ] if ;
118
119 : layout-queued ( -- seq )
120     [
121         in-layout? on
122         layout-queue [
123             dup layout find-world [ , ] when*
124         ] slurp-deque
125     ] { } make prune ;
126
127 : redraw-worlds ( seq -- )
128     [ dup update-hand draw-world ] each ;
129
130 : notify ( gadget -- )
131     dup graft-state>>
132     dup first { f f } { t t } ?
133     pick (>>graft-state) {
134         { { f t } [ dup activate-control graft* ] }
135         { { t f } [ dup deactivate-control ungraft* ] }
136     } case ;
137
138 : notify-queued ( -- )
139     graft-queue [ notify ] slurp-deque ;
140
141 : update-ui ( -- )
142     [ notify-queued layout-queued redraw-worlds ] assert-depth ;
143
144 : ui-wait ( -- )
145     10 sleep ;
146
147 : ui-try ( quot -- ) [ ui-error ] recover ;
148
149 SYMBOL: ui-thread
150
151 : ui-running ( quot -- )
152     t \ ui-running set-global
153     [ f \ ui-running set-global ] [ ] cleanup ; inline
154
155 : ui-running? ( -- ? )
156     \ ui-running get-global ;
157
158 : update-ui-loop ( -- )
159     ui-running? ui-thread get-global self eq? and [
160         ui-notify-flag get lower-flag
161         [ update-ui ] ui-try
162         update-ui-loop
163     ] when ;
164
165 : start-ui-thread ( -- )
166     [ self ui-thread set-global update-ui-loop ]
167     "UI update" spawn drop ;
168
169 : open-world-window ( world -- )
170     dup pref-dim >>dim dup relayout graft ;
171
172 : open-window ( gadget title -- )
173     f <world> open-world-window ;
174
175 : set-fullscreen? ( ? gadget -- )
176     find-world set-fullscreen* ;
177
178 : fullscreen? ( gadget -- ? )
179     find-world fullscreen* ;
180
181 : raise-window ( gadget -- )
182     find-world raise-window* ;
183
184 HOOK: close-window ui-backend ( gadget -- )
185
186 M: object close-window
187     find-world [ ungraft ] when* ;
188
189 : start-ui ( -- )
190     restore-windows? [
191         restore-windows
192     ] [
193         init-ui ui-hook get call
194     ] if
195     notify-ui-thread start-ui-thread ;
196
197 [
198     f \ ui-running set-global
199     <flag> ui-notify-flag set-global
200 ] "ui" add-init-hook
201
202 HOOK: ui ui-backend ( -- )
203
204 MAIN: ui
205
206 : with-ui ( quot -- )
207     ui-running? [
208         call
209     ] [
210         f windows set-global
211         [
212             ui-hook set
213             stop-after-last-window? on
214             ui
215         ] with-scope
216     ] if ;