Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 2c86b70

Browse files
committed
refine code
1 parent 60620a2 commit 2c86b70

File tree

7 files changed

+50
-40
lines changed

7 files changed

+50
-40
lines changed

Runtime/gestures/binding.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ void _handlePointerHoverEvent(PointerEvent evt) {
125125
timeStamp: evt.timeStamp,
126126
pointer: evt.pointer,
127127
device: evt.device,
128-
kind: evt.kind,
129-
position: evt.position
128+
kind: evt.kind
130129
), null);
131130
}
132131

@@ -136,8 +135,7 @@ void _handlePointerHoverEvent(PointerEvent evt) {
136135
timeStamp: evt.timeStamp,
137136
pointer: evt.pointer,
138137
device: evt.device,
139-
kind: evt.kind,
140-
position: evt.position
138+
kind: evt.kind
141139
), hitTestEntry);
142140
}
143141

Runtime/gestures/hover.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44

55
namespace Unity.UIWidgets.gestures {
66

7+
public delegate void PointerHoverEnterCallback(PointerHoverEvent evt);
8+
9+
public delegate void PointerHoverLeaveCallback();
10+
711
public class HoverRecognizer : DiagnosticableTree {
812
public HoverRecognizer(object debugOwner = null) {
913
this.debugOwner = debugOwner;
1014
}
1115

1216
readonly object debugOwner;
13-
14-
public Action OnPointerEnter = () => {};
1517

16-
public Action OnPointerLeave = () => {};
18+
public PointerHoverEnterCallback OnPointerEnter;
19+
20+
public PointerHoverLeaveCallback OnPointerLeave;
1721
}
1822
}

Runtime/painting/basic_types.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Unity.UIWidgets.painting {
55
public enum RenderComparison {
66
identical,
77
metadata,
8-
hoverCallback,
8+
function,
99
paint,
1010
layout,
1111
}

Runtime/painting/text_span.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void build(ParagraphBuilder builder, float textScaleFactor = 1.0f) {
4848
}
4949
}
5050

