]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/gestures/gestures-docs.factor
Merge branch 'master' into microseconds
[factor.git] / basis / ui / gestures / gestures-docs.factor
1 USING: ui.gadgets help.markup help.syntax hashtables
2 strings kernel system ;
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" } { "?" "a 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 } "." } ;
17
18 { propagate-gesture handle-gesture set-gestures } related-words
19
20 HELP: propagate-gesture
21 { $values { "gesture" "a gesture" } { "gadget" gadget } }
22 { $description "Calls " { $link handle-gesture } " on every parent of " { $snippet "gadget" } "." } ;
23
24 HELP: user-input
25 { $values { "string" string } { "gadget" gadget } }
26 { $description "Calls " { $link user-input* } " on every parent of the gadget." } ;
27
28 HELP: motion
29 { $class-description "Mouse motion gesture." }
30 { $examples { $code "T{ motion }" } } ;
31
32 HELP: drag
33 { $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." } ;
34
35 HELP: button-up
36 { $class-description "Mouse button up gesture. Instances have two slots:"
37     { $list
38         { { $snippet "mods" } " - a sequence of modifiers; see " { $link "keyboard-gestures" } }
39         { { $snippet "#" } " - a mouse button number, or " { $link f } " indicating no specific button is expected" }
40     }
41 }
42 { $examples { $code "T{ button-up f f 1 }" "T{ button-up }" } } ;
43
44 HELP: button-down
45 { $class-description "Mouse button down gesture. Instances have two slots:"
46     { $list
47         { { $snippet "mods" } " - a sequence of modifiers; see " { $link "keyboard-gestures" } }
48         { { $snippet "#" } " - a mouse button number, or " { $link f } " indicating no specific button is expected" }
49     }
50 }
51 { $examples { $code "T{ button-down f f 1 }" "T{ button-down }" } } ;
52
53 HELP: mouse-scroll
54 { $class-description "Scroll wheel motion gesture. When this gesture is sent, the " { $link scroll-direction } " global variable is set to a direction vector." }
55 { $examples { $code "T{ mouse-scroll }" } } ;
56
57 HELP: mouse-enter
58 { $class-description "Gesture sent when the mouse enters the bounds of a gadget." }
59 { $examples { $code "T{ mouse-enter }" } } ;
60
61 HELP: mouse-leave
62 { $class-description "Gesture sent when the mouse leaves the bounds of a gadget." }
63 { $examples { $code "T{ mouse-leave }" } } ;
64
65 HELP: gain-focus
66 { $class-description "Gesture sent when a gadget gains keyboard focus." }
67 { $examples { $code "T{ gain-focus }" } } ;
68
69 HELP: lose-focus
70 { $class-description "Gesture sent when a gadget loses keyboard focus." }
71 { $examples { $code "T{ lose-focus }" } } ;
72
73 HELP: cut-action
74 { $class-description "Gesture sent when the " { $emphasis "cut" } " standard window system action is invoked." }
75 { $examples { $code "T{ cut-action }" } } ;
76
77 HELP: copy-action
78 { $class-description "Gesture sent when the " { $emphasis "copy" } " standard window system action is invoked." }
79 { $examples { $code "T{ copy-action }" } } ;
80
81 HELP: paste-action
82 { $class-description "Gesture sent when the " { $emphasis "paste" } " standard window system action is invoked." }
83 { $examples { $code "T{ paste-action }" } } ;
84
85 HELP: delete-action
86 { $class-description "Gesture sent when the " { $emphasis "delete" } " standard window system action is invoked." }
87 { $examples { $code "T{ delete-action }" } } ;
88
89 HELP: select-all-action
90 { $class-description "Gesture sent when the " { $emphasis "select all" } " standard window system action is invoked." }
91 { $examples { $code "T{ select-all-action }" } } ;
92
93 HELP: C+
94 { $description "Control key modifier." } ;
95
96 HELP: A+
97 { $description "Alt key modifier." } ;
98
99 HELP: M+
100 { $description "Meta key modifier. This is the Command key on Mac OS X." } ;
101
102 HELP: S+
103 { $description "Shift key modifier." } ;
104
105 HELP: key-down
106 { $class-description "Key down gesture. Instances have two slots:"
107     { $list
108         { { $snippet "mods" } " - a sequence of modifiers; see " { $link "keyboard-gestures" } }
109     { { $snippet "sym" } " - a string denoting the key pressed; see " { $link "keyboard-gestures" } }
110     }
111 }
112 { $examples { $code "T{ key-down f { C+ } \"a\" }" "T{ key-down f f \"TAB\" }" } } ;
113
114 HELP: key-up
115 { $class-description "Key up gesture. Instances have two slots:"
116     { $list
117         { { $snippet "mods" } " - a sequence of modifiers; see " { $link "keyboard-gestures" } }
118     { { $snippet "sym" } " - a string denoting the key pressed; see " { $link "keyboard-gestures" } }
119     }
120 }
121 { $examples { $code "T{ key-up f { C+ } \"a\" }" "T{ key-up f f \"TAB\" }" } } ;
122
123 HELP: hand-gadget
124 { $var-description "Global variable. The gadget at the mouse location." } ;
125
126 HELP: hand-loc
127 { $var-description "Global variable. The mouse location relative to the top-left corner of the " { $link hand-world } "." } ;
128
129 { hand-loc hand-rel } related-words
130
131 HELP: hand-clicked
132 { $var-description "Global variable. The gadget at the location of the most recent click." } ;
133
134 HELP: hand-click-loc
135 { $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 } "." } ;
136
137 { hand-clicked hand-click-loc } related-words
138
139 HELP: hand-click#
140 { $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." } ;
141
142 HELP: hand-last-button
143 { $var-description "Global variable. The mouse button most recently pressed." } ;
144
145 HELP: hand-last-time
146 { $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 micros } "." } ;
147
148 HELP: hand-buttons
149 { $var-description "Global variable. A vector of mouse buttons currently held down." } ;
150
151 HELP: scroll-direction
152 { $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." } ;
153
154 HELP: double-click-timeout
155 { $var-description "Global variable. The maximum delay between two button presses which will still increment " { $link hand-click# } "." } ;
156
157 HELP: button-gesture
158 { $values { "gesture" "a gesture" } }
159 { $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." } ;
160
161 HELP: fire-motion
162 { $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." } ;
163
164 HELP: forget-rollover
165 { $description "Sends " { $link mouse-leave } " gestures to all gadgets containing the gadget under the mouse, and resets the " { $link hand-gadget } " variable." } ;
166
167 HELP: request-focus
168 { $values { "gadget" gadget } }
169 { $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." } ;
170
171 HELP: drag-loc
172 { $values { "loc" "a pair of integers" } }
173 { $description "Outputs the distance travelled by the mouse since the most recent press. Only meaningful inside a " { $link drag } " gesture handler." } ;
174
175 HELP: hand-rel
176 { $values { "gadget" gadget } { "loc" "a pair of integers" } }
177 { $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." } ;
178
179 HELP: hand-click-rel
180 { $values { "gadget" gadget } { "loc" "a pair of integers" } }
181 { $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." } ;
182
183 HELP: under-hand
184 { $values { "seq" "a new sequence" } }
185 { $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." } ;
186
187 HELP: gesture>string
188 { $values { "gesture" "a gesture" } { "string/f" { $maybe string } } }
189 { $contract "Creates a human-readable string from a gesture object, returning " { $link f } " if the gesture does not have a human-readable form." }
190 { $examples
191     { $example "USING: io ui.gestures ;" "T{ key-down f { C+ } \"x\" } gesture>string print" "C+x" }
192 } ;
193
194 ARTICLE: "ui-gestures" "UI gestures"
195 "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."
196 $nl
197 "There are two ways to define gesture handling logic. The simplest way is to associate a fixed set of gestures with a class:"
198 { $subsection set-gestures }
199 "Another way is to define a generic word on a class which handles all gestures sent to gadgets of that class:"
200 { $subsection handle-gesture }
201 "Sometimes a gesture needs to be presented to the user:"
202 { $subsection gesture>string }
203 "Keyboard input:"
204 { $subsection "ui-focus" }
205 { $subsection "keyboard-gestures" }
206 { $subsection "action-gestures" }
207 { $subsection "ui-user-input" }
208 "Mouse input:"
209 { $subsection "mouse-gestures" }
210 "Abstractions built on top of gestures:"
211 { $subsection "ui-commands" }
212 { $subsection "ui-operations" } ;
213
214 ARTICLE: "ui-focus" "Keyboard focus"
215 "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:"
216 { $subsection request-focus }
217 "The following example demonstrates defining a handler for a mouse click gesture which requests focus:"
218 { $code
219     "my-gadget H{"
220     "    { T{ button-down } [ request-focus ] }"
221     "} set-gestures"
222 }
223 "To nominate a single child as the default focusable child, implement a method on a generic word:"
224 { $subsection focusable-child* }
225 "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:"
226 { $subsection gain-focus }
227 { $subsection lose-focus } ;
228
229 ARTICLE: "keyboard-gestures" "Keyboard gestures"
230 "There are two types of keyboard gestures:"
231 { $subsection key-down }
232 { $subsection key-up }
233 "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:"
234 { $code "{ S+ C+ A+ M+ }" }
235 { $subsection S+ }
236 { $subsection C+ }
237 { $subsection A+ }
238 { $subsection M+ }
239 "A key symbol is either a single-character string denoting literal input, or one of the following strings:"
240 { $list
241   { $snippet "CLEAR" }
242   { $snippet "RET" }
243   { $snippet "ENTER" }
244   { $snippet "ESC" }
245   { $snippet "TAB" }
246   { $snippet "BACKSPACE" }
247   { $snippet "HOME" }
248   { $snippet "DELETE" }
249   { $snippet "END" }
250   { $snippet "F1" }
251   { $snippet "F2" }
252   { $snippet "F3" }
253   { $snippet "F4" }
254   { $snippet "F5" }
255   { $snippet "F6" }
256   { $snippet "F7" }
257   { $snippet "F8" }
258   { $snippet "LEFT" }
259   { $snippet "RIGHT" }
260   { $snippet "DOWN" }
261   { $snippet "UP" }
262   { $snippet "PAGE_UP" }
263   { $snippet "PAGE_DOWN" }
264 }
265 "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 "
266 { $code "T{ key-down f { C+ } \"S\" }" }
267 "The " { $snippet "RET" } ", " { $snippet "TAB" } " and " { $snippet "SPACE" } " keys are never delivered in their literal form (" { $snippet "\"\\n\"" } ", " { $snippet "\"\\t\"" } " or "  { $snippet "\" \"" } ")." ;
268
269 ARTICLE: "ui-user-input" "Free-form keyboard input"
270 "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:"
271 { $subsection user-input* } ;
272
273 ARTICLE: "mouse-gestures" "Mouse gestures"
274 "There are two types of mouse gestures indicating button clicks:"
275 { $subsection button-down }
276 { $subsection button-up }
277 "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 } ":"
278 { $code "T{ button-down f 1 }" "T{ button-down f f }" }
279 "Because tuple literals fill unspecified slots with " { $link f } ", the last gesture can be written as " { $snippet "T{ button-down }" } "."
280 $nl
281 "Gestures to indicate mouse motion, depending on whenever a button is held down or not:"
282 { $subsection motion }
283 { $subsection drag }
284 "Gestures to indicate that the mouse has crossed gadget boundaries:"
285 { $subsection mouse-enter }
286 { $subsection mouse-leave }
287 "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."
288 { $subsection hand-gadget }
289 { $subsection hand-world }
290 { $subsection hand-loc }
291 { $subsection hand-buttons }
292 { $subsection hand-clicked }
293 { $subsection hand-click-loc }
294 { $subsection hand-click# }
295 "There are some utility words for working with click locations:"
296 { $subsection hand-rel }
297 { $subsection hand-click-rel }
298 { $subsection drag-loc }
299 "Mouse scroll wheel gesture:"
300 { $subsection mouse-scroll }
301 "Global variable set when a mouse scroll wheel gesture is sent:"
302 { $subsection scroll-direction } ;
303
304 ARTICLE: "action-gestures" "Action gestures"
305 "Action gestures exist to keep keyboard shortcuts for common clipboard operations consistent."
306 { $subsection cut-action }
307 { $subsection copy-action }
308 { $subsection paste-action }
309 { $subsection delete-action }
310 { $subsection select-all-action }
311 "The following keyboard gestures, if not handled directly, send action gestures:"
312 { $table
313     { { $strong "Keyboard gesture" } { $strong "Action gesture" } }
314     { { $snippet "T{ key-down f { C+ } \"x\" }" } { $snippet "T{ cut-action }" } }
315     { { $snippet "T{ key-down f { C+ } \"c\" }" } { $snippet "T{ copy-action }" } }
316     { { $snippet "T{ key-down f { C+ } \"v\" }" } { $snippet "T{ paste-action }" } }
317     { { $snippet "T{ key-down f { C+ } \"a\" }" } { $snippet "T{ select-all }" } }
318 }
319 "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." ;
320
321 ABOUT: "ui-gestures"