Skip to content

Commit

Permalink
only allow item renderers to set rtti properties that are "primitives"
Browse files Browse the repository at this point in the history
  • Loading branch information
ianharrigan committed Aug 11, 2024
1 parent 72e512f commit 7a9235b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
4 changes: 2 additions & 2 deletions haxe/ui/core/BasicItemRenderer.hx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class BasicItemRenderer extends ItemRenderer {
addComponent(hbox);
}

private override function updateValues(value:Dynamic, fieldList:Array<String> = null) {
super.updateValues(value, fieldList);
private override function updateValues(value:Dynamic, fieldList:Array<String> = null, currentRecursionLevel:Null<Int> = 0) {
super.updateValues(value, fieldList, currentRecursionLevel);

if (_label.text != null) {
_label.show();
Expand Down
14 changes: 11 additions & 3 deletions haxe/ui/core/ItemRenderer.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package haxe.ui.core;

import haxe.ui.util.RTTI;
import haxe.ui.components.Image;
import haxe.ui.components.Label;
import haxe.ui.containers.Box;
Expand All @@ -17,6 +18,7 @@ class ItemRenderer extends Box {
@:clonable public var autoRegisterInteractiveEvents:Bool = true;
@:clonable public var recursiveStyling:Bool = false;
@:clonable public var allowLayoutProperties:Bool = true;
@:clonable public var maxRecursionLevel:Null<Int> = 5;

public function new() {
super();
Expand Down Expand Up @@ -210,7 +212,11 @@ class ItemRenderer extends Box {
dispatch(e2);
}

private function updateValues(value:Dynamic, fieldList:Array<String> = null) {
private function updateValues(value:Dynamic, fieldList:Array<String> = null, currentRecursionLevel:Null<Int> = 0) {
if (currentRecursionLevel > maxRecursionLevel) {
return;
}

if (fieldList == null) {
fieldList = Reflect.fields(value);
}
Expand Down Expand Up @@ -248,7 +254,7 @@ class ItemRenderer extends Box {
setComponentProperty(c, v, property);
}
} else if (Type.typeof(v) == TObject) {
updateValues(v);
updateValues(v, null, currentRecursionLevel + 1);
} else {
var isLayoutProp = false;
if (f == "layout") {
Expand All @@ -258,7 +264,9 @@ class ItemRenderer extends Box {
}
if (!isLayoutProp) {
try {
Reflect.setProperty(this, f, v);
if (RTTI.hasPrimitiveClassProperty(this.className, f)) {
Reflect.setProperty(this, f, v);
}
} catch (e:Dynamic) { }
} else if (allowLayoutProperties) {
var layoutProp = StringUtil.uncapitalizeFirstLetter(f.substring("layout".length));
Expand Down
72 changes: 71 additions & 1 deletion haxe/ui/util/RTTI.hx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,21 @@ class RTTI {
}

public static function hasClassProperty(className:String, propertyName:String) {
return getClassProperty(className, propertyName) != null;
var props = getClassProperties(className, false, true);
if (props == null) {
return false;
}

return props.exists(propertyName);
}

public static function hasPrimitiveClassProperty(className:String, propertyName:String) {
var props = getClassProperties(className, true, true);
if (props == null) {
return false;
}

return props.exists(propertyName);
}

public static function load() {
Expand All @@ -113,6 +127,62 @@ class RTTI {
classInfo = unserializer.unserialize();
}

private static var _allPropertiesCache:Map<String, Map<String, RTTIProperty>> = new Map<String, Map<String, RTTIProperty>>();
public static function getClassProperties(className:String, primitiveOnly:Bool = false, allProperties:Bool = true):Map<String, RTTIProperty> {
className = className.toLowerCase();

var cacheKey = className + "_" + primitiveOnly;

if (allProperties && _allPropertiesCache.exists(cacheKey)) {
return _allPropertiesCache.get(cacheKey);
}

var entry = classInfo.get(className);
var properties = null;
if (entry != null) {
properties = new Map<String, RTTIProperty>();
for (key in entry.properties.keys()) {
var entryProp = entry.properties.get(key);
if (!isPrimitiveProperty(entryProp)) {
continue;
}
properties.set(key, entryProp);
}

var superClass = entry.superClass;
while (superClass != null) {
var superEntry = getClassInfo(superClass);
if (superEntry == null) {
break;
}
for (key in superEntry.properties.keys()) {
var entryProp = superEntry.properties.get(key);
if (!isPrimitiveProperty(entryProp)) {
continue;
}
properties.set(key, entryProp);
}
superClass = superEntry.superClass;
}
}

if (allProperties && properties != null) {
_allPropertiesCache.set(cacheKey, properties);
}

return properties;
}

private static function isPrimitiveProperty(prop:RTTIProperty):Bool {
if (prop.propertyName == "data") {
return false;
}
if (prop.propertyType == "bool" || prop.propertyType == "int" || prop.propertyType == "float" || prop.propertyType == "string" || prop.propertyType == "variant" || prop.propertyType == "dynamic") {
return true;
}
return false;
}

public static function getClassInfo(className:String):RTTIEntry {
load();

Expand Down

0 comments on commit 7a9235b

Please sign in to comment.