浏览代码

[1.5.4] Finish TODOs in gesture_detector and text_selection.

/main
Yuncong Zhang 5 年前
当前提交
06160d76
共有 7 个文件被更改,包括 164 次插入177 次删除
  1. 160
      Runtime/gestures/long_press.cs
  2. 62
      Runtime/material/text_field.cs
  3. 2
      Runtime/rendering/editable.cs
  4. 11
      Runtime/widgets/basic.cs
  5. 56
      Runtime/widgets/gesture_detector.cs
  6. 44
      Runtime/widgets/text_selection.cs
  7. 6
      Samples/UIWidgetSample/LongPressSample.cs

160
Runtime/gestures/long_press.cs


public delegate void GestureLongPressUpCallback();
public delegate void GestureLongPressDragStartCallback(GestureLongPressDragStartDetails details);
public delegate void GestureLongPressStartCallback(LongPressStartDetails details);
public delegate void GestureLongPressDragUpdateCallback(GestureLongPressDragUpdateDetails details);
public delegate void GestureLongPressMoveUpdateCallback(LongPressMoveUpdateDetails details);
public delegate void GestureLongPressDragUpCallback(GestureLongPressDragUpDetails details);
public delegate void GestureLongPressEndCallback(LongPressEndDetails details);
public class GestureLongPressDragStartDetails {
public GestureLongPressDragStartDetails(
TimeSpan? sourceTimeStamp = null,
public class LongPressStartDetails {
public LongPressStartDetails(
this.sourceTimeStamp = sourceTimeStamp;
public readonly TimeSpan? sourceTimeStamp;
public class GestureLongPressDragUpdateDetails {
public GestureLongPressDragUpdateDetails(
TimeSpan? sourceTimeStamp = null,
public class LongPressMoveUpdateDetails {
public LongPressMoveUpdateDetails(
this.sourceTimeStamp = sourceTimeStamp;
public readonly TimeSpan? sourceTimeStamp;
public readonly Offset globalPosition;

public class GestureLongPressDragUpDetails {
public GestureLongPressDragUpDetails(
TimeSpan? sourceTimeStamp = null,
public class LongPressEndDetails {
public LongPressEndDetails(
this.sourceTimeStamp = sourceTimeStamp;
public readonly TimeSpan? sourceTimeStamp;
public LongPressGestureRecognizer(object debugOwner = null, PointerDeviceKind? kind = null) :
base(deadline: Constants.kLongPressTimeout, debugOwner: debugOwner, kind: kind) {
}
public LongPressGestureRecognizer(
float? postAcceptSlopTolerance = null,
object debugOwner = null,
PointerDeviceKind? kind = null) : base(
deadline: Constants.kLongPressTimeout,
postAcceptSlopTolerance: postAcceptSlopTolerance,
kind: kind,
debugOwner: debugOwner) { }
Offset _longPressOrigin;
public GestureLongPressStartCallback onLongPressStart;
public GestureLongPressMoveUpdateCallback onLongPressMoveUpdate;
public GestureLongPressEndCallback onLongPressEnd;
base.acceptGesture(this.primaryPointer);
if (this.onLongPress != null) {
this.invokeCallback<object>("onLongPress", () => {
this.onLongPress();

}
protected override void handlePrimaryPointer(PointerEvent evt) {
if (evt is PointerUpEvent) {
if (this._longPressAccepted && this.onLongPressUp != null) {
this._longPressAccepted = false;
this.invokeCallback<object>("onLongPressUp", () => {
this.onLongPressUp();
if (this.onLongPressStart != null) {
this.invokeCallback<object>("onLongPressStart",
() => {
this.onLongPressStart(new LongPressStartDetails(globalPosition: this._longPressOrigin));
}
else {
this.resolve(GestureDisposition.rejected);
}
}
else if (evt is PointerDownEvent || evt is PointerCancelEvent) {
this._longPressAccepted = false;
public override string debugDescription {
get { return "long press"; }
}
}
public class LongPressDragGestureRecognizer : PrimaryPointerGestureRecognizer {
public LongPressDragGestureRecognizer(object debugOwner = null) : base(
deadline: Constants.kLongPressTimeout,
postAcceptSlopTolerance: null,
debugOwner: debugOwner
) {
}
bool _longPressAccepted = false;
Offset _longPressOrigin;
TimeSpan? _longPressStartTimestamp;
public GestureLongPressDragStartCallback onLongPressStart;
public GestureLongPressDragUpdateCallback onLongPressDragUpdate;
public GestureLongPressDragUpCallback onLongPressUp;
protected override void handlePrimaryPointer(PointerEvent evt) {
if (evt is PointerUpEvent) {
if (this._longPressAccepted) {
if (this.onLongPressUp != null) {
this.invokeCallback<object>("onLongPressUp", () => {
this.onLongPressUp();
return null;
});
}
protected override void didExceedDeadline() {
this.resolve(GestureDisposition.accepted);
this._longPressAccepted = true;
base.acceptGesture(this.primaryPointer);
if (this.onLongPressStart != null) {
this.invokeCallback<object>("onLongPressStart", () => {
this.onLongPressStart(new GestureLongPressDragStartDetails(
sourceTimeStamp: this._longPressStartTimestamp,
globalPosition: this._longPressOrigin
));
return null;
});
}
}
if (this.onLongPressEnd != null) {
this.invokeCallback<object>("onLongPressEnd", () => {
this.onLongPressEnd(new LongPressEndDetails(globalPosition: evt.position));
return null;
});
}
protected override void handlePrimaryPointer(PointerEvent e) {
if (e is PointerUpEvent) {
if (this._longPressAccepted == true && this.onLongPressUp != null) {
this._longPressAccepted = false;
this.invokeCallback<object>("onLongPressUp", () => {
this.onLongPressUp(new GestureLongPressDragUpDetails(
sourceTimeStamp: e.timeStamp,
globalPosition: e.position
));
return null;
});
this._longPressAccepted = true;
else if (e is PointerDownEvent) {
else if (evt is PointerDownEvent || evt is PointerCancelEvent) {
this._longPressStartTimestamp = e.timeStamp;
this._longPressOrigin = e.position;
this._longPressOrigin = evt.position;
else if (e is PointerMoveEvent && this._longPressAccepted && this.onLongPressDragUpdate != null) {
this.invokeCallback<object>("onLongPressDrag", () => {
this.onLongPressDragUpdate(new GestureLongPressDragUpdateDetails(
sourceTimeStamp: e.timeStamp,
globalPosition: e.position,
offsetFromOrigin: e.position - this._longPressOrigin
else if (evt is PointerMoveEvent && this._longPressAccepted && this.onLongPressMoveUpdate != null) {
this.invokeCallback<object>("onLongPressMoveUpdate", () => {
this.onLongPressMoveUpdate(new LongPressMoveUpdateDetails(
globalPosition: evt.position,
offsetFromOrigin: evt.position - this._longPressOrigin
));
return null;
});

public override void acceptGesture(int pointer) {
}
protected override void didStopTrackingLastPointer(int pointer) {
this._longPressAccepted = false;
this._longPressOrigin = null;
this._longPressStartTimestamp = null;
base.didStopTrackingLastPointer(pointer);
}
get { return "long press drag"; }
get { return "long press"; }
}
}
}

62
Runtime/material/text_field.cs


this._cancelCurrentSplash();
}
void _handleLongPress() {
if (this.widget.enableInteractiveSelection == true) {
this._renderEditable.handleLongPress();
void _handleSingleLongTapStart(LongPressStartDetails details) {
if (this.widget.selectionEnabled) {
switch (Theme.of(this.context).platform) {
case RuntimePlatform.IPhonePlayer:
this._renderEditable.selectPositionAt(
from: details.globalPosition,
cause: SelectionChangedCause.longPress
);
break;
case RuntimePlatform.Android:
this._renderEditable.selectWord(cause: SelectionChangedCause.longPress);
Feedback.forLongPress(this.context);
break;
}
}
this._confirmCurrentSplash();
}
void _handleSingleLongTapMoveUpdate(LongPressMoveUpdateDetails details) {
if (this.widget.selectionEnabled) {
switch (Theme.of(this.context).platform) {
case RuntimePlatform.IPhonePlayer:
this._renderEditable.selectPositionAt(
from: details.globalPosition,
cause: SelectionChangedCause.longPress
);
break;
case RuntimePlatform.Android:
this._renderEditable.selectWordsInRange(
from: details.globalPosition - details.offsetFromOrigin,
to: details.globalPosition,
cause: SelectionChangedCause.longPress);
Feedback.forLongPress(this.context);
break;
}
}
this._confirmCurrentSplash();
void _handleSingleLongTapEnd(LongPressEndDetails details) {
this._editableTextKey.currentState.showToolbar();
void _handleDragSelectionStart(DragStartDetails details) {
void _handleDoubleTapDown(TapDownDetails details) {
if (this.widget.selectionEnabled) {
this._renderEditable.selectWord(cause: SelectionChangedCause.doubleTap);
this._editableTextKey.currentState.showToolbar();
}
}
void _handleMouseDragSelectionStart(DragStartDetails details) {
this._renderEditable.selectPositionAt(
from: details.globalPosition,
cause: SelectionChangedCause.drag);

void _handleDragSelectionUpdate(DragStartDetails startDetails,
void _handleMouseDragSelectionUpdate(DragStartDetails startDetails,
DragUpdateDetails updateDetails) {
this._renderEditable.selectPositionAt(
from: startDetails.globalPosition,

// onForcePressStart: forcePressEnabled ? this._handleForcePressStarted : null, // TODO: Remove this when force press is added
onSingleTapUp: this._handleSingleTapUp,
onSingleTapCancel: this._handleSingleTapCancel,
onSingleLongTapStart: this._handleLongPress,
onDragSelectionStart: this._handleDragSelectionStart,
onDragSelectionUpdate: this._handleDragSelectionUpdate,
onSingleLongTapStart: this._handleSingleLongTapStart,
onSingleLongTapMoveUpdate: this._handleSingleLongTapMoveUpdate,
onSingleLongTapEnd: this._handleSingleLongTapEnd,
onDoubleTapDown: this._handleDoubleTapDown,
onDragSelectionStart: this._handleMouseDragSelectionStart,
onDragSelectionUpdate: this._handleMouseDragSelectionUpdate,
behavior: HitTestBehavior.translucent,
child: child
)

2
Runtime/rendering/editable.cs


this.selectWordsInRange(from: this._lastTapDownPosition, cause: cause);
}
void selectWordsInRange(Offset from = null, Offset to = null, SelectionChangedCause? cause = null) {
public void selectWordsInRange(Offset from = null, Offset to = null, SelectionChangedCause? cause = null) {
D.assert(cause != null);
D.assert(from != null);
this._layoutText(this.constraints.maxWidth);

11
Runtime/widgets/basic.cs


PointerHoverEventListener onPointerHover = null,
PointerUpEventListener onPointerUp = null,
PointerCancelEventListener onPointerCancel = null,
// TODO: onPointerSignal = null,
PointerSignalEventListener onPointerSignal = null,
PointerScrollEventListener onPointerScroll = null,
PointerDragFromEditorEnterEventListener onPointerDragFromEditorEnter = null,
PointerDragFromEditorHoverEventListener onPointerDragFromEditorHover = null,

this.onPointerMove = onPointerMove;
this.onPointerUp = onPointerUp;
this.onPointerCancel = onPointerCancel;
this.onPointerSignal = onPointerSignal;
this.onPointerHover = onPointerHover;
this.onPointerExit = onPointerExit;
this.onPointerEnter = onPointerEnter;

public readonly PointerCancelEventListener onPointerCancel;
public readonly PointerSignalEventListener onPointerSignal;
public readonly PointerHoverEventListener onPointerHover;
public readonly PointerEnterEventListener onPointerEnter;

onPointerMove: this.onPointerMove,
onPointerUp: this.onPointerUp,
onPointerCancel: this.onPointerCancel,
onPointerSignal: this.onPointerSignal,
onPointerEnter: this.onPointerEnter,
onPointerExit: this.onPointerExit,
onPointerHover: this.onPointerHover,

renderObject.onPointerMove = this.onPointerMove;
renderObject.onPointerUp = this.onPointerUp;
renderObject.onPointerCancel = this.onPointerCancel;
renderObject.onPointerSignal = this.onPointerSignal;
renderObject.onPointerEnter = this.onPointerEnter;
renderObject.onPointerHover = this.onPointerHover;
renderObject.onPointerExit = this.onPointerExit;

if (this.onPointerCancel != null) {
listeners.Add("cancel");
}
if (this.onPointerSignal != null) {
listeners.Add("signal");
}
if (this.onPointerEnter != null) {

56
Runtime/widgets/gesture_detector.cs


GestureTapCancelCallback onTapCancel = null,
GestureDoubleTapCallback onDoubleTap = null,
GestureLongPressCallback onLongPress = null,
// TODO: GestureLongPressStartCallback onLongPressStart = null,
// TODO: GestureLongPressMoveUpdate onLongPressMoveUpdate = null,
GestureLongPressStartCallback onLongPressStart = null,
GestureLongPressMoveUpdateCallback onLongPressMoveUpdate = null,
// TODO: GestureLongPressEnd onLongPressEnd = null,
GestureLongPressDragStartCallback onLongPressDragStart = null,
GestureLongPressDragUpdateCallback onLongPressDragUpdate = null,
GestureLongPressDragUpCallback onLongPressDragUp = null,
GestureLongPressEndCallback onLongPressEnd = null,
GestureDragDownCallback onVerticalDragDown = null,
GestureDragStartCallback onVerticalDragStart = null,
GestureDragUpdateCallback onVerticalDragUpdate = null,

bool haveHorizontalDrag =
onHorizontalDragStart != null || onHorizontalDragUpdate != null ||
onHorizontalDragEnd != null;
bool haveLongPress = onLongPress != null || onLongPressUp != null;
bool haveLongPressDrag = onLongPressDragStart != null || onLongPressDragUpdate != null ||
onLongPressDragUp != null;
bool havePan = onPanStart != null || onPanUpdate != null || onPanEnd != null;
bool haveScale = onScaleStart != null || onScaleUpdate != null || onScaleEnd != null;
if (havePan || haveScale) {

}
}
if (haveLongPress && haveLongPressDrag) {
throw new UIWidgetsError(
"Incorrect GestureDetector arguments.\n" +
"Having both a long press and a long press drag recognizer is " +
"redundant as the long press drag is a superset of long press. " +
"Except long press drag allows for drags after the long press is " +
"triggered."
);
}
return true;
});

this.onDoubleTap = onDoubleTap;
this.onLongPress = onLongPress;
this.onLongPressUp = onLongPressUp;
this.onLongPressDragStart = onLongPressDragStart;
this.onLongPressDragUpdate = onLongPressDragUpdate;
this.onLongPressDragUp = onLongPressDragUp;
this.onLongPressStart = onLongPressStart;
this.onLongPressMoveUpdate = onLongPressMoveUpdate;
this.onLongPressEnd = onLongPressEnd;
this.onVerticalDragDown = onVerticalDragDown;
this.onVerticalDragStart = onVerticalDragStart;
this.onVerticalDragUpdate = onVerticalDragUpdate;

public readonly GestureDoubleTapCallback onDoubleTap;
public readonly GestureLongPressCallback onLongPress;
public readonly GestureLongPressUpCallback onLongPressUp;
public readonly GestureLongPressDragStartCallback onLongPressDragStart;
public readonly GestureLongPressDragUpdateCallback onLongPressDragUpdate;
public readonly GestureLongPressDragUpCallback onLongPressDragUp;
public readonly GestureLongPressStartCallback onLongPressStart;
public readonly GestureLongPressMoveUpdateCallback onLongPressMoveUpdate;
public readonly GestureLongPressEndCallback onLongPressEnd;
public readonly GestureDragDownCallback onVerticalDragDown;
public readonly GestureDragStartCallback onVerticalDragStart;
public readonly GestureDragUpdateCallback onVerticalDragUpdate;

);
}
if (this.onLongPress != null || this.onLongPressUp != null) {
if (this.onLongPress != null ||
this.onLongPressUp != null ||
this.onLongPressStart != null ||
this.onLongPressMoveUpdate != null ||
this.onLongPressEnd != null) {
instance.onLongPressStart = this.onLongPressStart;
instance.onLongPressMoveUpdate = this.onLongPressMoveUpdate;
instance.onLongPressEnd = this.onLongPressEnd;
}
);
}
if (this.onLongPressDragStart != null || this.onLongPressDragUpdate != null ||
this.onLongPressDragUp != null) {
gestures[typeof(LongPressDragGestureRecognizer)] =
new GestureRecognizerFactoryWithHandlers<LongPressDragGestureRecognizer>(
() => new LongPressDragGestureRecognizer(debugOwner: this),
(LongPressDragGestureRecognizer instance) => {
instance.onLongPressStart = this.onLongPressDragStart;
instance.onLongPressDragUpdate = this.onLongPressDragUpdate;
instance.onLongPressUp = this.onLongPressDragUp;
}
);
}

44
Runtime/widgets/text_selection.cs


GestureTapDownCallback onTapDown = null,
GestureTapUpCallback onSingleTapUp = null,
GestureTapCancelCallback onSingleTapCancel = null,
GestureLongPressCallback onSingleLongTapStart = null,
// TODO: GestureLongPressMoveUpdateCallback onSingleLongTapMoveUpdate = null,
// TODO: GestureLongPressEndCallback onSingleLongTapEnd = null,
GestureLongPressStartCallback onSingleLongTapStart = null,
GestureLongPressMoveUpdateCallback onSingleLongTapMoveUpdate = null,
GestureLongPressEndCallback onSingleLongTapEnd = null,
GestureTapDownCallback onDoubleTapDown = null,
GestureDragStartCallback onDragSelectionStart = null,
DragSelectionUpdateCallback onDragSelectionUpdate = null,

public readonly GestureTapCancelCallback onSingleTapCancel;
public readonly GestureLongPressCallback onSingleLongTapStart;
public readonly GestureLongPressStartCallback onSingleLongTapStart;
// TODO: public readonly GestureLongPressMoveUpdateCallback onSingleLongTapMoveUpdate;
public readonly GestureLongPressMoveUpdateCallback onSingleLongTapMoveUpdate;
// TODO: public readonly GestureLongPressEndCallback onSingleLongTapEnd;
public readonly GestureLongPressEndCallback onSingleLongTapEnd;
public readonly GestureTapDownCallback onDoubleTapDown;

this._lastDragUpdateDetails = null;
}
void _handleLongPressStart() {
void _handleLongPressStart(LongPressStartDetails details) {
this.widget.onSingleLongTapStart();
this.widget.onSingleLongTapStart(details);
// TODO: void _handleLongPressMoveUpdate(LongPressMoveUpdateDetails details) {
// }
void _handleLongPressMoveUpdate(LongPressMoveUpdateDetails details) {
if (!this._isDoubleTap && this.widget.onSingleLongTapMoveUpdate != null) {
this.widget.onSingleLongTapMoveUpdate(details);
}
}
// TODO: void _handleLongPressEnd(LongPressEndDetails details) {
// }
void _handleLongPressEnd(LongPressEndDetails details) {
if (!this._isDoubleTap && this.widget.onSingleLongTapEnd != null) {
this.widget.onSingleLongTapEnd(details);
}
this._isDoubleTap = false;
}
void _doubleTapTimeout() {
this._doubleTapTimer = null;

)
);
if (this.widget.onSingleLongTapStart != null // ||
// TODO: this.widget.onSingleLongTapMoveUpdatee != null ||
// TODO: this.widget.onSingleLongTapEnd != null
if (this.widget.onSingleLongTapStart != null ||
this.widget.onSingleLongTapMoveUpdate != null ||
this.widget.onSingleLongTapEnd != null
instance.onLongPress = this._handleLongPressStart;
// TODO: instance.onLongPressMoveUpdate = _handleLongPressMoveUpdate
// TODO: instance.onLongPressEnd = _handleLongPressEnd
instance.onLongPressStart = this._handleLongPressStart;
instance.onLongPressMoveUpdate = this._handleLongPressMoveUpdate;
instance.onLongPressEnd = this._handleLongPressEnd;
});
}

6
Samples/UIWidgetSample/LongPressSample.cs


class _LongPressSampleWidgetState : State<LongPressSampleWidget> {
public override Widget build(BuildContext context) {
return new GestureDetector(
onLongPressDragStart: (value) => { Debug.Log($"Long Press Drag Start: {value}"); },
onLongPressDragUpdate: (value) => { Debug.Log($"Long Press Drag Update: {value}"); },
onLongPressDragUp: (value) => { Debug.Log($"Long Press Drag Up: {value}"); },
onLongPressStart: (value) => { Debug.Log($"Long Press Drag Start: {value}"); },
onLongPressMoveUpdate: (value) => { Debug.Log($"Long Press Drag Update: {value}"); },
onLongPressEnd: (value) => { Debug.Log($"Long Press Drag Up: {value}"); },
onLongPressUp: () => { Debug.Log($"Long Press Up"); },
onLongPress: () => { Debug.Log($"Long Press"); },
child: new Center(

正在加载...
取消
保存