layoutconfig support margin

This commit is contained in:
pengfei.zhou 2019-07-24 19:58:30 +08:00
parent cfde85b773
commit d180c6df22
10 changed files with 85 additions and 13 deletions

View File

@ -4,6 +4,7 @@ import android.util.SparseArray;
import android.view.ViewGroup; import android.view.ViewGroup;
import com.github.penfeizhou.doric.DoricContext; import com.github.penfeizhou.doric.DoricContext;
import com.github.penfeizhou.doric.utils.DoricUtils;
import com.github.pengfeizhou.jscore.JSArray; import com.github.pengfeizhou.jscore.JSArray;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
@ -79,5 +80,26 @@ public abstract class GroupNode<F extends ViewGroup> extends ViewNode<F> {
return new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); return new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
} }
public abstract void blendChild(ViewNode viewNode, JSObject jsObject); protected void blendChild(ViewNode viewNode, JSObject jsObject) {
JSValue jsValue = jsObject.getProperty("margin");
ViewGroup.LayoutParams layoutParams = viewNode.getLayoutParams();
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());
}
}
}
} }

View File

@ -18,7 +18,7 @@ public class HLayoutNode extends LinearNode {
} }
@Override @Override
public LinearLayout build(JSObject jsObject) { protected LinearLayout build(JSObject jsObject) {
LinearLayout linearLayout = super.build(jsObject); LinearLayout linearLayout = super.build(jsObject);
linearLayout.setOrientation(LinearLayout.HORIZONTAL); linearLayout.setOrientation(LinearLayout.HORIZONTAL);
return linearLayout; return linearLayout;

View File

@ -18,7 +18,7 @@ public class ImageNode extends ViewNode<ImageView> {
} }
@Override @Override
public ImageView build(JSObject jsObject) { protected ImageView build(JSObject jsObject) {
return null; return null;
} }
} }

View File

@ -20,11 +20,16 @@ public class LinearNode extends GroupNode<LinearLayout> {
} }
@Override @Override
public void blendChild(ViewNode viewNode, JSObject layoutConfig) { protected void blendChild(ViewNode viewNode, JSObject layoutConfig) {
super.blendChild(viewNode, layoutConfig);
JSValue jsValue = layoutConfig.getProperty("alignment"); JSValue jsValue = layoutConfig.getProperty("alignment");
if (jsValue.isNumber()) { if (jsValue.isNumber()) {
((LinearLayout.LayoutParams) viewNode.getLayoutParams()).gravity = jsValue.asNumber().toInt(); ((LinearLayout.LayoutParams) viewNode.getLayoutParams()).gravity = jsValue.asNumber().toInt();
} }
JSValue weight = layoutConfig.getProperty("weight");
if (weight.isNumber()) {
((LinearLayout.LayoutParams) viewNode.getLayoutParams()).weight = weight.asNumber().toInt();
}
} }
@Override @Override
@ -33,7 +38,7 @@ public class LinearNode extends GroupNode<LinearLayout> {
} }
@Override @Override
public LinearLayout build(JSObject jsObject) { protected LinearLayout build(JSObject jsObject) {
return new LinearLayout(getContext()); return new LinearLayout(getContext());
} }

View File

@ -20,7 +20,8 @@ public class StackNode extends GroupNode<FrameLayout> {
} }
@Override @Override
public void blendChild(ViewNode viewNode, JSObject jsObject) { protected void blendChild(ViewNode viewNode, JSObject jsObject) {
super.blendChild(viewNode, jsObject);
JSValue jsValue = jsObject.getProperty("alignment"); JSValue jsValue = jsObject.getProperty("alignment");
if (jsValue.isNumber()) { if (jsValue.isNumber()) {
((FrameLayout.LayoutParams) viewNode.getLayoutParams()).gravity = jsValue.asNumber().toInt(); ((FrameLayout.LayoutParams) viewNode.getLayoutParams()).gravity = jsValue.asNumber().toInt();
@ -28,7 +29,7 @@ public class StackNode extends GroupNode<FrameLayout> {
} }
@Override @Override
public FrameLayout build(JSObject jsObject) { protected FrameLayout build(JSObject jsObject) {
return new FrameLayout(getContext()); return new FrameLayout(getContext());
} }

View File

@ -21,7 +21,7 @@ public class TextNode extends ViewNode<TextView> {
} }
@Override @Override
public TextView build(JSObject jsObject) { protected TextView build(JSObject jsObject) {
return new TextView(getContext()); return new TextView(getContext());
} }

View File

@ -18,7 +18,7 @@ public class VLayoutNode extends LinearNode {
} }
@Override @Override
public LinearLayout build(JSObject jsObject) { protected LinearLayout build(JSObject jsObject) {
LinearLayout linearLayout = super.build(jsObject); LinearLayout linearLayout = super.build(jsObject);
linearLayout.setOrientation(LinearLayout.VERTICAL); linearLayout.setOrientation(LinearLayout.VERTICAL);
return linearLayout; return linearLayout;

View File

@ -41,7 +41,7 @@ public abstract class ViewNode<T extends View> extends DoricComponent {
return getDoricContext().getContext(); return getDoricContext().getContext();
} }
public abstract T build(JSObject jsObject); protected abstract T build(JSObject jsObject);
void blend(JSObject jsObject, ViewGroup.LayoutParams layoutParams) { void blend(JSObject jsObject, ViewGroup.LayoutParams layoutParams) {
mLayoutParams = layoutParams; mLayoutParams = layoutParams;

View File

@ -1,4 +1,4 @@
import { ViewHolder, VMPanel, View, ViewModel, WRAP_CONTENT, Gravity, Mutable, NativeCall, Text, Color, VLayout, Panel, log, logw, loge, Group, Stack, } from "./index" import { StackConfig, ViewHolder, VMPanel, View, ViewModel, WRAP_CONTENT, Gravity, Mutable, NativeCall, Text, Color, VLayout, Panel, log, logw, loge, Group, Stack, } from "./index"
interface CountModel { interface CountModel {
count: number count: number
@ -45,7 +45,6 @@ class CounterVM extends ViewModel<CountModel, CounterView> {
} }
@Entry
class MyPage extends VMPanel<CountModel, CounterView>{ class MyPage extends VMPanel<CountModel, CounterView>{
getVMClass() { getVMClass() {
@ -73,3 +72,48 @@ class MyPage extends VMPanel<CountModel, CounterView>{
loge("Hello.HEGO") loge("Hello.HEGO")
} }
} }
class Snake {
}
class SnakeView extends ViewHolder {
build(root: Group): void {
root.bgColor = Color.parse('#000000')
const title = new Text
title.text = "Snake"
title.textSize = 20
title.textColor = Color.parse("#ffffff")
title.layoutConfig = {
alignment: new Gravity().centerX().top(),
margin: {
top: 20
}
} as StackConfig
root.addChild(title)
}
}
class SnakeVM extends ViewModel<Snake, SnakeView>{
binding(v: SnakeView, model: Snake) {
}
}
@Entry
class SnakePanel extends VMPanel<Snake, SnakeView>{
getVMClass() {
return SnakeVM
}
getModel() {
return new Snake
}
getViewHolder() {
return new SnakeView
}
}

View File

@ -65,7 +65,7 @@ export abstract class ViewModel<M extends Object, V extends ViewHolder> {
build(root: Group) { build(root: Group) {
this.viewHolder.build(root) this.viewHolder.build(root)
this.bind((data: M) => { this.bind((data: M) => {
this.binding(this.viewHolder, this.model) this.binding(this.viewHolder, data)
}) })
} }