已合并
keyframe ani适配 #16447
keyframe ani适配 #16447
已合并
qianli22创建于 2025年10月15日
共 5 个文件变更+198-0
@@ -635,6 +635,22 @@ namespace window {
635 y: int;635 y: int;
636 }636 }
637 637 
638+ export interface KeyFramePolicy {
639+ enable: boolean;
640+ interval?: long;
641+ distance?: int;
642+ animationDuration?: long;
643+ animationDelay?: long;
644+ }
645+ 
646+ export class KeyFramePolicyInternal implements KeyFramePolicy {
647+ enable: boolean;
648+ interval?: long;
649+ distance?: int;
650+ animationDuration?: long;
651+ animationDelay?: long;
652+ }
653+ 
638//enum WMError {654//enum WMError {
639 const WMError_WM_OK = 0655 const WMError_WM_OK = 0
640 const WMError_WM_DO_NOTHING = 1656 const WMError_WM_DO_NOTHING = 1
@@ -1991,6 +2007,7 @@ export class WindowInternal implements Window {
1991 private native setWindowLayoutFullScreen(nativeObj: long, isLayoutFullScreen: boolean): int;2007 private native setWindowLayoutFullScreen(nativeObj: long, isLayoutFullScreen: boolean): int;
1992 private native setWindowSystemBarProperties(nativeObj: long, systemBarProperties: SystemBarProperties): int;2008 private native setWindowSystemBarProperties(nativeObj: long, systemBarProperties: SystemBarProperties): int;
1993 private native setSpecificSystemBarEnabled(nativeObj: long, name: String, enable: boolean, enableAnimation: boolean): int;2009 private native setSpecificSystemBarEnabled(nativeObj: long, name: String, enable: boolean, enableAnimation: boolean): int;
2010+ private native setDragKeyFramePolicy(nativeObj: long, keyFramePolicy: KeyFramePolicy): KeyFramePolicy;
1994 private native snapshot(nativeObj: long): image.PixelMap;2011 private native snapshot(nativeObj: long): image.PixelMap;
1995 private native hideNonSystemFloatingWindows(nativeObj: long, shouldHide: boolean): void;2012 private native hideNonSystemFloatingWindows(nativeObj: long, shouldHide: boolean): void;
1996 private native opacity(nativeObj: long, opacity: double): void;2013 private native opacity(nativeObj: long, opacity: double): void;
@@ -2260,6 +2277,18 @@ export class WindowInternal implements Window {
2260 });2277 });
2261 }2278 }
2262 2279 
2280+ public setDragKeyFramePolicy(keyFramePolicy: KeyFramePolicy): Promise<KeyFramePolicy> {
2281+ return new Promise<KeyFramePolicy>((resolve: (value: KeyFramePolicy) => void, reject: (error: BusinessError) => void) => {
2282+ taskpool.execute(() => {
2283+ return this.setDragKeyFramePolicy(this.nativeObj, keyFramePolicy);
2284+ }).then((ret: NullishType) => {
2285+ resolve(ret as KeyFramePolicy);
2286+ }).catch((err: NullishType) => {
2287+ reject(err as BusinessError);
2288+ });
2289+ });
2290+ }
2291+ 
2263 public snapshot(): Promise<image.PixelMap> {2292 public snapshot(): Promise<image.PixelMap> {
2264 return new Promise<image.PixelMap>((resolve: (value: image.PixelMap) => void, reject: (error: BusinessError) => void) => {2293 return new Promise<image.PixelMap>((resolve: (value: image.PixelMap) => void, reject: (error: BusinessError) => void) => {
2265 taskpool.execute(() => {2294 taskpool.execute(() => {
@@ -3005,6 +3034,7 @@ export interface Window {
3005 setWindowSystemBarProperties(systemBarProperties: SystemBarProperties): Promise<void>;3034 setWindowSystemBarProperties(systemBarProperties: SystemBarProperties): Promise<void>;
3006 setWindowSystemBarProperties(systemBarProperties: SystemBarProperties, callback: AsyncCallback<void>): void;3035 setWindowSystemBarProperties(systemBarProperties: SystemBarProperties, callback: AsyncCallback<void>): void;
3007 setSpecificSystemBarEnabled(name: String, enable: boolean, enableAnimation?: boolean): Promise<void>;3036 setSpecificSystemBarEnabled(name: String, enable: boolean, enableAnimation?: boolean): Promise<void>;
3037+ setDragKeyFramePolicy(keyFramePolicy: KeyFramePolicy): Promise<KeyFramePolicy>;
3008 snapshot(): Promise<image.PixelMap>;3038 snapshot(): Promise<image.PixelMap>;
3009 snapshot(callback: AsyncCallback<image.PixelMap | undefined>): void;3039 snapshot(callback: AsyncCallback<image.PixelMap | undefined>): void;
3010 hideNonSystemFloatingWindows(shouldHide: boolean): Promise<void>;3040 hideNonSystemFloatingWindows(shouldHide: boolean): Promise<void>;
@@ -107,6 +107,7 @@ public:
107 void SetSystemBarProperties(ani_env* env, ani_object aniSystemBarProperties);107 void SetSystemBarProperties(ani_env* env, ani_object aniSystemBarProperties);
108 ani_object SetSpecificSystemBarEnabled(ani_env* env, ani_string, ani_boolean enable,108 ani_object SetSpecificSystemBarEnabled(ani_env* env, ani_string, ani_boolean enable,
109 ani_boolean enableAnimation);109 ani_boolean enableAnimation);
110+ ani_object SetDragKeyFramePolicy(ani_env* env, ani_object aniKeyFramePolicy);
110 ani_object Snapshot(ani_env* env);111 ani_object Snapshot(ani_env* env);
111 void HideNonSystemFloatingWindows(ani_env* env, ani_boolean shouldHide);112 void HideNonSystemFloatingWindows(ani_env* env, ani_boolean shouldHide);
112 void ResizeAsync(ani_env* env, ani_int width, ani_int height);113 void ResizeAsync(ani_env* env, ani_int width, ani_int height);
@@ -82,6 +82,10 @@ public:
82 static void* GetAbilityContext(ani_env *env, ani_object aniObj);82 static void* GetAbilityContext(ani_env *env, ani_object aniObj);
83 static ani_object CreateAniRectObject(ani_env* env, const Rect& rect);83 static ani_object CreateAniRectObject(ani_env* env, const Rect& rect);
84 static ani_object CreateWindowsProperties(ani_env* env, const WindowPropertyInfo& windowPropertyInfo);84 static ani_object CreateWindowsProperties(ani_env* env, const WindowPropertyInfo& windowPropertyInfo);
85+ static ani_status CheckPropertyNameUndefined(ani_env* env, const char* propertyName,
86+ ani_object object, bool& result);
87+ static bool ParseKeyFramePolicy(ani_env* env, ani_object aniKeyFramePolicy, KeyFramePolicy& keyFramePolicy);
88+ static ani_object CreateKeyFramePolicy(ani_env* env, const KeyFramePolicy& keyFramePolicy);
85 static ani_object CreateAniPixelMapArray(ani_env* env,89 static ani_object CreateAniPixelMapArray(ani_env* env,
86 const std::vector<std::shared_ptr<Media::PixelMap>>& pixelMaps);90 const std::vector<std::shared_ptr<Media::PixelMap>>& pixelMaps);
87 static ani_object CreateAniMainWindowInfoArray(ani_env* env, const std::vector<sptr<MainWindowInfo>>& infos);91 static ani_object CreateAniMainWindowInfoArray(ani_env* env, const std::vector<sptr<MainWindowInfo>>& infos);
@@ -1597,6 +1597,40 @@ ani_ref FindAniWindowObject(const std::string& windowName)
1597 return g_aniWindowMap[windowName];1597 return g_aniWindowMap[windowName];
1598}1598}
1599 1599 
1600+ani_object AniWindow::SetDragKeyFramePolicy(ani_env* env, ani_object aniKeyFramePolicy)
1601+{
1602+ if (windowToken_ == nullptr || env == nullptr) {
1603+ TLOGE(WmsLogTag::WMS_LAYOUT_PC, "[ANI] windowToken_ or env is nullptr");
1604+ return AniWindowUtils::AniThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1605+ }
1606+ KeyFramePolicy keyFramePolicy;
1607+ if (!AniWindowUtils::ParseKeyFramePolicy(env, aniKeyFramePolicy, keyFramePolicy)) {
1608+ TLOGE(WmsLogTag::WMS_LAYOUT_PC, "[ANI] Failed to convert parameter to keyFramePolicy");
1609+ return AniWindowUtils::AniThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1610+ }
1611+ if (!windowToken_->IsPcWindow()) {
1612+ TLOGE(WmsLogTag::WMS_LAYOUT_PC, "[ANI] device not support");
1613+ return AniWindowUtils::AniThrowError(env, WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT);
1614+ }
1615+ if (!WindowHelper::IsMainWindow(windowToken_->GetType())) {
1616+ TLOGE(WmsLogTag::WMS_LAYOUT_PC, "[ANI] only main window is valid");
1617+ return AniWindowUtils::AniThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
1618+ }
1619+ 
1620+ const WMError ret = windowToken_->SetDragKeyFramePolicy(keyFramePolicy);
1621+ const WmErrorCode errorCode = AniWindowUtils::ToErrorCode(ret);
1622+ const uint32_t windowId = windowToken_->GetWindowId();
1623+ if (errorCode != WmErrorCode::WM_OK) {
1624+ TLOGE(WmsLogTag::WMS_LAYOUT_PC, "[ANI] failed, windowId: %{public}u, ret: %{public}d",
1625+ windowId, static_cast<int32_t>(ret));
1626+ return AniWindowUtils::AniThrowError(env, errorCode);
1627+ }
1628+ TLOGI(WmsLogTag::WMS_LAYOUT_PC,
1629+ "[ANI] success, windowId: %{public}u, keyFramePolicy: %{public}s",
1630+ windowToken_->GetWindowId(), keyFramePolicy.ToString().c_str());
1631+ return AniWindowUtils::CreateKeyFramePolicy(env, keyFramePolicy);
1632+}
1633+ 
1600ani_object AniWindow::Snapshot(ani_env* env)1634ani_object AniWindow::Snapshot(ani_env* env)
1601{1635{
1602 if (windowToken_ == nullptr) {1636 if (windowToken_ == nullptr) {
@@ -2575,6 +2609,19 @@ static ani_int WindowSetSpecificSystemBarEnabled(ani_env* env, ani_object obj, a
2575 return ANI_OK;2609 return ANI_OK;
2576}2610}
2577 2611 
2612+static ani_object WindowSetDragKeyFramePolicy(ani_env* env, ani_object obj, ani_long nativeObj,
2613+ ani_object aniKeyFramePolicy)
2614+{
2615+ using namespace OHOS::Rosen;
2616+ TLOGI(WmsLogTag::WMS_LAYOUT_PC, "[ANI]");
2617+ AniWindow* aniWindow = reinterpret_cast<AniWindow*>(nativeObj);
2618+ if (aniWindow == nullptr || env == nullptr) {
2619+ TLOGE(WmsLogTag::WMS_LAYOUT_PC, "[ANI] aniWindow or env is nullptr");
2620+ return AniWindowUtils::AniThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
2621+ }
2622+ return aniWindow->SetDragKeyFramePolicy(env, aniKeyFramePolicy);
2623+}
2624+ 
2578static ani_object Snapshot(ani_env* env, ani_object obj, ani_long nativeObj)2625static ani_object Snapshot(ani_env* env, ani_object obj, ani_long nativeObj)
2579{2626{
2580 using namespace OHOS::Rosen;2627 using namespace OHOS::Rosen;
@@ -2928,6 +2975,9 @@ ani_status OHOS::Rosen::ANI_Window_Constructor(ani_vm *vm, uint32_t *result)
2928 reinterpret_cast<void *>(WindowSetSystemBarProperties)},2975 reinterpret_cast<void *>(WindowSetSystemBarProperties)},
2929 ani_native_function {"setSpecificSystemBarEnabled", "JLstd/core/String;ZZ:I",2976 ani_native_function {"setSpecificSystemBarEnabled", "JLstd/core/String;ZZ:I",
2930 reinterpret_cast<void *>(WindowSetSpecificSystemBarEnabled)},2977 reinterpret_cast<void *>(WindowSetSpecificSystemBarEnabled)},
2978+ ani_native_function {"setDragKeyFramePolicy",
2979+ "JL@ohos/window/window/KeyFramePolicy;:L@ohos/window/window/KeyFramePolicy;",
2980+ reinterpret_cast<void *>(WindowSetDragKeyFramePolicy)},
2931 ani_native_function {"snapshot", "J:L@ohos/multimedia/image/image/PixelMap;",2981 ani_native_function {"snapshot", "J:L@ohos/multimedia/image/image/PixelMap;",
2932 reinterpret_cast<void *>(Snapshot)},2982 reinterpret_cast<void *>(Snapshot)},
2933 ani_native_function {"hideNonSystemFloatingWindows", "JZ:V",2983 ani_native_function {"hideNonSystemFloatingWindows", "JZ:V",
@@ -946,6 +946,119 @@ ani_object AniWindowUtils::CreateAniMainWindowInfo(ani_env* env, const MainWindo
946 return mainWindowInfo;946 return mainWindowInfo;
947}947}
948 948 
949+ani_status AniWindowUtils::CheckPropertyNameUndefined(ani_env* env, const char* propertyName,
950+ ani_object object, bool& result)
951+{
952+ if (env == nullptr) {
953+ TLOGE(WmsLogTag::WMS_LAYOUT_PC, "[ANI] env is nullptr");
954+ return ANI_ERROR;
955+ }
956+ result = false;
957+ ani_ref ref;
958+ ani_status ret = env->Object_GetPropertyByName_Ref(object, propertyName, &ref);
959+ if (ret != ANI_OK) {
960+ TLOGE(WmsLogTag::DEFAULT, "[ANI] Object_GetPropertyByName_Ref %{public}s Failed, ret : %{public}u",
961+ propertyName, static_cast<int32_t>(ret));
962+ return ret;
963+ }
964+ ani_boolean isUndefined = false;
965+ ret = env->Reference_IsUndefined(ref, &isUndefined);
966+ if (ret != ANI_OK) {
967+ TLOGE(WmsLogTag::DEFAULT, "[ANI] Object_GetPropertyByName_Ref %{public}s Failed", propertyName);
968+ return ret;
969+ }
970+ result = isUndefined;
971+ return ANI_OK;
972+}
973+ 
974+bool AniWindowUtils::ParseKeyFramePolicy(ani_env* env, ani_object aniKeyFramePolicy, KeyFramePolicy& keyFramePolicy)
975+{
976+ if (env == nullptr) {
977+ TLOGE(WmsLogTag::WMS_LAYOUT_PC, "[ANI] env is nullptr");
978+ return false;
979+ }
980+ 
981+ ani_boolean enable = false;
982+ if (env->Object_GetPropertyByName_Boolean(aniKeyFramePolicy, "enable", &enable) != ANI_OK) {
L
Llollipopolive2025年10月17日

env调用前是否已判空

likedislike
983+ TLOGE(WmsLogTag::WMS_LAYOUT_PC, "[ANI] Failed to convert parameter to enable");
984+ return false;
985+ }
986+ keyFramePolicy.dragResizeType_ = enable ? DragResizeType::RESIZE_KEY_FRAME :
987+ DragResizeType::RESIZE_TYPE_UNDEFINED;
988+
989+ bool propertyUndefined = false;
990+ int32_t distance = 0;
991+ if (CheckPropertyNameUndefined(env, "distance", aniKeyFramePolicy, propertyUndefined) != ANI_OK ||
992+ !propertyUndefined) {
993+ if (GetPropertyIntObject(env, "distance", aniKeyFramePolicy, distance) != ANI_OK || distance < 0) {
994+ TLOGE(WmsLogTag::WMS_LAYOUT_PC, "[ANI] Failed to convert parameter to distance");
995+ return false;
996+ }
997+ keyFramePolicy.distance_ = static_cast<uint32_t>(distance);
998+ }
999+ 
1000+ int64_t longData = 0;
1001+ if (CheckPropertyNameUndefined(env, "interval", aniKeyFramePolicy, propertyUndefined) != ANI_OK ||
1002+ !propertyUndefined) {
1003+ if (GetPropertyLongObject(env, "interval", aniKeyFramePolicy, longData) != ANI_OK || longData <= 0) {
1004+ TLOGE(WmsLogTag::WMS_LAYOUT_PC, "[ANI] Failed to convert parameter to interval");
1005+ return false;
1006+ }
1007+ keyFramePolicy.interval_ = static_cast<uint32_t>(longData);
1008+ }
1009+ if (CheckPropertyNameUndefined(env, "animationDelay", aniKeyFramePolicy, propertyUndefined) != ANI_OK ||
1010+ !propertyUndefined) {
1011+ if (GetPropertyLongObject(env, "animationDelay", aniKeyFramePolicy, longData) != ANI_OK || longData < 0) {
1012+ TLOGE(WmsLogTag::WMS_LAYOUT_PC, "[ANI] Failed to convert parameter to animationDelay");
1013+ return false;
1014+ }
1015+ keyFramePolicy.animationDelay_ = static_cast<uint32_t>(longData);
1016+ }
1017+ if (CheckPropertyNameUndefined(env, "animationDuration", aniKeyFramePolicy, propertyUndefined) != ANI_OK ||
1018+ !propertyUndefined) {
1019+ if (GetPropertyLongObject(env, "animationDuration", aniKeyFramePolicy, longData) != ANI_OK || longData < 0) {
1020+ TLOGE(WmsLogTag::WMS_LAYOUT_PC, "[ANI] Failed to convert parameter to animationDuration");
1021+ return false;
1022+ }
1023+ keyFramePolicy.animationDuration_ = static_cast<uint32_t>(longData);
1024+ }
1025+ return true;
1026+}
1027+ 
1028+ani_object AniWindowUtils::CreateKeyFramePolicy(ani_env* env, const KeyFramePolicy& keyFramePolicy)
1029+{
L
Llollipopolive2025年10月17日

env调用前是否已判空

likedislike
1030+ if (env == nullptr) {
1031+ TLOGE(WmsLogTag::WMS_LAYOUT_PC, "[ANI] env is nullptr");
1032+ return AniWindowUtils::CreateAniUndefined(env);
1033+ }
1034+ ani_class aniClass;
1035+ if (env->FindClass("@ohos.window.window.KeyFramePolicyInternal", &aniClass) != ANI_OK) {
1036+ TLOGE(WmsLogTag::WMS_LAYOUT_PC, "[ANI] class not found");
1037+ return AniWindowUtils::CreateAniUndefined(env);
1038+ }
1039+ ani_method ctor;
1040+ if (env->Class_FindMethod(aniClass, "<ctor>", nullptr, &ctor) != ANI_OK) {
1041+ TLOGE(WmsLogTag::WMS_LAYOUT_PC, "[ANI] ctor not found");
1042+ return AniWindowUtils::CreateAniUndefined(env);
1043+ }
1044+ ani_object aniKeyFramePolicy;
1045+ if (env->Object_New(aniClass, ctor, &aniKeyFramePolicy) != ANI_OK) {
1046+ TLOGE(WmsLogTag::WMS_LAYOUT_PC, "[ANI] fail to new obj");
1047+ return AniWindowUtils::CreateAniUndefined(env);
1048+ }
1049+ CallAniMethodVoid(env, aniKeyFramePolicy, aniClass, "<set>enable", nullptr,
1050+ ani_boolean(keyFramePolicy.enabled()));
1051+ CallAniMethodVoid(env, aniKeyFramePolicy, aniClass, "<set>interval", nullptr,
1052+ CreateBaseTypeObject<long>(env, keyFramePolicy.interval_));
1053+ CallAniMethodVoid(env, aniKeyFramePolicy, aniClass, "<set>distance", nullptr,
1054+ CreateBaseTypeObject<int>(env, keyFramePolicy.distance_));
1055+ CallAniMethodVoid(env, aniKeyFramePolicy, aniClass, "<set>animationDuration", nullptr,
1056+ CreateBaseTypeObject<long>(env, keyFramePolicy.animationDuration_));
1057+ CallAniMethodVoid(env, aniKeyFramePolicy, aniClass, "<set>animationDelay", nullptr,
1058+ CreateBaseTypeObject<long>(env, keyFramePolicy.animationDelay_));
1059+ return aniKeyFramePolicy;
1060+}
1061+ 
949void AniWindowUtils::GetWindowSnapshotConfiguration(ani_env* env, ani_object config,1062void AniWindowUtils::GetWindowSnapshotConfiguration(ani_env* env, ani_object config,
950 WindowSnapshotConfiguration& windowSnapshotConfiguration)1063 WindowSnapshotConfiguration& windowSnapshotConfiguration)
951{1064{