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