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

Commit 7579cbd

Browse files
author
Yuncong Zhang
committed
Add strut style (framework part).
1 parent c7e0cd0 commit 7579cbd

File tree

13 files changed

+413
-35
lines changed

13 files changed

+413
-35
lines changed

Runtime/material/text_field.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Text;
23
using Unity.UIWidgets.foundation;
34
using Unity.UIWidgets.gestures;
45
using Unity.UIWidgets.painting;
@@ -23,6 +24,7 @@ public TextField(Key key = null, TextEditingController controller = null, FocusN
2324
InputDecoration decoration = null, bool noDecoration = false, TextInputType keyboardType = null,
2425
TextInputAction? textInputAction = null,
2526
TextCapitalization textCapitalization = TextCapitalization.none, TextStyle style = null,
27+
StrutStyle strutStyle = null,
2628
TextAlign textAlign = TextAlign.left, TextDirection textDirection = TextDirection.ltr,
2729
bool autofocus = false, bool obscureText = false, bool autocorrect = false, int? maxLines = 1,
2830
int? maxLength = null, bool maxLengthEnforced = true, ValueChanged<string> onChanged = null,
@@ -44,6 +46,7 @@ public TextField(Key key = null, TextEditingController controller = null, FocusN
4446
this.textInputAction = textInputAction;
4547
this.textCapitalization = textCapitalization;
4648
this.style = style;
49+
this.strutStyle = strutStyle;
4750
this.textAlign = textAlign;
4851
this.textDirection = textDirection;
4952
this.autofocus = autofocus;
@@ -84,6 +87,8 @@ public TextField(Key key = null, TextEditingController controller = null, FocusN
8487

8588
public readonly TextStyle style;
8689

90+
public readonly StrutStyle strutStyle;
91+
8792
public readonly TextAlign textAlign;
8893

8994
public readonly TextDirection textDirection;
@@ -462,6 +467,7 @@ public override Widget build(BuildContext context) {
462467
textInputAction: this.widget.textInputAction,
463468
textCapitalization: this.widget.textCapitalization,
464469
style: style,
470+
strutStyle: this.widget.strutStyle,
465471
textAlign: this.widget.textAlign,
466472
textDirection: this.widget.textDirection,
467473
autofocus: this.widget.autofocus,

Runtime/material/text_form_field.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public TextFormField(
1919
TextCapitalization textCapitalization = TextCapitalization.none,
2020
TextInputAction? textInputAction = null,
2121
TextStyle style = null,
22+
StrutStyle strutStyle = null,
2223
TextDirection? textDirection = null,
2324
TextAlign textAlign = TextAlign.left,
2425
bool autofocus = false,
@@ -59,6 +60,7 @@ public TextFormField(
5960
keyboardType: keyboardType,
6061
textInputAction: textInputAction,
6162
style: style,
63+
strutStyle: strutStyle,
6264
textAlign: textAlign,
6365
textDirection: textDirection ?? TextDirection.ltr,
6466
textCapitalization: textCapitalization,

Runtime/painting/strut_style.cs

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Unity.UIWidgets.foundation;
4+
using Unity.UIWidgets.ui;
5+
6+
namespace Unity.UIWidgets.painting {
7+
public class StrutStyle : Diagnosticable {
8+
public StrutStyle(
9+
string fontFamily = null,
10+
List<string> fontFamilyFallback = null,
11+
float? fontSize = null,
12+
float? height = null,
13+
float? leading = null,
14+
FontWeight fontWeight = null,
15+
FontStyle? fontStyle = null,
16+
bool forceStrutHeight = false,
17+
string debugLabel = null
18+
) {
19+
D.assert(fontSize == null || fontSize > 0);
20+
D.assert(leading == null || leading >= 0);
21+
this.fontFamily = fontFamily;
22+
this._fontFamilyFallback = fontFamilyFallback;
23+
this.fontSize = fontSize;
24+
this.height = height;
25+
this.fontWeight = fontWeight;
26+
this.fontStyle = fontStyle;
27+
this.leading = leading;
28+
this.forceStrutHeight = forceStrutHeight;
29+
this.debugLabel = debugLabel;
30+
}
31+
32+
public static StrutStyle fromTextStyle(
33+
TextStyle textStyle,
34+
string fontFamily = null,
35+
List<string> fontFamilyFallback = null,
36+
float? fontSize = null,
37+
float? height = null,
38+
float? leading = null,
39+
FontWeight fontWeight = null,
40+
FontStyle? fontStyle = null,
41+
bool forceStrutHeight = false,
42+
string debugLabel = null
43+
) {
44+
D.assert(textStyle != null);
45+
D.assert(fontSize == null || fontSize > 0);
46+
D.assert(leading == null || leading >= 0);
47+
return new StrutStyle(
48+
fontFamily: fontFamily ?? textStyle.fontFamily,
49+
fontFamilyFallback: fontFamilyFallback ?? textStyle.fontFamilyFallback,
50+
height: height ?? textStyle.height,
51+
fontSize: fontSize ?? textStyle.fontSize,
52+
fontWeight: fontWeight ?? textStyle.fontWeight,
53+
fontStyle: fontStyle ?? textStyle.fontStyle,
54+
debugLabel: debugLabel ?? textStyle.debugLabel
55+
);
56+
}
57+
58+
public static readonly StrutStyle disabled = new StrutStyle(
59+
height: 0.0f,
60+
leading: 0.0f
61+
);
62+
63+
public readonly string fontFamily;
64+
65+
public List<string> fontFamilyFallback {
66+
get { return this._fontFamilyFallback; }
67+
}
68+
69+
readonly List<string> _fontFamilyFallback;
70+
71+
public readonly float? fontSize;
72+
public readonly float? height;
73+
public readonly FontWeight fontWeight;
74+
public readonly FontStyle? fontStyle;
75+
public readonly float? leading;
76+
public readonly bool forceStrutHeight;
77+
public readonly string debugLabel;
78+
79+
public RenderComparison compareTo(StrutStyle other) {
80+
if (ReferenceEquals(this, other)) {
81+
return RenderComparison.identical;
82+
}
83+
84+
if (other == null) {
85+
return RenderComparison.layout;
86+
}
87+
88+
if (this.fontFamily != other.fontFamily ||
89+
this.fontSize != other.fontSize ||
90+
this.fontWeight != other.fontWeight ||
91+
this.fontStyle != other.fontStyle ||
92+
this.height != other.height ||
93+
this.leading != other.leading ||
94+
this.forceStrutHeight != other.forceStrutHeight ||
95+
!CollectionUtils.equalsList(this.fontFamilyFallback, other.fontFamilyFallback)) {
96+
return RenderComparison.layout;
97+
}
98+
99+
return RenderComparison.identical;
100+
}
101+
102+
public StrutStyle inheritFromTextStyle(TextStyle other) {
103+
if (other == null) {
104+
return this;
105+
}
106+
107+
return new StrutStyle(
108+
fontFamily: this.fontFamily ?? other.fontFamily,
109+
fontFamilyFallback: this.fontFamilyFallback ?? other.fontFamilyFallback,
110+
height: this.height ?? other.height,
111+
leading: this.leading,
112+
fontSize: this.fontSize ?? other.fontSize,
113+
fontWeight: this.fontWeight ?? other.fontWeight,
114+
fontStyle: this.fontStyle ?? other.fontStyle,
115+
forceStrutHeight: this.forceStrutHeight,
116+
debugLabel: this.debugLabel ?? other.debugLabel
117+
);
118+
}
119+
120+
public bool Equals(StrutStyle other) {
121+
if (ReferenceEquals(null, other)) {
122+
return false;
123+
}
124+
125+
if (ReferenceEquals(this, other)) {
126+
return true;
127+
}
128+
129+
return this.fontFamily == other.fontFamily &&
130+
this.fontSize == other.fontSize &&
131+
this.fontWeight == other.fontWeight &&
132+
this.fontStyle == other.fontStyle &&
133+
this.height == other.height &&
134+
this.leading == other.leading &&
135+
this.forceStrutHeight == other.forceStrutHeight;
136+
}
137+
138+
public override bool Equals(object obj) {
139+
if (ReferenceEquals(null, obj)) {
140+
return false;
141+
}
142+
143+
if (ReferenceEquals(this, obj)) {
144+
return true;
145+
}
146+
147+
if (obj.GetType() != this.GetType()) {
148+
return false;
149+
}
150+
151+
return this.Equals((StrutStyle) obj);
152+
}
153+
154+
public static bool operator ==(StrutStyle left, StrutStyle right) {
155+
return Equals(left, right);
156+
}
157+
158+
public static bool operator !=(StrutStyle left, StrutStyle right) {
159+
return !Equals(left, right);
160+
}
161+
162+
public override int GetHashCode() {
163+
unchecked {
164+
var hashCode = this.fontFamily?.GetHashCode() ?? 0;
165+
hashCode = (hashCode * 397) ^ (this.fontSize?.GetHashCode() ?? 0);
166+
hashCode = (hashCode * 397) ^ (this.fontWeight?.GetHashCode() ?? 0);
167+
hashCode = (hashCode * 397) ^ (this.fontStyle?.GetHashCode() ?? 0);
168+
hashCode = (hashCode * 397) ^ (this.height?.GetHashCode() ?? 0);
169+
hashCode = (hashCode * 397) ^ (this.leading?.GetHashCode() ?? 0);
170+
hashCode = (hashCode * 397) ^ this.forceStrutHeight.GetHashCode();
171+
return hashCode;
172+
}
173+
}
174+
175+
public override string toStringShort() {
176+
return $"{this.GetType()}";
177+
}
178+
179+
public override void debugFillProperties(DiagnosticPropertiesBuilder properties) {
180+
base.debugFillProperties(properties);
181+
if (this.debugLabel != null) {
182+
properties.add(new MessageProperty("debugLabel", this.debugLabel));
183+
}
184+
185+
List<DiagnosticsNode> styles = new List<DiagnosticsNode>();
186+
styles.Add(new StringProperty("family", this.fontFamily, defaultValue: Diagnostics.kNullDefaultValue,
187+
quoted: false));
188+
styles.Add(new EnumerableProperty<string>("familyFallback", this.fontFamilyFallback));
189+
styles.Add(new DiagnosticsProperty<float?>("size", this.fontSize,
190+
defaultValue: Diagnostics.kNullDefaultValue));
191+
string weightDescription = "";
192+
if (this.fontWeight != null) {
193+
weightDescription = this.fontWeight.weightValue.ToString();
194+
}
195+
196+
styles.Add(new DiagnosticsProperty<FontWeight>(
197+
"weight", this.fontWeight,
198+
description: weightDescription,
199+
defaultValue: Diagnostics.kNullDefaultValue
200+
));
201+
styles.Add(new EnumProperty<FontStyle?>("style", this.fontStyle,
202+
defaultValue: Diagnostics.kNullDefaultValue));
203+
styles.Add(new DiagnosticsProperty<float?>("height", this.height,
204+
defaultValue: Diagnostics.kNullDefaultValue));
205+
styles.Add(new FlagProperty("forceStrutHeight", value: this.forceStrutHeight,
206+
defaultValue: Diagnostics.kNullDefaultValue));
207+
208+
bool styleSpecified = styles.Any((DiagnosticsNode n) => !n.isFiltered(DiagnosticLevel.info));
209+
foreach (var style in styles) {
210+
properties.add(style);
211+
}
212+
213+
if (!styleSpecified) {
214+
properties.add(new FlagProperty("forceStrutHeight", value: this.forceStrutHeight,
215+
ifTrue: "<strut height forced>",
216+
ifFalse: "<strut height normal>"));
217+
}
218+
}
219+
}
220+
}

Runtime/painting/strut_style.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/painting/text_painter.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ public TextPainter(TextSpan text = null,
2525
TextDirection textDirection = TextDirection.ltr,
2626
float textScaleFactor = 1.0f,
2727
int? maxLines = null,
28-
string ellipsis = "") {
28+
string ellipsis = "",
29+
StrutStyle strutStyle = null) {
2930
this._text = text;
3031
this._textAlign = textAlign;
3132
this._textDirection = textDirection;
3233
this._textScaleFactor = textScaleFactor;
3334
this._maxLines = maxLines;
3435
this._ellipsis = ellipsis;
36+
this._strutStyle = strutStyle;
3537
}
3638

3739

@@ -133,6 +135,21 @@ public int? maxLines {
133135
}
134136
}
135137

138+
public StrutStyle strutStyle {
139+
get { return this._strutStyle; }
140+
set {
141+
if (this._strutStyle == value) {
142+
return;
143+
}
144+
145+
this._strutStyle = value;
146+
this._paragraph = null;
147+
this._needsLayout = true;
148+
}
149+
}
150+
151+
StrutStyle _strutStyle;
152+
136153
public float minIntrinsicWidth {
137154
get {
138155
Debug.Assert(!this._needsLayout);
@@ -322,7 +339,8 @@ ParagraphStyle _createParagraphStyle(TextDirection defaultTextDirection = TextDi
322339
textAlign: this.textAlign,
323340
textDirection: this.textDirection ?? defaultTextDirection,
324341
maxLines: this.maxLines,
325-
ellipsis: this.ellipsis
342+
ellipsis: this.ellipsis,
343+
strutStyle: this._strutStyle
326344
);
327345
}
328346

0 commit comments

Comments
 (0)