From 0887ba72e877e96370203e09e8e899624a2cfa89 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Wed, 13 Nov 2019 17:26:26 +0800 Subject: [PATCH] add supernode to apply layoutconfig --- .../main/java/pub/doric/shader/GroupNode.java | 62 +------------ .../java/pub/doric/shader/LinearNode.java | 4 +- .../main/java/pub/doric/shader/StackNode.java | 4 +- .../main/java/pub/doric/shader/SuperNode.java | 91 +++++++++++++++++++ .../main/java/pub/doric/shader/ViewNode.java | 12 +-- .../pub/doric/shader/list/ListAdapter.java | 4 +- .../java/pub/doric/shader/list/ListNode.java | 8 +- demo/src/ListDemo.ts | 30 ++++-- 8 files changed, 133 insertions(+), 82 deletions(-) create mode 100644 Android/doric/src/main/java/pub/doric/shader/SuperNode.java diff --git a/Android/doric/src/main/java/pub/doric/shader/GroupNode.java b/Android/doric/src/main/java/pub/doric/shader/GroupNode.java index 1f787c7f..ac6d3afe 100644 --- a/Android/doric/src/main/java/pub/doric/shader/GroupNode.java +++ b/Android/doric/src/main/java/pub/doric/shader/GroupNode.java @@ -19,7 +19,6 @@ import android.util.SparseArray; import android.view.ViewGroup; import pub.doric.DoricContext; -import pub.doric.utils.DoricUtils; import com.github.pengfeizhou.jscore.JSArray; import com.github.pengfeizhou.jscore.JSObject; @@ -35,7 +34,7 @@ import java.util.Map; * @Author: pengfei.zhou * @CreateDate: 2019-07-20 */ -public abstract class GroupNode extends ViewNode { +public abstract class GroupNode extends SuperNode { private Map mChildrenNode = new HashMap<>(); private SparseArray mIndexInfo = new SparseArray<>(); @@ -61,7 +60,7 @@ public abstract class GroupNode extends ViewNode { if (child == null) { child = ViewNode.create(getDoricContext(), type); child.index = i; - child.mParent = this; + child.mSuperNode = this; child.mId = id; mChildrenNode.put(id, child); } else { @@ -110,61 +109,4 @@ public abstract class GroupNode extends ViewNode { super.blend(view, layoutParams, name, prop); } } - - protected ViewGroup.LayoutParams generateDefaultLayoutParams() { - return new ViewGroup.LayoutParams(0, 0); - } - - protected void blendChild(ViewNode viewNode, JSObject jsObject) { - - - JSValue jsValue = jsObject.getProperty("margin"); - JSValue widthSpec = jsObject.getProperty("widthSpec"); - JSValue heightSpec = jsObject.getProperty("widthSpec"); - - ViewGroup.LayoutParams layoutParams = viewNode.getLayoutParams(); - if (widthSpec.isNumber()) { - switch (widthSpec.asNumber().toInt()) { - case 1: - layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT; - break; - case 2: - layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT; - break; - default: - break; - - } - } - if (heightSpec.isNumber()) { - switch (heightSpec.asNumber().toInt()) { - case 1: - layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT; - break; - case 2: - layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT; - break; - default: - break; - } - } - if (jsValue.isObject() && layoutParams instanceof ViewGroup.MarginLayoutParams) { - JSValue topVal = jsValue.asObject().getProperty("top"); - if (topVal.isNumber()) { - ((ViewGroup.MarginLayoutParams) layoutParams).topMargin = DoricUtils.dp2px(topVal.asNumber().toFloat()); - } - JSValue leftVal = jsValue.asObject().getProperty("left"); - if (leftVal.isNumber()) { - ((ViewGroup.MarginLayoutParams) layoutParams).leftMargin = DoricUtils.dp2px(leftVal.asNumber().toFloat()); - } - JSValue rightVal = jsValue.asObject().getProperty("right"); - if (rightVal.isNumber()) { - ((ViewGroup.MarginLayoutParams) layoutParams).rightMargin = DoricUtils.dp2px(rightVal.asNumber().toFloat()); - } - JSValue bottomVal = jsValue.asObject().getProperty("bottom"); - if (bottomVal.isNumber()) { - ((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin = DoricUtils.dp2px(bottomVal.asNumber().toFloat()); - } - } - } } diff --git a/Android/doric/src/main/java/pub/doric/shader/LinearNode.java b/Android/doric/src/main/java/pub/doric/shader/LinearNode.java index 80e7bb24..fd06dfae 100644 --- a/Android/doric/src/main/java/pub/doric/shader/LinearNode.java +++ b/Android/doric/src/main/java/pub/doric/shader/LinearNode.java @@ -36,8 +36,8 @@ public class LinearNode extends GroupNode { } @Override - protected void blendChild(ViewNode viewNode, JSObject layoutConfig) { - super.blendChild(viewNode, layoutConfig); + protected void blendChildLayoutConfig(ViewNode viewNode, JSObject layoutConfig) { + super.blendChildLayoutConfig(viewNode, layoutConfig); JSValue jsValue = layoutConfig.getProperty("alignment"); if (jsValue.isNumber()) { ((LinearLayout.LayoutParams) viewNode.getLayoutParams()).gravity = jsValue.asNumber().toInt(); diff --git a/Android/doric/src/main/java/pub/doric/shader/StackNode.java b/Android/doric/src/main/java/pub/doric/shader/StackNode.java index 4c781889..aa413105 100644 --- a/Android/doric/src/main/java/pub/doric/shader/StackNode.java +++ b/Android/doric/src/main/java/pub/doric/shader/StackNode.java @@ -36,8 +36,8 @@ public class StackNode extends GroupNode { } @Override - protected void blendChild(ViewNode viewNode, JSObject jsObject) { - super.blendChild(viewNode, jsObject); + protected void blendChildLayoutConfig(ViewNode viewNode, JSObject jsObject) { + super.blendChildLayoutConfig(viewNode, jsObject); JSValue jsValue = jsObject.getProperty("alignment"); if (jsValue.isNumber()) { ((FrameLayout.LayoutParams) viewNode.getLayoutParams()).gravity = jsValue.asNumber().toInt(); diff --git a/Android/doric/src/main/java/pub/doric/shader/SuperNode.java b/Android/doric/src/main/java/pub/doric/shader/SuperNode.java new file mode 100644 index 00000000..a9d7f83b --- /dev/null +++ b/Android/doric/src/main/java/pub/doric/shader/SuperNode.java @@ -0,0 +1,91 @@ +/* + * Copyright [2019] [Doric.Pub] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package pub.doric.shader; + +import android.view.View; +import android.view.ViewGroup; + +import com.github.pengfeizhou.jscore.JSObject; +import com.github.pengfeizhou.jscore.JSValue; + +import pub.doric.DoricContext; +import pub.doric.utils.DoricUtils; + +/** + * @Description: pub.doric.shader + * @Author: pengfei.zhou + * @CreateDate: 2019-11-13 + */ +public abstract class SuperNode extends ViewNode { + public SuperNode(DoricContext doricContext) { + super(doricContext); + } + + protected ViewGroup.LayoutParams generateDefaultLayoutParams() { + return new ViewGroup.LayoutParams(0, 0); + } + + protected void blendChildLayoutConfig(ViewNode viewNode, JSObject jsObject) { + JSValue jsValue = jsObject.getProperty("margin"); + JSValue widthSpec = jsObject.getProperty("widthSpec"); + JSValue heightSpec = jsObject.getProperty("heightSpec"); + + ViewGroup.LayoutParams layoutParams = viewNode.getLayoutParams(); + if (widthSpec.isNumber()) { + switch (widthSpec.asNumber().toInt()) { + case 1: + layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT; + break; + case 2: + layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT; + break; + default: + break; + + } + } + if (heightSpec.isNumber()) { + switch (heightSpec.asNumber().toInt()) { + case 1: + layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT; + break; + case 2: + layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT; + break; + default: + break; + } + } + if (jsValue.isObject() && layoutParams instanceof ViewGroup.MarginLayoutParams) { + JSValue topVal = jsValue.asObject().getProperty("top"); + if (topVal.isNumber()) { + ((ViewGroup.MarginLayoutParams) layoutParams).topMargin = DoricUtils.dp2px(topVal.asNumber().toFloat()); + } + JSValue leftVal = jsValue.asObject().getProperty("left"); + if (leftVal.isNumber()) { + ((ViewGroup.MarginLayoutParams) layoutParams).leftMargin = DoricUtils.dp2px(leftVal.asNumber().toFloat()); + } + JSValue rightVal = jsValue.asObject().getProperty("right"); + if (rightVal.isNumber()) { + ((ViewGroup.MarginLayoutParams) layoutParams).rightMargin = DoricUtils.dp2px(rightVal.asNumber().toFloat()); + } + JSValue bottomVal = jsValue.asObject().getProperty("bottom"); + if (bottomVal.isNumber()) { + ((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin = DoricUtils.dp2px(bottomVal.asNumber().toFloat()); + } + } + } +} diff --git a/Android/doric/src/main/java/pub/doric/shader/ViewNode.java b/Android/doric/src/main/java/pub/doric/shader/ViewNode.java index 4f5b83ea..50cc3980 100644 --- a/Android/doric/src/main/java/pub/doric/shader/ViewNode.java +++ b/Android/doric/src/main/java/pub/doric/shader/ViewNode.java @@ -42,7 +42,7 @@ import java.util.LinkedList; public abstract class ViewNode extends DoricContextHolder { protected T mView; int index; - ViewNode mParent; + SuperNode mSuperNode; String mId; private ViewGroup.LayoutParams mLayoutParams; @@ -52,8 +52,8 @@ public abstract class ViewNode extends DoricContextHolder { private DoricLayer doricLayer; - public void setParentNode(ViewNode parentNode) { - mParent = parentNode; + public void setSuperNode(SuperNode parentNode) { + mSuperNode = parentNode; } public View getDoricLayer() { @@ -128,8 +128,8 @@ public abstract class ViewNode extends DoricContextHolder { }); break; case "layoutConfig": - if (prop.isObject() && mParent instanceof GroupNode) { - ((GroupNode) mParent).blendChild(this, prop.asObject()); + if (prop.isObject() && mSuperNode != null) { + mSuperNode.blendChildLayoutConfig(this, prop.asObject()); } break; case "border": @@ -180,7 +180,7 @@ public abstract class ViewNode extends DoricContextHolder { ViewNode viewNode = this; do { ids.push(viewNode.mId); - viewNode = viewNode.mParent; + viewNode = viewNode.mSuperNode; } while (viewNode != null && !(viewNode instanceof RootNode)); return ids.toArray(new String[0]); diff --git a/Android/doric/src/main/java/pub/doric/shader/list/ListAdapter.java b/Android/doric/src/main/java/pub/doric/shader/list/ListAdapter.java index 56059445..88b3cd8f 100644 --- a/Android/doric/src/main/java/pub/doric/shader/list/ListAdapter.java +++ b/Android/doric/src/main/java/pub/doric/shader/list/ListAdapter.java @@ -51,7 +51,7 @@ public class ListAdapter extends RecyclerView.Adapter { +public class ListNode extends SuperNode { private final ListAdapter listAdapter; public ListNode(DoricContext doricContext) { @@ -79,4 +80,9 @@ public class ListNode extends ViewNode { break; } } + + @Override + protected void blendChildLayoutConfig(ViewNode viewNode, JSObject jsObject) { + super.blendChildLayoutConfig(viewNode, jsObject); + } } diff --git a/demo/src/ListDemo.ts b/demo/src/ListDemo.ts index b6b4bdb9..cef9a1be 100644 --- a/demo/src/ListDemo.ts +++ b/demo/src/ListDemo.ts @@ -1,4 +1,4 @@ -import { Group, Panel, List, text, gravity, Color, Stack, LayoutSpec, ListItem, NativeCall } from "doric"; +import { Group, Panel, List, text, gravity, Color, Stack, LayoutSpec, ListItem, NativeCall, listItem } from "doric"; @Entry class ListPanel extends Panel { @@ -9,17 +9,29 @@ class ListPanel extends Panel { heightSpec: LayoutSpec.AT_MOST, } rootView.addChild(list) - list.itemCount = 10 - list.bgColor = Color.parse("#ff00ff") + list.itemCount = 1000 list.renderItem = (idx) => { - const item = new ListItem - item.addChild(text({ - width: 100, - height: 100, + return listItem(text({ + layoutConfig: { + widthSpec: LayoutSpec.AT_MOST, + heightSpec: LayoutSpec.WRAP_CONTENT, + margin: { + left: 10, + right: 10, + top: 10, + bottom: 10, + }, + }, text: `第${idx}行内容`, textAlignment: gravity().center(), - })) - return item + })).also(it => { + it.gravity = gravity().center() + it.bgColor = Color.parse("#fff00f") + it.layoutConfig = { + widthSpec: LayoutSpec.AT_MOST, + heightSpec: LayoutSpec.WRAP_CONTENT, + } + }) } } } \ No newline at end of file