Skip to content

Commit 5e6e98d

Browse files
answershutoandycall
authored andcommitted
feat: add _target to gestureManger
1 parent 0833048 commit 5e6e98d

File tree

5 files changed

+67
-59
lines changed

5 files changed

+67
-59
lines changed

integration_tests/lib/bridge/from_native.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void _simulateKeyPress(Pointer<NativeString> nativeChars) {
136136
text: updatedText,
137137
selection: currentValue.selection.copyWith(baseOffset: baseOffset, extentOffset: extentOffset),
138138
);
139-
current.formatAndSetValue(value, true);
139+
current.formatAndSetValue(value, shouldDispatchEvent: true);
140140
} else {
141141
print('No focus input element found.');
142142
}

kraken/lib/src/dom/elements/input.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ class InputElement extends Element implements TextInputClient, TickerProvider {
468468
textInputConnection.setEditingState(localValue);
469469
}
470470

471-
void formatAndSetValue(TextEditingValue value, bool NeedDispatchEvent) {
471+
void formatAndSetValue(TextEditingValue value, { bool shouldDispatchEvent = false }) {
472472
final bool textChanged = textSelectionDelegate.textEditingValue?.text != value?.text;
473473
textSelectionDelegate.textEditingValue = value;
474474

@@ -484,7 +484,7 @@ class InputElement extends Element implements TextInputClient, TickerProvider {
484484
}
485485
// Sync value to input element property
486486
properties[VALUE] = value.text;
487-
if (NeedDispatchEvent) {
487+
if (shouldDispatchEvent) {
488488
// TODO: return the string containing the data that was added to the element,
489489
// which MAY be null if it doesn't apply.
490490
String inputData = '';
@@ -505,7 +505,7 @@ class InputElement extends Element implements TextInputClient, TickerProvider {
505505
_showCaretOnScreen();
506506
}
507507
_lastKnownRemoteTextEditingValue = value;
508-
formatAndSetValue(value, true);
508+
formatAndSetValue(value, shouldDispatchEvent: true);
509509
// To keep the cursor from blinking while typing, we want to restart the
510510
// cursor timer every time a new character is typed.
511511
_stopCursorTimer(resetCharTicks: false);
@@ -533,7 +533,7 @@ class InputElement extends Element implements TextInputClient, TickerProvider {
533533
selection: selection,
534534
composing: composing,
535535
);
536-
formatAndSetValue(newTextEditingValue, false);
536+
formatAndSetValue(newTextEditingValue);
537537
} else if (key == 'placeholder') {
538538
// Update placeholder text.
539539
updateTextSpan();

kraken/lib/src/gesture/gesture_manager.dart

+58-52
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,51 @@ import 'package:flutter/gestures.dart';
1212
class GestureManager {
1313

1414
static GestureManager _instance;
15+
GestureManager._();
1516

16-
GestureManager._internal();
17+
factory GestureManager.instance() {
18+
if (_instance == null) {
19+
_instance = GestureManager._();
20+
21+
_instance.gestures[ClickGestureRecognizer] = ClickGestureRecognizer();
22+
(_instance.gestures[ClickGestureRecognizer] as ClickGestureRecognizer).onClick = _instance.onClick;
23+
24+
_instance.gestures[SwipeGestureRecognizer] = SwipeGestureRecognizer();
25+
(_instance.gestures[SwipeGestureRecognizer] as SwipeGestureRecognizer).onSwipe = _instance.onSwipe;
26+
27+
_instance.gestures[PanGestureRecognizer] = PanGestureRecognizer();
28+
(_instance.gestures[PanGestureRecognizer] as PanGestureRecognizer).onStart = _instance.onPanStart;
29+
(_instance.gestures[PanGestureRecognizer] as PanGestureRecognizer).onUpdate = _instance.onPanUpdate;
30+
(_instance.gestures[PanGestureRecognizer] as PanGestureRecognizer).onEnd = _instance.onPanEnd;
31+
32+
_instance.gestures[LongPressGestureRecognizer] = LongPressGestureRecognizer();
33+
(_instance.gestures[LongPressGestureRecognizer] as LongPressGestureRecognizer).onLongPressEnd = _instance.onLongPressEnd;
1734

18-
factory GestureManager.getInstance() => _getInstance();
35+
_instance.gestures[ScaleGestureRecognizer] = ScaleGestureRecognizer();
36+
(_instance.gestures[ScaleGestureRecognizer] as ScaleGestureRecognizer).onStart = _instance.onScaleStart;
37+
(_instance.gestures[ScaleGestureRecognizer] as ScaleGestureRecognizer).onUpdate = _instance.onScaleUpdate;
38+
(_instance.gestures[ScaleGestureRecognizer] as ScaleGestureRecognizer).onEnd = _instance.onScaleEnd;
39+
}
40+
return _instance;
41+
}
1942

2043
final Map<Type, GestureRecognizer> gestures = <Type, GestureRecognizer>{};
2144

22-
List<RenderBoxModel> renderBoxModelList = [];
45+
List<RenderBoxModel> _renderBoxModelList = [];
46+
47+
RenderBoxModel _target;
48+
49+
void addTargetToList(RenderBoxModel renderBoxModel) {
50+
_renderBoxModelList.add(renderBoxModel);
51+
}
52+
53+
void clearTargetList() {
54+
if (_renderBoxModelList.length != 0) {
55+
// The target node triggered by the gesture is the bottom node of hittest.
56+
_target = _renderBoxModelList[0];
57+
}
58+
_renderBoxModelList = [];
59+
}
2360

2461
void addPointer(PointerEvent event) {
2562
gestures.forEach((key, gesture) {
@@ -28,87 +65,56 @@ class GestureManager {
2865
}
2966

3067
void onClick(String eventType, { PointerDownEvent down, PointerUpEvent up }) {
31-
if (renderBoxModelList.length != 0 && renderBoxModelList[0].onClick != null) {
32-
renderBoxModelList[0].onClick(eventType, up: up);
68+
if (_target != null && _target.onClick != null) {
69+
_target.onClick(eventType, up: up);
3370
}
34-
renderBoxModelList = [];
3571
}
3672

3773
void onSwipe(Event event) {
38-
if (renderBoxModelList.length != 0 && renderBoxModelList[0].onSwipe != null) {
39-
renderBoxModelList[0].onSwipe(event);
74+
if (_target != null && _target.onSwipe != null) {
75+
_target.onSwipe(event);
4076
}
41-
renderBoxModelList = [];
4277
}
4378

4479
void onPanStart(DragStartDetails details) {
45-
if (renderBoxModelList.length != 0 && renderBoxModelList[0].onPan != null) {
46-
renderBoxModelList[0].onPan(GestureEvent(EVENT_PAN, GestureEventInit( state: EVENT_STATE_START, deltaX: details.globalPosition.dx, deltaY: details.globalPosition.dy )));
80+
if (_target != null && _target.onPan != null) {
81+
_target.onPan(GestureEvent(EVENT_PAN, GestureEventInit( state: EVENT_STATE_START, deltaX: details.globalPosition.dx, deltaY: details.globalPosition.dy )));
4782
}
4883
}
4984

5085
void onPanUpdate(DragUpdateDetails details) {
51-
if (renderBoxModelList.length != 0 && renderBoxModelList[0].onPan != null) {
52-
renderBoxModelList[0].onPan(GestureEvent(EVENT_PAN, GestureEventInit( state: EVENT_STATE_UPDATE, deltaX: details.globalPosition.dx, deltaY: details.globalPosition.dy )));
86+
if (_target != null && _target.onPan != null) {
87+
_target.onPan(GestureEvent(EVENT_PAN, GestureEventInit( state: EVENT_STATE_UPDATE, deltaX: details.globalPosition.dx, deltaY: details.globalPosition.dy )));
5388
}
5489
}
5590

5691
void onPanEnd(DragEndDetails details) {
57-
if (renderBoxModelList.length != 0 && renderBoxModelList[0].onPan != null) {
58-
renderBoxModelList[0].onPan(GestureEvent(EVENT_PAN, GestureEventInit( state: EVENT_STATE_END, velocityX: details.velocity.pixelsPerSecond.dx, velocityY: details.velocity.pixelsPerSecond.dy )));
92+
if (_target != null && _target.onPan != null) {
93+
_target.onPan(GestureEvent(EVENT_PAN, GestureEventInit( state: EVENT_STATE_END, velocityX: details.velocity.pixelsPerSecond.dx, velocityY: details.velocity.pixelsPerSecond.dy )));
5994
}
60-
renderBoxModelList = [];
6195
}
6296

6397
void onScaleStart(ScaleStartDetails details) {
64-
if (renderBoxModelList.length != 0 && renderBoxModelList[0].onScale != null) {
65-
renderBoxModelList[0].onScale(GestureEvent(EVENT_SCALE, GestureEventInit( state: EVENT_STATE_START )));
98+
if (_target != null && _target.onScale != null) {
99+
_target.onScale(GestureEvent(EVENT_SCALE, GestureEventInit( state: EVENT_STATE_START )));
66100
}
67101
}
68102

69103
void onScaleUpdate(ScaleUpdateDetails details) {
70-
if (renderBoxModelList.length != 0 && renderBoxModelList[0].onScale != null) {
71-
renderBoxModelList[0].onScale(GestureEvent(EVENT_SCALE, GestureEventInit( state: EVENT_STATE_UPDATE, rotation: details.rotation, scale: details.scale )));
104+
if (_target != null && _target.onScale != null) {
105+
_target.onScale(GestureEvent(EVENT_SCALE, GestureEventInit( state: EVENT_STATE_UPDATE, rotation: details.rotation, scale: details.scale )));
72106
}
73107
}
74108

75109
void onScaleEnd(ScaleEndDetails details) {
76-
if (renderBoxModelList.length != 0 && renderBoxModelList[0].onScale != null) {
77-
renderBoxModelList[0].onScale(GestureEvent(EVENT_SCALE, GestureEventInit( state: EVENT_STATE_END )));
110+
if (_target != null && _target.onScale != null) {
111+
_target.onScale(GestureEvent(EVENT_SCALE, GestureEventInit( state: EVENT_STATE_END )));
78112
}
79-
renderBoxModelList = [];
80113
}
81114

82115
void onLongPressEnd(LongPressEndDetails details) {
83-
if (renderBoxModelList.length != 0 && renderBoxModelList[0].onLongPress != null) {
84-
renderBoxModelList[0].onLongPress(GestureEvent(EVENT_LONG_PRESS, GestureEventInit(deltaX: details.globalPosition.dx, deltaY: details.globalPosition.dy )));
85-
}
86-
renderBoxModelList = [];
87-
}
88-
89-
static _getInstance() {
90-
if (_instance == null) {
91-
_instance = GestureManager._internal();
92-
93-
_instance.gestures[ClickGestureRecognizer] = ClickGestureRecognizer();
94-
(_instance.gestures[ClickGestureRecognizer] as ClickGestureRecognizer).onClick = _instance.onClick;
95-
96-
_instance.gestures[SwipeGestureRecognizer] = SwipeGestureRecognizer();
97-
(_instance.gestures[SwipeGestureRecognizer] as SwipeGestureRecognizer).onSwipe = _instance.onSwipe;
98-
99-
_instance.gestures[PanGestureRecognizer] = PanGestureRecognizer();
100-
(_instance.gestures[PanGestureRecognizer] as PanGestureRecognizer).onStart = _instance.onPanStart;
101-
(_instance.gestures[PanGestureRecognizer] as PanGestureRecognizer).onUpdate = _instance.onPanUpdate;
102-
(_instance.gestures[PanGestureRecognizer] as PanGestureRecognizer).onEnd = _instance.onPanEnd;
103-
104-
_instance.gestures[LongPressGestureRecognizer] = LongPressGestureRecognizer();
105-
(_instance.gestures[LongPressGestureRecognizer] as LongPressGestureRecognizer).onLongPressEnd = _instance.onLongPressEnd;
106-
107-
_instance.gestures[ScaleGestureRecognizer] = ScaleGestureRecognizer();
108-
(_instance.gestures[ScaleGestureRecognizer] as ScaleGestureRecognizer).onStart = _instance.onScaleStart;
109-
(_instance.gestures[ScaleGestureRecognizer] as ScaleGestureRecognizer).onUpdate = _instance.onScaleUpdate;
110-
(_instance.gestures[ScaleGestureRecognizer] as ScaleGestureRecognizer).onEnd = _instance.onScaleEnd;
116+
if (_target != null && _target.onLongPress != null) {
117+
_target.onLongPress(GestureEvent(EVENT_LONG_PRESS, GestureEventInit(deltaX: details.globalPosition.dx, deltaY: details.globalPosition.dy )));
111118
}
112-
return _instance;
113119
}
114120
}

kraken/lib/src/rendering/pointer.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ mixin RenderPointerListenerMixin on RenderBox {
8181
/// location.
8282
if (event is PointerDownEvent) {
8383
if (entry.target is RenderBoxModel) {
84-
GestureManager.getInstance().renderBoxModelList.add(entry.target as RenderBoxModel);
84+
GestureManager.instance().addTargetToList(entry.target as RenderBoxModel);
8585
}
8686
}
8787

kraken/lib/src/rendering/viewport.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ class RenderViewportBox extends RenderProxyBox
7575
super.handleEvent(event, entry);
7676
if (event is PointerDownEvent) {
7777
_verticalDragGestureRecognizer.addPointer(event);
78-
GestureManager.getInstance().addPointer(event);
78+
GestureManager.instance().addPointer(event);
79+
} else if (event is PointerUpEvent) {
80+
GestureManager.instance().clearTargetList();
7981
}
8082
}
8183

0 commit comments

Comments
 (0)