From 5533827cc552d15c0cdf6ec2b9fa0755efa25d35 Mon Sep 17 00:00:00 2001 From: Grim Maple Date: Fri, 16 Dec 2022 18:18:52 +0300 Subject: [PATCH] Replace `undeaD.xml` with `arsd.dom`. Fixes #626 --- dub.json | 4 +- src/dlangui/graphics/resources.d | 38 ++++----- src/dlangui/widgets/styles.d | 128 ++++++++++++++++--------------- views/res/theme_default.xml | 10 +-- 4 files changed, 92 insertions(+), 88 deletions(-) diff --git a/dub.json b/dub.json index 0e27e8fc..21697e81 100644 --- a/dub.json +++ b/dub.json @@ -31,8 +31,8 @@ "dependencies": { "inilike": "~>1.2.2", - "undead": "~>1.1.7", - "icontheme": "~>1.2.3" + "icontheme": "~>1.2.3", + "arsd-official:dom": "~>10.9.7" }, "subPackages": [ diff --git a/src/dlangui/graphics/resources.d b/src/dlangui/graphics/resources.d index 52fce0a1..9bbf53d2 100644 --- a/src/dlangui/graphics/resources.d +++ b/src/dlangui/graphics/resources.d @@ -101,7 +101,7 @@ import dlangui.graphics.colors; import dlangui.graphics.drawbuf; import std.file; import std.algorithm; -import undead.xml; +import arsd.dom; import std.conv; import std.string; import std.path; @@ -985,22 +985,18 @@ class ImageDrawable : Drawable { } string attrValue(Element item, string attrname, string attrname2 = null) { - if (attrname in item.tag.attr) - return item.tag.attr[attrname]; - if (attrname2 && attrname2 in item.tag.attr) - return item.tag.attr[attrname2]; + return attrValue(item.attrs, attrname, attrname2); +} + +string attrValue(AttributeSet attr, string attrname, string attrname2 = null) { + if (attr.get(attrname) !is null) // TODO_GRIM: Add support of in to arsd.dom? + return attr.get(attrname); + if (attrname2 !is null && attr.get(attrname2) !is null) + return attr.get(attrname2); return null; } -string attrValue(ref string[string] attr, string attrname, string attrname2 = null) { - if (attrname in attr) - return attr[attrname]; - if (attrname2 && attrname2 in attr) - return attr[attrname2]; - return null; -} - -void extractStateFlag(ref string[string] attr, string attrName, string attrName2, State state, ref uint stateMask, ref uint stateValue) { +void extractStateFlag(ref AttributeSet attr, string attrName, string attrName2, State state, ref uint stateMask, ref uint stateValue) { string value = attrValue(attr, attrName, attrName2); if (value !is null) { if (value.equal("true")) @@ -1010,7 +1006,7 @@ void extractStateFlag(ref string[string] attr, string attrName, string attrName2 } /// converts XML attribute name to State (see http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList) -void extractStateFlags(ref string[string] attr, ref uint stateMask, ref uint stateValue) { +void extractStateFlags(AttributeSet attr, ref uint stateMask, ref uint stateValue) { extractStateFlag(attr, "state_pressed", "android:state_pressed", State.Pressed, stateMask, stateValue); extractStateFlag(attr, "state_focused", "android:state_focused", State.Focused, stateMask, stateValue); extractStateFlag(attr, "state_default", "android:state_default", State.Default, stateMask, stateValue); @@ -1148,9 +1144,9 @@ class StateDrawable : Drawable { return (nn[0] << 24) | (nn[1] << 16) | (nn[2] << 8) | (nn[3] << 0); } - bool load(Element element) { - foreach(item; element.elements) { - if (item.tag.name.equal("item")) { + bool load(XmlDocument document) { + foreach(item; document.root.children) { + if (item.tagName.equal("item")) { string drawableId = attrValue(item, "drawable", "android:drawable"); if (drawableId.startsWith("@drawable/")) drawableId = drawableId[10 .. $]; @@ -1160,7 +1156,7 @@ class StateDrawable : Drawable { transform.addAfter = colorTransformFromStringAdd(attrValue(item, "color_transform_add2", "android:transform_color_add2")); if (drawableId !is null) { uint stateMask, stateValue; - extractStateFlags(item.tag.attr, stateMask, stateValue); + extractStateFlags(item.attrs, stateMask, stateValue); if (drawableId !is null) { addState(stateMask, stateValue, drawableId, transform); } @@ -1183,10 +1179,10 @@ class StateDrawable : Drawable { //check(s); // Make a DOM tree - auto doc = new Document(s); + auto doc = new XmlDocument(s); return load(doc); - } catch (CheckException e) { + } catch (Exception e) { Log.e("Invalid XML file ", filename); return false; } diff --git a/src/dlangui/widgets/styles.d b/src/dlangui/widgets/styles.d index dcfe4b4d..21ab97c4 100644 --- a/src/dlangui/widgets/styles.d +++ b/src/dlangui/widgets/styles.d @@ -25,7 +25,7 @@ module dlangui.widgets.styles; import dlangui.core.config; -import undead.xml; +import arsd.dom; import std.string; import std.algorithm; @@ -1562,73 +1562,74 @@ string sanitizeBoxShadowProperty(string s) pure { /// load style attributes from XML element bool loadStyleAttributes(Style style, Element elem, bool allowStates) { //Log.d("Theme: loadStyleAttributes ", style.id, " ", elem.tag.attr); - if ("backgroundImageId" in elem.tag.attr) - style.backgroundImageId = elem.tag.attr["backgroundImageId"]; - if ("backgroundColor" in elem.tag.attr) - style.backgroundColor = decodeHexColor(elem.tag.attr["backgroundColor"]); - if ("textColor" in elem.tag.attr) - style.textColor = decodeHexColor(elem.tag.attr["textColor"]); - if ("margins" in elem.tag.attr) - style.margins = decodeRect(elem.tag.attr["margins"]); - if ("padding" in elem.tag.attr) - style.padding = decodeRect(elem.tag.attr["padding"]); - if ("border" in elem.tag.attr) - style.border = sanitizeBorderProperty(elem.tag.attr["border"]); - if ("boxShadow" in elem.tag.attr) - style.boxShadow = sanitizeBoxShadowProperty(elem.tag.attr["boxShadow"]); - if ("align" in elem.tag.attr) - style.alignment = decodeAlignment(elem.tag.attr["align"]); - if ("minWidth" in elem.tag.attr) - style.minWidth = decodeDimension(elem.tag.attr["minWidth"]); - if ("maxWidth" in elem.tag.attr) - style.maxWidth = decodeDimension(elem.tag.attr["maxWidth"]); - if ("minHeight" in elem.tag.attr) - style.minHeight = decodeDimension(elem.tag.attr["minHeight"]); - if ("maxHeight" in elem.tag.attr) - style.maxHeight = decodeDimension(elem.tag.attr["maxHeight"]); - if ("maxLines" in elem.tag.attr) - style.maxLines = decodeDimension(elem.tag.attr["maxLines"]); - if ("fontFace" in elem.tag.attr) - style.fontFace = elem.tag.attr["fontFace"]; - if ("fontFamily" in elem.tag.attr) - style.fontFamily = decodeFontFamily(elem.tag.attr["fontFamily"]); - if ("fontSize" in elem.tag.attr) - style.fontSize = cast(int)decodeDimension(elem.tag.attr["fontSize"]); - if ("fontWeight" in elem.tag.attr) - style.fontWeight = cast(ushort)decodeFontWeight(elem.tag.attr["fontWeight"]); - if ("layoutWidth" in elem.tag.attr) - style.layoutWidth = decodeLayoutDimension(elem.tag.attr["layoutWidth"]); - if ("layoutHeight" in elem.tag.attr) - style.layoutHeight = decodeLayoutDimension(elem.tag.attr["layoutHeight"]); - if ("alpha" in elem.tag.attr) - style.alpha = decodeDimension(elem.tag.attr["alpha"]); - if ("textFlags" in elem.tag.attr) - style.textFlags = decodeTextFlags(elem.tag.attr["textFlags"]); - if ("focusRectColors" in elem.tag.attr) - style.focusRectColors = decodeFocusRectColors(elem.tag.attr["focusRectColors"]); - foreach(item; elem.elements) { - if (allowStates && item.tag.name.equal("state")) { + if ("backgroundImageId" in elem.attributes) + style.backgroundImageId = elem.attrs["backgroundImageId"]; + if ("backgroundColor" in elem.attributes) + style.backgroundColor = decodeHexColor(elem.attrs["backgroundColor"]); + if ("textColor" in elem.attributes) + style.textColor = decodeHexColor(elem.attrs["textColor"]); + if ("margins" in elem.attributes) + style.margins = decodeRect(elem.attrs["margins"]); + if ("padding" in elem.attributes) + style.padding = decodeRect(elem.attrs["padding"]); + if ("border" in elem.attributes) + style.border = sanitizeBorderProperty(elem.attrs["border"]); + if ("boxShadow" in elem.attributes) + style.boxShadow = sanitizeBoxShadowProperty(elem.attrs["boxShadow"]); + if ("align" in elem.attributes) + style.alignment = decodeAlignment(elem.attrs["align"]); + if ("minWidth" in elem.attributes) + style.minWidth = decodeDimension(elem.attrs["minWidth"]); + if ("maxWidth" in elem.attributes) + style.maxWidth = decodeDimension(elem.attrs["maxWidth"]); + if ("minHeight" in elem.attributes) + style.minHeight = decodeDimension(elem.attrs["minHeight"]); + if ("maxHeight" in elem.attributes) + style.maxHeight = decodeDimension(elem.attrs["maxHeight"]); + if ("maxLines" in elem.attributes) + style.maxLines = decodeDimension(elem.attrs["maxLines"]); + if ("fontFace" in elem.attributes) + style.fontFace = elem.attrs["fontFace"]; + if ("fontFamily" in elem.attributes) + style.fontFamily = decodeFontFamily(elem.attrs["fontFamily"]); + if ("fontSize" in elem.attributes) + style.fontSize = cast(int)decodeDimension(elem.attrs["fontSize"]); + if ("fontWeight" in elem.attributes) + style.fontWeight = cast(ushort)decodeFontWeight(elem.attrs["fontWeight"]); + if ("layoutWidth" in elem.attributes) + style.layoutWidth = decodeLayoutDimension(elem.attrs["layoutWidth"]); + if ("layoutHeight" in elem.attributes) + style.layoutHeight = decodeLayoutDimension(elem.attrs["layoutHeight"]); + if ("alpha" in elem.attributes) + style.alpha = decodeDimension(elem.attrs["alpha"]); + if ("textFlags" in elem.attributes) + style.textFlags = decodeTextFlags(elem.attrs["textFlags"]); + if ("focusRectColors" in elem.attributes) + style.focusRectColors = decodeFocusRectColors(elem.attrs["focusRectColors"]); + foreach(item; elem.childNodes) { + if (allowStates && item.tagName.equal("state")) { uint stateMask = 0; uint stateValue = 0; - extractStateFlags(item.tag.attr, stateMask, stateValue); + extractStateFlags(item.attrs, stateMask, stateValue); if (stateMask) { Style state = style.getOrCreateState(stateMask, stateValue); + Log.d(item.attributes); loadStyleAttributes(state, item, false); } - } else if (item.tag.name.equal("drawable")) { + } else if (item.tagName.equal("drawable")) { // string drawableid = attrValue(item, "id"); string drawablevalue = attrValue(item, "value"); if (drawableid) style.setCustomDrawable(drawableid, drawablevalue); - } else if (item.tag.name.equal("color")) { + } else if (item.tagName.equal("color")) { // string colorid = attrValue(item, "id"); string colorvalue = attrValue(item, "value"); uint color = decodeHexColor(colorvalue, COLOR_TRANSPARENT); if (colorid) style.setCustomColor(colorid, color); - } else if (item.tag.name.equal("length")) { + } else if (item.tagName.equal("length")) { // string lenid = attrValue(item, "id"); string lenvalue = attrValue(item, "value"); @@ -1655,24 +1656,28 @@ bool loadStyleAttributes(Style style, Element elem, bool allowStates) { * --- * */ -bool loadTheme(Theme theme, Element doc, int level = 0) { - if (!doc.tag.name.equal("theme")) { +bool loadTheme(Theme theme, XmlDocument doc, int level = 0) { + if (!doc.root.tagName.equal("theme")) { Log.e(" element should be main in theme file!"); return false; } // - string id = attrValue(doc, "id"); - string parent = attrValue(doc, "parent"); + string id = attrValue(doc.root, "id"); + string parent = attrValue(doc.root, "parent"); theme.id = id; if (parent.length > 0) { // load base theme if (level < 3) // to prevent infinite recursion loadTheme(theme, parent, level + 1); } - loadStyleAttributes(theme, doc, false); - foreach(styleitem; doc.elements) { - if (styleitem.tag.name.equal("style")) { + loadStyleAttributes(theme, doc.root, false); + foreach(styleitem; doc.root.childNodes) { + if (styleitem.tagName.equal("style")) { // load @@ -540,7 +540,7 @@ textColor="#504840" padding="1pt,1pt,1pt,1pt"> - + - +