]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/gestures/gestures.factor
edabf341f658e635e99fc51a6ad2fd2337a89382
[factor.git] / basis / ui / gestures / gestures.factor
1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays ascii assocs boxes calendar classes columns
4 combinators combinators.short-circuit deques fry kernel make math
5 math.order math.parser math.vectors namespaces sequences sets system
6 timers ui.gadgets ui.gadgets.private words ;
7 IN: ui.gestures
8
9 : get-gesture-handler ( gesture gadget -- quot )
10     class-of superclasses-of [ "gestures" word-prop ] map assoc-stack ;
11
12 GENERIC: handle-gesture ( gesture gadget -- ? )
13
14 M: object handle-gesture
15     [ nip ]
16     [ get-gesture-handler ] 2bi
17     dup [ call( gadget -- ) f ] [ 2drop t ] if ;
18
19 GENERIC: handles-gesture? ( gesture gadget -- ? )
20
21 M: object handles-gesture? ( gesture gadget -- ? )
22     get-gesture-handler >boolean ;
23
24 : parents-handle-gesture? ( gesture gadget -- ? )
25     [ handles-gesture? not ] with each-parent not ;
26
27 : set-gestures ( class hash -- ) "gestures" set-word-prop ;
28
29 : gesture-queue ( -- deque ) \ gesture-queue get ;
30
31 GENERIC: send-queued-gesture ( request -- )
32
33 TUPLE: send-gesture-tuple gesture gadget ;
34
35 M: send-gesture-tuple send-queued-gesture
36     [ gesture>> ] [ gadget>> ] bi handle-gesture drop ;
37
38 : queue-gesture ( ... class -- )
39     boa gesture-queue push-front notify-ui-thread ; inline
40
41 : send-gesture ( gesture gadget -- )
42     \ send-gesture-tuple queue-gesture ;
43
44 : each-gesture ( gesture seq -- ) [ send-gesture ] with each ;
45
46 TUPLE: propagate-gesture-tuple gesture gadget ;
47
48 : resend-gesture ( gesture gadget -- ? )
49     [ handle-gesture ] with each-parent ;
50
51 M: propagate-gesture-tuple send-queued-gesture
52     [ gesture>> ] [ gadget>> ] bi resend-gesture drop ;
53
54 : propagate-gesture ( gesture gadget -- )
55     \ propagate-gesture-tuple queue-gesture ;
56
57 TUPLE: propagate-key-gesture-tuple gesture world ;
58
59 : world-focus ( world -- gadget )
60     dup focus>> [ world-focus ] [ ] ?if ;
61
62 M: propagate-key-gesture-tuple send-queued-gesture
63     [ gesture>> ] [ world>> world-focus ] bi
64     [ handle-gesture ] with each-parent drop ;
65
66 : propagate-key-gesture ( gesture world -- )
67     \ propagate-key-gesture-tuple queue-gesture ;
68
69 TUPLE: user-input-tuple string world ;
70
71 M: user-input-tuple send-queued-gesture
72     [ string>> ] [ world>> world-focus ] bi
73     [ user-input* ] with each-parent drop ;
74
75 : user-input ( string world -- )
76     '[ _ \ user-input-tuple queue-gesture ] unless-empty ;
77
78 ! Gesture objects
79 TUPLE: drag # ;             C: <drag> drag
80 TUPLE: button-up mods # ;   C: <button-up> button-up
81 TUPLE: button-down mods # ; C: <button-down> button-down
82 TUPLE: file-drop mods ;     C: <file-drop> file-drop
83
84 SYMBOL: dropped-files
85
86 SINGLETONS:
87     motion
88     mouse-scroll
89     mouse-enter mouse-leave
90     lose-focus gain-focus ;
91
92 ! Higher-level actions
93 SINGLETONS:
94     undo-action redo-action
95     cut-action copy-action paste-action
96     delete-action select-all-action
97     left-action right-action up-action down-action
98     zoom-in-action zoom-out-action
99     new-action open-action save-action save-as-action
100     revert-action close-action ;
101
102 UNION: action
103     undo-action redo-action
104     cut-action copy-action paste-action
105     delete-action select-all-action
106     left-action right-action up-action down-action
107     zoom-in-action zoom-out-action
108     new-action open-action save-action save-as-action
109     revert-action close-action ;
110
111 CONSTANT: action-gestures
112     {
113         { "z" undo-action }
114         { "y" redo-action }
115         { "x" cut-action }
116         { "c" copy-action }
117         { "v" paste-action }
118         { "a" select-all-action }
119         { "n" new-action }
120         { "o" open-action }
121         { "s" save-action }
122         { "S" save-as-action }
123         { "w" close-action }
124     }
125
126 ! Modifiers
127 SYMBOLS: C+ A+ M+ S+ ;
128
129 TUPLE: key-gesture mods sym ;
130
131 TUPLE: key-down < key-gesture ;
132
133 : new-key-gesture ( mods sym action? class -- key-gesture )
134     [ [ [ S+ swap remove f like ] dip ] unless ] dip boa ; inline
135
136 : <key-down> ( mods sym action? -- key-down )
137     key-down new-key-gesture ;
138
139 TUPLE: key-up < key-gesture ;
140
141 : <key-up> ( mods sym action? -- key-up )
142     key-up new-key-gesture ;
143
144 ! Hand state
145
146 ! Note that these are only really useful inside an event
147 ! handler, and that the locations hand-loc and hand-click-loc
148 ! are in the co-ordinate system of the world which contains
149 ! the gadget in question.
150 SYMBOL: hand-gadget
151 SYMBOL: hand-world
152 SYMBOL: hand-loc
153 { 0 0 } hand-loc set-global
154
155 SYMBOL: hand-clicked
156 SYMBOL: hand-click-loc
157 SYMBOL: hand-click#
158 SYMBOL: hand-last-button
159 SYMBOL: hand-last-time
160 0 hand-last-button set-global
161 0 hand-last-time set-global
162
163 SYMBOL: hand-buttons
164 V{ } clone hand-buttons set-global
165
166 SYMBOL: scroll-direction
167 { 0 0 } scroll-direction set-global
168
169 SYMBOL: double-click-timeout
170 300 milliseconds double-click-timeout set-global
171
172 : hand-moved? ( -- ? )
173     hand-loc get-global hand-click-loc get-global = not ;
174
175 : button-gesture ( gesture -- )
176     hand-clicked get-global propagate-gesture ;
177
178 : drag-gesture ( -- )
179     hand-buttons get-global
180     [ first <drag> button-gesture ] unless-empty ;
181
182 SYMBOL: drag-timer
183
184 <box> drag-timer set-global
185
186 : start-drag-timer ( -- )
187     hand-buttons get-global empty? [
188         [ drag-gesture ]
189         300 milliseconds
190         100 milliseconds
191         <timer>
192         [ drag-timer get-global >box ]
193         [ start-timer ] bi
194     ] when ;
195
196 : stop-drag-timer ( -- )
197     hand-buttons get-global empty? [
198         drag-timer get-global ?box
199         [ stop-timer ] [ drop ] if
200     ] when ;
201
202 : fire-motion ( -- )
203     hand-buttons get-global empty? [
204         motion hand-gadget get-global propagate-gesture
205     ] [
206         drag-gesture
207     ] if ;
208
209 : hand-gestures ( new old -- )
210     drop-prefix <reversed>
211     mouse-leave swap each-gesture
212     mouse-enter swap each-gesture ;
213
214 : forget-rollover ( -- )
215     f hand-world set-global
216     hand-gadget get-global
217     [ f hand-gadget set-global f ] dip
218     parents hand-gestures ;
219
220 : send-lose-focus ( gadget -- )
221     lose-focus swap send-gesture ;
222
223 : send-gain-focus ( gadget -- )
224     gain-focus swap send-gesture ;
225
226 : focus-child ( child gadget ? -- )
227     [
228         dup focus>> [
229             dup send-lose-focus
230             f swap t focus-child
231         ] when*
232         dupd focus<< [
233             send-gain-focus
234         ] when*
235     ] [
236         focus<<
237     ] if ;
238
239 : modifier ( mod modifiers -- seq )
240     [ second swap bitand 0 > ] with filter
241     0 <column> members [ f ] [ >array ] if-empty ;
242
243 : drag-loc ( -- loc )
244     hand-loc get-global hand-click-loc get-global v- ;
245
246 : hand-rel ( gadget -- loc )
247     hand-loc get-global swap screen-loc v- ;
248
249 : hand-click-rel ( gadget -- loc )
250     hand-click-loc get-global swap screen-loc v- ;
251
252 : multi-click-timeout? ( -- ? )
253     nano-count hand-last-time get - nanoseconds
254     double-click-timeout get before=? ;
255
256 : multi-click-button? ( button -- button ? )
257     dup hand-last-button get = ;
258
259 : multi-click-position? ( -- ? )
260     hand-loc get-global hand-click-loc get-global distance 10 <= ;
261
262 : multi-click? ( button -- ? )
263     {
264         [ multi-click-timeout? ]
265         [ multi-click-button? ]
266         [ multi-click-position? ]
267     } 0&& nip ;
268
269 : update-click# ( button -- )
270     [
271         dup multi-click? [
272             hand-click# inc
273         ] [
274             1 hand-click# namespaces:set
275         ] if
276         hand-last-button namespaces:set
277         nano-count hand-last-time namespaces:set
278     ] with-global ;
279
280 : update-clicked ( -- )
281     hand-gadget get-global hand-clicked set-global
282     hand-loc get-global hand-click-loc set-global ;
283
284 : under-hand ( -- seq )
285     hand-gadget get-global parents <reversed> ;
286
287 : move-hand ( loc world -- )
288     dup hand-world set-global
289     under-hand [
290         over hand-loc set-global
291         pick-up hand-gadget set-global
292         under-hand
293     ] dip hand-gestures ;
294
295 : send-button-down ( gesture loc world -- )
296     move-hand
297     start-drag-timer
298     dup #>>
299     dup update-click# hand-buttons get-global push
300     update-clicked
301     button-gesture ;
302
303 : send-button-up ( gesture loc world -- )
304     move-hand
305     dup #>> hand-buttons get-global remove! drop
306     stop-drag-timer
307     button-gesture ;
308
309 : send-scroll ( direction loc world -- )
310     move-hand
311     scroll-direction set-global
312     mouse-scroll hand-gadget get-global propagate-gesture ;
313
314 : send-action ( world gesture -- )
315     swap world-focus propagate-gesture ;
316
317 GENERIC: gesture>string ( gesture -- string/f )
318
319 HOOK: modifiers>string os ( modifiers -- string )
320
321 M: macosx modifiers>string
322     [
323         {
324             { A+ [ "\u002318" ] }
325             { M+ [ "\u002325" ] }
326             { S+ [ "\u0021e7" ] }
327             { C+ [ "\u002303" ] }
328         } case
329     ] map "" concat-as ;
330
331 M: object modifiers>string
332     [ name>> ] map "" concat-as ;
333
334 HOOK: keysym>string os ( keysym -- string )
335
336 M: macosx keysym>string >upper ;
337
338 M: object keysym>string dup length 1 = [ >lower ] when ;
339
340 M: key-down gesture>string
341     [ mods>> ] [ sym>> ] bi
342     {
343         { [ dup { [ length 1 = ] [ first LETTER? ] } 1&& ] [ [ S+ prefix ] dip ] }
344         { [ dup " " = ] [ drop "SPACE" ] }
345         [ ]
346     } cond
347     [ modifiers>string ] [ keysym>string ] bi* append ;
348
349 M: button-up gesture>string
350     [
351         dup mods>> modifiers>string %
352         "Click Button" %
353         #>> [ " " % # ] when*
354     ] "" make ;
355
356 M: button-down gesture>string
357     [
358         dup mods>> modifiers>string %
359         "Press Button" %
360         #>> [ " " % # ] when*
361     ] "" make ;
362
363 M: left-action gesture>string drop "Swipe left" ;
364
365 M: right-action gesture>string drop "Swipe right" ;
366
367 M: up-action gesture>string drop "Swipe up" ;
368
369 M: down-action gesture>string drop "Swipe down" ;
370
371 M: zoom-in-action gesture>string drop "Zoom in" ;
372
373 M: zoom-out-action gesture>string drop "Zoom out (pinch)" ;
374
375 HOOK: action-modifier os ( -- mod )
376
377 M: object action-modifier C+ ;
378 M: macosx action-modifier A+ ;
379
380 M: action gesture>string
381     action-gestures value-at
382     action-modifier 1array
383     swap f <key-down> gesture>string ;
384
385 M: object gesture>string drop f ;