• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Inspecting Page Layouts
2
3You can use the Inspector to examine page layouts. Its bidirectional positioning feature helps quickly locate components, modify attributes, and debug components in DevEco Studio, thereby improving development efficiency.
4
5ArkUI obtains information about all components in the currently displayed page, including the parent-child structure of the component tree, size, position, styles, attributes, and states. After obtaining the component tree information, it generates and displays it as an Inspector component tree. For details about the usage in DevEco Studio, see [Using ArkUI Inspector](ui-inspector-profiler.md#using-arkui-inspector).
6
7## Constraints
8
91. The real-time update functionality is not supported for animation-type components in the component tree.
10
112. Component attributes and styles can be obtained, but **Controller** and **Builder** objects cannot.
12
133. Component methods and events cannot be obtained.
14
15## Querying Component Tree and Component Information Using UIContext
16
17ArkUI provides the @ohos.arkui.UIContext (UIContext) extension capability. Use [getFilteredInspectorTree](../reference/apis-arkui/arkts-apis-uicontext-uicontext.md#getfilteredinspectortree12) to obtain the component tree and component attributes, and [getFilteredInspectorTreeById](../reference/apis-arkui/arkts-apis-uicontext-uicontext.md#getfilteredinspectortreebyid12) to obtain attributes of specified components and their child components. Querying with filter conditions is supported.
18
19The following example demonstrates the basic usage of **getFilteredInspectorTree** and **getFilteredInspectorTreeById**.
20
21```ts
22import { UIContext } from '@kit.ArkUI';
23@Entry
24@Component
25struct ComponentPage {
26  loopConsole(inspectorStr: string, i: string) {
27    console.log(`InsTree ${i}| type: ${JSON.parse(inspectorStr).$type}, ID: ${JSON.parse(inspectorStr).$ID}`);
28    if (JSON.parse(inspectorStr).$children) {
29      i += '-';
30      for (let index = 0; index < JSON.parse(inspectorStr).$children.length; index++) {
31        this.loopConsole(JSON.stringify(JSON.parse(inspectorStr).$children[index]), i);
32      }
33    }
34  }
35
36  build() {
37    Column() {
38      Text("Hello World")
39        .fontSize(20)
40        .id("TEXT")
41      Button('content').onClick(() => {
42        const uiContext: UIContext = this.getUIContext();
43        let inspectorStr = uiContext.getFilteredInspectorTree(['content']);
44        console.log(`InsTree : ${inspectorStr}`);
45        inspectorStr = JSON.stringify(JSON.parse(inspectorStr));
46        this.loopConsole(inspectorStr, '-');
47      })
48      Button('isLayoutInspector').onClick(() => {
49        const uiContext: UIContext = this.getUIContext();
50        let inspectorStr = uiContext.getFilteredInspectorTree(['isLayoutInspector']);
51        console.log(`InsTree : ${inspectorStr}`);
52        inspectorStr = JSON.stringify(JSON.parse(inspectorStr).content);
53        this.loopConsole(inspectorStr, '-');
54      })
55      Button('getFilteredInspectorTreeById').onClick(() => {
56        const uiContext: UIContext = this.getUIContext();
57        try {
58          let inspectorStr = uiContext.getFilteredInspectorTreeById('TEXT', 1, ["id", "src"]);
59          console.info(`result1: ${inspectorStr}`);
60          inspectorStr = JSON.stringify(JSON.parse(inspectorStr)['$children'][0]);
61          console.info(`result2: ${inspectorStr}`);
62          inspectorStr = uiContext.getFilteredInspectorTreeById('TEXT', 1, ["src"]);
63          inspectorStr = JSON.stringify(JSON.parse(inspectorStr)['$children'][0]);
64          console.info(`result3: ${inspectorStr}`);
65        } catch(e) {
66          console.info(`getFilteredInspectorTreeById error: ${e}`);
67        }
68      })
69
70    }
71    .width('100%')
72    .height('100%')
73  }
74}
75```
76
77## Using Layout Callbacks
78
79The [@ohos.arkui.inspector (Layout Callback)](../reference/apis-arkui/js-apis-arkui-inspector.md) module provides APIs for registering the component layout and drawing completion callbacks.
80
81The following example demonstrates the basic usage of layout callbacks.
82
83```ts
84import { inspector } from '@kit.ArkUI'
85
86@Entry
87@Component
88struct ImageExample {
89  build() {
90    Column() {
91      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start }) {
92        Row({ space: 5 }) {
93          Image($r('app.media.app_icon'))
94            .width(110)
95            .height(110)
96            .border({ width: 1 })
97            .id('IMAGE_ID')
98        }
99      }
100    }.height(320).width(360).padding({ right: 10, top: 10 })
101  }
102
103  listener:inspector.ComponentObserver = this.getUIContext().getUIInspector().createComponentObserver('IMAGE_ID')
104
105  aboutToAppear() {
106    let onLayoutComplete:()=>void=():void=>{
107      // Add functionality to be implemented.
108    }
109    let onDrawComplete:()=>void=():void=>{
110      // Add functionality to be implemented.
111    }
112    let onDrawChildrenComplete:()=>void=():void=>{
113      // Add functionality to be implemented.
114    }
115    let FuncLayout = onLayoutComplete // Bind to the current JS object.
116    let FuncDraw = onDrawComplete // Bind to the current JS object.
117    let FuncDrawChildren = onDrawChildrenComplete // Bind to the current JS object.
118    let OffFuncLayout = onLayoutComplete // Bind to the current JS object.
119    let OffFuncDraw = onDrawComplete // Bind to the current JS object.
120    let OffFuncDrawChildren = onDrawChildrenComplete // Bind to the current JS object.
121
122    this.listener.on('layout', FuncLayout)
123    this.listener.on('draw', FuncDraw)
124    this.listener.on('drawChildren', FuncDrawChildren)
125
126    // Unregister callbacks through the handle. You should decide when to call these APIs.
127    // this.listener.off('layout', OffFuncLayout)
128    // this.listener.off('draw', OffFuncDraw)
129    // this.listener.off('drawChildren', OffFuncDrawChildren)
130  }
131}
132```
133
134## Using the Extended Capabilities for Component Identification Attributes
135
136The following APIs provide extended capabilities for component identification attributes:
137- [getInspectorByKey](../reference/apis-arkui/arkui-ts/ts-universal-attributes-component-id.md#getinspectorbykey9): obtains all attributes of the component with the specified ID.
138- [getInspectorTree](../reference/apis-arkui/arkui-ts/ts-universal-attributes-component-id.md#getinspectortree9): obtains the component tree with component attributes.
139- [sendEventByKey](../reference/apis-arkui/arkui-ts/ts-universal-attributes-component-id.md#sendeventbykey9): sends an event to the component with the specified ID.
140
141The following example demonstrates the basic usage of **getInspectorByKey**, **getInspectorTree**, and **sendEventByKey**.
142
143```ts
144@Entry
145@Component
146struct ComponentPage {
147  build() {
148    Column() {
149      Text("Hello World")
150        .fontSize(20)
151        .id("TEXT")
152        .onClick(() => {
153          console.info(`Text is clicked`);
154        })
155      Button('getInspectorByKey').onClick(() => {
156        let result = getInspectorByKey("TEXT");
157        console.info(`result is ${result}`);
158      })
159      Button('getInspectorTree').onClick(() => {
160        let result = getInspectorTree();
161        console.info(`result is ${JSON.stringify(result)}`);
162      })
163      Button('sendEventByKey').onClick(() => {
164        sendEventByKey("TEXT", 10, "");
165      })
166    }
167    .width('100%')
168    .height('100%')
169  }
170}
171```
172<!--no_check-->