1/* 2 * Copyright (c) 2024-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { Constant } from '../base/Constant'; 17import { Decorator } from '../base/Decorator'; 18import { ArkInstanceFieldRef } from '../base/Ref'; 19import { Stmt } from '../base/Stmt'; 20import { Type } from '../base/Type'; 21import { ArkField } from '../model/ArkField'; 22import { ArkMethod } from '../model/ArkMethod'; 23import { ClassSignature, MethodSignature } from '../model/ArkSignature'; 24 25/** 26 * @category core/graph 27 */ 28export interface ViewTreeNode { 29 /** Component node name */ 30 name: string; 31 /** @deprecated Use {@link attributes} instead. */ 32 stmts: Map<string, [Stmt, (Constant | ArkInstanceFieldRef | MethodSignature)[]]>; 33 /** Component attribute stmts, key is attribute name, value is [Stmt, [Uses Values]]. */ 34 attributes: Map<string, [Stmt, (Constant | ArkInstanceFieldRef | MethodSignature)[]]>; 35 /** Used state values. */ 36 stateValues: Set<ArkField>; 37 /** Node's parent, CustomComponent and root node no parent. */ 38 parent: ViewTreeNode | null; 39 /** Node's children. */ 40 children: ViewTreeNode[]; 41 /** @deprecated Use {@link signature} instead. */ 42 classSignature?: ClassSignature | MethodSignature; 43 /** CustomComponent class signature or Builder method signature. */ 44 signature?: ClassSignature | MethodSignature; 45 46 /** 47 * Custom component value transfer 48 * - key: ArkField, child custom component class stateValue field. 49 * - value: ArkField | ArkMethod, parent component transfer value. 50 * key is BuilderParam, the value is Builder ArkMethod. 51 * Others, the value is parent class stateValue field. 52 */ 53 stateValuesTransfer?: Map<ArkField, ArkField | ArkMethod>; 54 55 /** BuilderParam placeholders ArkField. */ 56 builderParam?: ArkField; 57 58 /** builderParam bind builder method signature. */ 59 builder?: MethodSignature; 60 61 /** 62 * walk node and node's children 63 * @param selector Node selector function, return true skipping the follow-up nodes. 64 * @returns 65 * - true: There are nodes that meet the selector. 66 * - false: does not exist. 67 */ 68 walk(selector: (item: ViewTreeNode) => boolean): boolean; 69 70 /** 71 * Whether the node type is Builder. 72 * @returns true: node is Builder, false others. 73 */ 74 isBuilder(): boolean; 75 76 /** 77 * Whether the node type is custom component. 78 * @returns true: node is custom component, false others. 79 */ 80 isCustomComponent(): boolean; 81} 82 83/** 84 * ArkUI Component Tree 85 * @example 86 * // Component Class get ViewTree 87 * let arkClas: ArkClass = ...; 88 * let viewtree = arkClas.getViewTree(); 89 * 90 * // get viewtree root node 91 * let root: ViewTreeNode = viewtree.getRoot(); 92 * 93 * // get viewtree stateValues Map 94 * let stateValues: Map<ArkField, Set<ViewTreeNode>> = viewtree.getStateValues(); 95 * 96 * // walk all nodes 97 * root.walk((node) => { 98 * // check node is builder 99 * if (node.isBuilder()) { 100 * xx 101 * } 102 * 103 * // check node is sub CustomComponent 104 * if (node.isCustomComponent()) { 105 * xx 106 * } 107 * 108 * if (xxx) { 109 * // Skip the remaining nodes and end the traversal 110 * return true; 111 * } 112 * 113 * return false; 114 * }) 115 * 116 * @category core/graph 117 */ 118export interface ViewTree { 119 /** 120 * @deprecated Use {@link getStateValues} instead. 121 */ 122 isClassField(name: string): boolean; 123 124 /** 125 * @deprecated Use {@link getStateValues} instead. 126 */ 127 getClassFieldType(name: string): Decorator | Type | undefined; 128 129 /** 130 * Map of the component controlled by the state variable 131 * @returns 132 */ 133 getStateValues(): Map<ArkField, Set<ViewTreeNode>>; 134 135 /** 136 * ViewTree root node. 137 * @returns root node 138 */ 139 getRoot(): ViewTreeNode | null; 140} 141