From e7b824130532ecfc88178233a47f1cb402ef3820 Mon Sep 17 00:00:00 2001 From: maxli Date: Thu, 16 Nov 2023 17:47:31 +0800 Subject: [PATCH] feat(android): support fakeBold for text node --- .../uimanager/ControllerUpdateManger.java | 10 +++++++ .../com/tencent/renderer/NativeRenderer.java | 19 ++++++++---- .../tencent/renderer/component/Component.java | 4 +++ .../component/drawable/TextDrawable.java | 8 +++-- .../text/TextComponentController.java | 30 +++++++++++++++++++ .../com/tencent/renderer/node/RenderNode.java | 3 +- 6 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 renderer/native/android/src/main/java/com/tencent/renderer/component/text/TextComponentController.java diff --git a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/uimanager/ControllerUpdateManger.java b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/uimanager/ControllerUpdateManger.java index 954dfc1c328..7beb146bb8d 100644 --- a/renderer/native/android/src/main/java/com/tencent/mtt/hippy/uimanager/ControllerUpdateManger.java +++ b/renderer/native/android/src/main/java/com/tencent/mtt/hippy/uimanager/ControllerUpdateManger.java @@ -31,6 +31,7 @@ import com.tencent.renderer.component.ComponentController; import com.tencent.renderer.component.FlatViewGroup; import com.tencent.renderer.component.image.ImageComponentController; +import com.tencent.renderer.component.text.TextComponentController; import com.tencent.renderer.node.TextRenderNode; import com.tencent.renderer.node.TextVirtualNode; import com.tencent.renderer.utils.MapUtils; @@ -69,6 +70,8 @@ public class ControllerUpdateManger { @Nullable private ImageComponentController mImageComponentController; @Nullable + private TextComponentController mTextComponentController; + @Nullable private T mCustomPropsController; static { @@ -120,6 +123,7 @@ public boolean checkComponentProperty(@NonNull String key) { private static void initComponentPropsMap() { collectMethodHolder(ComponentController.class, sComponentPropsMethodMap); collectMethodHolder(ImageComponentController.class, sComponentPropsMethodMap); + collectMethodHolder(TextComponentController.class, sComponentPropsMethodMap); Method[] methods = TextVirtualNode.class.getMethods(); for (Method method : methods) { HippyControllerProps controllerProps = method @@ -282,6 +286,12 @@ private Object getComponentController(Class cls) { } return mImageComponentController; } + if (cls == TextComponentController.class) { + if (mTextComponentController == null) { + mTextComponentController = new TextComponentController(); + } + return mTextComponentController; + } return null; } diff --git a/renderer/native/android/src/main/java/com/tencent/renderer/NativeRenderer.java b/renderer/native/android/src/main/java/com/tencent/renderer/NativeRenderer.java index 8d598e96c88..3bd489681f0 100644 --- a/renderer/native/android/src/main/java/com/tencent/renderer/NativeRenderer.java +++ b/renderer/native/android/src/main/java/com/tencent/renderer/NativeRenderer.java @@ -501,16 +501,19 @@ public void createNode(final int rootId, @NonNull List nodeList) // If the node has a virtual parent, no need to create corresponding render node, // but need set the node data to the parent, for render node snapshot. createNodeTaskList.add( - () -> mRenderManager.onCreateVirtualNode(rootId, nodeId, pid, nodeIndex, node)); + () -> mRenderManager.onCreateVirtualNode(rootId, nodeId, pid, nodeIndex, + node)); } else { createNodeTaskList.add( - () -> mRenderManager.createNode(rootId, nodeId, nodePid, nodeIndex, className, + () -> mRenderManager.createNode(rootId, nodeId, nodePid, nodeIndex, + className, props)); // Because image and text may be rendered flat, it is not necessary to pre create a view. if (!className.equals(HippyImageViewController.CLASS_NAME) && !className.equals( HippyTextViewController.CLASS_NAME)) { createViewTaskList.add( - () -> mRenderManager.preCreateView(rootId, nodeId, nodePid, className, props)); + () -> mRenderManager.preCreateView(rootId, nodeId, nodePid, className, + props)); } } } @@ -552,8 +555,9 @@ public void updateNode(final int rootId, @NonNull List nodeList) VirtualNode parent = mVirtualNodeManager.checkVirtualParent(rootId, nodeId); if (parent != null) { final int pid = parent.getId(); - taskList.add(() -> mRenderManager.onUpdateVirtualNode(rootId, nodeId, pid, diffProps, - delProps)); + taskList.add( + () -> mRenderManager.onUpdateVirtualNode(rootId, nodeId, pid, diffProps, + delProps)); } else { taskList.add(() -> mRenderManager.updateNode(rootId, nodeId, diffProps, delProps)); } @@ -676,6 +680,8 @@ public void updateEventListener(final int rootId, @NonNull List eventLis throw new NativeRenderException(INVALID_NODE_DATA_ERR, TAG + ": updateEventListener: invalid negative id=" + nodeId); } + LogUtils.d(TAG, + "updateEventListener: id " + nodeId + ", eventProps " + eventProps + "\n "); mVirtualNodeManager.updateEventListener(rootId, nodeId, eventProps); taskList.add(() -> mRenderManager.updateEventListener(rootId, nodeId, eventProps)); } @@ -716,7 +722,8 @@ public void callUIFunction(final int rootId, final int nodeId, final long callba // Because call ui function will not follow with end batch, // can be directly post to the UI thread do execution. UIThreadUtils.runOnUiThread( - () -> mRenderManager.dispatchUIFunction(rootId, nodeId, functionName, params, promise)); + () -> mRenderManager.dispatchUIFunction(rootId, nodeId, functionName, params, + promise)); } @Override diff --git a/renderer/native/android/src/main/java/com/tencent/renderer/component/Component.java b/renderer/native/android/src/main/java/com/tencent/renderer/component/Component.java index ed1ac25baa3..21cfdb818f6 100644 --- a/renderer/native/android/src/main/java/com/tencent/renderer/component/Component.java +++ b/renderer/native/android/src/main/java/com/tencent/renderer/component/Component.java @@ -306,6 +306,10 @@ public TextDrawable ensureTextDrawable() { return mTextDrawable; } + public void setFakeBoldText(boolean isFakeBoldText) { + ensureTextDrawable().setFakeBoldText(isFakeBoldText); + } + public void setGestureEnable(boolean enable) { if (enable) { setComponentFlag(FLAG_GESTURE_ENABLE); diff --git a/renderer/native/android/src/main/java/com/tencent/renderer/component/drawable/TextDrawable.java b/renderer/native/android/src/main/java/com/tencent/renderer/component/drawable/TextDrawable.java index c1fe69fdf0c..b9f33f76fc7 100644 --- a/renderer/native/android/src/main/java/com/tencent/renderer/component/drawable/TextDrawable.java +++ b/renderer/native/android/src/main/java/com/tencent/renderer/component/drawable/TextDrawable.java @@ -49,7 +49,7 @@ public class TextDrawable extends Drawable { - private boolean mTextBold = false; + private boolean mFakeBoldText = false; private int mCustomTextColor = 0; private float mLeftPadding; private float mRightPadding; @@ -117,7 +117,7 @@ public void draw(@NonNull Canvas canvas) { canvas.translate(getTextLayoutOffsetX(), getTextLayoutOffsetY()); Paint paint = mLayout.getPaint(); if (paint != null) { - paint.setFakeBoldText(mTextBold); + paint.setFakeBoldText(mFakeBoldText); } mLayout.draw(canvas); canvas.restore(); @@ -133,6 +133,10 @@ public void setAlpha(int alpha) { } + public void setFakeBoldText(boolean isFakeBoldText) { + mFakeBoldText = isFakeBoldText; + } + @Override public void setColorFilter(ColorFilter colorFilter) { diff --git a/renderer/native/android/src/main/java/com/tencent/renderer/component/text/TextComponentController.java b/renderer/native/android/src/main/java/com/tencent/renderer/component/text/TextComponentController.java new file mode 100644 index 00000000000..bf4220d114b --- /dev/null +++ b/renderer/native/android/src/main/java/com/tencent/renderer/component/text/TextComponentController.java @@ -0,0 +1,30 @@ +/* Tencent is pleased to support the open source community by making Hippy available. + * Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved. + * + * 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 com.tencent.renderer.component.text; + +import androidx.annotation.NonNull; +import com.tencent.mtt.hippy.annotation.HippyControllerProps; +import com.tencent.renderer.component.Component; + +public class TextComponentController { + + @SuppressWarnings("unused") + @HippyControllerProps(name = "fakeBold", defaultType = HippyControllerProps.BOOLEAN) + public void setUrl(@NonNull Component component, boolean isFakeBold) { + component.setFakeBoldText(isFakeBold); + } +} diff --git a/renderer/native/android/src/main/java/com/tencent/renderer/node/RenderNode.java b/renderer/native/android/src/main/java/com/tencent/renderer/node/RenderNode.java index 05a398354a9..b80b54fbc97 100644 --- a/renderer/native/android/src/main/java/com/tencent/renderer/node/RenderNode.java +++ b/renderer/native/android/src/main/java/com/tencent/renderer/node/RenderNode.java @@ -41,6 +41,7 @@ import com.tencent.renderer.component.ComponentController; import com.tencent.renderer.component.image.ImageComponent; import com.tencent.renderer.component.image.ImageComponentController; +import com.tencent.renderer.component.text.TextComponentController; import com.tencent.renderer.utils.DiffUtils; import java.lang.ref.WeakReference; @@ -174,7 +175,7 @@ public NativeRender getNativeRender() { @Nullable public Component ensureComponentIfNeeded(Class cls) { - if (cls == ComponentController.class) { + if (cls == ComponentController.class || cls == TextComponentController.class) { if (mComponent == null) { mComponent = new Component(this); }