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