51-
public bool needHoverRecognizer {
51+
public bool hasHoverRecognizer {
5252
get {
5353
bool need = false;
5454
this.visitTextSpan((text) => {
@@ -58,7 +58,6 @@ public bool needHoverRecognizer {
5858
}
5959
return true;
6060
});
61-
6261
return need;
6362
}
6463
}
@@ -172,7 +171,7 @@ public RenderComparison compareTo(TextSpan other) {
172171
: RenderComparison.metadata;
173172

174173
if (!Equals(this.recognizer, other.recognizer)) {
175-
result = RenderComparison.hoverCallback > result ? RenderComparison.hoverCallback : result;
174+
result = RenderComparison.function > result ? RenderComparison.function : result;
176175
}
177176

178177
if (this.style != null) {

Runtime/rendering/paragraph.cs

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,8 @@ public RenderParagraph(TextSpan text,
5959
this._selection = null;
6060
this.onSelectionChanged = onSelectionChanged;
6161
this.selectionColor = selectionColor;
62-
63-
64-
this._listenToHoverEvent = this._textPainter.text.needHoverRecognizer;
65-
this._previousHoverSpan = null;
66-
this._pointerHoverInside = false;
62+
63+
this._resetHoverHandler();
6764
}
6865

6966
public Action onSelectionChanged;
@@ -91,7 +88,7 @@ public TextSpan text {
9188
case RenderComparison.identical:
9289
case RenderComparison.metadata:
9390
return;
94-
case RenderComparison.hoverCallback:
91+
case RenderComparison.function:
9592
this._textPainter.text = value;
9693
break;
9794
case RenderComparison.paint:
@@ -104,9 +101,7 @@ public TextSpan text {
104101
break;
105102
}
106103

107-
this._listenToHoverEvent = this._textPainter.text.needHoverRecognizer;
108-
this._previousHoverSpan = null;
109-
this._pointerHoverInside = false;
104+
this._resetHoverHandler();
110105
}
111106
}
112107

@@ -253,6 +248,16 @@ public bool hasFocus {
253248
}
254249
}
255250

251+
TextSpan _previousHoverSpan;
252+
bool _pointerHoverInside;
253+
bool _hasHoverRecognizer;
254+
255+
void _resetHoverHandler() {
256+
this._hasHoverRecognizer = this._textPainter.text.hasHoverRecognizer;
257+
this._previousHoverSpan = null;
258+
this._pointerHoverInside = false;
259+
}
260+
256261
void _handleKeyEvent(RawKeyEvent keyEvent) {
257262
//only allow KCommand.copy
258263
if (keyEvent is RawKeyUpEvent) {
@@ -328,29 +333,18 @@ void _handleSelectionChanged(TextSelection selection,
328333
this.onSelectionChanged?.Invoke();
329334
}
330335

331-
TextSpan _previousHoverSpan = null;
332-
bool _pointerHoverInside = false;
333-
bool _listenToHoverEvent = false;
334336

335-
public override void handleEvent(PointerEvent evt, HitTestEntry entry) {
336-
D.assert(this.debugHandleEvent(evt, entry));
337-
if (evt is PointerDownEvent) {
338-
this._layoutTextWithConstraints(this.constraints);
339-
Offset offset = ((BoxHitTestEntry) entry).localPosition;
340-
TextPosition position = this._textPainter.getPositionForOffset(offset);
341-
TextSpan span = this._textPainter.text.getSpanForPosition(position);
342-
span?.recognizer?.addPointer((PointerDownEvent) evt);
343-
}
344-
345-
if (!this._listenToHoverEvent) {
337+
void _handlePointerHover(PointerEvent evt) {
338+
if (!this._hasHoverRecognizer) {
346339
return;
347340
}
348341

349342
if (evt is PointerEnterEvent) {
350343
this._pointerHoverInside = true;
351-
} else if (evt is PointerLeaveEvent) {
344+
}
345+
else if (evt is PointerLeaveEvent) {
352346
this._pointerHoverInside = false;
353-
this._previousHoverSpan?.hoverRecognizer?.OnPointerLeave();
347+
this._previousHoverSpan?.hoverRecognizer?.OnPointerLeave?.Invoke();
354348
this._previousHoverSpan = null;
355349
}
356350
else if (evt is PointerHoverEvent && this._pointerHoverInside) {
@@ -360,13 +354,27 @@ public override void handleEvent(PointerEvent evt, HitTestEntry entry) {
360354
TextSpan span = this._textPainter.text.getSpanForPosition(position);
361355

362356
if (this._previousHoverSpan != span) {
363-
this._previousHoverSpan?.hoverRecognizer?.OnPointerLeave();
364-
span?.hoverRecognizer?.OnPointerEnter();
357+
this._previousHoverSpan?.hoverRecognizer?.OnPointerLeave?.Invoke();
358+
span?.hoverRecognizer?.OnPointerEnter?.Invoke((PointerHoverEvent) evt);
365359
this._previousHoverSpan = span;
366360
}
367361
}
368362
}
369363

364+
public override void handleEvent(PointerEvent evt, HitTestEntry entry) {
365+
D.assert(this.debugHandleEvent(evt, entry));
366+
if (evt is PointerDownEvent) {
367+
this._layoutTextWithConstraints(this.constraints);
368+
Offset offset = ((BoxHitTestEntry) entry).localPosition;
369+
TextPosition position = this._textPainter.getPositionForOffset(offset);
370+
TextSpan span = this._textPainter.text.getSpanForPosition(position);
371+
span?.recognizer?.addPointer((PointerDownEvent) evt);
372+
return;
373+
}
374+
375+
this._handlePointerHover(evt);
376+
}
377+
370378
protected override void performLayout() {
371379
this._layoutTextWithConstraints(this.constraints);
372380
var textSize = this._textPainter.size;
@@ -413,6 +421,7 @@ void _paintSelection(Canvas canvas, Offset effectiveOffset) {
413421
foreach (var box in this._selectionRects) {
414422
barPath.addRect(box.toRect().shift(effectiveOffset));
415423
}
424+
416425
canvas.drawPath(barPath, paint);
417426
}
418427

Samples/UIWidgetSample/MaterialSample.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public override void initState() {
184184
Debug.Log("Tap");
185185
};
186186
this._hoverRecognizer = new HoverRecognizer();
187-
this._hoverRecognizer.OnPointerEnter = () => { Debug.Log("Pointer Enter"); };
187+
this._hoverRecognizer.OnPointerEnter = (evt) => { Debug.Log("Pointer Enter"); };
188188
this._hoverRecognizer.OnPointerLeave = () => { Debug.Log("Pointer Leave"); };
189189
}
190190

Samples/UIWidgetSample/txt/TextSpanGesture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public override void initState() {
3333
Debug.Log("Tap");
3434
};
3535
this._hoverRecognizer = new HoverRecognizer();
36-
this._hoverRecognizer.OnPointerEnter = () => { Debug.Log("Pointer Enter"); };
36+
this._hoverRecognizer.OnPointerEnter = (evt) => { Debug.Log("Pointer Enter"); };
3737
this._hoverRecognizer.OnPointerLeave = () => { Debug.Log("Pointer Leave"); };
3838
}
3939

0 commit comments

Comments
 (0)