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 GENERIC: handle-gesture ( gesture gadget -- ? )
12 M: object handle-gesture
14 [ class superclasses [ "gestures" word-prop ] map assoc-stack ] 2bi
15 dup [ call( gadget -- ) f ] [ 2drop t ] if ;
17 : set-gestures ( class hash -- ) "gestures" set-word-prop ;
19 : gesture-queue ( -- deque ) \ gesture-queue get ;
21 GENERIC: send-queued-gesture ( request -- )
23 TUPLE: send-gesture gesture gadget ;
25 M: send-gesture send-queued-gesture
26 [ gesture>> ] [ gadget>> ] bi handle-gesture drop ;
28 : queue-gesture ( ... class -- )
29 boa gesture-queue push-front notify-ui-thread ; inline
31 : send-gesture ( gesture gadget -- )
32 \ send-gesture queue-gesture ;
34 : each-gesture ( gesture seq -- ) [ send-gesture ] with each ;
36 TUPLE: propagate-gesture gesture gadget ;
38 : resend-gesture ( gesture gadget -- ? )
39 [ handle-gesture ] with each-parent ;
41 M: propagate-gesture send-queued-gesture
42 [ gesture>> ] [ gadget>> ] bi resend-gesture drop ;
44 : propagate-gesture ( gesture gadget -- )
45 \ propagate-gesture queue-gesture ;
47 TUPLE: propagate-key-gesture gesture world ;
49 : world-focus ( world -- gadget )
50 dup focus>> [ world-focus ] [ ] ?if ;
52 M: propagate-key-gesture send-queued-gesture
53 [ gesture>> ] [ world>> world-focus ] bi
54 [ handle-gesture ] with each-parent drop ;
56 : propagate-key-gesture ( gesture world -- )
57 \ propagate-key-gesture queue-gesture ;
59 TUPLE: user-input string world ;
61 M: user-input send-queued-gesture
62 [ string>> ] [ world>> world-focus ] bi
63 [ user-input* ] with each-parent drop ;
65 : user-input ( string world -- )
66 '[ _ \ user-input queue-gesture ] unless-empty ;
69 TUPLE: drag # ; C: <drag> drag
70 TUPLE: button-up mods # ; C: <button-up> button-up
71 TUPLE: button-down mods # ; C: <button-down> button-down
76 mouse-enter mouse-leave
77 lose-focus gain-focus ;
79 ! Higher-level actions
81 undo-action redo-action
82 cut-action copy-action paste-action
83 delete-action select-all-action
84 left-action right-action up-action down-action
85 zoom-in-action zoom-out-action
86 new-action open-action save-action save-as-action
87 revert-action close-action ;
90 undo-action redo-action
91 cut-action copy-action paste-action
92 delete-action select-all-action
93 left-action right-action up-action down-action
94 zoom-in-action zoom-out-action
95 new-action open-action save-action save-as-action
96 revert-action close-action ;
98 CONSTANT: action-gestures
105 { "a" select-all-action }
109 { "S" save-as-action }
114 SYMBOLS: C+ A+ M+ S+ ;
116 TUPLE: key-gesture mods sym ;
118 TUPLE: key-down < key-gesture ;
120 : new-key-gesture ( mods sym action? class -- mods' sym' )
121 [ [ [ S+ swap remove f like ] dip ] unless ] dip boa ; inline
123 : <key-down> ( mods sym action? -- key-down )
124 key-down new-key-gesture ;
126 TUPLE: key-up < key-gesture ;
128 : <key-up> ( mods sym action? -- key-up )
129 key-up new-key-gesture ;
133 ! Note that these are only really useful inside an event
134 ! handler, and that the locations hand-loc and hand-click-loc
135 ! are in the co-ordinate system of the world which contains
136 ! the gadget in question.
140 { 0 0 } hand-loc set-global
143 SYMBOL: hand-click-loc
145 SYMBOL: hand-last-button
146 SYMBOL: hand-last-time
147 0 hand-last-button set-global
148 <zero> hand-last-time set-global
151 V{ } clone hand-buttons set-global
153 SYMBOL: scroll-direction
154 { 0 0 } scroll-direction set-global
156 SYMBOL: double-click-timeout
157 300 milliseconds double-click-timeout set-global
159 : hand-moved? ( -- ? )
160 hand-loc get hand-click-loc get = not ;
162 : button-gesture ( gesture -- )
163 hand-clicked get-global propagate-gesture ;
165 : drag-gesture ( -- )
166 hand-buttons get-global
167 [ first <drag> button-gesture ] unless-empty ;
171 <box> drag-timer set-global
173 : start-drag-timer ( -- )
174 hand-buttons get-global empty? [
176 300 milliseconds hence
178 add-alarm drag-timer get-global >box
181 : stop-drag-timer ( -- )
182 hand-buttons get-global empty? [
183 drag-timer get-global ?box
184 [ cancel-alarm ] [ drop ] if
188 hand-buttons get-global empty? [
189 motion hand-gadget get-global propagate-gesture
194 : hand-gestures ( new old -- )
195 drop-prefix <reversed>
196 mouse-leave swap each-gesture
197 mouse-enter swap each-gesture ;
199 : forget-rollover ( -- )
200 f hand-world set-global
201 hand-gadget get-global
202 [ f hand-gadget set-global f ] dip
203 parents hand-gestures ;
205 : send-lose-focus ( gadget -- )
206 lose-focus swap send-gesture ;
208 : send-gain-focus ( gadget -- )
209 gain-focus swap send-gesture ;
211 : focus-child ( child gadget ? -- )
224 : modifier ( mod modifiers -- seq )
225 [ second swap bitand 0 > ] with filter
226 0 <column> prune [ f ] [ >array ] if-empty ;
228 : drag-loc ( -- loc )
229 hand-loc get-global hand-click-loc get-global v- ;
231 : hand-rel ( gadget -- loc )
232 hand-loc get-global swap screen-loc v- ;
234 : hand-click-rel ( gadget -- loc )
235 hand-click-loc get-global swap screen-loc v- ;
237 : multi-click-timeout? ( -- ? )
238 now hand-last-time get time- double-click-timeout get before=? ;
240 : multi-click-button? ( button -- button ? )
241 dup hand-last-button get = ;
243 : multi-click-position? ( -- ? )
244 hand-loc get hand-click-loc get distance 10 <= ;
246 : multi-click? ( button -- ? )
248 [ multi-click-timeout? ]
249 [ multi-click-button? ]
250 [ multi-click-position? ]
253 : update-click# ( button -- )
261 now hand-last-time set
264 : update-clicked ( -- )
265 hand-gadget get-global hand-clicked set-global
266 hand-loc get-global hand-click-loc set-global ;
268 : under-hand ( -- seq )
269 hand-gadget get-global parents <reversed> ;
271 : move-hand ( loc world -- )
272 dup hand-world set-global
274 over hand-loc set-global
275 pick-up hand-gadget set-global
277 ] dip hand-gestures ;
279 : send-button-down ( gesture loc world -- )
283 dup update-click# hand-buttons get-global push
287 : send-button-up ( gesture loc world -- )
289 dup #>> hand-buttons get-global delete
293 : send-wheel ( direction loc world -- )
295 scroll-direction set-global
296 mouse-scroll hand-gadget get-global propagate-gesture ;
298 : send-action ( world gesture -- )
299 swap world-focus propagate-gesture ;
301 GENERIC: gesture>string ( gesture -- string/f )
303 HOOK: modifiers>string os ( modifiers -- string )
305 M: macosx modifiers>string
308 { A+ [ "\u002318" ] }
309 { M+ [ "\u002325" ] }
310 { S+ [ "\u0021e7" ] }
311 { C+ [ "\u002303" ] }
315 M: object modifiers>string
316 [ name>> ] map "" join ;
318 HOOK: keysym>string os ( keysym -- string )
320 M: macosx keysym>string >upper ;
322 M: object keysym>string dup length 1 = [ >lower ] when ;
324 M: key-down gesture>string
325 [ mods>> ] [ sym>> ] bi
327 { [ dup { [ length 1 = ] [ first LETTER? ] } 1&& ] [ [ S+ prefix ] dip ] }
328 { [ dup " " = ] [ drop "SPACE" ] }
331 [ modifiers>string ] [ keysym>string ] bi* append ;
333 M: button-up gesture>string
335 dup mods>> modifiers>string %
337 #>> [ " " % # ] when*
340 M: button-down gesture>string
342 dup mods>> modifiers>string %
344 #>> [ " " % # ] when*
347 M: left-action gesture>string drop "Swipe left" ;
349 M: right-action gesture>string drop "Swipe right" ;
351 M: up-action gesture>string drop "Swipe up" ;
353 M: down-action gesture>string drop "Swipe down" ;
355 M: zoom-in-action gesture>string drop "Zoom in" ;
357 M: zoom-out-action gesture>string drop "Zoom out (pinch)" ;
359 HOOK: action-modifier os ( -- mod )
361 M: object action-modifier C+ ;
362 M: macosx action-modifier A+ ;
364 M: action gesture>string
365 action-gestures value-at
366 action-modifier 1array
367 swap f <key-down> gesture>string ;
369 M: object gesture>string drop f ;