浏览代码

make widget immutable.

/main
kg 6 年前
当前提交
e77cbe99
共有 8 个文件被更改,包括 283 次插入27 次删除
  1. 6
      Assets/UIWidgets/foundation/change_notifier.cs
  2. 2
      Assets/UIWidgets/foundation/debug.cs
  3. 126
      Assets/UIWidgets/foundation/node.cs
  4. 67
      Assets/UIWidgets/foundation/node.mixin.gen.cs
  5. 69
      Assets/UIWidgets/foundation/node.mixin.njk
  6. 20
      Assets/UIWidgets/widgets/basic.cs
  7. 16
      Assets/UIWidgets/widgets/framework.cs
  8. 4
      Assets/UIWidgets/widgets/text.cs

6
Assets/UIWidgets/foundation/change_notifier.cs


T value { get; }
}
public class ChangeNotifier : Listenable, IDisposable {
public class ChangeNotifier : Listenable {
ObserverList<VoidCallback> _listeners = new ObserverList<VoidCallback>();
bool _debugAssertNotDisposed() {

public void removeListener(VoidCallback listener) {
D.assert(this._debugAssertNotDisposed());
this._listeners.Remove(listener);
}
public void Dispose() {
this.dispose();
}
public virtual void dispose() {

2
Assets/UIWidgets/foundation/debug.cs


}
}
public static bool debugPrintGestureArenaDiagnostics = true;
public static bool debugPrintGestureArenaDiagnostics = false;
public static bool debugPrintHitTestResults = false;

126
Assets/UIWidgets/foundation/node.cs


using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
class _DependencyList : IEquatable<_DependencyList> {
internal _DependencyList(Type type, object target) {
D.assert(type != null);
this._type = type;
var fields = _getTypeFields(type);
this._list = new List<object>(fields.Length);
foreach (var field in fields) {
this._list.Add(field.GetValue(target));
}
}
readonly List<object> _list;
readonly Type _type;
static readonly Dictionary<Type, FieldInfo[]> _typeFields = new Dictionary<Type, FieldInfo[]>();
static FieldInfo[] _getTypeFields(Type type) {
FieldInfo[] fields;
if (_typeFields.TryGetValue(type, out fields)) {
return fields;
}
_typeFields[type] = fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
D.assert(() => {
foreach (var field in fields) {
if (!field.IsInitOnly) {
throw new UIWidgetsError(
type + " should be immutable. All public fields need to be readonly. " +
field + " is not readonly.");
}
}
return true;
});
return fields;
}
static bool _sequenceEquals(IList list1, IList list2) {
if (list1 == null && list2 == null) {
return true;
}
if (list1 == null || list2 == null) {
return true;
}
if (list1.Count != list2.Count) {
return false;
}
for (var i = 0; i < list1.Count; i++) {
var item1 = list1[i];
var item2 = list2[i];
if (item1 is IList && item2 is IList) {
if (!_sequenceEquals((IList) item1, (IList) item2)) {
return false;
}
} else {
if (!object.Equals(item1, item2)) {
return false;
}
}
}
return true;
}
public bool Equals(_DependencyList other) {
if (object.ReferenceEquals(null, other)) return false;
if (object.ReferenceEquals(this, other)) return true;
return this._type == other._type && _sequenceEquals(this._list, other._list);
}
public override bool Equals(object obj) {
if (object.ReferenceEquals(null, obj)) return false;
if (object.ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return this.Equals((_DependencyList) obj);
}
static int _sequenceHashCode(IList list) {
unchecked {
if (list == null) {
return 0;
}
var hashCode = 0;
for (var i = 0; i < list.Count; i++) {
var item = list[i];
if (item is IList) {
hashCode = (hashCode * 397) ^ _sequenceHashCode((IList) item);
} else {
hashCode = (hashCode * 397) ^ (item == null ? 0 : item.GetHashCode());
}
}
return hashCode;
}
}
public override int GetHashCode() {
unchecked {
return (this._type.GetHashCode() * 397) ^ _sequenceHashCode(this._list);
}
}
public static bool operator ==(_DependencyList left, _DependencyList right) {
return object.Equals(left, right);
}
public static bool operator !=(_DependencyList left, _DependencyList right) {
return !object.Equals(left, right);
}
}
}

67
Assets/UIWidgets/foundation/node.mixin.gen.cs


using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
public class AbstractNode {

}
public class AbstractNodeMixinDiagnosticableTree : DiagnosticableTree {

if (this.attached) {
child.detach();
}
}
}
public abstract class CanonicalMixinDiagnosticableTree : DiagnosticableTree {
_DependencyList _dependencyList;
_DependencyList _getDependencyList() {
if (this._dependencyList == null) {
this._dependencyList = new _DependencyList(this.GetType(), this);
}
return this._dependencyList;
}
CanonicalMixinDiagnosticableTree _canonical;
CanonicalMixinDiagnosticableTree _getCanonical() {
if (this._canonical != null) {
return this._canonical;
}
var weakReference = _canonicalObjects.putIfAbsent(this._getDependencyList(), () => {
return new WeakReference(this);
});
if (weakReference.Target == null) {
weakReference.Target = this;
}
return this._canonical = (CanonicalMixinDiagnosticableTree) weakReference.Target;
}
~CanonicalMixinDiagnosticableTree() {
if (this == this._canonical) {
_canonicalObjects.Remove(this._dependencyList);
}
}
static readonly Dictionary<_DependencyList, WeakReference> _canonicalObjects =
new Dictionary<_DependencyList, WeakReference>();
public override bool Equals(object obj) {
if (object.ReferenceEquals(null, obj)) {
return false;
}
if (object.ReferenceEquals(this, obj)) {
return true;
}
if (obj.GetType() != this.GetType()) {
return false;
}
return object.ReferenceEquals(this._getCanonical(), ((CanonicalMixinDiagnosticableTree) obj)._getCanonical());
}
public override int GetHashCode() {
return RuntimeHelpers.GetHashCode(this._getCanonical());
}
}

69
Assets/UIWidgets/foundation/node.mixin.njk


using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
{% macro AbstractNodeMixin(with) %}
{% macro AbstractNodeMixin(with) %}
{% set className = 'AbstractNode' if with == '' else 'AbstractNodeMixin' + with %}
{% set extends = '' if with == '' else ' : ' + with %}

{{ AbstractNodeMixin('') }}
{{ AbstractNodeMixin('DiagnosticableTree') }}
{% macro CanonicalMixin(with) %}
public abstract class CanonicalMixin{{with}} : {{with}} {
_DependencyList _dependencyList;
_DependencyList _getDependencyList() {
if (this._dependencyList == null) {
this._dependencyList = new _DependencyList(this.GetType(), this);
}
return this._dependencyList;
}
CanonicalMixin{{with}} _canonical;
CanonicalMixin{{with}} _getCanonical() {
if (this._canonical != null) {
return this._canonical;
}
var weakReference = _canonicalObjects.putIfAbsent(this._getDependencyList(), () => {
return new WeakReference(this);
});
if (weakReference.Target == null) {
weakReference.Target = this;
}
return this._canonical = (CanonicalMixin{{with}}) weakReference.Target;
}
~CanonicalMixin{{with}}() {
if (this == this._canonical) {
_canonicalObjects.Remove(this._dependencyList);
}
}
static readonly Dictionary<_DependencyList, WeakReference> _canonicalObjects =
new Dictionary<_DependencyList, WeakReference>();
public override bool Equals(object obj) {
if (object.ReferenceEquals(null, obj)) {
return false;
}
if (object.ReferenceEquals(this, obj)) {
return true;
}
if (obj.GetType() != this.GetType()) {
return false;
}
return object.ReferenceEquals(this._getCanonical(), ((CanonicalMixin{{with}}) obj)._getCanonical());
}
public override int GetHashCode() {
return RuntimeHelpers.GetHashCode(this._getCanonical());
}
}
{% endmacro %}
{{ CanonicalMixin('DiagnosticableTree') }}
}

