Refactor getParentTextRoot and getTextRoot method

This commit is contained in:
magicismight
2017-08-08 15:27:11 +08:00
parent b62c407b41
commit f32800bfbd
2 changed files with 15 additions and 47 deletions
@@ -29,7 +29,6 @@ class GroupShadowNode extends RenderableShadowNode {
@Nullable ReadableMap mFont; @Nullable ReadableMap mFont;
private GlyphContext mGlyphContext; private GlyphContext mGlyphContext;
private GroupShadowNode textRoot;
@ReactProp(name = "font") @ReactProp(name = "font")
public void setFont(@Nullable ReadableMap font) { public void setFont(@Nullable ReadableMap font) {
@@ -47,11 +46,9 @@ class GroupShadowNode extends RenderableShadowNode {
return mGlyphContext; return mGlyphContext;
} }
@SuppressWarnings("ConstantConditions")
GlyphContext getTextRootGlyphContext() { GlyphContext getTextRootGlyphContext() {
if (textRoot == null) { return getTextRoot().getGlyphContext();
textRoot = getTextRoot();
}
return textRoot.getGlyphContext();
} }
void pushGlyphContext() { void pushGlyphContext() {
@@ -58,7 +58,6 @@ abstract class VirtualNode extends LayoutShadowNode {
private SvgViewShadowNode mSvgShadowNode; private SvgViewShadowNode mSvgShadowNode;
private Path mCachedClipPath; private Path mCachedClipPath;
private GroupShadowNode mParentTextRoot;
private GroupShadowNode mTextRoot; private GroupShadowNode mTextRoot;
private float canvasHeight = -1; private float canvasHeight = -1;
private float canvasWidth = -1; private float canvasWidth = -1;
@@ -73,51 +72,12 @@ abstract class VirtualNode extends LayoutShadowNode {
return true; return true;
} }
@android.support.annotation.Nullable
GroupShadowNode getTextRoot() { GroupShadowNode getTextRoot() {
GroupShadowNode shadowNode = getTextRoot(GroupShadowNode.class);
if (shadowNode == null) {
return getTextRoot(TextShadowNode.class);
}
return shadowNode;
}
GroupShadowNode getParentTextRoot() {
GroupShadowNode shadowNode = getParentTextRoot(GroupShadowNode.class);
if (shadowNode == null) {
return getParentTextRoot(TextShadowNode.class);
}
return shadowNode;
}
@android.support.annotation.Nullable
private GroupShadowNode getParentTextRoot(Class shadowNodeClass) {
ReactShadowNode node = this.getParent();
if (mParentTextRoot == null) {
while (node != null) {
if (node.getClass() == shadowNodeClass) {
mParentTextRoot = (GroupShadowNode)node;
break;
}
ReactShadowNode parent = node.getParent();
if (!(parent instanceof VirtualNode)) {
node = null;
} else {
node = parent;
}
}
}
return mParentTextRoot;
}
@android.support.annotation.Nullable
private GroupShadowNode getTextRoot(Class shadowNodeClass) {
VirtualNode node = this; VirtualNode node = this;
if (mTextRoot == null) { if (mTextRoot == null) {
while (node != null) { while (node != null) {
if (node.getClass() == shadowNodeClass) { if (node instanceof GroupShadowNode && ((GroupShadowNode) node).getGlyphContext() != null) {
mTextRoot = (GroupShadowNode)node; mTextRoot = (GroupShadowNode)node;
break; break;
} }
@@ -135,6 +95,17 @@ abstract class VirtualNode extends LayoutShadowNode {
return mTextRoot; return mTextRoot;
} }
@android.support.annotation.Nullable
GroupShadowNode getParentTextRoot() {
ReactShadowNode parent = this.getParent();
if (!(parent instanceof VirtualNode)) {
return null;
} else {
return ((VirtualNode) parent).getTextRoot();
}
}
private double getFontSizeFromContext() { private double getFontSizeFromContext() {
GroupShadowNode root = getTextRoot(); GroupShadowNode root = getTextRoot();
if (root == null) { if (root == null) {