| OLD | NEW |
| 1 Gestures | 1 Gestures |
| 2 ======== | 2 ======== |
| 3 | 3 |
| 4 ```javascript | 4 ```javascript |
| 5 | 5 |
| 6 callback GestureCallback void (Event event); | 6 callback GestureCallback void (Event event); |
| 7 | 7 |
| 8 abstract class Gesture { | 8 abstract class Gesture { |
| 9 constructor (); | 9 constructor (); |
| 10 | 10 |
| 11 // Gestures cycle through states: | 11 // Gestures cycle through states: |
| 12 // - idle: nothing is going on | 12 // - idle: nothing is going on (active, accepted, and discarding |
| 13 // are false). |
| 13 // - buffering: the GestureChooser is passing in some events, but | 14 // - buffering: the GestureChooser is passing in some events, but |
| 14 // hasn't yet committed to using this Gesture, and this Gesture | 15 // hasn't yet committed to using this Gesture, and this Gesture |
| 15 // hasn't yet decided that this set of events isn't interesting. | 16 // hasn't yet decided that this set of events isn't interesting |
| 17 // (active is true, accepted is false, discarding is false). |
| 16 // - forwarding: this Gesture is still interesting and the | 18 // - forwarding: this Gesture is still interesting and the |
| 17 // GestureChooser has decided to use this Gesture so events are | 19 // GestureChooser has decided to use this Gesture so events are |
| 18 // being sent along | 20 // being sent along (active is true, accepted is true, discarding |
| 21 // is false). |
| 19 // - discarding: this Gesture got cancelled or didn't match the | 22 // - discarding: this Gesture got cancelled or didn't match the |
| 20 // pattern | 23 // pattern (active is true, accepted is false, discarding is |
| 24 // true). |
| 21 | 25 |
| 22 Boolean processEvent(Event event); | 26 // TODO(ianh): Need to handle gestures that want to send events |
| 27 // beyond the end of the gesture, e.g. inertia in scrolling. In that |
| 28 // mode, gestures are sending events but are simultaneously no |
| 29 // longer "active"... |
| 30 |
| 31 Boolean processEvent(EventTarget target, Event event); |
| 23 // as the events are received, they get examined to see if they fit | 32 // as the events are received, they get examined to see if they fit |
| 24 // the pattern for the gesture; if they do, then returns true, else, | 33 // the pattern for the gesture; if they do, then returns true, else, |
| 25 // returns false | 34 // returns false |
| 26 // - returning true after false has been returned is a contract | 35 // - returning true after false has been returned is a contract |
| 27 // violation unless active became false in between | 36 // violation unless active became false in between |
| 37 // - manipulating the event is a contract violation |
| 38 // |
| 28 // TODO(ianh): replace processEvent()'s return value with an enum: | 39 // TODO(ianh): replace processEvent()'s return value with an enum: |
| 29 // - acceptable (true and active is true) | 40 // - acceptable (true and active is true) |
| 30 // - discarding (false but active is still true) | 41 // - discarding (false but active is still true) |
| 31 // - finished (false and active is now false) | 42 // - finished (false and active is now false) |
| 32 // - in such a world, the contract would be that you can't return | 43 // in such a world, the contract would be that you can't return |
| 33 // 'acceptable' after returning 'discarding' without first | 44 // 'acceptable' after returning 'discarding' without first returning |
| 34 // returning 'finished' | 45 // 'finished' |
| 35 | 46 |
| 36 void accept(GestureCallback callback); | 47 void accept(GestureCallback callback); |
| 37 // send the buffered gesture events to callback, and use that | 48 // set accepted to true, send the buffered gesture events to |
| 38 // callback for all future Gesture events until the gesture is | 49 // callback, and use that callback for all future Gesture events |
| 39 // complete | 50 // until the gesture is complete |
| 40 // - call this immediately after getting a positive result from | 51 // - call this immediately after getting a positive result from |
| 41 // processEvent() | 52 // processEvent() |
| 53 |
| 54 // internal API: |
| 55 // void sendEvent(Event event) |
| 56 // - assert: active is true, discarding is false |
| 57 // - if accepted is true, then send the event straight to the |
| 58 // callback |
| 59 // - otherwise, add it to the buffer |
| 60 // |
| 61 // void discard() |
| 62 // - throw away the buffer, set discarding to true and accepted to |
| 63 // false |
| 42 | 64 |
| 43 readonly attribute Boolean active; // not idle (buffering, forwarding, or disc
arding) | 65 readonly attribute Boolean active; // not idle (buffering, forwarding, or disc
arding) |
| 44 readonly attribute Boolean accepted; // true if active and accept() has been c
alled (forwarding or discarding) | 66 readonly attribute Boolean accepted; // true if active and accept() has been c
alled (forwarding or discarding) |
| 45 readonly attribute Boolean discarding; // true if active and processEvent() ha
s returned false (discarding) | 67 readonly attribute Boolean discarding; // true if active and processEvent() ha
s returned false (discarding) |
| 46 // 'active' is currently part of the contract between Gesture and GestureChoos
er (the other two are not) | 68 // 'active' is currently part of the contract between Gesture and GestureChoos
er (the other two are not) |
| 69 |
| 70 // active can be true even if processEvent() returned false; this is |
| 71 // the discarding state. It means that the Gesture isn't sending any |
| 72 // more events, but that the pointers haven't yet reached a state in |
| 73 // which a new touch could begin. For example, a Tap gesture where |
| 74 // the finger has gone out of the bounding box can't be retriggered |
| 75 // until the finger is lifted. |
| 47 | 76 |
| 48 } | 77 } |
| 49 | 78 |
| 50 class GestureChooser : EventTarget { | 79 class GestureChooser : EventTarget { |
| 51 constructor (EventTarget? target = null, Array<Gesture> candidates = []); | 80 constructor (EventTarget? target = null, Array<Gesture> candidates = []); |
| 52 // throws if any of the candidates are active | 81 // throws if any of the candidates are active |
| 53 | 82 |
| 54 readonly attribute EventTarget? target; | 83 readonly attribute EventTarget? target; |
| 55 void setTarget(EventTarget? target); | 84 void setTarget(EventTarget? target); |
| 56 | 85 |
| 57 Array<Gesture> getGestures(); | 86 Array<Gesture> getGestures(); |
| 58 void addGesture(Gesture candidate); | 87 void addGesture(Gesture candidate); |
| 59 // throw if candidates.active is true | 88 // throw if candidates.active is true |
| 60 void removeGesture(Gesture candidate); | 89 void removeGesture(Gesture candidate); |
| 61 // if active is true and candidate was the last Gesture in our list | 90 // if active is true and candidate was the last Gesture in our list |
| 62 // to be active, set active and accepted to false | 91 // to be active, set active and accepted to false |
| 63 | 92 |
| 64 // while target is not null and the list of candidates is not empty, | 93 // while target is not null and the list of candidates is not empty, |
| 65 // ensures that it is registered as an event listener for | 94 // ensures that it is registered as an event listener for |
| 66 // pointer-down, pointer-move, and pointer-up events on the target; | 95 // pointer-down, pointer-move, and pointer-up events on the target; |
| 67 // when the target changes, or when the list of candidates is | 96 // when the target changes, or when the list of candidates is |
| 68 // emptied, unregisters itself | 97 // emptied, unregisters itself |
| 69 | 98 |
| 70 readonly attribute Boolean active; // at least one of the gestures is active (
initially false) | 99 readonly attribute Boolean active; // at least one of the gestures is active (
initially false) |
| 71 readonly attribute Boolean accepted; // we accepted a gesture since the last t
ime active was false (initially false) | 100 readonly attribute Boolean accepted; // we accepted a gesture since the last t
ime active was false (initially false) |
| 72 // any time one of the pointer events is received: | 101 // any time one of the pointer events is received: |
| 102 // - if it's pointer-down and it's already captured, ignore the |
| 103 // event; otherwise: |
| 73 // - let /candidates/ be a list of gestures, initially empty | 104 // - let /candidates/ be a list of gestures, initially empty |
| 74 // - if none of the registered gestures are active, then add all of | 105 // - if none of the registered gestures are active, then add all of |
| 75 // them to /candidates/ otherwise, add all the active ones to | 106 // them to /candidates/ otherwise, add all the active ones to |
| 76 // /candidates/ | 107 // /candidates/ |
| 77 // - call processEvent() with the event on all the Gestures in | 108 // - call processEvent() with the event on all the Gestures in |
| 78 // /candidates/ | 109 // /candidates/ |
| 110 // - if it's pointer-down and at least one Gesture returned true, |
| 111 // then capture the event |
| 79 // - if accepted is false, and exactly one of the processEvent() | 112 // - if accepted is false, and exactly one of the processEvent() |
| 80 // methods returned true, then set accepted to true and call that | 113 // methods returned true, then set accepted to true and call that |
| 81 // Gesture's accept() method, passing it a method that fires the | 114 // Gesture's accept() method, passing it a method that fires the |
| 82 // provided event on the current target (if not null) | 115 // provided event on the current target (if not null) |
| 83 // - if all the processEvent() methods returned false, and all the | 116 // - if all the processEvent() methods returned false, and all the |
| 84 // Gestures are no longer active, then set active and accepted to | 117 // Gestures are no longer active, then set active and accepted to |
| 85 // false; otherwise, set active to true | 118 // false; otherwise, set active to true |
| 86 } | 119 } |
| 120 |
| 121 class TapGesture : Gesture { |
| 122 Boolean processEvent(EventTarget target, Event event); |
| 123 // if discarding is true: |
| 124 // - if the event is a primary pointer-up, set active, accepted, and |
| 125 // discarding to false, and return false |
| 126 // - otherwise, just return false |
| 127 // if EventTarget isn't an Element: |
| 128 // - set active to true, discard(), and return false |
| 129 // if the event is pointer-down: |
| 130 // - if it's primary: |
| 131 // - assert: active is false |
| 132 // - sendEvent() a tap-down event |
| 133 // - set active to true and return true |
| 134 // - otherwise, if we're active: |
| 135 // - return true |
| 136 // - otherwise: |
| 137 // - return false |
| 138 // if the event is pointer-move: |
| 139 // - if it's primary: |
| 140 // - if it hit tests within target's bounding box: |
| 141 // - sendEvent() a tap-move event |
| 142 // - return true |
| 143 // - otherwise: |
| 144 // - sendEvent() a tap-cancel event |
| 145 // - discard() and return false |
| 146 // - otherwise, if we're active: |
| 147 // - return true |
| 148 // - otherwise: |
| 149 // - return false |
| 150 // if the event is pointer-up: |
| 151 // - if it's primary: |
| 152 // - sendEvent() a tap event |
| 153 // - set accepted and active to false, discard(), and return false |
| 154 // - otherwise, if we're active: |
| 155 // - return true |
| 156 // - otherwise: |
| 157 // - return false |
| 158 } |
| 159 |
| 160 class ScrollGesture : Gesture { |
| 161 Boolean processEvent(EventTarget target, Event event); |
| 162 // this fires the following events: |
| 163 // TODO(ianh): fill this in |
| 164 } |
| 165 |
| 87 ``` | 166 ``` |
| OLD | NEW |