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

Commit 4925071

Browse files
author
Yuncong Zhang
committed
[1.5.4] Upgrade editable.
1 parent 9205dc6 commit 4925071

File tree

5 files changed

+107
-8
lines changed

5 files changed

+107
-8
lines changed

Runtime/painting/text_painter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ public Offset getOffsetForCaret(TextPosition position, Rect caretPrototype) {
239239
return this._caretMetrics.offset;
240240
}
241241

242+
public float getFullHeightForCaret(TextPosition position, Rect caretPrototype) {
243+
this._computeCaretMetrics(position, caretPrototype);
244+
return this._caretMetrics.fullHeight ?? 0;
245+
}
246+
242247
_CaretMetrics _caretMetrics;
243248

244249
TextPosition _previousCaretPosition;

Runtime/rendering/binding.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public RendererBinding(bool inEditorWindow = false) {
1818

1919
Window.instance.onMetricsChanged += this.handleMetricsChanged;
2020
Window.instance.onTextScaleFactorChanged += this.handleTextScaleFactorChanged;
21+
Window.instance.onPlatformBrightnessChanged += this.handlePlatformBrightnessChanged;
2122
this.initRenderView();
2223
D.assert(this.renderView != null);
2324
this.addPersistentFrameCallback(this._handlePersistentFrameCallback);

Runtime/rendering/box.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,12 +1093,12 @@ protected override void debugAssertDoesMeetConstraints() {
10931093

10941094
if (this.constraints.hasBoundedWidth) {
10951095
testIntrinsicsForValues(this.getMinIntrinsicWidth, this.getMaxIntrinsicWidth, "Width",
1096-
this.constraints.maxWidth);
1096+
this.constraints.maxHeight);
10971097
}
10981098

10991099
if (this.constraints.hasBoundedHeight) {
11001100
testIntrinsicsForValues(this.getMinIntrinsicHeight, this.getMaxIntrinsicHeight, "Height",
1101-
this.constraints.maxHeight);
1101+
this.constraints.maxWidth);
11021102
}
11031103

11041104
debugCheckingIntrinsics = false;

