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
86 mouse-enter mouse-leave
87 lose-focus gain-focus ;
89 ! Higher-level actions
91 undo-action redo-action
92 cut-action copy-action paste-action
93 delete-action select-all-action
94 left-action right-action up-action down-action
95 zoom-in-action zoom-out-action
96 new-action open-action save-action save-as-action
97 revert-action close-action ;
100 undo-action redo-action
101 cut-action copy-action paste-action
102 delete-action select-all-action
103 left-action right-action up-action down-action
104 zoom-in-action zoom-out-action
105 new-action open-action save-action save-as-action
106 revert-action close-action ;
108 CONSTANT: action-gestures
115 { "a" select-all-action }
119 { "S" save-as-action }
124 SYMBOLS: C+ A+ M+ S+ ;
126 TUPLE: key-gesture mods sym ;
128 TUPLE: key-down < key-gesture ;
130 : new-key-gesture ( mods sym action? class -- key-gesture )
131 [ [ [ S+ swap remove f like ] dip ] unless ] dip boa ; inline
133 : <key-down> ( mods sym action? -- key-down )
134 key-down new-key-gesture ;
136 TUPLE: key-up < key-gesture ;
138 : <key-up> ( mods sym action? -- key-up )
139 key-up new-key-gesture ;
143 ! Note that these are only really useful inside an event
144 ! handler, and that the locations hand-loc and hand-click-loc
145 ! are in the co-ordinate system of the world which contains
146 ! the gadget in question.
150 { 0 0 } hand-loc set-global
153 SYMBOL: hand-click-loc
155 SYMBOL: hand-last-button
156 SYMBOL: hand-last-time
157 0 hand-last-button set-global
158 0 hand-last-time set-global
161 V{ } clone hand-buttons set-global
163 SYMBOL: scroll-direction
164 { 0 0 } scroll-direction set-global
166 SYMBOL: double-click-timeout
167 300 milliseconds double-click-timeout set-global
169 : hand-moved? ( -- ? )
170 hand-loc get-global hand-click-loc get-global = not ;
172 : button-gesture ( gesture -- )
173 hand-clicked get-global propagate-gesture ;
175 : drag-gesture ( -- )
176 hand-buttons get-global
177 [ first <drag> button-gesture ] unless-empty ;
181 <box> drag-timer set-global
183 : start-drag-timer ( -- )
184 hand-buttons get-global empty? [
189 [ drag-timer get-global >box ]
193 : stop-drag-timer ( -- )
194 hand-buttons get-global empty? [
195 drag-timer get-global ?box
196 [ stop-timer ] [ drop ] if
200 hand-buttons get-global empty? [
201 motion hand-gadget get-global propagate-gesture
206 : hand-gestures ( new old -- )
207 drop-prefix <reversed>
208 mouse-leave swap each-gesture
209 mouse-enter swap each-gesture ;
211 : forget-rollover ( -- )
212 f hand-world set-global
213 hand-gadget get-global
214 [ f hand-gadget set-global f ] dip
215 parents hand-gestures ;
217 : send-lose-focus ( gadget -- )
218 lose-focus swap send-gesture ;
220 : send-gain-focus ( gadget -- )
221 gain-focus swap send-gesture ;
223 : focus-child ( child gadget ? -- )
236 : modifier ( mod modifiers -- seq )
237 [ second swap bitand 0 > ] with filter
238 0 <column> members [ f ] [ >array ] if-empty ;
240 : drag-loc ( -- loc )
241 hand-loc get-global hand-click-loc get-global v- ;
243 : hand-rel ( gadget -- loc )
244 hand-loc get-global swap screen-loc v- ;
246 : hand-click-rel ( gadget -- loc )
247 hand-click-loc get-global swap screen-loc v- ;
249 : multi-click-timeout? ( -- ? )
250 nano-count hand-last-time get - nanoseconds
251 double-click-timeout get before=? ;
253 : multi-click-button? ( button -- button ? )
254 dup hand-last-button get = ;
256 : multi-click-position? ( -- ? )
257 hand-loc get-global hand-click-loc get-global distance 10 <= ;
259 : multi-click? ( button -- ? )
261 [ multi-click-timeout? ]
262 [ multi-click-button? ]
263 [ multi-click-position? ]
266 : update-click# ( button -- )
271 1 hand-click# namespaces:set
273 hand-last-button namespaces:set
274 nano-count hand-last-time namespaces:set
277 : update-clicked ( -- )
278 hand-gadget get-global hand-clicked set-global
279 hand-loc get-global hand-click-loc set-global ;
281 : under-hand ( -- seq )
282 hand-gadget get-global parents <reversed> ;
284 : move-hand ( loc world -- )
285 dup hand-world set-global
287 over hand-loc set-global
288 pick-up hand-gadget set-global
290 ] dip hand-gestures ;
292 : send-button-down ( gesture loc world -- )
296 dup update-click# hand-buttons get-global push
300 : send-button-up ( gesture loc world -- )
302 dup #>> hand-buttons get-global remove! drop
306 : send-scroll ( direction loc world -- )
308 scroll-direction set-global
309 mouse-scroll hand-gadget get-global propagate-gesture ;
311 : send-action ( world gesture -- )
312 swap world-focus propagate-gesture ;
314 GENERIC: gesture>string ( gesture -- string/f )
316 HOOK: modifiers>string os ( modifiers -- string )
318 M: macosx modifiers>string
321 { A+ [ "\u002318" ] }
322 { M+ [ "\u002325" ] }
323 { S+ [ "\u0021e7" ] }
324 { C+ [ "\u002303" ] }
328 M: object modifiers>string
329 [ name>> ] map "" concat-as ;
331 HOOK: keysym>string os ( keysym -- string )
333 M: macosx keysym>string >upper ;
335 M: object keysym>string dup length 1 = [ >lower ] when ;
337 M: key-down gesture>string
338 [ mods>> ] [ sym>> ] bi
340 { [ dup { [ length 1 = ] [ first LETTER? ] } 1&& ] [ [ S+ prefix ] dip ] }
341 { [ dup " " = ] [ drop "SPACE" ] }
344 [ modifiers>string ] [ keysym>string ] bi* append ;
346 M: button-up gesture>string
348 dup mods>> modifiers>string %
350 #>> [ " " % # ] when*
353 M: button-down gesture>string
355 dup mods>> modifiers>string %
357 #>> [ " " % # ] when*
360 M: left-action gesture>string drop "Swipe left" ;
362 M: right-action gesture>string drop "Swipe right" ;
364 M: up-action gesture>string drop "Swipe up" ;
366 M: down-action gesture>string drop "Swipe down" ;
368 M: zoom-in-action gesture>string drop "Zoom in" ;
370 M: zoom-out-action gesture>string drop "Zoom out (pinch)" ;
372 HOOK: action-modifier os ( -- mod )
374 M: object action-modifier C+ ;
375 M: macosx action-modifier A+ ;
377 M: action gesture>string
378 action-gestures value-at
379 action-modifier 1array
380 swap f <key-down> gesture>string ;
382 M: object gesture>string drop f ;