Redo: Add buttons to gestures (#31819)

* Revert "Revert "Add buttons to gestures (#30339)" (#31801)"

This reverts commit 8fd7fa492a.

* Synthesise kPrimaryButton for unknown devices

* Change TestPointer to a better API
This commit is contained in:
Tong Mu
2019-04-30 09:56:24 -07:00
committed by GitHub
parent 71a89a469a
commit fea2c7d671
16 changed files with 1932 additions and 218 deletions

View File

@@ -22,9 +22,15 @@ class TestPointer {
///
/// Multiple [TestPointer]s created with the same pointer identifier will
/// interfere with each other if they are used in parallel.
TestPointer([this.pointer = 1, this.kind = PointerDeviceKind.touch])
TestPointer([
this.pointer = 1,
this.kind = PointerDeviceKind.touch,
int buttons = kPrimaryButton,
])
: assert(kind != null),
assert(pointer != null);
assert(pointer != null),
assert(buttons != null),
_buttons = buttons;
/// The pointer identifier used for events generated by this object.
///
@@ -35,6 +41,11 @@ class TestPointer {
/// [PointerDeviceKind.touch].
final PointerDeviceKind kind;
/// The kind of buttons to simulate on Down and Move events. Defaults to
/// [kPrimaryButton].
int get buttons => _buttons;
int _buttons;
/// Whether the pointer simulated by this object is currently down.
///
/// A pointer is released (goes up) by calling [up] or [cancel].
@@ -51,8 +62,14 @@ class TestPointer {
/// If a custom event is created outside of this class, this function is used
/// to set the [isDown].
bool setDownInfo(PointerEvent event, Offset newLocation) {
bool setDownInfo(
PointerEvent event,
Offset newLocation, {
int buttons,
}) {
_location = newLocation;
if (buttons != null)
_buttons = buttons;
switch (event.runtimeType) {
case PointerDownEvent:
assert(!isDown);
@@ -72,15 +89,25 @@ class TestPointer {
///
/// By default, the time stamp on the event is [Duration.zero]. You can give a
/// specific time stamp by passing the `timeStamp` argument.
PointerDownEvent down(Offset newLocation, {Duration timeStamp = Duration.zero}) {
///
/// By default, the set of buttons in the last down or move event is used.
/// You can give a specific set of buttons by passing the `buttons` argument.
PointerDownEvent down(
Offset newLocation, {
Duration timeStamp = Duration.zero,
int buttons,
}) {
assert(!isDown);
_isDown = true;
_location = newLocation;
if (buttons != null)
_buttons = buttons;
return PointerDownEvent(
timeStamp: timeStamp,
kind: kind,
pointer: pointer,
position: location,
buttons: _buttons,
);
}
@@ -91,7 +118,14 @@ class TestPointer {
///
/// [isDown] must be true when this is called, since move events can only
/// be generated when the pointer is down.
PointerMoveEvent move(Offset newLocation, {Duration timeStamp = Duration.zero}) {
///
/// By default, the set of buttons in the last down or move event is used.
/// You can give a specific set of buttons by passing the `buttons` argument.
PointerMoveEvent move(
Offset newLocation, {
Duration timeStamp = Duration.zero,
int buttons,
}) {
assert(
isDown,
'Move events can only be generated when the pointer is down. To '
@@ -99,12 +133,15 @@ class TestPointer {
'up, use hover() instead.');
final Offset delta = newLocation - location;
_location = newLocation;
if (buttons != null)
_buttons = buttons;
return PointerMoveEvent(
timeStamp: timeStamp,
kind: kind,
pointer: pointer,
position: newLocation,
delta: delta,
buttons: _buttons,
);
}