fix add child to group,not make root dirty

This commit is contained in:
pengfei.zhou 2019-07-24 12:48:30 +08:00
parent ac5dc091d5
commit f33c320b0c
7 changed files with 97 additions and 76 deletions

View File

@ -1,7 +1,6 @@
package com.github.penfeizhou.doric.shader; package com.github.penfeizhou.doric.shader;
import android.util.SparseArray; import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import com.github.penfeizhou.doric.DoricContext; import com.github.penfeizhou.doric.DoricContext;
@ -26,9 +25,11 @@ public abstract class GroupNode<F extends ViewGroup> extends ViewNode<F> {
} }
@Override @Override
public void blend(JSObject jsObject, ViewGroup.LayoutParams layoutParams) { protected void blend(F view, ViewGroup.LayoutParams layoutParams, String name, JSValue prop) {
super.blend(jsObject, layoutParams); super.blend(view, layoutParams, name, prop);
JSArray jsArray = jsObject.getProperty("children").asArray(); switch (name) {
case "children":
JSArray jsArray = prop.asArray();
int i; int i;
for (i = 0; i < jsArray.size(); i++) { for (i = 0; i < jsArray.size(); i++) {
JSValue jsValue = jsArray.get(i); JSValue jsValue = jsArray.get(i);
@ -67,9 +68,16 @@ public abstract class GroupNode<F extends ViewGroup> extends ViewNode<F> {
mIndexInfo.remove(i); mIndexInfo.remove(i);
} }
} }
break;
default:
super.blend(view, layoutParams, name, prop);
break;
}
} }
protected ViewGroup.LayoutParams generateDefaultLayoutParams() { protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
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);
} }

View File

@ -19,6 +19,14 @@ public class LinearNode extends GroupNode<LinearLayout> {
super(doricContext); super(doricContext);
} }
@Override
public void blendChild(ViewNode viewNode, JSObject layoutConfig) {
JSValue jsValue = layoutConfig.getProperty("alignment");
if (jsValue.isNumber()) {
((LinearLayout.LayoutParams) viewNode.getLayoutParams()).gravity = jsValue.asNumber().toInt();
}
}
@Override @Override
public LinearLayout build(JSObject jsObject) { public LinearLayout build(JSObject jsObject) {
return new LinearLayout(getContext()); return new LinearLayout(getContext());

View File

@ -12,16 +12,11 @@ import com.github.pengfeizhou.jscore.JSObject;
* @CreateDate: 2019-07-20 * @CreateDate: 2019-07-20
*/ */
@DoricPlugin(name = "Root") @DoricPlugin(name = "Root")
public class RootNode extends GroupNode<FrameLayout> { public class RootNode extends StackNode {
public RootNode(DoricContext doricContext) { public RootNode(DoricContext doricContext) {
super(doricContext); super(doricContext);
} }
@Override
public FrameLayout build(JSObject jsObject) {
return new FrameLayout(getContext());
}
public void setRootView(FrameLayout rootView) { public void setRootView(FrameLayout rootView) {
this.mView = rootView; this.mView = rootView;
} }

View File

@ -19,6 +19,14 @@ public class StackNode extends GroupNode<FrameLayout> {
super(doricContext); super(doricContext);
} }
@Override
public void blendChild(ViewNode viewNode, JSObject jsObject) {
JSValue jsValue = jsObject.getProperty("alignment");
if (jsValue.isNumber()) {
((FrameLayout.LayoutParams) viewNode.getLayoutParams()).gravity = jsValue.asNumber().toInt();
}
}
@Override @Override
public FrameLayout build(JSObject jsObject) { public FrameLayout build(JSObject jsObject) {
return new FrameLayout(getContext()); return new FrameLayout(getContext());
@ -34,4 +42,9 @@ public class StackNode extends GroupNode<FrameLayout> {
super.blend(view, layoutParams, name, prop); super.blend(view, layoutParams, name, prop);
} }
} }
@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
} }

View File

