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 locals ;
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?
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 world world-focus preedit? [
68 gesture world \ propagate-key-gesture-tuple queue-gesture
71 TUPLE: user-input-tuple string world ;
73 M: user-input-tuple send-queued-gesture
74 [ string>> ] [ world>> world-focus ] bi
75 [ user-input* ] with each-parent drop ;
77 : user-input ( string world -- )
78 '[ _ \ user-input-tuple queue-gesture ] unless-empty ;
81 TUPLE: drag # ; C: <drag> drag
82 TUPLE: button-up mods # ; C: <button-up> button-up
83 TUPLE: button-down mods # ; C: <button-down> button-down
84 TUPLE: file-drop mods ; C: <file-drop> file-drop
91 mouse-enter mouse-leave
92 lose-focus gain-focus ;
94 ! Higher-level actions
96 undo-action redo-action
97 cut-action copy-action paste-action
98 delete-action select-all-action
99 left-action right-action up-action down-action
100 zoom-in-action zoom-out-action
101 new-action open-action save-action save-as-action
102 revert-action close-action ;
105 undo-action redo-action
106 cut-action copy-action paste-action
107 delete-action select-all-action
108 left-action right-action up-action down-action
109 zoom-in-action zoom-out-action
110 new-action open-action save-action save-as-action
111 revert-action close-action ;
113 CONSTANT: action-gestures
120 { "a" select-all-action }
124 { "S" save-as-action }
129 SYMBOLS: C+ A+ M+ S+ ;
131 TUPLE: key-gesture mods sym ;
133 TUPLE: key-down < key-gesture ;
135 : new-key-gesture ( mods sym action? class -- key-gesture )
136 [ [ [ S+ swap remove f like ] dip ] unless ] dip boa ; inline
138 : <key-down> ( mods sym action? -- key-down )
139 key-down new-key-gesture ;
141 TUPLE: key-up < key-gesture ;
143 : <key-up> ( mods sym action? -- key-up )
144 key-up new-key-gesture ;
148 ! Note that these are only really useful inside an event
149 ! handler, and that the locations hand-loc and hand-click-loc
150 ! are in the co-ordinate system of the world which contains
151 ! the gadget in question.
155 { 0 0 } hand-loc set-global
158 SYMBOL: hand-click-loc
160 SYMBOL: hand-last-button
161 SYMBOL: hand-last-time
162 0 hand-last-button set-global
163 0 hand-last-time set-global
166 V{ } clone hand-buttons set-global
168 SYMBOL: scroll-direction
169 { 0 0 } scroll-direction set-global
171 SYMBOL: double-click-timeout
172 300 milliseconds double-click-timeout set-global
174 : hand-moved? ( -- ? )
175 hand-loc get-global hand-click-loc get-global = not ;
177 : button-gesture ( gesture -- )
178 hand-clicked get-global propagate-gesture ;
180 : drag-gesture ( -- )
181 hand-buttons get-global
182 [ first <drag> button-gesture ] unless-empty ;
186 <box> drag-timer set-global
188 : start-drag-timer ( -- )
189 hand-buttons get-global empty? [
194 [ drag-timer get-global >box ]
198 : stop-drag-timer ( -- )
199 hand-buttons get-global empty? [
200 drag-timer get-global ?box
201 [ stop-timer ] [ drop ] if
205 hand-buttons get-global empty? [
206 motion hand-gadget get-global propagate-gesture
211 : hand-gestures ( new old -- )
212 drop-prefix <reversed>
213 mouse-leave swap each-gesture
214 mouse-enter swap each-gesture ;
216 : forget-rollover ( -- )
217 f hand-world set-global
218 hand-gadget get-global
219 [ f hand-gadget set-global f ] dip
220 parents hand-gestures ;
222 : send-lose-focus ( gadget -- )
223 lose-focus swap send-gesture ;
225 : send-gain-focus ( gadget -- )
226 gain-focus swap send-gesture ;
228 : focus-child ( child gadget ? -- )
241 : modifier ( mod modifiers -- seq )
242 [ second swap bitand 0 > ] with filter
243 0 <column> members [ f ] [ >array ] if-empty ;
245 : drag-loc ( -- loc )
246 hand-loc get-global hand-click-loc get-global v- ;
248 : hand-rel ( gadget -- loc )
249 hand-loc get-global swap screen-loc v- ;
251 : hand-click-rel ( gadget -- loc )
252 hand-click-loc get-global swap screen-loc v- ;
254 : multi-click-timeout? ( -- ? )
255 nano-count hand-last-time get - nanoseconds
256 double-click-timeout get before=? ;
258 : multi-click-button? ( button -- button ? )
259 dup hand-last-button get = ;
261 : multi-click-position? ( -- ? )
262 hand-loc get-global hand-click-loc get-global distance 10 <= ;
264 : multi-click? ( button -- ? )
266 [ multi-click-timeout? ]
267 [ multi-click-button? ]
268 [ multi-click-position? ]
271 : update-click# ( button -- )
276 1 hand-click# namespaces:set
278 hand-last-button namespaces:set
279 nano-count hand-last-time namespaces:set
282 : update-clicked ( -- )
283 hand-gadget get-global hand-clicked set-global
284 hand-loc get-global hand-click-loc set-global ;
286 : under-hand ( -- seq )
287 hand-gadget get-global parents <reversed> ;
289 : move-hand ( loc world -- )
290 dup hand-world set-global
292 over hand-loc set-global
293 pick-up hand-gadget set-global
295 ] dip hand-gestures ;
297 : send-button-down ( gesture loc world -- )
301 dup update-click# hand-buttons get-global push
305 : send-button-up ( gesture loc world -- )
307 dup #>> hand-buttons get-global remove! drop
311 : send-scroll ( direction loc world -- )
313 scroll-direction set-global
314 mouse-scroll hand-gadget get-global propagate-gesture ;
316 : send-action ( world gesture -- )
317 swap world-focus propagate-gesture ;
319 GENERIC: gesture>string ( gesture -- string/f )
321 HOOK: modifiers>string os ( modifiers -- string )
323 M: macosx modifiers>string
326 { M+ [ "\u002318" ] }
327 { A+ [ "\u002325" ] }
328 { S+ [ "\u0021e7" ] }
329 { C+ [ "\u002303" ] }
333 M: object modifiers>string
334 [ name>> ] map "" concat-as ;
336 HOOK: keysym>string os ( keysym -- string )
338 M: macosx keysym>string >upper ;
340 M: object keysym>string dup length 1 = [ >lower ] when ;
342 M: key-down gesture>string
343 [ mods>> ] [ sym>> ] bi
345 { [ dup { [ length 1 = ] [ first LETTER? ] } 1&& ] [ [ S+ prefix ] dip ] }
346 { [ dup " " = ] [ drop "SPACE" ] }
349 [ modifiers>string ] [ keysym>string ] bi* append ;
351 M: button-up gesture>string
353 dup mods>> modifiers>string %
355 #>> [ " " % # ] when*
358 M: button-down gesture>string
360 dup mods>> modifiers>string %
362 #>> [ " " % # ] when*
365 M: file-drop gesture>string drop "Drop files" ;
367 M: left-action gesture>string drop "Swipe left" ;
369 M: right-action gesture>string drop "Swipe right" ;
371 M: up-action gesture>string drop "Swipe up" ;
373 M: down-action gesture>string drop "Swipe down" ;
375 M: zoom-in-action gesture>string drop "Zoom in" ;
377 M: zoom-out-action gesture>string drop "Zoom out (pinch)" ;
379 HOOK: action-modifier os ( -- mod )
381 M: object action-modifier C+ ;
382 M: macosx action-modifier M+ ;
384 M: action gesture>string
385 action-gestures value-at
386 action-modifier 1array
387 swap f <key-down> gesture>string ;
389 M: object gesture>string drop f ;