]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/gestures/gestures-docs.factor
Reformat
[factor.git] / basis / ui / gestures / gestures-docs.factor
1 USING: hashtables help.markup help.syntax kernel strings system
2 ui.gadgets ui.gadgets.worlds ;
3 IN: ui.gestures
4
5 HELP: set-gestures
6 { $values { "class" "a class word" } { "hash" hashtable } }
7 { $description "Sets the gestures a gadget class responds to. The hashtable maps gestures to quotations with stack effect " { $snippet "( gadget -- )" } "." } ;
8
9 HELP: handle-gesture
10 { $values { "gesture" "a gesture" } { "gadget" "the receiver of the gesture" } { "?" boolean } }
11 { $contract "Handles a gesture sent to a gadget."
12 $nl
13 "Outputs " { $link f } " if the gesture was handled, and " { $link t } " if the gesture should be passed on to the gadget's parent."
14 $nl
15 "The default implementation looks at the " { $snippet "\"gestures\"" } " word property of each superclass of the gadget's class." }
16 { $notes "Methods should be defined on this word if you desire to handle an arbitrary set of gestures. To define handlers for a fixed set, it is easier to use " { $link set-gestures } ". If you define a method on " { $snippet "handle-gesture" } ", you should also override " { $link handles-gesture? } "." } ;
17
18 HELP: handles-gesture?
19 { $values { "gesture" "a gesture" } { "gadget" "the receiver of the gesture" } { "?" boolean } }
20 { $contract "Returns a true value if " { $snippet "gadget" } " would handle " { $snippet "gesture" } " in its " { $link handle-gesture } " method."
21 $nl
22 "The default implementation looks at the " { $snippet "\"gestures\"" } " word property of each superclass of the gadget's class and returns true if a handler is present for " { $snippet "gesture" } "." }
23 { $notes "This word is used in Factor's MacOS X UI to validate menu items." } ;
24
25 HELP: parents-handle-gesture?
26 { $values { "gesture" "a gesture" } { "gadget" "the receiver of the gesture" } { "?" boolean } }
27 { $contract "Returns a true value if " { $snippet "gadget" } " or any of its ancestors would handle " { $snippet "gesture" } " in its " { $link handle-gesture } " method." } ;
28
29 { propagate-gesture handle-gesture handles-gesture? set-gestures } related-words
30
31 HELP: propagate-gesture
32 { $values { "gesture" "a gesture" } { "gadget" gadget } }
33 { $description "Calls " { $link handle-gesture } " on every parent of the " { $snippet "gadget" } ", starting with the " { $snippet "gadget" } " itself." } ;
34
35 HELP: motion
36 { $class-description "Mouse motion gesture." }
37 { $examples { $code "motion" } } ;
38
39 HELP: drag
40 { $class-description "Mouse drag gesture. The " { $snippet "#" } " slot is either set to a mouse button number, or " { $link f } " indicating no specific button is expected." } ;
41
42 HELP: button-up
43 { $class-description "Mouse button up gesture. Instances have two slots:"
44     { $slots
45         { "mods" { "a sequence of modifiers; see " { $link "keyboard-gestures" } } }
46         { "#" { "a mouse button number, or " { $link f } " indicating no specific button is expected" } }
47     }
48 }
49 { $examples { $code "T{ button-up f f 1 }" "T{ button-up }" } } ;
50
51 HELP: button-down
52 { $class-description "Mouse button down gesture. Instances have two slots:"
53     { $slots
54         { "mods" { "a sequence of modifiers; see " { $link "keyboard-gestures" } } }
55         { "#" { "a mouse button number, or " { $link f } " indicating no specific button is expected" } }
56     }
57 }
58 { $examples { $code "T{ button-down f f 1 }" "T{ button-down }" } } ;
59
60 HELP: mouse-scroll
61 { $class-description "Scroll wheel motion gesture. When this gesture is sent, the " { $link scroll-direction } " global variable is set to a direction vector." }
62 { $examples { $code "mouse-scroll" } } ;
63
64 HELP: mouse-enter
65 { $class-description "Gesture sent when the mouse enters the bounds of a gadget." }
66 { $examples { $code "mouse-enter" } } ;
67
68 HELP: mouse-leave
69 { $class-description "Gesture sent when the mouse leaves the bounds of a gadget." }
70 { $examples { $code "mouse-leave" } } ;
71
72 HELP: gain-focus
73 { $class-description "Gesture sent when a gadget gains keyboard focus." }
74 { $examples { $code "gain-focus" } } ;
75
76 HELP: lose-focus
77 { $class-description "Gesture sent when a gadget loses keyboard focus." }
78 { $examples { $code "lose-focus" } } ;
79
80 HELP: cut-action
81 { $class-description "Gesture sent when the " { $emphasis "cut" } " standard window system action is invoked." }
82 { $examples { $code "cut-action" } } ;
83
84 HELP: copy-action
85 { $class-description "Gesture sent when the " { $emphasis "copy" } " standard window system action is invoked." }
86 { $examples { $code "copy-action" } } ;
87
88 HELP: paste-action
89 { $class-description "Gesture sent when the " { $emphasis "paste" } " standard window system action is invoked." }
90 { $examples { $code "paste-action" } } ;
91
92 HELP: delete-action
93 { $class-description "Gesture sent when the " { $emphasis "delete" } " standard window system action is invoked." }
94 { $examples { $code "delete-action" } } ;
95
96 HELP: select-all-action
97 { $class-description "Gesture sent when the " { $emphasis "select all" } " standard window system action is invoked." }
98 { $examples { $code "select-all-action" } } ;
99
100 HELP: new-action
101 { $class-description "Gesture sent when the " { $emphasis "new" } " standard window system action is invoked." }
102 { $examples { $code "new-action" } } ;
103
104 HELP: open-action
105 { $class-description "Gesture sent when the " { $emphasis "open" } " standard window system action is invoked." }
106 { $examples { $code "open-action" } } ;
107
108 HELP: save-action
109 { $class-description "Gesture sent when the " { $emphasis "save" } " standard window system action is invoked." }
110 { $examples { $code "save-action" } } ;
111
112 HELP: save-as-action
113 { $class-description "Gesture sent when the " { $emphasis "save as" } " standard window system action is invoked." }
114 { $examples { $code "save-as-action" } } ;
115
116 HELP: revert-action
117 { $class-description "Gesture sent when the " { $emphasis "revert" } " standard window system action is invoked." }
118 { $examples { $code "revert-action" } } ;
119
120 HELP: close-action
121 { $class-description "Gesture sent when the " { $emphasis "close" } " standard window system action is invoked." }
122 { $examples { $code "close-action" } } ;
123
124 HELP: C+
125 { $description "Control key modifier." } ;
126
127 HELP: A+
128 { $description "Alt key modifier. This is the Option key on Mac OS X." } ;
129
130 HELP: M+
131 { $description "Meta key modifier. This is the Command key on Mac OS X and the Windows key on other Unix and Windows platforms." } ;
132
133 HELP: S+
134 { $description "Shift key modifier." } ;
135
136 HELP: key-down
137 { $class-description "Key down gesture. Instances have two slots:"
138     { $slots
139         { "mods" { "a sequence of modifiers; see " { $link "keyboard-gestures" } } }
140         { "sym" { "a string denoting the key pressed; see " { $link "keyboard-gestures" } } }
141     }
142 }
143 { $examples { $code "T{ key-down f { C+ } \"a\" }" "T{ key-down f f \"TAB\" }" } } ;
144
145 HELP: key-up
146 { $class-description "Key up gesture. Instances have two slots:"
147     { $slots
148         { "mods" { "a sequence of modifiers; see " { $link "keyboard-gestures" } } }
149     { "sym" { "a string denoting the key pressed; see " { $link "keyboard-gestures" } } }
150     }
151 }
152 { $examples { $code "T{ key-up f { C+ } \"a\" }" "T{ key-up f f \"TAB\" }" } } ;
153
154 HELP: hand-gadget
155 { $var-description "Global variable. The gadget at the mouse location." } ;
156
157 HELP: hand-loc
158 { $var-description "Global variable. The mouse location relative to the top-left corner of the " { $link hand-world } "." } ;
159
160 { hand-loc hand-rel } related-words
161
162 HELP: hand-clicked
163 { $var-description "Global variable. The gadget at the location of the most recent click." } ;
164
165 HELP: hand-click-loc
166 { $var-description "Global variable. The mouse location at the time of the most recent click relative to the top-left corner of the " { $link hand-world } "." } ;
167
168 { hand-clicked hand-click-loc } related-words
169
170 HELP: hand-click#
171 { $var-description "Global variable. The number of times the mouse was clicked in short succession. This counter is reset when " { $link double-click-timeout } " expires." } ;
172
173 HELP: hand-last-button
174 { $var-description "Global variable. The mouse button most recently pressed." } ;
175
176 HELP: hand-last-time
177 { $var-description "Global variable. The timestamp of the most recent mouse button click. This timestamp has the same format as the output value of " { $link nano-count } "." } ;
178
179 HELP: hand-buttons
180 { $var-description "Global variable. A vector of mouse buttons currently held down." } ;
181
182 HELP: scroll-direction
183 { $var-description "Global variable. If the most recent gesture was a " { $link mouse-scroll } ", this holds a pair of integers indicating the direction of the scrolling as a two-dimensional vector." } ;
184
185 HELP: double-click-timeout
186 { $var-description "Global variable. The maximum delay between two button presses which will still increment " { $link hand-click# } "." } ;
187
188 HELP: button-gesture
189 { $values { "gesture" "a gesture" } }
190 { $description "Sends a gesture to the most recently clicked gadget, and if the gadget does not respond to the gesture, removes specific button number information from the gesture and sends it again." } ;
191
192 HELP: fire-motion
193 { $description "Sends a " { $link motion } " or " { $link drag } " gesture to the gadget under the mouse, depending on whether a mouse button is being held down or not." } ;
194
195 HELP: forget-rollover
196 { $description "Sends " { $link mouse-leave } " gestures to all gadgets containing the gadget under the mouse, and resets the " { $link hand-gadget } " variable." } ;
197
198 HELP: request-focus
199 { $values { "gadget" gadget } }
200 { $description "Gives keyboard focus to the " { $link focusable-child } " of the gadget. This may result in " { $link lose-focus } " and " { $link gain-focus } " gestures being sent." } ;
201
202 HELP: drag-loc
203 { $values { "loc" "a pair of integers" } }
204 { $description "Outputs the distance travelled by the mouse since the most recent press. Only meaningful inside a " { $link drag } " gesture handler." } ;
205
206 HELP: hand-rel
207 { $values { "gadget" gadget } { "loc" "a pair of integers" } }
208 { $description "Outputs the location of the mouse relative to the top-left corner of the gadget. Only meaningful inside a " { $link button-down } ", " { $link button-up } ", " { $link motion } " or " { $link drag } " gesture handler, where the gadget is contained in the same world as the gadget receiving the gesture." } ;
209
210 HELP: hand-click-rel
211 { $values { "gadget" gadget } { "loc" "a pair of integers" } }
212 { $description "Outputs the location of the last mouse relative to the top-left corner of the gadget. Only meaningful inside a " { $link button-down } ", " { $link button-up } ", " { $link motion } " or " { $link drag } " gesture handler, where the gadget is contained in the same world as the gadget receiving the gesture." } ;
213
214 HELP: under-hand
215 { $values { "seq" "a new sequence" } }
216 { $description "Outputs a sequence where the first element is the " { $link hand-world } " and the last is the " { $link hand-gadget } ", with all parents in between." } ;
217
218 HELP: gesture>string
219 { $values { "gesture" "a gesture" } { "string/f" { $maybe string } } }
220 { $contract "Creates a human-readable string from a gesture object, returning " { $link f } " if the gesture does not have a human-readable form." }
221 { $examples
222     { $unchecked-example "USING: io ui.gestures ;" "T{ key-down f { C+ } \"x\" } gesture>string print" "C+x" }
223 } ;
224
225 HELP: left-action
226 { $class-description "Gesture sent when the user performs a multi-touch three-finger swipe left." } ;
227
228 HELP: right-action
229 { $class-description "Gesture sent when the user performs a multi-touch three-finger swipe right." } ;
230
231 HELP: up-action
232 { $class-description "Gesture sent when the user performs a multi-touch three-finger swipe up." } ;
233
234 HELP: down-action
235 { $class-description "Gesture sent when the user performs a multi-touch three-finger swipe down." } ;
236
237 HELP: world-focus
238 { $values { "world" world } { "gadget" gadget } }
239 { $description "Gets the gadget that is in focus for the world." } ;
240
241 HELP: zoom-in-action
242 { $class-description "Gesture sent when the user performs a multi-touch two-finger pinch in." } ;
243
244 HELP: zoom-out-action
245 { $class-description "Gesture sent when the user performs a multi-touch two-finger pinch out." } ;
246
247 ARTICLE: "gesture-differences" "Gesture handling differences between platforms"
248 "On Mac OS X, the modifier keys map as follows:"
249 { $table
250     { { $link S+ } "Shift" }
251     { { $link A+ } "Option" }
252     { { $link C+ } "Control" }
253     { { $link M+ } "Command (Apple)" }
254 }
255 "On Windows and X11:"
256 { $table
257     { { $link S+ } "Shift" }
258     { { $link A+ } "Alt" }
259     { { $link C+ } "Control" }
260     { { $link M+ } "Windows key (often called Super on Unix)" }
261 }
262 "On Windows, " { $link key-up } " gestures are not reported for all keyboard events."
263 $nl
264 { $link "multitouch-gestures" } " are only supported on Mac OS X."
265 $nl
266 { $link "filedrop-gestures" } " are only supported on Windows." ;
267
268 ARTICLE: "ui-gestures" "UI gestures"
269 "User actions such as keyboard input and mouse button clicks deliver " { $emphasis "gestures" } " to gadgets. If the direct receiver of the gesture does not handle it, the gesture is passed on to the receiver's parent, and this way it travels up the gadget hierarchy. Gestures which are not handled at some point are ignored."
270 $nl
271 "There are two ways to define gesture handling logic. The simplest way is to associate a fixed set of gestures with a class:"
272 { $subsections set-gestures }
273 "Another way is to define a generic word on a class which handles all gestures sent to gadgets of that class:"
274 { $subsections handle-gesture }
275 "Sometimes a gesture needs to be presented to the user:"
276 { $subsections gesture>string }
277 "Keyboard input:"
278 { $subsections
279     "ui-focus"
280     "keyboard-gestures"
281     "action-gestures"
282     "ui-user-input"
283 }
284 "Mouse input:"
285 { $subsections
286     "mouse-gestures"
287     "multitouch-gestures"
288     "filedrop-gestures"
289 }
290 "Guidelines for cross-platform applications:"
291 { $subsections "gesture-differences" }
292 "Abstractions built on top of gestures:"
293 { $subsections
294     "ui-commands"
295     "ui-operations"
296 } ;
297
298 ARTICLE: "ui-focus" "Keyboard focus"
299 "The gadget with keyboard focus is the current receiver of keyboard gestures and user input. Gadgets that wish to receive keyboard input should request focus when clicked:"
300 { $subsections request-focus }
301 "The following example demonstrates defining a handler for a mouse click gesture which requests focus:"
302 { $code
303     "my-gadget H{"
304     "    { T{ button-down } [ request-focus ] }"
305     "} set-gestures"
306 }
307 "To nominate a single child as the default focusable child, implement a method on a generic word:"
308 { $subsections focusable-child* }
309 "Gestures are sent to a gadget when it gains or loses focus; this can be used to change the gadget's appearance, for example by displaying a border:"
310 { $subsections
311     gain-focus
312     lose-focus
313 } ;
314
315 ARTICLE: "keyboard-gestures" "Keyboard gestures"
316 "There are two types of keyboard gestures:"
317 { $subsections
318     key-down
319     key-up
320 }
321 "Each keyboard gesture has a set of modifiers and a key symbol. The set modifiers is denoted by an array which must either be " { $link f } ", or an order-preserving subsequence of the following:"
322 { $code "{ S+ C+ A+ M+ }" }
323 { $subsections
324     S+
325     C+
326     A+
327     M+
328 }
329 "A key symbol is either a single-character string denoting literal input, or one of the following strings:"
330 { $list
331   { $snippet "CLEAR" }
332   { $snippet "RET" }
333   { $snippet "ENTER" }
334   { $snippet "ESC" }
335   { $snippet "TAB" }
336   { $snippet "BACKSPACE" }
337   { $snippet "HOME" }
338   { $snippet "DELETE" }
339   { $snippet "END" }
340   { $snippet "F1" }
341   { $snippet "F2" }
342   { $snippet "F3" }
343   { $snippet "F4" }
344   { $snippet "F5" }
345   { $snippet "F6" }
346   { $snippet "F7" }
347   { $snippet "F8" }
348   { $snippet "F9" }
349   { $snippet "F10" }
350   { $snippet "F11" }
351   { $snippet "F12" }
352   { $snippet "LEFT" }
353   { $snippet "RIGHT" }
354   { $snippet "DOWN" }
355   { $snippet "UP" }
356   { $snippet "PAGE_UP" }
357   { $snippet "PAGE_DOWN" }
358 }
359 "The " { $link S+ } " modifier is only ever used with the above action keys; alphanumeric input input with the shift key is delivered without the " { $link S+ } " modifier set, instead the input itself is upper case. For example, the gesture corresponding to " { $snippet "s" } " with the Control and Shift keys pressed is presented as "
360 { $code "T{ key-down f { C+ } \"S\" }" }
361 "The " { $snippet "RET" } " and " { $snippet "TAB" } " keys are never delivered in their literal form (" { $snippet "\"\\n\"" } " and " { $snippet "\"\\t\"" } ")." ;
362
363 ARTICLE: "ui-user-input" "Free-form keyboard input"
364 "Whereas keyboard gestures are intended to be used for keyboard shortcuts, certain gadgets such as text fields need to accept free-form keyboard input. This can be done by implementing a generic word:"
365 { $subsections user-input* } ;
366
367 ARTICLE: "mouse-gestures" "Mouse gestures"
368 "There are two types of mouse gestures indicating button clicks:"
369 { $subsections
370     button-down
371     button-up
372 }
373 "When a mouse button is pressed or released, two gestures are sent. The first gesture indicates the specific button number, and if this gesture is not handled, the second has a button number set to " { $link f } ":"
374 { $code "T{ button-down f 1 }" "T{ button-down f f }" }
375 "Because tuple literals fill unspecified slots with " { $link f } ", the last gesture can be written as " { $snippet "T{ button-down }" } "."
376 $nl
377 "Gestures to indicate mouse motion, depending on whenever a button is held down or not:"
378 { $subsections
379     motion
380     drag
381 }
382 "Gestures to indicate that the mouse has crossed gadget boundaries:"
383 { $subsections
384     mouse-enter
385     mouse-leave
386 }
387 "A number of global variables are set after a mouse gesture is sent. These variables can be read to obtain additional information about the gesture."
388 { $subsections
389     hand-gadget
390     hand-world
391     hand-loc
392     hand-buttons
393     hand-clicked
394     hand-click-loc
395     hand-click#
396 }
397 "There are some utility words for working with click locations:"
398 { $subsections
399     hand-rel
400     hand-click-rel
401     drag-loc
402 }
403 "Mouse scroll wheel gesture:"
404 { $subsections mouse-scroll }
405 "Global variable set when a mouse scroll wheel gesture is sent:"
406 { $subsections scroll-direction } ;
407
408 ARTICLE: "multitouch-gestures" "Multi-touch gestures"
409 "Multi-touch gestures are only supported on Mac OS X with newer MacBook and MacBook Pro models."
410 $nl
411 "Three-finger swipe:"
412 { $subsections
413     left-action
414     right-action
415     up-action
416     down-action
417 }
418 "Two-finger pinch:"
419 { $subsections
420     zoom-in-action
421     zoom-out-action
422 } ;
423
424 ARTICLE: "filedrop-gestures" "File drop gestures"
425 "File drop gestures are only supported on Windows. When user drags-and-drops a file or a group of files from another application, the following gesture can be handled:"
426 { $subsections file-drop } ;
427
428 HELP: file-drop
429 { $class-description "File drop gesture. The " { $slot "mods" } " slot contains the keyboard modifiers active at the time of the drop (see " { $link "keyboard-gestures" } "). The " { $link dropped-files } " global variable contains an array of full paths of the files that were dropped."
430 $nl
431 "The " { $link hand-loc } " global variable contains the drop location. If the user dropped files onto the non-client area of a window (the caption or the border), the gesture will not be triggered, but the contents of the " { $link dropped-files } " will be updated." }
432 { $examples
433 "A typical gesture handler looks like this:
434 " { $snippet "your-gadget-class H{
435     { T{ file-drop } [
436         dropped-files get-global [ nip ] curry models:change-model
437     ] }
438 } set-gestures" } } ;
439
440 HELP: dropped-files
441 { $var-description "The global variable holds an array of full paths of the files that were dropped by the last " { $link file-drop } " gesture." } ;
442
443 ARTICLE: "action-gestures" "Action gestures"
444 "Action gestures exist to keep keyboard shortcuts for common application operations consistent."
445 { $subsections
446     undo-action
447     redo-action
448     cut-action
449     copy-action
450     paste-action
451     delete-action
452     select-all-action
453     new-action
454     open-action
455     save-action
456     save-as-action
457     revert-action
458     close-action
459 }
460 "The following keyboard gestures, if not handled directly by any gadget in the hierarchy until reaching the world, are re-sent as action gestures to the first gadget:"
461 { $table
462     { { $strong "Keyboard gesture" } { $strong "Action gesture" } }
463     { { $snippet "T{ key-down f { C+ } \"z\" }" } { $snippet "undo-action" } }
464     { { $snippet "T{ key-down f { C+ } \"y\" }" } { $snippet "redo-action" } }
465     { { $snippet "T{ key-down f { C+ } \"x\" }" } { $snippet "cut-action" } }
466     { { $snippet "T{ key-down f { C+ } \"c\" }" } { $snippet "copy-action" } }
467     { { $snippet "T{ key-down f { C+ } \"v\" }" } { $snippet "paste-action" } }
468     { { $snippet "T{ key-down f { C+ } \"a\" }" } { $snippet "select-all-action" } }
469     { { $snippet "T{ key-down f { C+ } \"n\" }" } { $snippet "new-action" } }
470     { { $snippet "T{ key-down f { C+ } \"o\" }" } { $snippet "open-action" } }
471     { { $snippet "T{ key-down f { C+ } \"s\" }" } { $snippet "save-action" } }
472     { { $snippet "T{ key-down f { C+ } \"S\" }" } { $snippet "save-as-action" } }
473     { { $snippet "T{ key-down f { C+ } \"w\" }" } { $snippet "close-action" } }
474 }
475 "Action gestures should be used in place of the above keyboard gestures if possible. For example, on Mac OS X, the standard " { $strong "Edit" } " menu items send action gestures." ;
476
477 ABOUT: "ui-gestures"