@ -25,8 +25,9 @@ import java.util.LinkedList;
public abstract class ViewNode<T extends View> extends DoricComponent { public abstract class ViewNode<T extends View> extends DoricComponent {
protected T mView; protected T mView;
int index; int index;
ViewNode<ViewGroup> mParent; GroupNode mParent;
String mId; String mId;
private ViewGroup.LayoutParams mLayoutParams;
public ViewNode(DoricContext doricContext) { public ViewNode(DoricContext doricContext) {
super(doricContext); super(doricContext);
@ -43,6 +44,7 @@ public abstract class ViewNode<T extends View> extends DoricComponent {
public abstract T build(JSObject jsObject); public abstract T build(JSObject jsObject);
void blend(JSObject jsObject, ViewGroup.LayoutParams layoutParams) { void blend(JSObject jsObject, ViewGroup.LayoutParams layoutParams) {
mLayoutParams = layoutParams;
if (mView == null) { if (mView == null) {
mView = build(jsObject); mView = build(jsObject);
} }
@ -93,14 +95,8 @@ public abstract class ViewNode<T extends View> extends DoricComponent {
}); });
break; break;
case "layoutConfig": case "layoutConfig":
JSObject layoutConfig = prop.asObject(); if (prop.isObject() && mParent != null) {
JSValue jsValue = layoutConfig.getProperty("alignment"); mParent.blendChild(this, prop.asObject());
if (jsValue.isNumber()) {
if (layoutParams instanceof LinearLayout.LayoutParams) {
((LinearLayout.LayoutParams) layoutParams).gravity = jsValue.asNumber().toInt();
} else if (layoutParams instanceof FrameLayout.LayoutParams) {
((FrameLayout.LayoutParams) layoutParams).gravity = jsValue.asNumber().toInt();
}
} }
break; break;
default: default:
@ -136,10 +132,7 @@ public abstract class ViewNode<T extends View> extends DoricComponent {
} }
public ViewGroup.LayoutParams getLayoutParams() { public ViewGroup.LayoutParams getLayoutParams() {
if (mView != null) { return mLayoutParams;
return mView.getLayoutParams();
}
return null;
} }
public String getId() { public String getId() {

View File

@ -1,4 +1,5 @@
import { Gravity, Mutable, NativeCall, Text, Color, VLayout, Panel, log, logw, loge, Group, Stack, } from "./index" import { Gravity, Mutable, NativeCall, Text, Color, VLayout, Panel, log, logw, loge, Group, Stack, } from "./index"
import { WRAP_CONTENT } from "./src/ui/view";
@Entry @Entry
@ -7,14 +8,16 @@ export class MyPage extends Panel {
build(rootView: Group): void { build(rootView: Group): void {
const state = Mutable.of(1) const state = Mutable.of(1)
const numberView = new Text const numberView = new Text
numberView.width = 100 numberView.width = WRAP_CONTENT
numberView.height = 200 numberView.height = WRAP_CONTENT
numberView.top = 50 numberView.top = 50
state.bind((v) => { state.bind((v) => {
numberView.text = v.toString() numberView.text = v.toString()
}) })
numberView.textSize = 40 numberView.textSize = 40
numberView.centerX = rootView.width / 2 numberView.layoutConfig = {
alignment: new Gravity().centerX()
}
rootView.addChild(numberView) rootView.addChild(numberView)
const click = new Text const click = new Text
click.textSize = 20 click.textSize = 20
@ -22,7 +25,7 @@ export class MyPage extends Panel {
click.onClick = () => { click.onClick = () => {
state.set(state.get() + 1) state.set(state.get() + 1)
} }
click.top = numberView.bottom + 20 click.top = 200
click.layoutConfig = { click.layoutConfig = {
alignment: new Gravity().centerX() alignment: new Gravity().centerX()
@ -33,24 +36,17 @@ export class MyPage extends Panel {
const vlayout = new VLayout const vlayout = new VLayout
vlayout.width = this.getRootView().width vlayout.width = this.getRootView().width
vlayout.height = 500 vlayout.height = 500
vlayout.bgColor = Color.parse('#ff00ff')
vlayout.top = 50 vlayout.top = 50
vlayout.centerX = this.getRootView().width / 2 vlayout.centerX = this.getRootView().width / 2
vlayout.space = 0 vlayout.space = 0
vlayout.gravity = (new Gravity()).bottom() vlayout.gravity = (new Gravity()).bottom()
const v = [1, 2, 3,].map(e => { vlayout.onClick = () => {
const stack = new Stack const stack = new Stack
stack.width = stack.height = 50 stack.width = stack.height = 50
stack.bgColor = Color.safeParse('#00ff00') stack.bgColor = Color.safeParse('#00ff00')
vlayout.addChild(stack) vlayout.addChild(stack)
stack.onClick = () => {
loge('stack:onClick')
if (vlayout.space !== undefined) {
loge('change space')
vlayout.space += 10
} }
}
})
rootView.addChild(vlayout) rootView.addChild(vlayout)
} }

View File

@ -140,7 +140,7 @@ export abstract class View implements Modeling {
} }
this.__dirty_props__[propKey] = newV this.__dirty_props__[propKey] = newV
if (this.parent instanceof Group) { if (this.parent instanceof Group) {
this.parent.onChildPropertyChanged(this, propKey, oldV, newV) this.parent.onChildPropertyChanged(this)
} }
} }
@ -220,6 +220,10 @@ export abstract class Group extends View {
const childrenModel = this.getDirtyChildrenModel() const childrenModel = this.getDirtyChildrenModel()
childrenModel[parseInt(index)] = value.nativeViewModel childrenModel[parseInt(index)] = value.nativeViewModel
} }
if (this.parent) {
this.parent.onChildPropertyChanged(this)
}
return Reflect.set(target, index, value) return Reflect.set(target, index, value)
} }
}) })
@ -246,9 +250,13 @@ export abstract class Group extends View {
return super.toModel() return super.toModel()
} }
onChildPropertyChanged(child: View, propKey: string, oldV: Model, newV: Model) { onChildPropertyChanged(child: View) {
this.getDirtyChildrenModel()[this.children.indexOf(child)] = child.nativeViewModel this.getDirtyChildrenModel()[this.children.indexOf(child)] = child.nativeViewModel
} }
isDirty() {
return super.isDirty()
}
} }
export class Stack extends Group { export class Stack extends Group {