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 ;
9 : get-gesture-handler ( gesture gadget -- quot )
10 class-of superclasses-of [ "gestures" word-prop ] map assoc-stack ;
12 GENERIC: handle-gesture ( gesture gadget -- ? )
14 M: object handle-gesture
16 [ get-gesture-handler ] 2bi
17 dup [ call( gadget -- ) f ] [ 2drop t ] if ;
19 GENERIC: handles-gesture? ( gesture gadget -- ? )
21 M: object handles-gesture? ( gesture gadget -- ? )
22 get-gesture-handler >boolean ;
24 : parents-handle-gesture? ( gesture gadget -- ? )
25 [ handles-gesture? not ] with each-parent not ;
27 : set-gestures ( class hash -- ) "gestures" set-word-prop ;
29 : gesture-queue ( -- deque ) \ gesture-queue get ;
31 GENERIC: send-queued-gesture ( request -- )
33 TUPLE: send-gesture-tuple gesture gadget ;
35 M: send-gesture-tuple send-queued-gesture
36 [ gesture>> ] [ gadget>> ] bi handle-gesture drop ;
38 : queue-gesture ( ... class -- )
39 boa gesture-queue push-front notify-ui-thread ; inline
41 : send-gesture ( gesture gadget -- )
42 \ send-gesture-tuple queue-gesture ;
44 : each-gesture ( gesture seq -- ) [ send-gesture ] with each ;
46 TUPLE: propagate-gesture-tuple gesture gadget ;
48 : resend-gesture ( gesture gadget -- ? )
49 [ handle-gesture ] with each-parent ;
51 M: propagate-gesture-tuple send-queued-gesture
52 [ gesture>> ] [ gadget>> ] bi resend-gesture drop ;
54 : propagate-gesture ( gesture gadget -- )
55 \ propagate-gesture-tuple queue-gesture ;
57 TUPLE: propagate-key-gesture-tuple gesture world ;
59 : world-focus ( world -- gadget )
60 dup focus>> [ world-focus ] [ ] ?if ;
62 M: propagate-key-gesture-tuple send-queued-gesture
63 [ gesture>> ] [ world>> world-focus ] bi
64 [ handle-gesture ] with each-parent drop ;
66 : propagate-key-gesture ( gesture world -- )
67 \ propagate-key-gesture-tuple queue-gesture ;
69 TUPLE: user-input-tuple string world ;
71 M: user-input-tuple send-queued-gesture
72 [ string>> ] [ world>> world-focus ] bi
73 [ user-input* ] with each-parent drop ;
75 : user-input ( string world -- )
76 '[ _ \ user-input-tuple queue-gesture ] unless-empty ;
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
89 mouse-enter mouse-leave
90 lose-focus gain-focus ;
92 ! Higher-level actions
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 ;
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 ;
111 CONSTANT: action-gestures
118 { "a" select-all-action }
122 { "S" save-as-action }
127 SYMBOLS: C+ A+ M+ S+ ;
129 TUPLE: key-gesture mods sym ;
131 TUPLE: key-down < key-gesture ;
133 : new-key-gesture ( mods sym action? class -- key-gesture )
134 [ [ [ S+ swap remove f like ] dip ] unless ] dip boa ; inline
136 : <key-down> ( mods sym action? -- key-down )
137 key-down new-key-gesture ;
139 TUPLE: key-up < key-gesture ;
141 : <key-up> ( mods sym action? -- key-up )
142 key-up new-key-gesture ;
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.
153 { 0 0 } hand-loc set-global
156 SYMBOL: hand-click-loc
158 SYMBOL: hand-last-button
159 SYMBOL: hand-last-time
160 0 hand-last-button set-global
161 0 hand-last-time set-global
164 V{ } clone hand-buttons set-global
166 SYMBOL: scroll-direction
167 { 0 0 } scroll-direction set-global
169 SYMBOL: double-click-timeout
170 300 milliseconds double-click-timeout set-global
172 : hand-moved? ( -- ? )
173 hand-loc get-global hand-click-loc get-global = not ;
175 : button-gesture ( gesture -- )
176 hand-clicked get-global propagate-gesture ;
178 : drag-gesture ( -- )
179 hand-buttons get-global
180 [ first <drag> button-gesture ] unless-empty ;
184 <box> drag-timer set-global
186 : start-drag-timer ( -- )
187 hand-buttons get-global empty? [
192 [ drag-timer get-global >box ]
196 : stop-drag-timer ( -- )
197 hand-buttons get-global empty? [
198 drag-timer get-global ?box
199 [ stop-timer ] [ drop ] if
203 hand-buttons get-global empty? [
204 motion hand-gadget get-global propagate-gesture
209 : hand-gestures ( new old -- )
210 drop-prefix <reversed>
211 mouse-leave swap each-gesture
212 mouse-enter swap each-gesture ;
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 ;
220 : send-lose-focus ( gadget -- )
221 lose-focus swap send-gesture ;
223 : send-gain-focus ( gadget -- )
224 gain-focus swap send-gesture ;
226 : focus-child ( child gadget ? -- )
239 : modifier ( mod modifiers -- seq )
240 [ second swap bitand 0 > ] with filter
241 0 <column> members [ f ] [ >array ] if-empty ;
243 : drag-loc ( -- loc )
244 hand-loc get-global hand-click-loc get-global v- ;
246 : hand-rel ( gadget -- loc )
247 hand-loc get-global swap screen-loc v- ;
249 : hand-click-rel ( gadget -- loc )
250 hand-click-loc get-global swap screen-loc v- ;
252 : multi-click-timeout? ( -- ? )
253 nano-count hand-last-time get - nanoseconds
254 double-click-timeout get before=? ;
256 : multi-click-button? ( button -- button ? )
257 dup hand-last-button get = ;
259 : multi-click-position? ( -- ? )
260 hand-loc get-global hand-click-loc get-global distance 10 <= ;
262 : multi-click? ( button -- ? )
264 [ multi-click-timeout? ]
265 [ multi-click-button? ]
266 [ multi-click-position? ]
269 : update-click# ( button -- )
274 1 hand-click# namespaces:set
276 hand-last-button namespaces:set
277 nano-count hand-last-time namespaces:set
280 : update-clicked ( -- )
281 hand-gadget get-global hand-clicked set-global
282 hand-loc get-global hand-click-loc set-global ;
284 : under-hand ( -- seq )
285 hand-gadget get-global parents <reversed> ;
287 : move-hand ( loc world -- )
288 dup hand-world set-global
290 over hand-loc set-global
291 pick-up hand-gadget set-global
293 ] dip hand-gestures ;
295 : send-button-down ( gesture loc world -- )
299 dup update-click# hand-buttons get-global push
303 : send-button-up ( gesture loc world -- )
305 dup #>> hand-buttons get-global remove! drop
309 : send-scroll ( direction loc world -- )
311 scroll-direction set-global
312 mouse-scroll hand-gadget get-global propagate-gesture ;
314 : send-action ( world gesture -- )
315 swap world-focus propagate-gesture ;
317 GENERIC: gesture>string ( gesture -- string/f )
319 HOOK: modifiers>string os ( modifiers -- string )
321 M: macosx modifiers>string
324 { A+ [ "\u002318" ] }
325 { M+ [ "\u002325" ] }
326 { S+ [ "\u0021e7" ] }
327 { C+ [ "\u002303" ] }
331 M: object modifiers>string
332 [ name>> ] map "" concat-as ;
334 HOOK: keysym>string os ( keysym -- string )
336 M: macosx keysym>string >upper ;
338 M: object keysym>string dup length 1 = [ >lower ] when ;
340 M: key-down gesture>string
341 [ mods>> ] [ sym>> ] bi
343 { [ dup { [ length 1 = ] [ first LETTER? ] } 1&& ] [ [ S+ prefix ] dip ] }
344 { [ dup " " = ] [ drop "SPACE" ] }
347 [ modifiers>string ] [ keysym>string ] bi* append ;
349 M: button-up gesture>string
351 dup mods>> modifiers>string %
353 #>> [ " " % # ] when*
356 M: button-down gesture>string
358 dup mods>> modifiers>string %
360 #>> [ " " % # ] when*
363 M: file-drop gesture>string drop "Drop files" ;
365 M: left-action gesture>string drop "Swipe left" ;
367 M: right-action gesture>string drop "Swipe right" ;
369 M: up-action gesture>string drop "Swipe up" ;
371 M: down-action gesture>string drop "Swipe down" ;
373 M: zoom-in-action gesture>string drop "Zoom in" ;
375 M: zoom-out-action gesture>string drop "Zoom out (pinch)" ;
377 HOOK: action-modifier os ( -- mod )
379 M: object action-modifier C+ ;
380 M: macosx action-modifier A+ ;
382 M: action gesture>string
383 action-gestures value-at
384 action-modifier 1array
385 swap f <key-down> gesture>string ;
387 M: object gesture>string drop f ;