浏览代码

table widget

/main
xingwei.zhu 6 年前
当前提交
5642b2db
共有 7 个文件被更改,包括 1728 次插入2 次删除
  1. 31
      Samples/UIWidgetSample/MaterialSample.cs
  2. 1001
      Runtime/rendering/table.cs
  3. 11
      Runtime/rendering/table.cs.meta
  4. 272
      Runtime/rendering/table_border.cs
  5. 11
      Runtime/rendering/table_border.cs.meta
  6. 393
      Runtime/widgets/table.cs
  7. 11
      Runtime/widgets/table.cs.meta

31
Samples/UIWidgetSample/MaterialSample.cs


namespace UIWidgetsSample {
public class MaterialSample : UIWidgetsSamplePanel {
int testCaseId = 3;
int testCaseId = 4;
new MaterialTabBarWidget()
new MaterialTabBarWidget(),
new TableWidget()
};
protected override Widget createWidget() {

protected override void OnEnable() {
base.OnEnable();
FontManager.instance.addFont(Resources.Load<Font>(path: "MaterialIcons-Regular"));
}
}
class TableWidget : StatelessWidget {
public TableWidget(Key key = null) : base(key) {
}
public override Widget build(BuildContext context) {
return new Scaffold(
body: new Table(
children: new List<TableRow> {
new TableRow(
decoration: new BoxDecoration(color: Colors.blue),
children: new List<Widget> {
new Text("item 1"),
new Text("item 2")
}
),
new TableRow(children: new List<Widget> {
new Text("item 3"),
new Text("item 4")
}
)
},
defaultVerticalAlignment: TableCellVerticalAlignment.middle));
}
}

1001
Runtime/rendering/table.cs
文件差异内容过多而无法显示
查看文件

11
Runtime/rendering/table.cs.meta


fileFormatVersion: 2
guid: d05c9824abf8446e3b28c9734f104dd4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

272
Runtime/rendering/table_border.cs


using System.Collections.Generic;
using System.Linq;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.ui;
namespace Unity.UIWidgets.rendering {
public class TableBorder {
public TableBorder(
BorderSide top = null,
BorderSide right = null,
BorderSide bottom = null,
BorderSide left = null,
BorderSide horizontalInside = null,
BorderSide verticalInside = null
) {
this.top = top ?? BorderSide.none;
this.right = right ?? BorderSide.none;
this.bottom = bottom ?? BorderSide.none;
this.left = left ?? BorderSide.none;
this.horizontalInside = horizontalInside ?? BorderSide.none;
this.verticalInside = verticalInside ?? BorderSide.none;
}
public static TableBorder all(
Color color = null,
float width = 1.0f,
BorderStyle style = BorderStyle.solid
) {
color = color ?? new Color(0xFF000000);
BorderSide side = new BorderSide(color: color, width: width, style: style);
return new TableBorder(
top: side, right: side, bottom: side, left: side, horizontalInside: side, verticalInside: side);
}
public static TableBorder symmetric(
BorderSide inside = null,
BorderSide outside = null
) {
inside = inside ?? BorderSide.none;
outside = outside ?? BorderSide.none;
return new TableBorder(
top: outside,
right: outside,
bottom: outside,
left: outside,
horizontalInside: inside,
verticalInside: inside
);
}
public readonly BorderSide top;
public readonly BorderSide right;
public readonly BorderSide bottom;
public readonly BorderSide left;
public readonly BorderSide horizontalInside;
public readonly BorderSide verticalInside;
public EdgeInsets dimensions {
get {
return EdgeInsets.fromLTRB(this.left.width,
this.top.width,
this.right.width,
this.bottom.width);
}
}
public bool isUniform {
get {
D.assert(this.top != null);
D.assert(this.right != null);
D.assert(this.bottom != null);
D.assert(this.left != null);
D.assert(this.horizontalInside != null);
D.assert(this.verticalInside != null);
Color topColor = this.top.color;
if (this.right.color != topColor ||
this.bottom.color != topColor ||
this.left.color != topColor ||
this.horizontalInside.color != topColor ||
this.verticalInside.color != topColor) {
return false;
}
float topWidth = this.top.width;
if (this.right.width != topWidth ||
this.bottom.width != topWidth ||
this.left.width != topWidth ||
this.horizontalInside.width != topWidth ||
this.verticalInside.width != topWidth) {
return false;
}
BorderStyle topStyle = this.top.style;
if (this.right.style != topStyle ||
this.bottom.style != topStyle ||
this.left.style != topStyle ||
this.horizontalInside.style != topStyle ||
this.verticalInside.style != topStyle) {
return false;
}
return true;
}
}
TableBorder scale(float t) {
return new TableBorder(
top: this.top.scale(t),
right: this.right.scale(t),
bottom: this.bottom.scale(t),
left: this.left.scale(t),
horizontalInside: this.horizontalInside.scale(t),
verticalInside: this.verticalInside.scale(t)
);
}
public static TableBorder lerp(TableBorder a, TableBorder b, float t) {
if (a == null && b == null) {
return null;
}
if (a == null) {
return b.scale(t);
}
if (b == null) {
return a.scale(1.0f - t);
}
return new TableBorder(
top: BorderSide.lerp(a.top, b.top, t),
right: BorderSide.lerp(a.right, b.right, t),
bottom: BorderSide.lerp(a.bottom, b.bottom, t),
left: BorderSide.lerp(a.left, b.left, t),
horizontalInside: BorderSide.lerp(a.horizontalInside, b.horizontalInside, t),
verticalInside: BorderSide.lerp(a.verticalInside, b.verticalInside, t)
);
}
public void paint(Canvas canvas, Rect rect, List<float> rows, List<float> columns) {
D.assert(this.top != null);
D.assert(this.right != null);
D.assert(this.bottom != null);
D.assert(this.left != null);
D.assert(this.horizontalInside != null);
D.assert(this.verticalInside != null);
D.assert(canvas != null);
D.assert(rect != null);
D.assert(rows != null);
D.assert(rows.isEmpty() || (rows.First() >= 0.0f && rows.Last() <= rect.height));
D.assert(columns != null);
D.assert(columns.isEmpty() || (columns.First() >= 0.0f && columns.Last() <= rect.width));
if (columns.isNotEmpty() || rows.isNotEmpty()) {
Paint paint = new Paint();
if (columns.isNotEmpty()) {
switch (this.verticalInside.style) {
case BorderStyle.solid: {
paint.color = this.verticalInside.color;
paint.strokeWidth = this.verticalInside.width;
paint.style = PaintingStyle.stroke;
Path path = new Path();
foreach (float x in columns) {
path.moveTo(rect.left + x, rect.top);
path.lineTo(rect.left + x, rect.bottom);
}
canvas.drawPath(path, paint);
break;
}
case BorderStyle.none: {
break;
}
}
}
if (rows.isNotEmpty()) {
switch (this.horizontalInside.style) {
case BorderStyle.solid: {
paint.color = this.horizontalInside.color;
paint.strokeWidth = this.horizontalInside.width;
paint.style = PaintingStyle.stroke;
Path path = new Path();
foreach (float y in rows) {
path.moveTo(rect.left, rect.top + y);
path.lineTo(rect.right, rect.top + y);
}
canvas.drawPath(path, paint);
break;
}
case BorderStyle.none: {
break;
}
}
}
BorderUtils.paintBorder(canvas, rect, top: this.top, right: this.right, bottom: this.bottom,
left: this.left);
}
}
public bool Equals(TableBorder other) {
if (ReferenceEquals(null, other)) {
return false;
}
if (ReferenceEquals(this, other)) {
return true;
}
return other.top == this.top &&
other.right == this.right &&
other.bottom == this.bottom &&
other.left == this.left &&
other.horizontalInside == this.horizontalInside &&
other.verticalInside == this.verticalInside;
}
public override bool Equals(object obj) {
if (ReferenceEquals(null, obj)) {
return false;
}
if (ReferenceEquals(this, obj)) {
return true;
}
if (obj.GetType() != this.GetType()) {
return false;
}
return this.Equals((TableBorder) obj);
}
public static bool operator ==(TableBorder left, TableBorder right) {
return Equals(left, right);
}
public static bool operator !=(TableBorder left, TableBorder right) {
return !Equals(left, right);
}
public override int GetHashCode() {
unchecked {
var hashCode = this.top.GetHashCode();
hashCode = (hashCode * 397) ^ this.right.GetHashCode();
hashCode = (hashCode * 397) ^ this.bottom.GetHashCode();
hashCode = (hashCode * 397) ^ this.left.GetHashCode();
hashCode = (hashCode * 397) ^ this.horizontalInside.GetHashCode();
hashCode = (hashCode * 397) ^ this.verticalInside.GetHashCode();
return hashCode;
}
}
public override string ToString() {
return
$"TableBorder({this.top}, {this.right}, {this.bottom}, {this.left}, {this.horizontalInside}, {this.verticalInside})";
}
}
}

11
Runtime/rendering/table_border.cs.meta


fileFormatVersion: 2
guid: ca951bb29ed0546f0b6c9ae9a42985bc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

393
Runtime/widgets/table.cs


using System.Collections.Generic;
using System.Linq;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.rendering;
using Unity.UIWidgets.ui;
namespace Unity.UIWidgets.widgets {
public class TableRow {
public TableRow(
LocalKey key = null,
Decoration decoration = null,
List<Widget> children = null
) {
this.key = key;
this.decoration = decoration;
this.children = children;
}
public readonly LocalKey key;
public readonly Decoration decoration;
public readonly List<Widget> children;
public override string ToString() {
return "TableRow("
+ (this.key != null ? this.key.ToString() : "")
+ (this.decoration != null ? this.decoration.toString() : "")
+ (this.children == null ? "child list is null" :
this.children.isEmpty() ? "no children" :
this.children.ToString())
+ ")";
}
}
class _TableElementRow {
public _TableElementRow(
LocalKey key = null,
List<Element> children = null
) {
this.key = key;
this.children = children;
}
public readonly LocalKey key;
public readonly List<Element> children;
}
public class Table : RenderObjectWidget {
public Table(
Key key = null,
List<TableRow> children = null,
Dictionary<int, TableColumnWidth> columnWidths = null,
TableColumnWidth defaultColumnWidth = null,
TableBorder border = null,
TableCellVerticalAlignment defaultVerticalAlignment = TableCellVerticalAlignment.top,
TextBaseline? textBaseline = null
) : base(key: key) {
children = children ?? new List<TableRow>();
defaultColumnWidth = defaultColumnWidth ?? new FlexColumnWidth(1.0f);
this.children = children;
this.columnWidths = columnWidths;
this.defaultColumnWidth = defaultColumnWidth;
this.border = border;
this.defaultVerticalAlignment = defaultVerticalAlignment;
this.textBaseline = textBaseline;
D.assert(() => {
if (children.Any((TableRow row) => {
return row.children.Any((Widget cell) => { return cell == null; });
})) {
throw new UIWidgetsError(
"One of the children of one of the rows of the table was null.\n" +
"The children of a TableRow must not be null."
);
}
return true;
});
D.assert(() => {
if (children.Any((TableRow row1) => {
return row1.key != null &&
children.Any((TableRow row2) => { return row1 != row2 && row1.key == row2.key; });
})) {
throw new UIWidgetsError(
"Two or more TableRow children of this Table had the same key.\n" +
"All the keyed TableRow children of a Table must have different Keys."
);
}
return true;
});
D.assert(() => {
if (children.isNotEmpty()) {
int cellCount = this.children.First().children.Count;
if (children.Any((TableRow row) => { return row.children.Count != cellCount; })) {
throw new UIWidgetsError(
"Table contains irregular row lengths.\n" +
"Every TableRow in a Table must have the same number of children, so that every cell is filled. " +
"Otherwise, the table will contain holes."
);
}
}
return true;
});
this._rowDecorations = null;
if (children.Any((TableRow row) => { return row.decoration != null; })) {
this._rowDecorations = new List<Decoration>();
foreach (TableRow row in children) {
this._rowDecorations.Add(row.decoration);
}
}
D.assert(() => {
List<Widget> flatChildren = new List<Widget>();
foreach (TableRow row in children) {
flatChildren.AddRange(row.children);
}
if (WidgetsD.debugChildrenHaveDuplicateKeys(this, flatChildren)) {
throw new UIWidgetsError(
"Two or more cells in this Table contain widgets with the same key.\n" +
"Every widget child of every TableRow in a Table must have different keys. The cells of a Table are " +
"flattened out for processing, so separate cells cannot have duplicate keys even if they are in " +
"different rows."
);
}
return true;
});
}
public readonly List<TableRow> children;
public readonly Dictionary<int, TableColumnWidth> columnWidths;
public readonly TableColumnWidth defaultColumnWidth;
public readonly TableBorder border;
public readonly TableCellVerticalAlignment defaultVerticalAlignment;
public readonly TextBaseline? textBaseline;
public readonly List<Decoration> _rowDecorations;
public override Element createElement() {
return new _TableElement(this);
}
public override RenderObject createRenderObject(BuildContext context) {
return new RenderTable(
columns: this.children.isNotEmpty() ? this.children[0].children.Count : 0,
rows: this.children.Count,
columnWidths: this.columnWidths,
defaultColumnWidth: this.defaultColumnWidth,
border: this.border,
rowDecorations: this._rowDecorations,
configuration: ImageUtils.createLocalImageConfiguration(context),
defaultVerticalAlignment: this.defaultVerticalAlignment,
textBaseline: this.textBaseline
);
}
public override void updateRenderObject(BuildContext context, RenderObject renderObject) {
RenderTable _renderObject = (RenderTable) renderObject;
D.assert(_renderObject.columns == (this.children.isNotEmpty() ? this.children[0].children.Count : 0));
D.assert(_renderObject.rows == this.children.Count);
_renderObject.columnWidths = this.columnWidths;
_renderObject.defaultColumnWidth = this.defaultColumnWidth;
_renderObject.border = this.border;
_renderObject.rowDecorations = this._rowDecorations;
_renderObject.configuration = ImageUtils.createLocalImageConfiguration(context);
_renderObject.defaultVerticalAlignment = this.defaultVerticalAlignment;
_renderObject.textBaseline = this.textBaseline;
}
}
class _TableElement : RenderObjectElement {
public _TableElement(Table widget) : base(widget) {
}
public new Table widget {
get { return (Table) base.widget; }
}
public new RenderTable renderObject {
get { return (RenderTable) base.renderObject; }
}
List<_TableElementRow> _children = new List<_TableElementRow>();
bool _debugWillReattachChildren = false;
public override void mount(Element parent, object newSlot) {
base.mount(parent, newSlot);
D.assert(!this._debugWillReattachChildren);
D.assert(() => {
this._debugWillReattachChildren = true;
return true;
});
this._children.Clear();
foreach (TableRow row in this.widget.children) {
List<Element> elements = new List<Element>();
foreach (Widget child in row.children) {
D.assert(child != null);
elements.Add(this.inflateWidget(child, null));
}
this._children.Add(
new _TableElementRow(
key: row.key,
children: elements)
);
}
D.assert(() => {
this._debugWillReattachChildren = false;
return true;
});
this._updateRenderObjectChildren();
}
protected override void insertChildRenderObject(RenderObject child, object slot) {
D.assert(this._debugWillReattachChildren);
this.renderObject.setupParentData(child);
}
protected override void moveChildRenderObject(RenderObject child, object slot) {
D.assert(this._debugWillReattachChildren);
}
protected override void removeChildRenderObject(RenderObject child) {
D.assert(() => {
if (this._debugWillReattachChildren) {
return true;
}
foreach (Element forgottenChild in this._forgottenChildren) {
if (forgottenChild.renderObject == child) {
return true;
}
}
return false;
});
TableCellParentData childParentData = (TableCellParentData) child.parentData;
this.renderObject.setChild(childParentData.x, childParentData.y, null);
}
readonly HashSet<Element> _forgottenChildren = new HashSet<Element>();
public override void update(Widget newWidget) {
D.assert(!this._debugWillReattachChildren);
D.assert(() => {
this._debugWillReattachChildren = true;
return true;
});
Table _newWidget = (Table) newWidget;
Dictionary<LocalKey, List<Element>> oldKeyedRows = new Dictionary<LocalKey, List<Element>>();
foreach (_TableElementRow row in this._children) {
if (row.key != null) {
oldKeyedRows[row.key] = row.children;
}
}
List<_TableElementRow> oldUnkeyedRows = new List<_TableElementRow>();
foreach (_TableElementRow row in this._children) {
if (row.key == null) {
oldUnkeyedRows.Add(row);
}
}
List<_TableElementRow> newChildren = new List<_TableElementRow>();
HashSet<List<Element>> taken = new HashSet<List<Element>>();
int unkeyedRow = 0;
foreach (TableRow row in _newWidget.children) {
List<Element> oldChildren = null;
if (row.key != null && oldKeyedRows.ContainsKey(row.key)) {
oldChildren = oldKeyedRows[row.key];
taken.Add(oldChildren);
}
else if (row.key == null && unkeyedRow < oldUnkeyedRows.Count) {
oldChildren = oldUnkeyedRows[unkeyedRow].children;
unkeyedRow++;
}
else {
oldChildren = new List<Element>();
}
newChildren.Add(new _TableElementRow(
key: row.key,
children: this.updateChildren(oldChildren, row.children,
forgottenChildren: this._forgottenChildren))
);
}
while (unkeyedRow < oldUnkeyedRows.Count) {
this.updateChildren(oldUnkeyedRows[unkeyedRow].children, new List<Widget>(),
forgottenChildren: this._forgottenChildren);
unkeyedRow++;
}
foreach (List<Element> oldChildren in oldKeyedRows.Values) {
if (taken.Contains(oldChildren)) {
continue;
}
this.updateChildren(oldChildren, new List<Widget>(), forgottenChildren: this._forgottenChildren);
}
D.assert(() => {
this._debugWillReattachChildren = false;
return true;
});
this._children = newChildren;
this._updateRenderObjectChildren();
this._forgottenChildren.Clear();
base.update(newWidget);
D.assert(this.widget == newWidget);
}
void _updateRenderObjectChildren() {
D.assert(this.renderObject != null);
List<RenderBox> renderBoxes = new List<RenderBox>();
foreach (_TableElementRow row in this._children) {
foreach (Element child in row.children) {
renderBoxes.Add((RenderBox) child.renderObject);
}
}
this.renderObject.setFlatChildren(
this._children.isNotEmpty() ? this._children[0].children.Count : 0,
renderBoxes
);
}
public override void visitChildren(ElementVisitor visitor) {
foreach (_TableElementRow row in this._children) {
foreach (Element child in row.children) {
if (!this._forgottenChildren.Contains(child)) {
visitor(child);
}
}
}
}
protected override void forgetChild(Element child) {
this._forgottenChildren.Add(child);
}
}
public class TableCell : ParentDataWidget<Table> {
public TableCell(
Key key = null,
TableCellVerticalAlignment? verticalAlignment = null,
Widget child = null
) : base(key: key, child: child) {
this.verticalAlignment = verticalAlignment;
}
public readonly TableCellVerticalAlignment? verticalAlignment;
public override void applyParentData(RenderObject renderObject) {
TableCellParentData parentData = (TableCellParentData) renderObject.parentData;
if (parentData.verticalAlignment != this.verticalAlignment) {
parentData.verticalAlignment = this.verticalAlignment;
AbstractNodeMixinDiagnosticableTree targetParent = renderObject.parent;
if (targetParent is RenderObject) {
((RenderObject) targetParent).markNeedsLayout();
}
}
}
public override void debugFillProperties(DiagnosticPropertiesBuilder properties) {
base.debugFillProperties(properties);
properties.add(new EnumProperty<TableCellVerticalAlignment?>("verticalAlignment", this.verticalAlignment));
}
}
}

11
Runtime/widgets/table.cs.meta


fileFormatVersion: 2
guid: 17e636abadff64cdeb536be14444d7eb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存