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 16 17// WARNING! THIS FILE IS AUTO-GENERATED, DO NOT MAKE CHANGES, THEY WILL BE LOST ON NEXT GENERATION! 18 19import { FrameNode, FrameNodeInternal, FrameNodeUtils } from "arkui/FrameNode" 20import { GlobalScope_ohos_font } from "arkui/component/arkui-external" 21import { GlobalScope_ohos_measure_utils } from "arkui/component/arkui-external" 22import { UIContextDispatchKeyEvent, UIContextAtomicServiceBar } from "arkui/component/arkui-custom" 23import { FontOptions, FontInfo } from "@ohos/font" 24import { MeasureOptions } from "@ohos/measure" 25import { SizeOptions } from "arkui/component/units" 26import { ArkUIGeneratedNativeModule } from "#components" 27import { int32 } from "@koalaui/common" 28import { nullptr } from "@koalaui/interop" 29import { _animateTo } from "arkui/handwritten/ArkAnimation" 30import { AnimateParam } from 'arkui/component' 31import { AnimatorResult, AnimatorOptions, Animator} from "@ohos/animator" 32import { Context } from "#external" 33import { ArkUIAniModule } from "arkui.ani" 34import { Serializer } from "arkui/component/peers/Serializer" 35import { componentUtils } from "@ohos/arkui/componentUtils" 36import { focusController } from "@ohos/arkui/focusController" 37import { Frame } from "arkui/Graphics" 38import { KeyEvent } from "arkui/component/common" 39import { Nullable } from "arkui/component/enums" 40import { KeyProcessingMode } from "arkui/component/focus" 41import { uiObserver } from "@ohos/arkui/observer" 42import { AlertDialog, AlertDialogParamWithConfirm, AlertDialogParamWithButtons, 43 AlertDialogParamWithOptions }from "arkui/component/alertDialog" 44import inspector from "@ohos/arkui/inspector" 45import router from '@ohos/router' 46import promptAction from '@ohos/promptAction'; 47import { ContextMenu } from 'arkui/component/contextMenu'; 48import { Router as RouterExt } from 'arkui/handwritten'; 49import { ComputableState } from '@koalaui/runtime' 50import { PeerNode } from 'arkui/PeerNode' 51 52export class UIInspector { 53 instanceId_: int32 = -1; 54 constructor(instanceId: int32) { 55 this.instanceId_ = instanceId; 56 } 57 public createComponentObserver(id: string): inspector.ComponentObserver { 58 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 59 let componentObserver = inspector.createComponentObserver(id); 60 ArkUIAniModule._Common_Restore_InstanceId(); 61 return componentObserver; 62 } 63} 64 65export class Font { 66 instanceId_: int32 = 10001; 67 constructor(instanceId: int32) { 68 this.instanceId_ = instanceId; 69 } 70 public registerFont(options: FontOptions) : void { 71 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 72 GlobalScope_ohos_font.registerFont(options); 73 ArkUIAniModule._Common_Restore_InstanceId(); 74 } 75 public getSystemFontList() : Array<string> { 76 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 77 let arrayResult_ = GlobalScope_ohos_font.getSystemFontList(); 78 ArkUIAniModule._Common_Restore_InstanceId(); 79 return arrayResult_; 80 } 81 public getFontByName(fontName : string) : FontInfo { 82 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 83 let fontInfo : FontInfo = GlobalScope_ohos_font.getFontByName(fontName); 84 ArkUIAniModule._Common_Restore_InstanceId(); 85 return fontInfo; 86 } 87} 88 89export class MeasureUtils { 90 instanceId_: int32 = 10001; 91 constructor(instanceId: int32) { 92 this.instanceId_ = instanceId; 93 } 94 public measureText(options: MeasureOptions) : number { 95 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 96 let width = GlobalScope_ohos_measure_utils.measureText(options); 97 ArkUIAniModule._Common_Restore_InstanceId(); 98 return width; 99 } 100 public measureTextSize(options: MeasureOptions) : SizeOptions { 101 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 102 let sizeOptions = GlobalScope_ohos_measure_utils.measureTextSize(options); 103 ArkUIAniModule._Common_Restore_InstanceId(); 104 return sizeOptions; 105 } 106} 107 108export class Router { 109 instanceId_: int32 = 100000; 110 router_: RouterExt | undefined = undefined; 111 constructor(instanceId: int32) { 112 this.instanceId_ = instanceId; 113 } 114 public setRouter(router: RouterExt) { 115 this.router_ = router; 116 } 117 public getRouter(): RouterExt { 118 return this.router_!; 119 } 120 public pushUrl(options: router.RouterOptions): Promise<void> { 121 if (this.router_ === undefined) { 122 throw Error("router set in uiContext is empty"); 123 } 124 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 125 let result = new Promise<void>((resolve, reject) => { 126 this.router_!.push(options); 127 }); 128 ArkUIAniModule._Common_Restore_InstanceId(); 129 return result; 130 } 131 132 public replaceUrl(options: router.RouterOptions): Promise<void> { 133 if (this.router_ === undefined) { 134 throw Error("router set in uiContext is empty"); 135 } 136 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 137 let result = new Promise<void>((resolve, reject) => { 138 this.router_!.replace(options); 139 }); 140 ArkUIAniModule._Common_Restore_InstanceId(); 141 return result; 142 } 143 144 public back(options?:router.RouterOptions): void { 145 if (this.router_ === undefined) { 146 throw Error("router set in uiContext is empty"); 147 } 148 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 149 this.router_!.back(options); 150 ArkUIAniModule._Common_Restore_InstanceId(); 151 } 152 153 public clear(): void { 154 if (this.router_ === undefined) { 155 throw Error("router set in uiContext is empty"); 156 } 157 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 158 this.router_!.clear(); 159 ArkUIAniModule._Common_Restore_InstanceId(); 160 } 161 public getLength(): string { 162 if (this.router_ === undefined) { 163 throw Error("router set in uiContext is empty"); 164 } 165 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 166 let result = this.router_!.getLength(); 167 ArkUIAniModule._Common_Restore_InstanceId(); 168 return result; 169 } 170 171 public getParams(): Object { 172 if (this.router_ === undefined) { 173 throw Error("router set in uiContext is empty"); 174 } 175 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 176 let result = this.router_!.getParams(); 177 ArkUIAniModule._Common_Restore_InstanceId(); 178 return result; 179 } 180 181 public getState(): router.RouterState { 182 if (this.router_ === undefined) { 183 throw Error("router set in uiContext is empty"); 184 } 185 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 186 let result = this.router_!.getState(); 187 ArkUIAniModule._Common_Restore_InstanceId(); 188 return result; 189 } 190 191 public getStateByIndex(index: number): router.RouterState | undefined { 192 if (this.router_ === undefined) { 193 throw Error("router set in uiContext is empty"); 194 } 195 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 196 let result = this.router_!.getStateByIndex(index); 197 ArkUIAniModule._Common_Restore_InstanceId(); 198 return result; 199 } 200 201 public getStateByUrl(url: string): Array<router.RouterState> { 202 if (this.router_ === undefined) { 203 throw Error("router set in uiContext is empty"); 204 } 205 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 206 let result = this.router_!.getStateByUrl(url); 207 ArkUIAniModule._Common_Restore_InstanceId(); 208 return result; 209 } 210 211 public getStateRoot(): Array<ComputableState<PeerNode>> { 212 if (this.router_ === undefined) { 213 throw Error("router set in uiContext is empty"); 214 } 215 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 216 let result = this.router_!.getEntryRootValue(); 217 ArkUIAniModule._Common_Restore_InstanceId(); 218 return result; 219 } 220} 221 222export interface AtomicServiceBar { 223 getBarRect(): Frame; 224} 225 226export class AtomicServiceBarInternal implements AtomicServiceBar { 227 instanceId_: int32; 228 constructor(instanceId: int32) { 229 this.instanceId_ = instanceId; 230 } 231 public getBarRect(): Frame { 232 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 233 let frame = UIContextAtomicServiceBar.getBarRect(); 234 ArkUIAniModule._Common_Restore_InstanceId(); 235 return frame; 236 } 237} 238export class ComponentUtils { 239 instanceId_: int32 240 constructor(instanceId: int32) { 241 this.instanceId_ = instanceId; 242 } 243 244 public getRectangleById(id: string): componentUtils.ComponentInfo { 245 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 246 let componentInformation = componentUtils.getRectangleById(id); 247 ArkUIAniModule._Common_Restore_InstanceId(); 248 return componentInformation; 249 } 250} 251 252 253export class FocusController { 254 instanceId_: int32 255 constructor(instanceId: int32) { 256 this.instanceId_ = instanceId; 257 } 258 259 public clearFocus(): void { 260 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 261 focusController.clearFocus(); 262 ArkUIAniModule._Common_Restore_InstanceId(); 263 } 264 265 public requestFocus(key: string): void { 266 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 267 focusController.requestFocus(key); 268 ArkUIAniModule._Common_Restore_InstanceId(); 269 } 270 271 public activate(isActive: boolean, autoInactive?: boolean): void { 272 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 273 focusController.activate(isActive, autoInactive); 274 ArkUIAniModule._Common_Restore_InstanceId(); 275 } 276 277 public setAutoFocusTransfer(isAutoFocusTransfer: boolean): void { 278 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 279 focusController.setAutoFocusTransfer(isAutoFocusTransfer); 280 ArkUIAniModule._Common_Restore_InstanceId(); 281 } 282 public setKeyProcessingMode(mode: KeyProcessingMode): void { 283 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 284 focusController.setKeyProcessingMode(mode); 285 ArkUIAniModule._Common_Restore_InstanceId(); 286 } 287} 288 289class ContextMenuController { 290 instanceId_: int32 291 constructor(instanceId: int32) { 292 this.instanceId_ = instanceId; 293 } 294 295 public close(): void { 296 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 297 ContextMenu.close(); 298 ArkUIAniModule._Common_Restore_InstanceId(); 299 } 300} 301 302export class PromptAction { 303 instanceId_: int32 = 100000; 304 constructor(instanceId: int32) { 305 this.instanceId_ = instanceId; 306 } 307 308 showToast(options: promptAction.ShowToastOptions): void { 309 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 310 promptAction.showToast(options); 311 ArkUIAniModule._Common_Restore_InstanceId(); 312 } 313} 314 315export class UIContext { 316 instanceId_: int32 = 100000; 317 observer_ :UIObserver |null = null; 318 router_: Router; 319 focusController_: FocusController; 320 componentUtils_: ComponentUtils; 321 atomicServiceBar_: AtomicServiceBarInternal; 322 uiInspector_: UIInspector | null = null; 323 contextMenuController_: ContextMenuController; 324 promptAction_: PromptAction | null = null; 325 326 constructor(instanceId: int32) { 327 this.instanceId_ = instanceId; 328 this.focusController_ = new FocusController(instanceId); 329 this.componentUtils_ = new ComponentUtils(instanceId); 330 this.atomicServiceBar_ = new AtomicServiceBarInternal(instanceId); 331 this.contextMenuController_ = new ContextMenuController(instanceId); 332 this.router_ = new Router(instanceId); 333 } 334 public getFont() : Font { 335 let font : Font = new Font(this.instanceId_); 336 return font; 337 } 338 public getMeasureUtils() : MeasureUtils { 339 let measureUtils : MeasureUtils = new MeasureUtils(this.instanceId_); 340 return measureUtils; 341 } 342 public getFrameNodeById(id: string): FrameNode | null { 343 const id_casted = id as (string); 344 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 345 const retval = ArkUIGeneratedNativeModule._FrameNode_getFrameNodeByKey(id_casted); 346 if (retval === nullptr) { 347 ArkUIAniModule._Common_Restore_InstanceId(); 348 return null; 349 } 350 let node = FrameNodeUtils.searchNodeInRegisterProxy(retval); 351 if (!node) { 352 node = FrameNodeUtils.createFrameNode(this, retval); 353 } 354 ArkUIAniModule._Common_Restore_InstanceId(); 355 return node; 356 } 357 getAttachedFrameNodeById(id: string): FrameNode | null { 358 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 359 const retval = ArkUIGeneratedNativeModule._FrameNode_getAttachedFrameNodeById(id); 360 if (retval === nullptr) { 361 ArkUIAniModule._Common_Restore_InstanceId(); 362 return null; 363 } 364 let node = FrameNodeUtils.searchNodeInRegisterProxy(retval); 365 if (!node) { 366 node = FrameNodeUtils.createFrameNode(this, retval); 367 } 368 ArkUIAniModule._Common_Restore_InstanceId(); 369 return node; 370 } 371 getFrameNodeByNodeId(id: number): FrameNode | null { 372 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 373 const retval = ArkUIGeneratedNativeModule._FrameNode_getFrameNodeById(id); 374 if (retval === nullptr) { 375 ArkUIAniModule._Common_Restore_InstanceId(); 376 return null; 377 } 378 let node = FrameNodeUtils.searchNodeInRegisterProxy(retval); 379 if (!node) { 380 node = FrameNodeUtils.createFrameNode(this, retval); 381 } 382 ArkUIAniModule._Common_Restore_InstanceId(); 383 return node; 384 } 385 getFrameNodeByUniqueId(id: number): FrameNode | null { 386 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 387 const retval = ArkUIGeneratedNativeModule._FrameNode_getFrameNodeByUniqueId(id); 388 if (retval === nullptr) { 389 ArkUIAniModule._Common_Restore_InstanceId(); 390 return null; 391 } 392 let node = FrameNodeUtils.searchNodeInRegisterProxy(retval); 393 if (!node) { 394 node = FrameNodeUtils.createFrameNode(this, retval); 395 } 396 ArkUIAniModule._Common_Restore_InstanceId(); 397 return node; 398 } 399 getHostContext(): Context | undefined { 400 return ArkUIAniModule._Common_GetHostContext(this.instanceId_); 401 } 402 403 public getAtomicServiceBar(): Nullable<AtomicServiceBar> { 404 return this.atomicServiceBar_; 405 } 406 407 public dispatchKeyEvent(node: number | string, event: KeyEvent): boolean { 408 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 409 let result = UIContextDispatchKeyEvent.dispatchKeyEvent(node, event); 410 ArkUIAniModule._Common_Restore_InstanceId(); 411 return result; 412 } 413 414 public getFocusController(): FocusController { 415 return this.focusController_; 416 } 417 418 public getContextMenuController(): ContextMenuController { 419 return this.contextMenuController_; 420 } 421 422 public getComponentUtils(): ComponentUtils { 423 return this.componentUtils_; 424 } 425 426 public getRouter(): Router { 427 if (this.router_ === undefined) { 428 this.router_ = new Router(this.instanceId_); 429 } 430 return this.router_; 431 } 432 433 public setRouter(router: RouterExt): void { 434 this.router_.setRouter(router); 435 } 436 437 public animateTo(param: AnimateParam, event: (() => void)): void { 438 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_) 439 _animateTo(param, event); 440 ArkUIAniModule._Common_Restore_InstanceId(); 441 } 442 443 public createAnimator(options: AnimatorOptions): AnimatorResult { 444 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_) 445 let animatorRet = Animator.create(options); 446 ArkUIAniModule._Common_Restore_InstanceId(); 447 return animatorRet; 448 } 449 public setFrameCallback(onFrameCallback: ((index: number) => void), onIdleCallback: ((index: number) => void), 450 delayTime: number): void { 451 const onFrameCallback_casted = onFrameCallback as (((index: number) => void)) 452 const onIdleCallback_casted = onIdleCallback as (((index: number) => void)) 453 const delayTime_casted = delayTime as (number) 454 this.setFrameCallback_serialize(onFrameCallback_casted, onIdleCallback_casted, delayTime_casted) 455 return 456 } 457 private setFrameCallback_serialize(onFrameCallback: ((index: number) => void), 458 onIdleCallback: ((index: number) => void), 459 delayTime: number): void { 460 const thisSerializer: Serializer = Serializer.hold() 461 thisSerializer.holdAndWriteCallback(onFrameCallback) 462 thisSerializer.holdAndWriteCallback(onIdleCallback) 463 ArkUIGeneratedNativeModule._SystemOps_setFrameCallback(thisSerializer.asBuffer(), 464 thisSerializer.length(), delayTime) 465 thisSerializer.release() 466 } 467 runScopedTask(callback: () => void): void { 468 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_) 469 if (callback !== undefined) { 470 callback() 471 } 472 ArkUIAniModule._Common_Restore_InstanceId() 473 } 474 clearResourceCache(): void { 475 ArkUIGeneratedNativeModule._SystemOps_resourceManagerReset() 476 } 477 postFrameCallback(frameCallback: FrameCallback): void { 478 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_) 479 const onFrameFunc = frameCallback.onFrame 480 const onIdleFunc = frameCallback.onIdle 481 this.setFrameCallback(onFrameFunc, onIdleFunc, 0) 482 ArkUIAniModule._Common_Restore_InstanceId() 483 } 484 postDelayedFrameCallback(frameCallback: FrameCallback, delayTime: number): void { 485 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_) 486 const onFrameFunc = frameCallback.onFrame 487 const onIdleFunc = frameCallback.onIdle 488 this.setFrameCallback(onFrameFunc, onIdleFunc, delayTime) 489 ArkUIAniModule._Common_Restore_InstanceId() 490 } 491 public getUIInspector(): UIInspector { 492 if (!this.uiInspector_) { 493 this.uiInspector_ = new UIInspector(this.instanceId_); 494 } 495 return this.uiInspector_ as UIInspector; 496 } 497 public getUIObserver(): UIObserver { 498 if (!this.observer_) { 499 this.observer_ = new UIObserver(this.instanceId_); 500 } 501 return this.observer_ as UIObserver; 502 } 503 504 public getPromptAction(): PromptAction { 505 if (!this.promptAction_) { 506 this.promptAction_ = new PromptAction(this.instanceId_); 507 } 508 return this.promptAction_ as PromptAction; 509 } 510 511 public showAlertDialog(options: AlertDialogParamWithConfirm | AlertDialogParamWithButtons | 512 AlertDialogParamWithOptions): void { 513 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_); 514 AlertDialog.show(options); 515 ArkUIAniModule._Common_Restore_InstanceId(); 516 } 517 518 // @ts-ignore 519 public freezeUINode(id: number, isFrozen: boolean): void { 520 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_) 521 ArkUIGeneratedNativeModule._UIContextImp_freezeUINode1(id, isFrozen ? 1 : 0); 522 ArkUIAniModule._Common_Restore_InstanceId() 523 } 524 525 // @ts-ignore 526 public freezeUINode(id: string, isFrozen: boolean): void { 527 ArkUIAniModule._Common_Sync_InstanceId(this.instanceId_) 528 ArkUIGeneratedNativeModule._UIContextImp_freezeUINode0(id, isFrozen ? 1 : 0); 529 ArkUIAniModule._Common_Restore_InstanceId() 530 } 531} 532export abstract class FrameCallback { 533 onFrame(frameTimeInNano: number): void {} 534 onIdle(timeLeftInNano: number): void {} 535} 536 537export type Callback<T,V = void> = (data: T) => V 538export class UIObserver { 539 private instanceId_: number = 100000; 540 private observerImpl: uiObserver.UIObserver | null = null; 541 542 constructor(instanceId: number) { 543 this.instanceId_ = instanceId; 544 this.createUIObserver(this.instanceId_); 545 } 546 547 private createUIObserver(id: number): uiObserver.UIObserver | null { 548 this.observerImpl = uiObserver.createUIObserver(id); 549 return this.observerImpl; 550 } 551 552 public on(type: string, callback:Callback<uiObserver.DensityInfo>): void { 553 if (type == 'densityUpdate') { 554 this.observerImpl!.on('densityUpdate', callback); 555 } 556 } 557 558 public off(type: string, callback?: (() => void) | undefined): void { 559 if (this.observerImpl) { 560 this.observerImpl!.off(type, callback); 561 } 562 } 563}