• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { Scene } from '../Scene';
17import { ArkMethod } from '../core/model/ArkMethod';
18import { Stmt } from '../core/base/Stmt';
19import { FunctionType } from '../core/base/Type';
20
21export const LIFECYCLE_METHOD_NAME: string[] = [
22    'onCreate', // 组件实例创建
23    'onDestroy', // 组件实例销毁
24    'onWindowStageCreate', // 窗口创建
25    'onWindowStageDestroy', // 窗口销毁
26    'onForeground', // 应用进入前台
27    'onBackground', // 应用进入后台
28    'onBackup', // 应用数据备份
29    'onRestore', // 应用数据恢复
30    'onContinue',
31    'onNewWant',
32    'onDump',
33    'onSaveState',
34    'onShare',
35    'onPrepareToTerminate',
36    'onBackPressed',
37    'onSessionCreate',
38    'onSessionDestory',
39    'onAddForm',
40    'onCastToNormalForm',
41    'onUpdateForm',
42    'onChangeFormVisibility',
43    'onFormEvent',
44    'onRemoveForm',
45    'onConfigurationUpdate',
46    'onAcquireFormState',
47    'onWindowStageWillDestroy',
48];
49export const CALLBACK_METHOD_NAME: string[] = [
50    'onClick', // 点击事件,当用户点击组件时触发
51    'onTouch', // 触摸事件,当手指在组件上按下、滑动、抬起时触发
52    'onAppear', // 组件挂载显示时触发
53    'onDisAppear', // 组件卸载消失时触发
54    'onDragStart', // 拖拽开始事件,当组件被长按后开始拖拽时触发
55    'onDragEnter', // 拖拽进入组件范围时触发
56    'onDragMove', // 拖拽在组件范围内移动时触发
57    'onDragLeave', // 拖拽离开组件范围内时触发
58    'onDrop', // 拖拽释放目标,当在本组件范围内停止拖拽行为时触发
59    'onKeyEvent', // 按键事件,当组件获焦后,按键动作触发
60    'onFocus', // 焦点事件,当组件获取焦点时触发
61    'onBlur', // 当组件失去焦点时触发的回调
62    'onHover', // 鼠标悬浮事件,鼠标进入或退出组件时触发
63    'onMouse', // 鼠标事件,当鼠标按键点击或在组件上移动时触发
64    'onAreaChange', // 组件区域变化事件,组件尺寸、位置变化时触发
65    'onVisibleAreaChange', // 组件可见区域变化事件,组件在屏幕中的显示区域面积变化时触发
66];
67
68export const COMPONENT_LIFECYCLE_METHOD_NAME: string[] = [
69    'build',
70    'aboutToAppear',
71    'aboutToDisappear',
72    'aboutToReuse',
73    'aboutToRecycle',
74    'onWillApplyTheme',
75    'onLayout',
76    'onPlaceChildren',
77    'onMeasure',
78    'onMeasureSize',
79    'onPageShow',
80    'onPageHide',
81    'onFormRecycle',
82    'onFormRecover',
83    'onBackPress',
84    'pageTransition',
85    'onDidBuild',
86];
87
88export interface AbilityMessage {
89    srcEntry: string;
90    name: string;
91    srcEntrance: string;
92}
93
94export function getCallbackMethodFromStmt(stmt: Stmt, scene: Scene): ArkMethod | null {
95    const invokeExpr = stmt.getInvokeExpr();
96    if (
97        invokeExpr === undefined ||
98        invokeExpr.getMethodSignature().getDeclaringClassSignature().getClassName() !== '' ||
99        !CALLBACK_METHOD_NAME.includes(invokeExpr.getMethodSignature().getMethodSubSignature().getMethodName())
100    ) {
101        return null;
102    }
103
104    for (const arg of invokeExpr.getArgs()) {
105        const argType = arg.getType();
106        if (argType instanceof FunctionType) {
107            const cbMethod = scene.getMethod(argType.getMethodSignature());
108            if (cbMethod) {
109                return cbMethod;
110            }
111        }
112    }
113    return null;
114}
115
116export function addCfg2Stmt(method: ArkMethod): void {
117    const cfg = method.getCfg();
118    if (cfg) {
119        for (const block of cfg.getBlocks()) {
120            for (const stmt of block.getStmts()) {
121                stmt.setCfg(cfg);
122            }
123        }
124    }
125}
126