20
Assets/UIWidgets/widgets/basic.cs


((RenderImage) renderObject).alignment = this.alignment;
}
public ui.Image image;
public double? width;
public double? height;
public double scale;
public Color color;
public BlendMode blendMode;
public BoxFit fit;
public Alignment alignment;
public ImageRepeat repeat;
public Rect centerSlice;
public readonly ui.Image image;
public readonly double? width;
public readonly double? height;
public readonly double scale;
public readonly Color color;
public readonly BlendMode blendMode;
public readonly BoxFit fit;
public readonly Alignment alignment;
public readonly ImageRepeat repeat;
public readonly Rect centerSlice;
}
public class Listener : SingleChildRenderObjectWidget {

16
Assets/UIWidgets/widgets/framework.cs


}
}
public abstract class Widget : DiagnosticableTree {
public abstract class Widget : CanonicalMixinDiagnosticableTree {
protected Widget(Key key = null) {
this.key = key;
}

}
public override bool debugIsValidAncestor(RenderObjectWidget ancestor) {
D.assert(typeof(T) != typeof(RenderObjectWidget));
D.assert(typeof(T) != typeof(RenderObjectWidget));
return ancestor is T;
}

bool foundValidAncestor = false,
IEnumerable<Widget> badAncestors = null
) {
D.assert(typeof(T) != typeof(RenderObjectWidget));
D.assert(typeof(T) != typeof(RenderObjectWidget));
"{2} has no {1} ancestor at all.\n", this.GetType(), typeof(T), description);
"{2} has no {1} ancestor at all.\n", this.GetType(), typeof(T), description);
"{2} has a {1} ancestor, but there are other widgets between them:\n",
"{2} has a {1} ancestor, but there are other widgets between them:\n",
this.GetType(), typeof(T), description);
foreach (Widget ancestor in badAncestors) {

}
if (child != null) {
if (child.widget == newWidget) {
if (object.Equals(child.widget, newWidget)) {
if (!object.Equals(child.slot, newSlot)) {
this.updateSlotForChild(child, newSlot);
}

if (this._inheritedWidgets != null) {
this._inheritedWidgets.TryGetValue(targetType, out ancestor);
}
if (ancestor != null) {
return this.inheritFromElement(ancestor, aspect: aspect);
}

if (this._inheritedWidgets != null) {
this._inheritedWidgets.TryGetValue(targetType, out ancestor);
}
return ancestor;
}

internal override void _updateInheritance() {
Dictionary<Type, InheritedElement> incomingWidgets =
this._parent == null ? null : this._parent._inheritedWidgets;
if (incomingWidgets != null) {
this._inheritedWidgets = new Dictionary<Type, InheritedElement>(incomingWidgets);
} else {

4
Assets/UIWidgets/widgets/text.cs


}
public static DefaultTextStyle fallback() {
return new DefaultTextStyle();
return _fallback;
static readonly DefaultTextStyle _fallback = new DefaultTextStyle();
public static Widget merge(
Key key = null,

正在加载...
取消
保存