1 ! Copyright (C) 2005, 2009 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 alarms combinators
6 sets columns fry deques ui.gadgets ui.gadgets.private ascii
7 combinators.short-circuit ;
10 : get-gesture-handler ( gesture gadget -- quot )
11 class superclasses [ "gestures" word-prop ] map assoc-stack ;
13 GENERIC: handle-gesture ( gesture gadget -- ? )
15 M: object handle-gesture
17 [ get-gesture-handler ] 2bi
18 dup [ call( gadget -- ) f ] [ 2drop t ] if ;
20 GENERIC: handles-gesture? ( gesture gadget -- ? )
22 M: object handles-gesture? ( gesture gadget -- ? )
23 get-gesture-handler >boolean ;
25 : parents-handle-gesture? ( gesture gadget -- ? )
26 [ handles-gesture? not ] with each-parent not ;
28 : set-gestures ( class hash -- ) "gestures" set-word-prop ;
30 : gesture-queue ( -- deque ) \ gesture-queue get ;
32 GENERIC: send-queued-gesture ( request -- )
34 TUPLE: send-gesture gesture gadget ;
36 M: send-gesture send-queued-gesture
37 [ gesture>> ] [ gadget>> ] bi handle-gesture drop ;
39 : queue-gesture ( ... class -- )
40 boa gesture-queue push-front notify-ui-thread ; inline
42 : send-gesture ( gesture gadget -- )
43 \ send-gesture queue-gesture ;
45 : each-gesture ( gesture seq -- ) [ send-gesture ] with each ;
47 TUPLE: propagate-gesture gesture gadget ;
49 : resend-gesture ( gesture gadget -- ? )
50 [ handle-gesture ] with each-parent ;
52 M: propagate-gesture send-queued-gesture
53 [ gesture>> ] [ gadget>> ] bi resend-gesture drop ;
55 : propagate-gesture ( gesture gadget -- )
56 \ propagate-gesture queue-gesture ;
58 TUPLE: propagate-key-gesture gesture world ;
60 : world-focus ( world -- gadget )
61 dup focus>> [ world-focus ] [ ] ?if ;
63 M: propagate-key-gesture send-queued-gesture
64 [ gesture>> ] [ world>> world-focus ] bi
65 [ handle-gesture ] with each-parent drop ;
67 : propagate-key-gesture ( gesture world -- )
68 \ propagate-key-gesture queue-gesture ;
70 TUPLE: user-input string world ;
72 M: user-input send-queued-gesture
73 [ string>> ] [ world>> world-focus ] bi
74 [ user-input* ] with each-parent drop ;
76 : user-input ( string world -- )
77 '[ _ \ user-input queue-gesture ] unless-empty ;
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
87 mouse-enter mouse-leave
88 lose-focus gain-focus ;
90 ! Higher-level actions
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 ;
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 ;
109 CONSTANT: action-gestures
116 { "a" select-all-action }
120 { "S" save-as-action }
125 SYMBOLS: C+ A+ M+ S+ ;
127 TUPLE: key-gesture mods sym ;
129 TUPLE: key-down < key-gesture ;
131 : new-key-gesture ( mods sym action? class -- mods' sym' )
132 [ [ [ S+ swap remove f like ] dip ] unless ] dip boa ; inline
134 : <key-down> ( mods sym action? -- key-down )
135 key-down new-key-gesture ;
137 TUPLE: key-up < key-gesture ;
139 : <key-up> ( mods sym action? -- key-up )
140 key-up new-key-gesture ;
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.
151 { 0 0 } hand-loc set-global
154 SYMBOL: hand-click-loc
156 SYMBOL: hand-last-button
157 SYMBOL: hand-last-time
158 0 hand-last-button set-global
159 0 hand-last-time set-global
162 V{ } clone hand-buttons set-global
164 SYMBOL: scroll-direction
165 { 0 0 } scroll-direction set-global
167 SYMBOL: double-click-timeout
168 300 milliseconds double-click-timeout set-global
170 : hand-moved? ( -- ? )
171 hand-loc get hand-click-loc get = not ;
173 : button-gesture ( gesture -- )
174 hand-clicked get-global propagate-gesture ;
176 : drag-gesture ( -- )
177 hand-buttons get-global
178 [ first <drag> button-gesture ] unless-empty ;
182 <box> drag-timer set-global
184 : start-drag-timer ( -- )
185 hand-buttons get-global empty? [
189 add-alarm drag-timer get-global >box
192 : stop-drag-timer ( -- )
193 hand-buttons get-global empty? [
194 drag-timer get-global ?box
195 [ cancel-alarm ] [ drop ] if
199 hand-buttons get-global empty? [
200 motion hand-gadget get-global propagate-gesture
205 : hand-gestures ( new old -- )
206 drop-prefix <reversed>
207 mouse-leave swap each-gesture
208 mouse-enter swap each-gesture ;
210 : forget-rollover ( -- )
211 f hand-world set-global
212 hand-gadget get-global
213 [ f hand-gadget set-global f ] dip
214 parents hand-gestures ;
216 : send-lose-focus ( gadget -- )
217 lose-focus swap send-gesture ;
219 : send-gain-focus ( gadget -- )
220 gain-focus swap send-gesture ;
222 : focus-child ( child gadget ? -- )
235 : modifier ( mod modifiers -- seq )
236 [ second swap bitand 0 > ] with filter
237 0 <column> prune [ f ] [ >array ] if-empty ;
239 : drag-loc ( -- loc )
240 hand-loc get-global hand-click-loc get-global v- ;
242 : hand-rel ( gadget -- loc )
243 hand-loc get-global swap screen-loc v- ;
245 : hand-click-rel ( gadget -- loc )
246 hand-click-loc get-global swap screen-loc v- ;
248 : multi-click-timeout? ( -- ? )
249 nano-count hand-last-time get - nanoseconds
250 double-click-timeout get before=? ;
252 : multi-click-button? ( button -- button ? )
253 dup hand-last-button get = ;
255 : multi-click-position? ( -- ? )
256 hand-loc get hand-click-loc get distance 10 <= ;
258 : multi-click? ( button -- ? )
260 [ multi-click-timeout? ]
261 [ multi-click-button? ]
262 [ multi-click-position? ]
265 : update-click# ( button -- )
273 nano-count hand-last-time set
276 : update-clicked ( -- )
277 hand-gadget get-global hand-clicked set-global
278 hand-loc get-global hand-click-loc set-global ;
280 : under-hand ( -- seq )
281 hand-gadget get-global parents <reversed> ;
283 : move-hand ( loc world -- )
284 dup hand-world set-global
286 over hand-loc set-global
287 pick-up hand-gadget set-global
289 ] dip hand-gestures ;
291 : send-button-down ( gesture loc world -- )
295 dup update-click# hand-buttons get-global push
299 : send-button-up ( gesture loc world -- )
301 dup #>> hand-buttons get-global remove! drop
305 : send-wheel ( direction loc world -- )
307 scroll-direction set-global
308 mouse-scroll hand-gadget get-global propagate-gesture ;
310 : send-action ( world gesture -- )
311 swap world-focus propagate-gesture ;
313 GENERIC: gesture>string ( gesture -- string/f )
315 HOOK: modifiers>string os ( modifiers -- string )
317 M: macosx modifiers>string
320 { A+ [ "\u002318" ] }
321 { M+ [ "\u002325" ] }
322 { S+ [ "\u0021e7" ] }
323 { C+ [ "\u002303" ] }
327 M: object modifiers>string
328 [ name>> ] map "" join ;
330 HOOK: keysym>string os ( keysym -- string )
332 M: macosx keysym>string >upper ;
334 M: object keysym>string dup length 1 = [ >lower ] when ;
336 M: key-down gesture>string
337 [ mods>> ] [ sym>> ] bi
339 { [ dup { [ length 1 = ] [ first LETTER? ] } 1&& ] [ [ S+ prefix ] dip ] }
340 { [ dup " " = ] [ drop "SPACE" ] }
343 [ modifiers>string ] [ keysym>string ] bi* append ;
345 M: button-up gesture>string
347 dup mods>> modifiers>string %
349 #>> [ " " % # ] when*
352 M: button-down gesture>string
354 dup mods>> modifiers>string %
356 #>> [ " " % # ] when*
359 M: left-action gesture>string drop "Swipe left" ;
361 M: right-action gesture>string drop "Swipe right" ;
363 M: up-action gesture>string drop "Swipe up" ;
365 M: down-action gesture>string drop "Swipe down" ;
367 M: zoom-in-action gesture>string drop "Zoom in" ;
369 M: zoom-out-action gesture>string drop "Zoom out (pinch)" ;
371 HOOK: action-modifier os ( -- mod )
373 M: object action-modifier C+ ;
374 M: macosx action-modifier A+ ;
376 M: action gesture>string
377 action-gestures value-at
378 action-modifier 1array
379 swap f <key-down> gesture>string ;
381 M: object gesture>string drop f ;