Runtime/rendering/editable.cs

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Xml.Schema;
23
using Unity.UIWidgets.foundation;
34
using Unity.UIWidgets.gestures;
45
using Unity.UIWidgets.painting;
@@ -49,6 +50,8 @@ public class RenderEditable : RenderBox {
4950
TextPainter _textPainter;
5051
Color _cursorColor;
5152
int? _maxLines;
53+
int? _minLines;
54+
bool _expands;
5255
Color _selectionColor;
5356
ViewportOffset _offset;
5457
ValueNotifier<bool> _showCursor;
@@ -78,6 +81,8 @@ public RenderEditable(
7881
Color backgroundCursorColor = null,
7982
bool? hasFocus = null,
8083
int? maxLines = 1,
84+
int? minLines = null,
85+
bool expands = false,
8186
StrutStyle strutStyle = null,
8287
Color selectionColor = null,
8388
TextSelection selection = null,
@@ -87,21 +92,25 @@ public RenderEditable(
8792
bool ignorePointer = false,
8893
float cursorWidth = 1.0f,
8994
Radius cursorRadius = null,
90-
bool? enableInteractiveSelection = null,
9195
bool paintCursorAboveText = false,
9296
Offset cursorOffset = null,
9397
float devicePixelRatio = 1.0f,
98+
bool? enableInteractiveSelection = null,
9499
EdgeInsets floatingCursorAddedMargin = null,
95100
TextSelectionDelegate textSelectionDelegate = null) {
96101
floatingCursorAddedMargin = floatingCursorAddedMargin ?? EdgeInsets.fromLTRB(4, 4, 4, 5);
97102
D.assert(textSelectionDelegate != null);
103+
D.assert(minLines == null || minLines > 0);
104+
D.assert((minLines == null) || maxLines >= minLines, () => "minLines can't be greater than maxLines");
98105
this._textPainter = new TextPainter(text: text, textAlign: textAlign, textDirection: textDirection,
99106
textScaleFactor: textScaleFactor, strutStyle: strutStyle);
100107
this._cursorColor = cursorColor;
101108

102109
this._showCursor = showCursor ?? new ValueNotifier<bool>(false);
103110
this._hasFocus = hasFocus ?? false;
104111
this._maxLines = maxLines;
112+
this._minLines = minLines;
113+
this._expands = expands;
105114
this._selectionColor = selectionColor;
106115
this._selection = selection;
107116
this._obscureText = obscureText;
@@ -237,6 +246,27 @@ public ValueListenable<bool> selectionEndInViewport {
237246

238247
readonly ValueNotifier<bool> _selectionEndInViewport = new ValueNotifier<bool>(true);
239248

249+
void _updateSelectionExtentsVisibility(Offset effectiveOffset) {
250+
Rect visibleRegion = Offset.zero & this.size;
251+
Offset startOffset = this._textPainter.getOffsetForCaret(
252+
new TextPosition(offset: this._selection.start, affinity: this._selection.affinity),
253+
Rect.zero
254+
);
255+
256+
float visibleRegionSlop = 0.5f;
257+
this._selectionStartInViewport.value = visibleRegion
258+
.inflate(visibleRegionSlop)
259+
.contains(startOffset + effectiveOffset);
260+
261+
Offset endOffset = this._textPainter.getOffsetForCaret(
262+
new TextPosition(offset: this._selection.end, affinity: this._selection.affinity),
263+
Rect.zero
264+
);
265+
this._selectionEndInViewport.value = visibleRegion
266+
.inflate(visibleRegionSlop)
267+
.contains(endOffset + effectiveOffset);
268+
}
269+
240270
int _extentOffset = -1;
241271

242272
int _baseOffset = -1;
@@ -624,7 +654,7 @@ public ValueNotifier<bool> showCursor {
624654
}
625655
}
626656

627-
bool _hasFocus;
657+
bool _hasFocus = false;
628658
bool _listenerAttached = false;
629659

630660
public bool hasFocus {
@@ -663,6 +693,31 @@ public int? maxLines {
663693
}
664694
}
665695

696+
public int? minLines {
697+
get { return this._minLines; }
698+
set {
699+
D.assert(value == null || value > 0);
700+
if (this._minLines == value) {
701+
return;
702+
}
703+
704+
this._minLines = value;
705+
this.markNeedsTextLayout();
706+
}
707+
}
708+
709+
public bool expands {
710+
get { return this._expands; }
711+
set {
712+
if (this.expands == value) {
713+
return;
714+
}
715+
716+
this._expands = value;
717+
this.markNeedsTextLayout();
718+
}
719+
}
720+
666721
public Color selectionColor {
667722
get { return this._selectionColor; }
668723
set {
@@ -985,7 +1040,7 @@ public override void handleEvent(PointerEvent evt, HitTestEntry entry) {
9851040
}
9861041

9871042
public void handleTapDown(TapDownDetails details) {
988-
this._lastTapDownPosition = details.globalPosition - this._paintOffset;
1043+
this._lastTapDownPosition = details.globalPosition;
9891044
if (!Application.isMobilePlatform) {
9901045
this.selectPosition(SelectionChangedCause.tap);
9911046
}
@@ -1089,7 +1144,7 @@ public void selectWordEdge(SelectionChangedCause cause) {
10891144
D.assert(this._lastTapDownPosition != null);
10901145
if (this.onSelectionChanged != null) {
10911146
TextPosition position =
1092-
this._textPainter.getPositionForOffset(this.globalToLocal(this._lastTapDownPosition));
1147+
this._textPainter.getPositionForOffset(this.globalToLocal(this._lastTapDownPosition - this._paintOffset));
10931148
TextRange word = this._textPainter.getWordBoundary(position);
10941149
if (position.offset - word.start <= 1) {
10951150
this.onSelectionChanged(
@@ -1188,12 +1243,25 @@ Offset _getPixelPerfectCursorOffset(Rect caretRect) {
11881243

11891244
void _paintCaret(Canvas canvas, Offset effectiveOffset, TextPosition textPosition) {
11901245
D.assert(this._textLayoutLastWidth == this.constraints.maxWidth);
1191-
var caretOffset = this._textPainter.getOffsetForCaret(textPosition, this._caretPrototype);
11921246
var paint = new Paint() {color = this._floatingCursorOn ? this.backgroundCursorColor : this._cursorColor};
1247+
var caretOffset = this._textPainter.getOffsetForCaret(textPosition, this._caretPrototype) + effectiveOffset;
11931248
Rect caretRect = this._caretPrototype.shift(caretOffset + effectiveOffset);
11941249
if (this._cursorOffset != null) {
11951250
caretRect = caretRect.shift(this._cursorOffset);
11961251
}
1252+
1253+
#if !UNITY_IOS
1254+
if (this._textPainter.getFullHeightForCaret(textPosition, this._caretPrototype) != null) {
1255+
caretRect = Rect.fromLTWH(
1256+
caretRect.left,
1257+
caretRect.top - _kCaretHeightOffset,
1258+
caretRect.width,
1259+
this._textPainter.getFullHeightForCaret(textPosition, this._caretPrototype)
1260+
);
1261+
}
1262+
#endif
1263+
1264+
caretRect = caretRect.shift(this._getPixelPerfectCursorOffset(caretRect));
11971265

11981266
if (this.cursorRadius == null) {
11991267
canvas.drawRect(caretRect, paint);
@@ -1384,10 +1452,26 @@ void markNeedsSemanticsUpdate() {
13841452
}
13851453

13861454
float _preferredHeight(float width) {
1387-
if (this.maxLines != null) {
1455+
bool lockedMax = this.maxLines != null && this.minLines == null;
1456+
bool lockedBoth = this.maxLines != null && this.minLines == this.maxLines;
1457+
bool singleLine = this.maxLines == 1;
1458+
if (singleLine || lockedMax || lockedBoth) {
13881459
return this.preferredLineHeight * this.maxLines.Value;
13891460
}
13901461

1462+
bool minLimited = this.minLines != null && this.minLines > 1;
1463+
bool maxLimited = this.maxLines != null;
1464+
if (minLimited || maxLimited) {
1465+
this._layoutText(width);
1466+
if (minLimited && this._textPainter.height < this.preferredLineHeight * this.minLines.Value) {
1467+
return this.preferredLineHeight * this.minLines.Value;
1468+
}
1469+
1470+
if (maxLimited && this._textPainter.height > this.preferredLineHeight * this.maxLines.Value) {
1471+
return this.preferredLineHeight * this.maxLines.Value;
1472+
}
1473+
}
1474+
13911475
if (!width.isFinite()) {
13921476
var text = this._textPainter.text.text;
13931477
int lines = 1;
@@ -1460,6 +1544,8 @@ public override void debugFillProperties(DiagnosticPropertiesBuilder properties)
14601544
properties.add(new DiagnosticsProperty<Color>("cursorColor", this.cursorColor));
14611545
properties.add(new DiagnosticsProperty<ValueNotifier<bool>>("showCursor", this.showCursor));
14621546
properties.add(new DiagnosticsProperty<int?>("maxLines", this.maxLines));
1547+
properties.add(new DiagnosticsProperty<int?>("minLines", this.minLines));
1548+
properties.add(new DiagnosticsProperty<bool>("expands", this.expands));
14631549
properties.add(new DiagnosticsProperty<Color>("selectionColor", this.selectionColor));
14641550
properties.add(new DiagnosticsProperty<float>("textScaleFactor", this.textScaleFactor));
14651551
properties.add(new DiagnosticsProperty<TextSelection>("selection", this.selection));

Runtime/ui/window.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ public VoidCallback onTextScaleFactorChanged {
209209

210210
VoidCallback _onTextScaleFactorChanged;
211211

212+
public VoidCallback onPlatformBrightnessChanged {
213+
get { return this._onPlatformBrightnessChanged; }
214+
set { this._onPlatformBrightnessChanged = value; }
215+
}
216+
217+
VoidCallback _onPlatformBrightnessChanged;
218+
212219
public FrameCallback onBeginFrame {
213220
get { return this._onBeginFrame; }
214221
set { this._onBeginFrame = value; }

0 commit comments

Comments
 (0)