harmony 鸿蒙BuilderNode
BuilderNode
提供能够挂载系统组件的自定义节点BuilderNode。BuilderNode仅可作为叶子节点使用。使用方式参考BuilderNode开发指南。
说明:
本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
如果在跨页面复用BuilderNode时显示异常,可参考跨页面复用注意事项。
当前不支持在预览器中使用BuilderNode。
BuilderNode下的自定义组件支持使用@Prop装饰器,不支持使用@Reusable、@Link、@Provide、@Consume装饰器。
从API version 12开始,自定义组件支持接收LocalStorage实例。可以通过传递LocalStorage实例来使用LocalStorage相关的装饰器@LocalStorageProp、@LocalStorageLink。
其余装饰器行为未定义,不建议使用。
导入模块
import { BuilderNode, RenderOptions, NodeRenderType } from "@kit.ArkUI";
NodeRenderType
节点渲染类型枚举。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
名称 | 值 | 说明 |
---|---|---|
RENDER_TYPE_DISPLAY | 0 | 表示该节点将被显示到屏幕上。 |
RENDER_TYPE_TEXTURE | 1 | 表示该节点将被导出为纹理。 |
说明:
RENDER_TYPE_TEXTURE类型目前仅在BuilderNode持有组件树的根节点为自定义组件时以及XComponentNode中设置生效。
在BuilderNode的情况下,目前在作为根节点的自定义组件中支持纹理导出的有以下组件:Badge、Blank、Button、CanvasGradient、CanvasPattern、CanvasRenderingContext2D、Canvas、CheckboxGroup、Checkbox、Circle、ColumnSplit、Column、ContainerSpan、Counter、DataPanel、Divider、Ellipse、Flex、Gauge、Hyperlink、ImageBitmap、ImageData、Image、Line、LoadingProgress、Marquee、Matrix2D、OffscreenCanvasRenderingContext2D、OffscreenCanvas、Path2D、Path、PatternLock、Polygon、Polyline、Progress、QRCode、Radio、Rating、Rect、RelativeContainer、RowSplit、Row、Shape、Slider、Span、Stack、TextArea、TextClock、TextInput、TextTimer、Text、Toggle、Video(不含全屏播放能力)、Web、XComponent。
从API version 12开始,新增以下组件支持纹理导出:DatePicker、ForEach、Grid、IfElse、LazyForEach、List、Scroll、Swiper、TimePicker、@Component修饰的自定义组件、NodeContainer以及NodeContainer下挂载的FrameNode和RenderNode。
使用方式可参考同层渲染绘制。
RenderOptions
创建BuilderNode时的可选参数。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
名称 | 类型 | 必填 | 说明 |
---|---|---|---|
selfIdealSize | Size | 否 | 节点的理想大小。 |
type | NodeRenderType | 否 | 节点的渲染类型。 |
surfaceId | string | 否 | 纹理接收方的surfaceId。纹理接收方一般为OH_NativeImage。 |
BuilderNode
class BuilderNode<Args extends Object[]>
BuilderNode支持通过无状态的UI方法@Builder生成组件树,并持有组件树的根节点。不支持定义为状态变量。BuilderNode中持有的FrameNode仅用于将该BuilderNode作为子节点挂载到其他FrameNode上。对BuilderNode持有的FrameNode进行属性设置与子节点操作可能会产生未定义行为,因此不建议通过BuilderNode的getFrameNode方法和FrameNode的getRenderNode方法获取RenderNode,并通过RenderNode的接口对其进行属性设置与子节点操作。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
constructor
constructor(uiContext: UIContext, options?: RenderOptions)
当将BuilderNode生成的内容嵌入到其它RenderNode中显示时,即将BuilderNode对应的RenderNode挂载到另一个RenderNode中显示,需要显式指定RenderOptions中的selfIdealSize,否则Builder内的节点默认父组件布局约束为[0,0],即不设置selfIdealSize则认为BuilderNode中子树的根节点大小为[0,0]。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
uiContext | UIContext | 是 | UI上下文,获取方式可参考UIContext获取方法。 |
options | RenderOptions | 否 | BuilderNode的构造可选参数。默认值:undefined。 |
说明 uiContext的入参需要为一个有效的值,即UI上下文正确,如果传入非法值或者未设置,会导致创建失败。
build
build(builder: WrappedBuilder<Args>, arg?: Object): void
依照传入的对象创建组件树,并持有组件树的根节点。无状态的UI方法@Builder最多拥有一个根节点。 支持自定义组件。
说明
@Builder嵌套使用的时候需要保证内外的@Builder方法的入参对象一致。
最外层的@Builder只支持一个入参。
需要操作BuilderNode中的对象时,需要保证其引用不被回收。当BuilderNode对象被虚拟机回收之后,它的FrameNode、RenderNode对象也会与后端节点解引用。即从BuilderNode中获取的FrameNode对象不对应任何一个节点。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
builder | WrappedBuilder<Args> | 是 | 创建对应节点树的时候所需的无状态UI方法@Builder。 |
arg | Object | 否 | builder的入参。当前仅支持一个入参,且入参对象类型与@Builder定义的入参类型保持一致。 |
build12+
build(builder: WrappedBuilder<Args>, arg: Object, options: BuildOptions): void
依照传入的对象创建组件树,并持有组件树的根节点。无状态的UI方法@Builder最多拥有一个根节点。 支持自定义组件。
说明
@Builder进行创建和更新的规格参考@Builder。
最外层的@Builder只支持一个入参。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
builder | WrappedBuilder<Args> | 是 | 创建对应节点树的时候所需的无状态UI方法@Builder。 |
arg | Object | 是 | builder的入参。当前仅支持一个入参,且入参对象类型与@Builder定义的入参类型保持一致。 |
options | BuildOptions | 是 | build的配置参数,判断是否支持@Builder中嵌套@Builder的行为。 |
示例:
import { BuilderNode, NodeContent } from "@kit.ArkUI";
interface ParamsInterface {
text: string;
func: Function;
}
@Builder
function buildTextWithFunc(fun: Function) {
Text(fun())
.fontSize(50)
.fontWeight(FontWeight.Bold)
.margin({ bottom: 36 })
}
@Builder
function buildText(params: ParamsInterface) {
Column() {
Text(params.text)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.margin({ bottom: 36 })
buildTextWithFunc(params.func)
}
}
@Entry
@Component
struct Index {
@State message: string = "HELLO";
private content: NodeContent = new NodeContent();
build() {
Row() {
Column() {
Button('addBuilderNode')
.onClick(() => {
let buildNode = new BuilderNode<[ParamsInterface]>(this.getUIContext());
buildNode.build(wrapBuilder<[ParamsInterface]>(buildText), {
text: this.message, func: () => {
return "FUNCTION";
}
}, { nestingBuilderSupported: true });
this.content.addFrameNode(buildNode.getFrameNode());
buildNode.dispose();
})
ContentSlot(this.content)
}
.id("column")
.width('100%')
.height('100%')
}
.height('100%')
}
}
BuildOptions12+
build的可选参数。
系统能力: SystemCapability.ArkUI.ArkUI.Full
名称 | 类型 | 必填 | 说明 |
---|---|---|---|
nestingBuilderSupported | boolean | 否 | 是否支持Builder嵌套Builder进行使用。其中,false表示Builder使用的入参一致,true表示Builder使用的入参不一致。默认值:false 原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。 |
localStorage20+ | LocalStorage | 否 | 给当前builderNode设置localStorage,挂载在此builderNode下的自定义组件共享该localStorage,如果自定义组件构造函数同时也传入localStorage,优先使用构造函数中传入的localStorage。默认值:null 原子化服务API: 从API version 20开始,该接口支持在原子化服务 |
InputEventType20+
type InputEventType = TouchEvent|MouseEvent|AxisEvent
postInputEvent的参数,定义用于透传的输入事件类型。
原子化服务API: 从API version 20开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
类型 | 说明 |
---|---|
TouchEvent | 触摸事件。 |
MouseEvent | 鼠标事件。 |
AxisEvent | 轴事件。 |
getFrameNode
getFrameNode(): FrameNode|null
获取BuilderNode中的FrameNode。在BuilderNode执行build操作之后,才会生成FrameNode。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
返回值:
类型 | 说明 |
---|---|
FrameNode |null | 一个FrameNode对象。若该BuilderNode不包含FrameNode,则返回空对象null。 |
示例1:
BuilderNode作为NodeContainer的根节点返回。
import { NodeController, BuilderNode, FrameNode, UIContext } from "@kit.ArkUI";
class Params {
text: string = "";
constructor(text: string) {
this.text = text;
}
}
@Builder
function buildText(params: Params) {
Column() {
Text(params.text)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.margin({bottom: 36})
}
}
class TextNodeController extends NodeController {
private textNode: BuilderNode<[Params]>|null = null;
private message: string = "DEFAULT";
constructor(message: string) {
super();
this.message = message;
}
makeNode(context: UIContext): FrameNode|null {
this.textNode = new BuilderNode(context);
this.textNode.build(wrapBuilder<[Params]>(buildText), new Params(this.message));
return this.textNode.getFrameNode();
}
}
@Entry
@Component
struct Index {
@State message: string = "hello";
build() {
Row() {
Column() {
NodeContainer(new TextNodeController(this.message))
.width('100%')
.height(100)
.backgroundColor('#FFF0F0F0')
}
.width('100%')
.height('100%')
}
.height('100%')
}
}
示例2:
BuilderNode的FrameNode挂到其它FrameNode下。
import { NodeController, BuilderNode, FrameNode, UIContext } from "@kit.ArkUI";
class Params {
text: string = "";
constructor(text: string) {
this.text = text;
}
}
@Builder
function buildText(params: Params) {
Column() {
Text(params.text)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.margin({ bottom: 36 })
}
}
class TextNodeController extends NodeController {
private rootNode: FrameNode|null = null;
private textNode: BuilderNode<[Params]>|null = null;
private message: string = "DEFAULT";
constructor(message: string) {
super();
this.message = message;
}
makeNode(context: UIContext): FrameNode|null {
this.rootNode = new FrameNode(context);
this.textNode = new BuilderNode(context, { selfIdealSize: { width: 150, height: 150 } });
this.textNode.build(wrapBuilder<[Params]>(buildText), new Params(this.message));
if (this.rootNode !== null) {
this.rootNode.appendChild(this.textNode?.getFrameNode());
}
return this.rootNode;
}
}
@Entry
@Component
struct Index {
@State message: string = "hello";
build() {
Row() {
Column() {
NodeContainer(new TextNodeController(this.message))
.width('100%')
.height(100)
.backgroundColor('#FFF0F0F0')
}
.width('100%')
.height('100%')
}
.height('100%')
}
}
示例3:
BuilderNode的RenderNode挂到其它RenderNode下。由于RenderNode不传递布局约束,不推荐通过该方式挂载节点。
import { NodeController, BuilderNode, FrameNode, UIContext, RenderNode } from "@kit.ArkUI";
class Params {
text: string = "";
constructor(text: string) {
this.text = text;
}
}
@Builder
function buildText(params: Params) {
Column() {
Text(params.text)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.margin({ bottom: 36 })
}
}
class TextNodeController extends NodeController {
private rootNode: FrameNode|null = null;
private textNode: BuilderNode<[Params]>|null = null;
private message: string = "DEFAULT";
constructor(message: string) {
super();
this.message = message;
}
makeNode(context: UIContext): FrameNode|null {
this.rootNode = new FrameNode(context);
let renderNode = new RenderNode();
renderNode.clipToFrame = false;
this.textNode = new BuilderNode(context, { selfIdealSize: { width: 150, height: 150 } });
this.textNode.build(wrapBuilder<[Params]>(buildText), new Params(this.message));
const textRenderNode = this.textNode?.getFrameNode()?.getRenderNode();
const rootRenderNode = this.rootNode.getRenderNode();
if (rootRenderNode !== null) {
rootRenderNode.appendChild(renderNode);
renderNode.appendChild(textRenderNode);
}
return this.rootNode;
}
}
@Entry
@Component
struct Index {
@State message: string = "hello";
build() {
Row() {
Column() {
NodeContainer(new TextNodeController(this.message))
.width('100%')
.height(100)
.backgroundColor('#FFF0F0F0')
}
.width('100%')
.height('100%')
}
.height('100%')
}
}
update
update(arg: Object): void
根据提供的参数更新BuilderNode,该参数为build方法调用时传入的参数类型相同。对自定义组件进行update的时候需要在自定义组件中使用的变量定义为@Prop类型。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
arg | Object | 是 | 用于更新BuilderNode的参数,和build调用时传入的参数类型一致。 |
示例:
import { NodeController, BuilderNode, FrameNode, UIContext } from "@kit.ArkUI";
class Params {
text: string = "";
constructor(text: string) {
this.text = text;
}
}
// 自定义组件
@Component
struct TextBuilder {
@Prop message: string = "TextBuilder";
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.margin({bottom: 36})
.backgroundColor(Color.Gray)
}
}
}
}
@Builder
function buildText(params: Params) {
Column() {
Text(params.text)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.margin({ bottom: 36 })
TextBuilder({message: params.text}) // 自定义组件
}
}
class TextNodeController extends NodeController {
private rootNode: FrameNode|null = null;
private textNode: BuilderNode<[Params]>|null = null;
private message: string = "";
constructor(message: string) {
super();
this.message = message;
}
makeNode(context: UIContext): FrameNode|null {
this.textNode = new BuilderNode(context);
this.textNode.build(wrapBuilder<[Params]>(buildText), new Params(this.message));
return this.textNode.getFrameNode();
}
update(message: string) {
if (this.textNode !== null) {
this.textNode.update(new Params(message));
}
}
}
@Entry
@Component
struct Index {
@State message: string = "hello";
private textNodeController: TextNodeController = new TextNodeController(this.message);
private count = 0;
build() {
Row() {
Column() {
NodeContainer(this.textNodeController)
.width('100%')
.height(200)
.backgroundColor('#FFF0F0F0')
Button('Update')
.onClick(() => {
this.count += 1;
const message = "Update " + this.count.toString();
this.textNodeController.update(message);
})
}
.width('100%')
.height('100%')
}
.height('100%')
}
}
postTouchEvent
postTouchEvent(event: TouchEvent): boolean
将原始事件派发到某个BuilderNode创建出的FrameNode上。
postTouchEvent是从组件树的中间节点往下分发,需要变换到父组件坐标系才能分发成功,参考下图。
OffsetA为buildNode相对于父组件的偏移量,可以通过FrameNode中的getPositionToParent获取。OffsetB为point点相对于buildNode的偏移量,可以通过TouchEvent 获取。OffsetC为OffsetA与OffsetB的和,是传给postTouchEvent的最终结果。
说明:
传入的坐标值需要转换为px,如果builderNode有仿射变换,则需要再叠加仿射变换。
在webview中,内部已经处理过坐标系变换,可以将TouchEvent事件直接下发。
同一时间戳,postTouchEvent只能调用一次。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
event | TouchEvent | 是 | 触摸事件。 |
返回值:
类型 | 说明 |
---|---|
boolean | 派发事件是否成功。true为已命中响应事件的组件,false为未命中任何可响应事件的组件。 说明: 如果未按照预期命中组件,需要确认以下几点: 1.坐标系是否转换正确。 2.组件是否可交互状态。 3.是否绑定事件。 |
示例:
import { NodeController, BuilderNode, FrameNode, UIContext } from '@kit.ArkUI';
class Params {
text: string = "this is a text";
}
@Builder
function ButtonBuilder(params: Params) {
Column() {
Button(`button ` + params.text)
.borderWidth(2)
.backgroundColor(Color.Orange)
.width("100%")
.height("100%")
.gesture(
TapGesture()
.onAction((event: GestureEvent) => {
console.log("TapGesture");
})
)
}
.width(500)
.height(300)
.backgroundColor(Color.Gray)
}
class MyNodeController extends NodeController {
private rootNode: BuilderNode<[Params]>|null = null;
private wrapBuilder: WrappedBuilder<[Params]> = wrapBuilder(ButtonBuilder);
makeNode(uiContext: UIContext): FrameNode|null {
this.rootNode = new BuilderNode(uiContext);
this.rootNode.build(this.wrapBuilder, { text: "this is a string" });
return this.rootNode.getFrameNode();
}
// 坐标转换示例
postTouchEvent(event: TouchEvent, uiContext: UIContext): boolean {
if (this.rootNode == null) {
return false;
}
let node: FrameNode|null = this.rootNode.getFrameNode();
let offsetX: number|null|undefined = node?.getPositionToParent().x;
let offsetY: number|null|undefined = node?.getPositionToParent().y;
let changedTouchLen = event.changedTouches.length;
for (let i = 0; i < changedTouchLen; i++) {
if (offsetX != null && offsetY != null && offsetX != undefined && offsetY != undefined) {
event.changedTouches[i].x = uiContext.vp2px(offsetX + event.changedTouches[i].x);
event.changedTouches[i].y = uiContext.vp2px(offsetY + event.changedTouches[i].y);
}
}
let result = this.rootNode.postTouchEvent(event);
console.log("result " + result);
return result;
}
}
@Entry
@Component
struct MyComponent {
private nodeController: MyNodeController = new MyNodeController();
build() {
Column() {
NodeContainer(this.nodeController)
.height(300)
.width(500)
Column()
.width(500)
.height(300)
.backgroundColor(Color.Pink)
.onTouch((event) => {
if (event != undefined) {
this.nodeController.postTouchEvent(event, this.getUIContext());
}
})
}
}
}
dispose12+
dispose(): void
立即释放当前BuilderNode对象对实体节点的引用关系。关于BuilderNode的解绑场景请参见节点解绑。
说明:
当BuilderNode对象调用dispose之后,会与后端实体节点解除引用关系。若前端对象BuilderNode无法释放,容易导致内存泄漏。建议在不再需要对该BuilderNode对象进行操作时,开发者主动调用dispose释放后端节点,以减少引用关系的复杂性,降低内存泄漏的风险。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
import { RenderNode, FrameNode, NodeController, BuilderNode } from "@kit.ArkUI";
@Component
struct TestComponent {
build() {
Column() {
Text('This is a BuilderNode.')
.fontSize(16)
.fontWeight(FontWeight.Bold)
}
.width('100%')
.backgroundColor(Color.Gray)
}
aboutToAppear() {
console.error('aboutToAppear');
}
aboutToDisappear() {
console.error('aboutToDisappear');
}
}
@Builder
function buildComponent() {
TestComponent()
}
class MyNodeController extends NodeController {
private rootNode: FrameNode|null = null;
private builderNode: BuilderNode<[]>|null = null;
makeNode(uiContext: UIContext): FrameNode|null {
this.rootNode = new FrameNode(uiContext);
this.builderNode = new BuilderNode(uiContext, { selfIdealSize: { width: 200, height: 100 } });
this.builderNode.build(new WrappedBuilder(buildComponent));
const rootRenderNode = this.rootNode!.getRenderNode();
if (rootRenderNode !== null) {
rootRenderNode.size = { width: 200, height: 200 };
rootRenderNode.backgroundColor = 0xff00ff00;
rootRenderNode.appendChild(this.builderNode!.getFrameNode()!.getRenderNode());
}
return this.rootNode;
}
dispose() {
if (this.builderNode !== null) {
this.builderNode.dispose();
}
}
removeBuilderNode() {
const rootRenderNode = this.rootNode!.getRenderNode();
if (rootRenderNode !== null && this.builderNode !== null && this.builderNode.getFrameNode() !== null) {
rootRenderNode.removeChild(this.builderNode!.getFrameNode()!.getRenderNode());
}
}
}
@Entry
@Component
struct Index {
private myNodeController: MyNodeController = new MyNodeController();
build() {
Column({ space: 4 }) {
NodeContainer(this.myNodeController)
Button('BuilderNode dispose')
.onClick(() => {
this.myNodeController.removeBuilderNode();
this.myNodeController.dispose();
})
.width('100%')
}
}
}
reuse12+
reuse(param?: Object): void
触发BuilderNode中的自定义组件的复用。组件复用请参见@Reusable装饰器:组件复用。关于BuilderNode的解绑场景请参见节点解绑。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
param | Object | 否 | 用于复用BuilderNode的参数,和build调用时传入的参数类型一致。 |
recycle12+
recycle(): void
- 触发BuilderNode中自定义组件的回收。自定义组件的回收是组件复用机制中的环节,具体信息请参见@Reusable装饰器:组件复用。
- BuilderNode通过reuse和recycle完成其内外自定义组件之间的复用事件传递,具体使用场景请参见节点复用能力。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
import { FrameNode, NodeController, BuilderNode, UIContext } from "@kit.ArkUI";
const TEST_TAG: string = "Reuse+Recycle";
class MyDataSource {
private dataArray: string[] = [];
private listener: DataChangeListener|null = null;
public totalCount(): number {
return this.dataArray.length;
}
public getData(index: number) {
return this.dataArray[index];
}
public pushData(data: string) {
this.dataArray.push(data);
}
public reloadListener(): void {
this.listener?.onDataReloaded();
}
public registerDataChangeListener(listener: DataChangeListener): void {
this.listener = listener;
}
public unregisterDataChangeListener(): void {
this.listener = null;
}
}
class Params {
item: string = '';
constructor(item: string) {
this.item = item;
}
}
@Builder
function buildNode(param: Params = new Params("hello")) {
Row() {
Text(`C${param.item} -- `)
ReusableChildComponent2({ item: param.item }) //该自定义组件在BuilderNode中无法被正确复用
}
}
class MyNodeController extends NodeController {
public builderNode: BuilderNode<[Params]>|null = null;
public item: string = "";
makeNode(uiContext: UIContext): FrameNode|null {
if (this.builderNode == null) {
this.builderNode = new BuilderNode(uiContext, { selfIdealSize: { width: 300, height: 200 } });
this.builderNode.build(wrapBuilder<[Params]>(buildNode), new Params(this.item));
}
return this.builderNode.getFrameNode();
}
}
// 被回收复用的自定义组件,其状态变量会更新,而子自定义组件ReusableChildComponent3中的状态变量也会更新,但BuilderNode会阻断这一传递过程
@Reusable
@Component
struct ReusableChildComponent {
@Prop item: string = '';
@Prop switch: string = '';
private controller: MyNodeController = new MyNodeController();
aboutToAppear() {
this.controller.item = this.item;
}
aboutToRecycle(): void {
console.log(`${TEST_TAG} ReusableChildComponent aboutToRecycle ${this.item}`);
// 当开关为open,通过BuilderNode的reuse接口和recycle接口传递给其下的自定义组件,例如ReusableChildComponent2,完成复用
if (this.switch === 'open') {
this.controller?.builderNode?.recycle();
}
}
aboutToReuse(params: object): void {
console.log(`${TEST_TAG} ReusableChildComponent aboutToReuse ${JSON.stringify(params)}`);
// 当开关为open,通过BuilderNode的reuse接口和recycle接口传递给其下的自定义组件,例如ReusableChildComponent2,完成复用
if (this.switch === 'open') {
this.controller?.builderNode?.reuse(params);
}
}
build() {
Row() {
Text(`A${this.item}--`)
ReusableChildComponent3({ item: this.item })
NodeContainer(this.controller);
}
}
}
@Component
struct ReusableChildComponent2 {
@Prop item: string = "false";
aboutToReuse(params: Record<string, object>) {
console.log(`${TEST_TAG} ReusableChildComponent2 aboutToReuse ${JSON.stringify(params)}`);
}
aboutToRecycle(): void {
console.log(`${TEST_TAG} ReusableChildComponent2 aboutToRecycle ${this.item}`);
}
build() {
Row() {
Text(`D${this.item}`)
.fontSize(20)
.backgroundColor(Color.Yellow)
.margin({ left: 10 })
}.margin({ left: 10, right: 10 })
}
}
@Component
struct ReusableChildComponent3 {
@Prop item: string = "false";
aboutToReuse(params: Record<string, object>) {
console.log(`${TEST_TAG} ReusableChildComponent3 aboutToReuse ${JSON.stringify(params)}`);
}
aboutToRecycle(): void {
console.log(`${TEST_TAG} ReusableChildComponent3 aboutToRecycle ${this.item}`);
}
build() {
Row() {
Text(`B${this.item}`)
.fontSize(20)
.backgroundColor(Color.Yellow)
.margin({ left: 10 })
}.margin({ left: 10, right: 10 })
}
}
@Entry
@Component
struct Index {
@State data: MyDataSource = new MyDataSource();
aboutToAppear() {
for (let i = 0; i < 100; i++) {
this.data.pushData(i.toString());
}
}
build() {
Column() {
List({ space: 3 }) {
LazyForEach(this.data, (item: string) => {
ListItem() {
ReusableChildComponent({
item: item,
switch: 'open' // 将open改为close可观察到,BuilderNode不通过reuse和recycle接口传递复用时,BuilderNode内部的自定义组件的行为表现
})
}
}, (item: string) => item)
}
.width('100%')
.height('100%')
}
}
}
updateConfiguration12+
updateConfiguration(): void
传递系统环境变化事件,触发节点的全量更新。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
说明:
updateConfiguration接口用于通知对象更新,更新所使用的系统环境由应用当前的系统环境变化决定。
示例:
import { NodeController, BuilderNode, FrameNode, UIContext } from "@kit.ArkUI";
import { AbilityConstant, Configuration, ConfigurationConstant, EnvironmentCallback } from '@kit.AbilityKit';
class Params {
text: string = "";
constructor(text: string) {
this.text = text;
}
}
// 自定义组件
@Component
struct TextBuilder {
// 作为自定义组件中需要更新的属性,数据类型为基础属性,定义为@Prop
@Prop message: string = "TextBuilder";
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.margin({ bottom: 36 })
}
}
}
}
@Builder
function buildText(params: Params) {
Column() {
Text(params.text)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.margin({ bottom: 36 })
TextBuilder({ message: params.text }) // 自定义组件
}.backgroundColor($r('sys.color.ohos_id_color_background'))
}
class TextNodeController extends NodeController {
private textNode: BuilderNode<[Params]>|null = null;
private message: string = "";
constructor(message: string) {
super();
this.message = message;
}
makeNode(context: UIContext): FrameNode|null {
return this.textNode?.getFrameNode() ? this.textNode?.getFrameNode() : null;
}
createNode(context: UIContext) {
this.textNode = new BuilderNode(context);
this.textNode.build(wrapBuilder<[Params]>(buildText), new Params(this.message));
builderNodeMap.push(this.textNode);
}
deleteNode() {
let node = builderNodeMap.pop();
node?.dispose();
}
update(message: string) {
if (this.textNode !== null) {
// 调用update进行更新。
this.textNode.update(new Params(message));
}
}
}
// 记录创建的自定义节点对象
const builderNodeMap: Array<BuilderNode<[Params]>> = new Array();
function updateColorMode() {
builderNodeMap.forEach((value, index) => {
// 通知BuilderNode环境变量改变
value.updateConfiguration();
})
}
@Entry
@Component
struct Index {
@State message: string = "hello";
private textNodeController: TextNodeController = new TextNodeController(this.message);
private count = 0;
aboutToAppear(): void {
let environmentCallback: EnvironmentCallback = {
onMemoryLevel: (level: AbilityConstant.MemoryLevel): void => {
console.log('onMemoryLevel');
},
onConfigurationUpdated: (config: Configuration): void => {
console.log('onConfigurationUpdated ' + JSON.stringify(config));
updateColorMode();
}
};
// 注册监听回调
this.getUIContext().getHostContext()?.getApplicationContext().on('environment', environmentCallback);
// 设置应用深浅色跟随系统
this.getUIContext()
.getHostContext()?.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET);
//创建自定义节点并添加至map
this.textNodeController.createNode(this.getUIContext());
}
aboutToDisappear(): void {
//移除map中的引用,并将自定义节点释放
this.textNodeController.deleteNode();
}
build() {
Row() {
Column() {
NodeContainer(this.textNodeController)
.width('100%')
.height(200)
.backgroundColor('#FFF0F0F0')
Button('Update')
.onClick(() => {
this.count += 1;
const message = "Update " + this.count.toString();
this.textNodeController.update(message);
})
Button('切换深色')
.onClick(() => {
this.getUIContext()
.getHostContext()?.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_DARK);
})
Button('设置浅色')
.onClick(() => {
this.getUIContext()
.getHostContext()?.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT);
})
}
.width('100%')
.height('100%')
}
.height('100%')
}
}
isDisposed20+
isDisposed(): boolean
查询当前BuilderNode对象是否已解除与后端实体节点的引用关系。前端节点均绑定有相应的后端实体节点,当节点调用dispose接口解除绑定后,再次调用接口可能会出现crash、返回默认值的情况。由于业务需求,可能存在节点在dispose后仍被调用接口的情况。为此,提供此接口以供开发者在操作节点前检查其有效性,避免潜在风险。
原子化服务API: 从API version 20开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
返回值:
类型 | 说明 |
---|---|
boolean | 后端实体节点是否解除引用。true为节点已与后端实体节点解除引用,false为节点未与后端实体节点解除引用。 |
postInputEvent20+
postInputEvent(event: InputEventType): boolean
将事件分发到目标节点。
offsetA为builderNode相对于父组件的偏移,offsetB为命中位置相对于builderNode的偏移,offsetC为offsetA+offsetB,最终输入给postInputEvent中的window信息。
说明:
传入的坐标值需要转换为px,坐标转换示例可以参考下面示例代码。
鼠标左键点击事件会转换成触摸事件,转发时需注意不要在外层同时绑定触摸事件和鼠标事件,规格可查看onTouch。
注入事件为轴事件时,由于轴事件中缺少旋转轴信息与捏合轴信息,因此注入的事件无法触发pinch捏合手势与rotate旋转手势。
转发的事件会在被分发到的目标组件所在的子树里做touchtest,并触发对应手势,原始事件也会触发当前组件所在组件树中的手势。不保证两类手势的竞争结果。
如果是开发者构造的事件,必填字段必须赋值,比如触摸事件的touches字段,轴事件的scrollStep字段。要保证事件的完整,比如触摸事件的TouchTypeDOWN和UP都要有,防止出现未定义行为。
Webview已经处理过坐标系变换,可以将事件直接下发。
postTouchEvent接口需要提供手势坐标相对于post事件对端内的局部坐标,postInputEvent接口需要提供手势坐标相对于post事件对端内的窗口坐标。
不建议同一个事件转发多次。不支持UIExtensionComponent。
原子化服务API: 从API version 20开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
event | InputEventType | 是 | 用于透传的输入事件。 |
返回值:
类型 | 说明 |
---|---|
boolean | 事件是否被消费。true表示事件已被消费,false表示事件未被消费。 |
示例
示例1(鼠标事件)
该示例演示了在自定义组件中截获鼠标事件并进行坐标转换的完整流程。组件通过onMouse回调读取本地x/y,再结合FrameNode.getPositionToParent()得到的偏移量,调用vp2px将相对坐标转换为像素坐标,更新MouseEvent的windowX/windowY、displayX/displayY。最后通过rootNode.postInputEvent(event)将转换后的鼠标事件分发给子节点进行处理。
import { NodeController, BuilderNode, FrameNode, UIContext, PromptAction } from '@kit.ArkUI';
import { InputEventType } from '@ohos.arkui.node';
class Params {
text: string = "this is a text"
uiContext: UIContext|null = null
}
@Builder
function ButtonBuilder(params: Params) {
Column() {
Button(params.text)
.borderWidth(2)
.align(Alignment.Center)
.backgroundColor(Color.Orange)
.fontSize(20)
.width("45%")
.height("30%")
.offset({x: 100, y: 100})
.onMouse((event) => {
let promptAction: PromptAction = params.uiContext!.getPromptAction();
promptAction.showToast({
message: 'onMouse',
duration: 3000
});
console.log('onMouse')
})
}
.width(500)
.height(300)
.backgroundColor(Color.Gray)
}
class MyNodeController extends NodeController {
private rootNode: BuilderNode<[Params]>|null = null;
private wrapBuilder: WrappedBuilder<[Params]> = wrapBuilder(ButtonBuilder);
makeNode(uiContext: UIContext): FrameNode|null {
this.rootNode = new BuilderNode(uiContext);
this.rootNode.build(this.wrapBuilder, { text: "This is a string", uiContext })
return this.rootNode.getFrameNode();
}
postInputEvent(event: InputEventType, uiContext: UIContext): boolean {
if (this.rootNode == null) {
return false;
}
let node: FrameNode|null = this.rootNode.getFrameNode();
let offsetX: number|null|undefined = node?.getPositionToParent().x;
let offsetY: number|null|undefined = node?.getPositionToParent().y;
if (event.source == SourceType.Mouse) {
let mouseEvent = event as MouseEvent;
if (offsetX != null && offsetY != null && offsetX != undefined && offsetY != undefined) {
mouseEvent.windowX = uiContext.vp2px(offsetX + mouseEvent.x)
mouseEvent.windowY = uiContext.vp2px(offsetY + mouseEvent.y)
mouseEvent.displayX = uiContext.vp2px(offsetX + mouseEvent.x)
mouseEvent.displayY = uiContext.vp2px(offsetY + mouseEvent.y)
mouseEvent.x = uiContext.vp2px(mouseEvent.x)
mouseEvent.y = uiContext.vp2px(mouseEvent.y)
}
}
let result = this.rootNode.postInputEvent(event);
return result;
}
}
@Entry
@Component
struct MyComponent {
private nodeController: MyNodeController = new MyNodeController();
build() {
Stack() {
NodeContainer(this.nodeController)
.height(300)
.width(500)
Column()
.width(500)
.height(300)
.backgroundColor(Color.Transparent)
.onMouse((event) => {
if (event != undefined) {
this.nodeController.postInputEvent(event, this.getUIContext());
}
})
}.offset({top: 100})
}
}
示例2(触摸事件)
该示例演示了在自定义组件中截获触摸事件并对触点坐标进行转换的完整流程。在onTouch回调中,遍历TouchEvent的changedTouches和touches数组,对每个触点的x/y加上组件偏移量并调用vp2px转换为像素,更新各自的windowX/windowY、displayX/displayY。最后同样通过rootNode.postInputEvent(event)将转换后的触摸事件分发给子节点处理。
import { NodeController, BuilderNode, FrameNode, UIContext, PromptAction } from '@kit.ArkUI';
import { InputEventType } from '@ohos.arkui.node';
class Params {
text: string = "this is a text"
uiContext: UIContext|null = null
}
@Builder
function ButtonBuilder(params: Params) {
Column() {
Button(params.text)
.borderWidth(2)
.align(Alignment.Center)
.backgroundColor(Color.Orange)
.fontSize(20)
.width("45%")
.height("30%")
.offset({x: 100, y: 100})
.onTouch((event) => {
let promptAction: PromptAction = params.uiContext!.getPromptAction();
promptAction.showToast({
message: 'onTouch',
duration: 3000
});
console.log('onTouch')
})
}
.width(500)
.height(300)
.backgroundColor(Color.Gray)
}
class MyNodeController extends NodeController {
private rootNode: BuilderNode<[Params]>|null = null;
private wrapBuilder: WrappedBuilder<[Params]> = wrapBuilder(ButtonBuilder);
makeNode(uiContext: UIContext): FrameNode|null {
this.rootNode = new BuilderNode(uiContext);
this.rootNode.build(this.wrapBuilder, { text: "This is a string", uiContext })
return this.rootNode.getFrameNode();
}
postInputEvent(event: InputEventType, uiContext: UIContext): boolean {
if (this.rootNode == null) {
return false;
}
let node: FrameNode|null = this.rootNode.getFrameNode();
let offsetX: number|null|undefined = node?.getPositionToParent().x;
let offsetY: number|null|undefined = node?.getPositionToParent().y;
// 只转发原始事件,不转发鼠标模拟的触摸事件
if (event.source == SourceType.TouchScreen) {
let touchevent = event as TouchEvent;
let changedTouchLen = touchevent.changedTouches.length;
for (let i = 0; i < changedTouchLen; i++) {
if (offsetX != null && offsetY != null && offsetX != undefined && offsetY != undefined) {
touchevent.changedTouches[i].windowX = uiContext.vp2px(offsetX + touchevent.changedTouches[i].x);
touchevent.changedTouches[i].windowY = uiContext.vp2px(offsetY + touchevent.changedTouches[i].y);
touchevent.changedTouches[i].displayX = uiContext.vp2px(offsetX + touchevent.changedTouches[i].x);
touchevent.changedTouches[i].displayY = uiContext.vp2px(offsetY + touchevent.changedTouches[i].y);
}
}
let touchesLen = touchevent.touches.length;
for (let i = 0; i < touchesLen; i++) {
if (offsetX != null && offsetY != null && offsetX != undefined && offsetY != undefined) {
touchevent.touches[i].windowX = uiContext.vp2px(offsetX + touchevent.touches[i].x);
touchevent.touches[i].windowY = uiContext.vp2px(offsetY + touchevent.touches[i].y);
touchevent.touches[i].displayX = uiContext.vp2px(offsetX + touchevent.touches[i].x);
touchevent.touches[i].displayY = uiContext.vp2px(offsetY + touchevent.touches[i].y);
}
}
}
let result = this.rootNode.postInputEvent(event);
return result;
}
}
@Entry
@Component
struct MyComponent {
private nodeController: MyNodeController = new MyNodeController();
build() {
Stack() {
NodeContainer(this.nodeController)
.height(300)
.width(500)
Column()
.width(500)
.height(300)
.backgroundColor(Color.Transparent)
.onTouch((event) => {
if (event != undefined) {
this.nodeController.postInputEvent(event, this.getUIContext());
}
})
}.offset({top: 100})
}
}
示例3(轴事件)
该示例演示了在自定义组件中截获滚轮或触控板轴事件并进行坐标转换的完整流程。在onAxisEvent回调中,先获取事件的相对x/y,再加上组件偏移量后调用vp2px转换为像素,更新AxisEvent的windowX/windowY、displayX/displayY,最后通过rootNode.postInputEvent(event)将转换后的轴事件分发给子节点进行处理。
import { NodeController, BuilderNode, FrameNode, UIContext, PromptAction } from '@kit.ArkUI';
import { InputEventType } from '@ohos.arkui.node';
class Params {
text: string = "this is a text"
uiContext: UIContext|null = null
}
@Builder
function ButtonBuilder(params: Params) {
Column() {
Button(params.text)
.borderWidth(2)
.align(Alignment.Center)
.backgroundColor(Color.Orange)
.fontSize(20)
.width("45%")
.height("30%")
.offset({x: 100, y: 100})
.onAxisEvent((event) => {
let promptAction: PromptAction = params.uiContext!.getPromptAction();
promptAction.showToast({
message: 'onAxisEvent',
duration: 3000
});
console.log('onAxisEvent')
})
}
.width(500)
.height(300)
.backgroundColor(Color.Gray)
}
class MyNodeController extends NodeController {
private rootNode: BuilderNode<[Params]>|null = null;
private wrapBuilder: WrappedBuilder<[Params]> = wrapBuilder(ButtonBuilder);
makeNode(uiContext: UIContext): FrameNode|null {
this.rootNode = new BuilderNode(uiContext);
this.rootNode.build(this.wrapBuilder, { text: "This is a string", uiContext })
return this.rootNode.getFrameNode();
}
postInputEvent(event: InputEventType, uiContext: UIContext): boolean {
if (this.rootNode == null) {
return false;
}
let node: FrameNode|null = this.rootNode.getFrameNode();
let offsetX: number|null|undefined = node?.getPositionToParent().x;
let offsetY: number|null|undefined = node?.getPositionToParent().y;
let axiseEvent = event as AxisEvent;
if (offsetX != null && offsetY != null && offsetX != undefined && offsetY != undefined) {
axiseEvent.windowX = uiContext.vp2px(offsetX + axiseEvent.x)
axiseEvent.windowY = uiContext.vp2px(offsetY + axiseEvent.y)
axiseEvent.displayX = uiContext.vp2px(offsetX + axiseEvent.x)
axiseEvent.displayY = uiContext.vp2px(offsetY + axiseEvent.y)
axiseEvent.x = uiContext.vp2px(axiseEvent.x)
axiseEvent.y = uiContext.vp2px(axiseEvent.y)
}
let result = this.rootNode.postInputEvent(event);
return result;
}
}
@Entry
@Component
struct MyComponent {
private nodeController: MyNodeController = new MyNodeController();
build() {
Stack() {
NodeContainer(this.nodeController)
.height(300)
.width(500)
Column()
.width(500)
.height(300)
.backgroundColor(Color.Transparent)
.onAxisEvent((event) => {
if (event != undefined) {
this.nodeController.postInputEvent(event, this.getUIContext());
}
})
}.offset({top: 100})
}
}
示例4(BuilderNode共享localStorage)
该示例演示了如何在BuilderNode通过build方法传入外部localStorage,此时挂载在BuilderNode的所有自定义组件共享该localStorage。
let globalBuilderNode: BuilderNode<[Params]>|null = null;
@Builder
function buildText(params: Params) {
Column() {
Text(params.text)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.margin({bottom: 36})
Test()
}
}
let localStorage: LocalStorage = new LocalStorage();
localStorage.setOrCreate('PropA', 'PropA');
class TextNodeController extends NodeController {
private rootNode: FrameNode|null = null;
makeNode(context: UIContext): FrameNode|null {
this.rootNode = new FrameNode(context);
if (globalBuilderNode === null) {
globalBuilderNode = new BuilderNode(context);
globalBuilderNode.build(wrapBuilder<[Params]>(buildText), new Params('builder node text'), { localStorage: localStorage })
}
this.rootNode.appendChild(globalBuilderNode.getFrameNode());
return this.rootNode;
}
}
@Entry(localStorage)
@Component
struct Index {
private controller: TextNodeController = new TextNodeController();
@LocalStorageLink('PropA') PropA: string = 'Hello World';
build() {
Row() {
Column() {
Text(this.PropA)
NodeContainer(this.controller)
}
.width('100%')
.height('100%')
}
.height('100%')
}
}
示例5(检验BuilderNode是否有效)
该示例演示了BuilderNode释放节点前后分别使用isDisposed接口验证节点的状态,释放节点前节点调用isDisposed接口返回true,释放节点后节点调用isDisposed接口返回false。
import { RenderNode, FrameNode, NodeController, BuilderNode } from "@kit.ArkUI";
@Component
struct TestComponent {
build() {
Column() {
Text('This is a BuilderNode.')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}
.width('100%')
.height(30)
.backgroundColor(Color.Gray)
}
aboutToAppear() {
console.error('aboutToAppear');
}
aboutToDisappear() {
console.error('aboutToDisappear');
}
}
@Builder
function buildComponent() {
TestComponent()
}
class MyNodeController extends NodeController {
private rootNode: FrameNode|null = null;
private builderNode: BuilderNode<[]>|null = null;
makeNode(uiContext: UIContext): FrameNode|null {
this.rootNode = new FrameNode(uiContext);
this.builderNode = new BuilderNode(uiContext, { selfIdealSize: { width: 200, height: 100 } });
this.builderNode.build(new WrappedBuilder(buildComponent));
const rootRenderNode = this.rootNode!.getRenderNode();
if (rootRenderNode !== null) {
rootRenderNode.size = { width: 300, height: 300 };
rootRenderNode.backgroundColor = 0xffd5d5d5;
rootRenderNode.appendChild(this.builderNode!.getFrameNode()!.getRenderNode());
}
return this.rootNode;
}
dispose() {
if (this.builderNode !== null) {
this.builderNode.dispose();
}
}
isDisposed() : string{
if (this.builderNode !== null) {
if (this.builderNode.isDisposed()) {
return 'builderNode isDisposed is true';
}
else {
return 'builderNode isDisposed is false';
}
}
return 'builderNode is null';
}
removeBuilderNode() {
const rootRenderNode = this.rootNode!.getRenderNode();
if (rootRenderNode !== null && this.builderNode !== null && this.builderNode.getFrameNode() !== null) {
rootRenderNode.removeChild(this.builderNode!.getFrameNode()!.getRenderNode());
}
}
}
@Entry
@Component
struct Index {
@State text: string = ''
private myNodeController: MyNodeController = new MyNodeController();
build() {
Column({ space: 4 }) {
NodeContainer(this.myNodeController)
Button('BuilderNode dispose')
.onClick(() => {
this.myNodeController.removeBuilderNode();
this.myNodeController.dispose();
this.text = '';
})
.width(200)
.height(50)
Button('BuilderNode isDisposed')
.onClick(() => {
this.text = this.myNodeController.isDisposed();
})
.width(200)
.height(50)
Text(this.text)
.fontSize(25)
}
.width('100%')
.height('100%')
}
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙ARKUI_TextPickerCascadeRangeContent
harmony 鸿蒙ARKUI_TextPickerRangeContent
harmony 鸿蒙ArkUI_AnimateCompleteCallback
harmony 鸿蒙ArkUI_ContextCallback
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