浏览代码

update tap to latest flutter to include more detailed info in TapDetails + update selectable_text to accept customized onTap callbacks

/main
xingwei.zhu 5 年前
当前提交
eb11123f
共有 3 个文件被更改,包括 176 次插入79 次删除
  1. 2
      Runtime/gestures/recognizer.cs
  2. 217
      Runtime/gestures/tap.cs
  3. 36
      Runtime/widgets/selectable_text.cs

2
Runtime/gestures/recognizer.cs


protected virtual void handleNonAllowedPointer(PointerDownEvent evt) {
}
protected bool isPointerAllowed(PointerDownEvent evt) {
protected virtual bool isPointerAllowed(PointerDownEvent evt) {
return this._kind == null || this._kind == evt.kind;
}

217
Runtime/gestures/tap.cs


public class TapDownDetails {
public TapDownDetails(Offset globalPosition = null,
Offset localPosition = null,
PointerDeviceKind kind = PointerDeviceKind.touch) {
PointerDeviceKind kind = PointerDeviceKind.touch,
int device = 0) {
this.device = device;
}
public readonly Offset globalPosition;

public readonly PointerDeviceKind kind;
public readonly int device;
}
public delegate void GestureTapDownCallback(TapDownDetails details);

public delegate void GestureTapCancelCallback();
public class TapGestureRecognizer : PrimaryPointerGestureRecognizer {
public TapGestureRecognizer(object debugOwner = null)
: base(deadline: Constants.kPressTimeout, debugOwner: debugOwner) { }
public GestureTapDownCallback onTapDown;
public abstract class BaseTapGestureRecognizer : PrimaryPointerGestureRecognizer {
public BaseTapGestureRecognizer(object debugOwner = null)
: base(deadline: Constants.kPressTimeout, debugOwner: debugOwner) {
}
public GestureTapUpCallback onTapUp;
bool _sentTapDown = false;
bool _wonArenaForPrimaryPointer = false;
public GestureTapCallback onTap;
PointerDownEvent _down;
PointerUpEvent _up;
public GestureTapCancelCallback onTapCancel;
protected abstract void handleTapDown(PointerDownEvent down);
bool _sentTapDown = false;
protected abstract void handleTapUp(PointerDownEvent down, PointerUpEvent up);
bool _wonArenaForPrimaryPointer = false;
protected abstract void handleTapCancel(PointerDownEvent down, PointerCancelEvent cancel, string reason);
Offset _finalPosition;
public override void addAllowedPointer(PointerDownEvent evt) {
if (this.state == GestureRecognizerState.ready) {
this._down = evt;
}
base.addAllowedPointer(evt);
}
this._finalPosition = evt.position;
if (this._wonArenaForPrimaryPointer) {
this.resolve(GestureDisposition.accepted);
this._checkUp(evt);
}
}
else if (evt is PointerCancelEvent) {
if (this._sentTapDown && this.onTapCancel != null) {
this.invokeCallback<object>("onTapCancel", () => this.onTapCancel);
this._up = (PointerUpEvent) evt;
this._checkUp();
} else if (evt is PointerCancelEvent) {
this.resolve(GestureDisposition.rejected);
if (this._sentTapDown) {
this._checkCancel((PointerCancelEvent) evt, "");
} else if (evt.buttons != this._down.buttons) {
this.resolve(GestureDisposition.rejected);
this.stopTrackingPointer(this.primaryPointer);
}
}

if (this.onTapCancel != null) {
this.invokeCallback<object>("spontaneous onTapCancel", () => {
this.onTapCancel();
return null;
});
}
this._checkCancel(null, "spontaneous");
base.resolve(disposition);
}

public override void acceptGesture(int pointer) {
base.acceptGesture(pointer);
this._checkUp(null);
this._checkUp();
}
}

if (this._sentTapDown && this.onTapCancel != null) {
this.invokeCallback<object>("forced onTapCancel", () => {
this.onTapCancel();
return null;
});
D.assert(this.state != GestureRecognizerState.possible);
if (this._sentTapDown) {
this._checkCancel(null, "forced");
}
this._reset();

void _checkDown() {
if (!this._sentTapDown) {
if (this.onTapDown != null) {
this.invokeCallback<object>("onTapDown", () => {
this.onTapDown(new TapDownDetails(globalPosition: this.initialPosition));
return null;
});
}
if (this._sentTapDown) {
return;
}
this.handleTapDown(down: this._down);
}
void _checkUp() {
if (!this._wonArenaForPrimaryPointer || this._up == null) {
return;
}
this.handleTapUp(down: this._down, up: this._up);
this._reset();
}
void _checkCancel(PointerCancelEvent evt, string note) {
this.handleTapCancel(down: this._down, cancel: evt, reason: note);
}
void _reset() {
this._sentTapDown = false;
this._wonArenaForPrimaryPointer = false;
this._up = null;
this._down = null;
}
public override string debugDescription {
get { return "base tap"; }
}
public override void debugFillProperties(DiagnosticPropertiesBuilder properties) {
base.debugFillProperties(properties);
properties.add(new FlagProperty("wonArenaForPrimaryPointer", value: this._wonArenaForPrimaryPointer, ifTrue: "won arena"));
properties.add(new DiagnosticsProperty<Offset>("finalPosition", this._up?.position, defaultValue: null));
properties.add(new DiagnosticsProperty<Offset>("finalLocalPosition", this._up?.position, defaultValue: this._up?.position));
properties.add(new DiagnosticsProperty<int>("button", this._down?.buttons?? 0, defaultValue: 0));
properties.add(new FlagProperty("sentTapDown", value: this._sentTapDown, ifTrue: "sent tap down"));
}
}
this._sentTapDown = true;
public class TapGestureRecognizer : BaseTapGestureRecognizer {
public TapGestureRecognizer(object debugOwner = null) : base(debugOwner: debugOwner) {
}
public GestureTapDownCallback onTapDown;
public GestureTapUpCallback onTapUp;
public GestureTapCallback onTap;
public GestureTapCancelCallback onTapCancel;
public GestureTapDownCallback onSecondaryTapDown;
public GestureTapUpCallback onSecondaryTapUp;
public GestureTapCancelCallback onSecondaryTapCancel;
protected override bool isPointerAllowed(PointerDownEvent evt) {
if (this.onTapDown == null &&
this.onTap == null &&
this.onTapUp == null &&
this.onTapCancel == null) {
return false;
return base.isPointerAllowed(evt);
void _checkUp(PointerEvent evt) {
if (this._finalPosition != null) {
if (this.onTapUp != null) {
this.invokeCallback<object>("onTapUp", () => {
this.onTapUp(new TapUpDetails(globalPosition: this._finalPosition,
kind: evt?.kind ?? PointerDeviceKind.touch,
device: evt?.device ?? 0));
return null;
});
}
protected override void handleTapDown(PointerDownEvent down) {
if (this.onTapDown != null) {
TapDownDetails details = new TapDownDetails(
globalPosition: down.position,
kind: down.kind,
device: down.device
);
if (this.onTap != null) {
this.invokeCallback<object>("onTap", () => {
this.onTap();
this.invokeCallback<object>("onTapDown", () => {
this.onTapDown(details);
}
this._reset();
void _reset() {
this._sentTapDown = false;
this._wonArenaForPrimaryPointer = false;
this._finalPosition = null;
protected override void handleTapUp(PointerDownEvent down, PointerUpEvent up) {
TapUpDetails details = new TapUpDetails(
globalPosition: up.position,
kind: up.kind,
device: up.device
);
if (this.onTapUp != null) {
this.invokeCallback<object>("onTapUp", () => {
this.onTapUp(details);
return null;
});
}
if (this.onTap != null) {
this.invokeCallback<object>("onTap", () => {
this.onTap();
return null;
});
}
protected override void handleTapCancel(PointerDownEvent down, PointerCancelEvent cancel, string note) {
if (this.onTapCancel != null) {
this.invokeCallback<object>("onTapCancel", () => {
this.onTapCancel();
return null;
});
}
}
}
public override void debugFillProperties(DiagnosticPropertiesBuilder properties) {
base.debugFillProperties(properties);
properties.add(new FlagProperty("wonArenaForPrimaryPointer",
value: this._wonArenaForPrimaryPointer,
ifTrue: "won arena"));
properties.add(new DiagnosticsProperty<Offset>("finalPosition",
this._finalPosition, defaultValue: Diagnostics.kNullDefaultValue));
properties.add(new FlagProperty("sentTapDown",
value: this._sentTapDown, ifTrue: "sent tap down"));
}
}
}

36
Runtime/widgets/selectable_text.cs


using Unity.UIWidgets.painting;
using Unity.UIWidgets.rendering;
using Unity.UIWidgets.ui;
using Color = Unity.UIWidgets.ui.Color;
using Constants = Unity.UIWidgets.gestures.Constants;
using TextStyle = Unity.UIWidgets.painting.TextStyle;

float? textScaleFactor = null,
int? maxLines = null,
FocusNode focusNode = null,
Color selectionColor = null) : base(key) {
Color selectionColor = null,
GestureTapDownCallback onTapDown = null,
GestureTapUpCallback onTapUp = null,
GestureTapCancelCallback onTapCancel = null) : base(key) {
D.assert(data != null);
this.textSpan = null;
this.data = data;

this.maxLines = maxLines;
this.focusNode = focusNode ?? new FocusNode();
this.selectionColor = selectionColor;
this.onTapDown = onTapDown;
this.onTapUp = onTapUp;
this.onTapCancel = onTapCancel;
}
public SelectableText(TextSpan textSpan,

float? textScaleFactor = null,
int? maxLines = null,
FocusNode focusNode = null,
Color selectionColor = null) : base(key) {
Color selectionColor = null,
GestureTapDownCallback onTapDown = null,
GestureTapUpCallback onTapUp = null,
GestureTapCancelCallback onTapCancel = null) : base(key) {
D.assert(textSpan != null);
this.textSpan = textSpan;
this.data = null;

this.maxLines = maxLines;
this.focusNode = focusNode ?? new FocusNode();
this.selectionColor = selectionColor;
this.onTapDown = onTapDown;
this.onTapUp = onTapUp;
this.onTapCancel = onTapCancel;
}
public static SelectableText rich(TextSpan textSpan,

float? textScaleFactor = null,
int? maxLines = null,
FocusNode focusNode = null,
Color selectionColor = null) {
Color selectionColor = null,
GestureTapDownCallback onTapDown = null,
GestureTapUpCallback onTapUp = null,
GestureTapCancelCallback onTapCancel = null) {
return new SelectableText(
textSpan, key,
style,

textScaleFactor,
maxLines,
focusNode,
selectionColor);
selectionColor,
onTapDown,
onTapUp,
onTapCancel);
}
public readonly string data;

public readonly int? maxLines;
public readonly Color selectionColor;
public readonly GestureTapDownCallback onTapDown;
public readonly GestureTapUpCallback onTapUp;
public readonly GestureTapCancelCallback onTapCancel;
public override State createState() {
return new _SelectableTextState();

}
void _handleTapDown(TapDownDetails details) {
this.widget.onTapDown?.Invoke(details);
this.widget.onTapUp?.Invoke(details);
this.widget.onTapCancel?.Invoke();
}
void _handleLongPress() {

正在加载...
取消
保存