1# @ohos.arkui.UIContext (UIContext) (System API) 2 3In the stage model, a window stage or window can use the **loadContent** API to load pages, create a UI instance, and render page content to the associated window. Naturally, UI instances and windows are associated on a one-by-one basis. Some global UI APIs are executed in the context of certain UI instances. When calling these APIs, you must identify the UI context, and consequently UI instance, by tracing the call chain. If these APIs are called on a non-UI page or in some asynchronous callback, the current UI context may fail to be identified, resulting in API execution errors. 4 5**@ohos.window** adds the [getUIContext](./js-apis-window.md#getuicontext10) API in API version 10 for obtaining the **UIContext** object of a UI instance. The API provided by the **UIContext** object can be directly applied to the corresponding UI instance. 6 7> **NOTE** 8> 9> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 10> 11> You can preview how this component looks on a real device, but not in DevEco Studio Previewer. 12> 13> This topic describes only system APIs provided by the module. For details about its public APIs, see [@ohos.arkui.UIContext (UIContext)](js-apis-arkui-UIContext.md). 14 15## UIContext 16 17In the following API examples, you must first use [getUIContext()](./js-apis-window.md#getuicontext10) in **@ohos.window** to obtain a **UIContext** instance, and then call the APIs using the obtained instance. In this document, the **UIContext** instance is represented by **uiContext**. 18 19### setDynamicDimming<sup>12+<sup> 20 21setDynamicDimming(id: string, value: number): void 22 23Sets the dynamic dimming degree of the component. 24 25 26> **NOTE** 27> 28> Applying other visual effects after this API is called may result in conflicts. 29 30**System capability**: SystemCapability.ArkUI.ArkUI.Full 31 32**Parameters** 33 34| Name| Type| Mandatory| Description| 35| ------- | ------- | ------- | ------- | 36| id | string | Yes| Component ID.| 37| value | number | Yes| Dynamic dimming degree of the component. The value range is [0, 1]. The component is brighter with a larger value.| 38 39**Example** 40 41```ts 42@Entry 43@Component 44struct Index { 45 @State 46 myCount : number = 100 47 48 build() { 49 Column(){ 50 Image($r('app.media.testImage')).width(500).height(800).id("test") 51 }.width("100%").height("100%").onClick(()=>{ 52 this.getUIContext().setDynamicDimming("test",1) 53 animateTo({duration:5000 },()=>{ 54 this.getUIContext().setDynamicDimming("test",0) 55 }) 56 }) 57 } 58} 59``` 60 61 62### animateToImmediately<sup>12+</sup> 63 64animateToImmediately(param: AnimateParam , event: () => void): void 65 66Implements immediate delivery of an explicit animation through a **UIContext** object. When multiple property animations are loaded at once, you can call this API to immediately execute the transition animation for state changes caused by the specified closure function. 67 68**Atomic service API**: This API can be used in atomic services since API version 12. 69 70**System capability**: SystemCapability.ArkUI.ArkUI.Full 71 72**Parameters** 73 74| Name | Type | Mandatory | Description | 75| ----- | ---------------------------------------- | ---- | ------------------------------------- | 76| param | [AnimateParam](arkui-ts/ts-explicit-animation.md#animateparam) | Yes | Animation settings. | 77| event | () => void | Yes | Closure function that displays the animation. The system automatically inserts the transition animation if the state changes in the closure function.| 78 79**Example** 80 81This example shows how to use **animateToImmediately** to implement immediate delivery of an explicit animation through a **UIContext** object. 82 83```ts 84// xxx.ets 85@Entry 86@Component 87struct AnimateToImmediatelyExample { 88 @State widthSize: number = 250 89 @State heightSize: number = 100 90 @State opacitySize: number = 0 91 private flag: boolean = true 92 uiContext: UIContext | null | undefined = this.getUIContext(); 93 94 build() { 95 Column() { 96 Column() 97 .width(this.widthSize) 98 .height(this.heightSize) 99 .backgroundColor(Color.Green) 100 .opacity(this.opacitySize) 101 Button('change size') 102 .margin(30) 103 .onClick(() => { 104 if (this.flag) { 105 this.uiContext?.animateToImmediately({ 106 delay: 0, 107 duration: 1000 108 }, () => { 109 this.opacitySize = 1 110 }) 111 this.uiContext?.animateTo({ 112 delay: 1000, 113 duration: 1000 114 }, () => { 115 this.widthSize = 150 116 this.heightSize = 60 117 }) 118 } else { 119 this.uiContext?.animateToImmediately({ 120 delay: 0, 121 duration: 1000 122 }, () => { 123 this.widthSize = 250 124 this.heightSize = 100 125 }) 126 this.uiContext?.animateTo({ 127 delay: 1000, 128 duration: 1000 129 }, () => { 130 this.opacitySize = 0 131 }) 132 } 133 this.flag = !this.flag 134 }) 135 }.width('100%').margin({ top: 5 }) 136 } 137} 138``` 139 140 141### freezeUINode<sup>18+</sup> 142 143freezeUINode(id: string, isFrozen: boolean): void 144 145Sets whether to freeze a specific component by **id** to prevent it from marking itself as dirty and triggering layout updates. 146 147**Atomic service API**: This API can be used in atomic services since API version 18. 148 149**System capability**: SystemCapability.ArkUI.ArkUI.Full 150 151**Parameters** 152 153| Name | Type | Mandatory | Description | 154| --- | --- | --- | --- | 155| id | string | Yes| ID of the target component.| 156| isFrozen | boolean | Yes| Whether to freeze the component.<br>Default value: **false**| 157 158**Error codes** 159 160For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 161 162| ID| Error Message| 163| -------- | -------- | 164| 202 | The caller is not a system application. | 165 166```js 167@Entry 168@Component 169struct Index { 170 @State columnWidth1: string = '100%'; 171 @State currentIndex: number = 0; 172 private controller: TabsController = new TabsController(); 173 174 build() { 175 Column() { 176 Tabs({ 177 barPosition: BarPosition.Start, 178 index: this.currentIndex, 179 controller: this.controller 180 }) { 181 TabContent() { 182 Column() 183 .width(this.columnWidth1) 184 .height('100%') 185 .backgroundColor('#00CB87') 186 } 187 .tabBar('green') 188 .id('tab1') 189 .onWillHide(() => { 190 this.getUIContext().freezeUINode('tab1', true); 191 }) 192 .onWillShow(() => { 193 this.getUIContext().freezeUINode('tab1', false); 194 }) 195 196 TabContent() { 197 Column() 198 .width('100%') 199 .height('100%') 200 .backgroundColor('#007DFF') 201 } 202 .tabBar('blue') 203 .id('tab2') 204 .onWillHide(() => { 205 this.getUIContext().freezeUINode('tab2', true); 206 }) 207 .onWillShow(() => { 208 this.getUIContext().freezeUINode('tab1', true); 209 this.columnWidth1 = '50%'; 210 setTimeout(() => { 211 this.getUIContext().freezeUINode('tab1', false); 212 this.columnWidth1 = '20%'; 213 }, 5000) 214 }) 215 216 TabContent() { 217 Column() 218 .width('100%') 219 .height('100%') 220 .backgroundColor('#FFBF00') 221 } 222 .tabBar('yellow') 223 .id('tab3') 224 .onWillHide(() => { 225 this.getUIContext().freezeUINode('tab3', true); 226 }) 227 .onWillShow(() => { 228 this.getUIContext().freezeUINode('tab3', false); 229 }) 230 231 } 232 .vertical(false) 233 .barMode(BarMode.Fixed) 234 .barWidth(360) 235 .barHeight(56) 236 .animationDuration(0) 237 .onChange((index: number) => { 238 this.currentIndex = index; 239 }) 240 .width(360) 241 .height(296) 242 .margin({ top: 52 }) 243 .backgroundColor('#F1F3F5') 244 }.width('100%') 245 } 246} 247``` 248 249### freezeUINode<sup>18+</sup> 250 251freezeUINode(uniqueId: number, isFrozen: boolean): void 252 253Sets whether to freeze a specific component by **uniqueId** to prevent it from marking itself as dirty and triggering layout updates. 254 255**Atomic service API**: This API can be used in atomic services since API version 18. 256 257**System capability**: SystemCapability.ArkUI.ArkUI.Full 258 259**Parameters** 260 261| Name | Type | Mandatory | Description | 262| --- | --- | --- | --- | 263| uniqueId | number | Yes| Unique ID of the target component.| 264| isFrozen | boolean | Yes| Whether to freeze the component.<br>Default value: **false**| 265 266**Error codes** 267 268For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 269 270| ID| Error Message| 271| -------- | -------- | 272| 202 | The caller is not a system application. | 273 274```js 275@Entry 276@Component 277struct Index { 278 @State columnWidth1: string = '100%'; 279 @State currentIndex: number = 0; 280 private controller: TabsController = new TabsController(); 281 282 build() { 283 Column() { 284 Tabs({ 285 barPosition: BarPosition.Start, 286 index: this.currentIndex, 287 controller: this.controller 288 }) { 289 TabContent() { 290 Column() 291 .width(this.columnWidth1) 292 .height('100%') 293 .backgroundColor('#00CB87') 294 } 295 .tabBar('green') 296 .id('tab1') 297 .onWillHide(() => { 298 const node = this.getUIContext().getFrameNodeById('tab1'); 299 const uniqueId = node?.getUniqueId(); 300 this.getUIContext().freezeUINode(uniqueId, true); 301 }) 302 .onWillShow(() => { 303 const node = this.getUIContext().getFrameNodeById('tab1'); 304 const uniqueId = node?.getUniqueId(); 305 this.getUIContext().freezeUINode(uniqueId, false) 306 }) 307 308 TabContent() { 309 Column() 310 .width('100%') 311 .height('100%') 312 .backgroundColor('#007DFF') 313 } 314 .tabBar('blue') 315 .id('tab2') 316 .onWillHide(() => { 317 const node = this.getUIContext().getFrameNodeById('tab2'); 318 const uniqueId = node?.getUniqueId(); 319 this.getUIContext().freezeUINode(uniqueId, true); 320 }) 321 .onWillShow(() => { 322 const node = this.getUIContext().getFrameNodeById('tab1'); 323 const uniqueId = node?.getUniqueId(); 324 this.getUIContext().freezeUINode(uniqueId, true); 325 326 this.columnWidth1 = '50%'; 327 setTimeout(() => { 328 this.getUIContext().freezeUINode(uniqueId, false); 329 this.columnWidth1 = '20%'; 330 }, 5000) 331 }) 332 333 TabContent() { 334 Column() 335 .width('100%') 336 .height('100%') 337 .backgroundColor('#FFBF00') 338 } 339 .tabBar('yellow') 340 .id('tab3') 341 .onWillHide(() => { 342 const node = this.getUIContext().getFrameNodeById('tab3'); 343 const uniqueId = node?.getUniqueId(); 344 this.getUIContext().freezeUINode(uniqueId, true); 345 }) 346 .onWillShow(() => { 347 const node = this.getUIContext().getFrameNodeById('tab3'); 348 const uniqueId = node?.getUniqueId(); 349 this.getUIContext().freezeUINode(uniqueId, false); 350 }) 351 352 } 353 .vertical(false) 354 .barMode(BarMode.Fixed) 355 .barWidth(360) 356 .barHeight(56) 357 .animationDuration(0) 358 .onChange((index: number) => { 359 this.currentIndex = index; 360 }) 361 .width(360) 362 .height(296) 363 .margin({ top: 52 }) 364 .backgroundColor('#F1F3F5') 365 }.width('100%') 366 } 367} 368``` 369