From d5859d24b36c601f61458e15a563b18c6d2ea4cb Mon Sep 17 00:00:00 2001 From: hid06197980 <2089450903@qq.com> Date: Sat, 26 Jul 2025 12:34:39 +0800 Subject: [PATCH] ANI need getsubxxx change Signed-off-by: hid06197980 <2089450903@qq.com> --- .../window_stage_ani/ets/@ohos.window.ets | 39 +++++++++++++ .../include/ani_window_stage.h | 3 + .../window_stage_ani/src/ani_window_stage.cpp | 57 +++++++++++++++++++ 3 files changed, 99 insertions(+) diff --git a/interfaces/kits/ani/window_runtime/window_stage_ani/ets/@ohos.window.ets b/interfaces/kits/ani/window_runtime/window_stage_ani/ets/@ohos.window.ets index 500c61eebf..79113fc25b 100644 --- a/interfaces/kits/ani/window_runtime/window_stage_ani/ets/@ohos.window.ets +++ b/interfaces/kits/ani/window_runtime/window_stage_ani/ets/@ohos.window.ets @@ -20,6 +20,8 @@ import image from '@ohos.multimedia.image'; import BaseContext from 'application.BaseContext'; import hilog from '@ohos.hilog'; import { ColorMetrics } from '@ohos.arkui.node'; +import rpc from '@ohos.rpc'; +import dialogRequest from '@ohos.app.ability.dialogRequest'; export type AsyncCallbackVoid = (err: BusinessError) => void; export type WindowEventCallback = (data: window.WindowEventType) => void; export type WindowRectCallback = (data: window.Rect) => void; @@ -692,6 +694,17 @@ export enum ModalityType { APPLICATION_MODALITY = 1 } +export interface SubWindowOptions { + title: string; + decorEnabled: boolean; + isModal?: boolean; + isTopmost?: boolean; + modalityType?: ModalityType; + windowRect?: Rect; + maximizeSupported?: boolean; + zLevel?: number; +} + export interface SystemBarProperties { /** * The color of the status bar. @@ -1748,6 +1761,7 @@ export class WindowStageInternal implements WindowStage { public native createSubWindowSync(nativeObj: long, name: String): Window; native onSync(nativeObj: long, eventType: 'windowStageEvent', callback: Object): void; native offSync(nativeObj: long, eventType: 'windowStageEvent', callback?: Object): void; + public native getSubWindowSync(nativeObj: long): Array; public setWindowRectAutoSave(enabled: boolean, isSaveBySpecifiedFlag: boolean): Promise { return new Promise((resolve: (value: undefined) => void, reject: (error: BusinessError) => void): void => { @@ -1892,6 +1906,29 @@ export class WindowStageInternal implements WindowStage { public off(eventType: 'windowStageEvent', callback?: Callback): void { this.offSync(this.nativeObj, eventType, callback); } + + public getSubWindow(): Promise> { + return new Promise>((resolve: (value: Array) => void, + reject: (error: BusinessError) => void) => { + taskpool.execute((): Array => { + return this.getSubWindowSync(this.nativeObj); + }).then((ret: NullishType) => { + resolve(ret as Array); + }).catch((err: NullishType) => { + reject(err as BusinessError); + }) + }) + } + + public getSubWindow(callback: AsyncCallback | undefined>): void { + taskpool.execute((): Array => { + return this.getSubWindowSync(this.nativeObj); + }).then((ret: NullishType) => { + callback(new BusinessError(), ret as Array); + }).catch((err: NullishType) => { + callback(err as BusinessError, undefined); + }) + } } export interface WindowStage { @@ -1911,6 +1948,8 @@ export interface WindowStage { setShowOnLockScreen(showOnLockScreen: boolean): void; on(eventType: 'windowStageEvent', callback: Callback): void; off(eventType: 'windowStageEvent', callback?: Callback): void; + getSubWindow(callback: AsyncCallback | undefined>): void; + getSubWindow(): Promise>; } export native function windowDestroyCallback(nativeObj: long): void; diff --git a/interfaces/kits/ani/window_runtime/window_stage_ani/include/ani_window_stage.h b/interfaces/kits/ani/window_runtime/window_stage_ani/include/ani_window_stage.h index e615db79ba..f819c68f4a 100644 --- a/interfaces/kits/ani/window_runtime/window_stage_ani/include/ani_window_stage.h +++ b/interfaces/kits/ani/window_runtime/window_stage_ani/include/ani_window_stage.h @@ -40,6 +40,7 @@ class AniWindowStage { ani_ref callback); static void UnregisterWindowCallback(ani_env* env, ani_object obj, ani_long nativeObj, ani_string type, ani_ref callback); + static ani_object GetSubWindow(ani_env* env, ani_object obj, ani_long nativeObj); void SetWindowRectAutoSave(ani_env* env, ani_boolean enabled, ani_boolean isSaveBySpecifiedFlag); ani_boolean IsWindowRectAutoSave(ani_env* env); @@ -54,6 +55,8 @@ private: void OnSetShowOnLockScreen(ani_env* env, ani_boolean showOnLockScreen); void OnRegisterWindowCallback(ani_env* env, ani_string type, ani_ref callback); void OnUnregisterWindowCallback(ani_env* env, ani_string type, ani_ref callback); + ani_object OnGetSubWindow(ani_env* env); + ani_object CreateSubWindowArrayObject(ani_env* env, std::vector>& subWindowVec); std::weak_ptr windowScene_; std::unique_ptr registerManager_ = nullptr; }; diff --git a/interfaces/kits/ani/window_runtime/window_stage_ani/src/ani_window_stage.cpp b/interfaces/kits/ani/window_runtime/window_stage_ani/src/ani_window_stage.cpp index 3192528053..484b206652 100644 --- a/interfaces/kits/ani/window_runtime/window_stage_ani/src/ani_window_stage.cpp +++ b/interfaces/kits/ani/window_runtime/window_stage_ani/src/ani_window_stage.cpp @@ -413,6 +413,61 @@ ani_ref AniWindowStage::OnCreateSubWindow(ani_env* env, ani_string name) } return CreateAniWindowObject(env, window); } + +ani_object AniWindowStage::CreateSubWindowArrayObject(ani_env* env, std::vector>& subWindowVec) +{ + if (env == nullptr) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] null env"); + return AniWindowUtils::CreateAniUndefined(env); + } + ani_array_ref arrayValue = nullptr; + ani_class cls = nullptr; + if (env->FindClass("L@ohos/window/window/WindowInternal", &cls) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] class not found"); + return AniWindowUtils::CreateAniUndefined(env); + }; + ani_ref element = CreateAniWindowObject(env, subWindowVec[0]); + if (env->Array_New_Ref(cls, subWindowVec.size(), element, &arrayValue) != ANI_OK) { + TLOGE(WmsLogTag::DEFAULT, "[ANI] create array fail"); + return AniWindowUtils::CreateAniUndefined(env); + }; + if (arrayValue == nullptr) { + TLOGD(WmsLogTag::DEFAULT, "Failed to convert subWinVec to jsArrayObject"); + return AniWindowUtils::CreateAniUndefined(env); + } + for (size_t i = 0; i < subWindowVec.size(); i++) { + if (env->Array_Set_Ref(arrayValue, i, CreateAniWindowObject(env, subWindowVec[i])) != ANI_OK) { + TLOGD(WmsLogTag::DEFAULT, "Failed to set ref"); + return AniWindowUtils::CreateAniUndefined(env); + } + } + return reinterpret_cast(arrayValue); +} + +ani_object AniWindowStage::GetSubWindow(ani_env* env, ani_object obj, ani_long nativeObj) +{ + TLOGD(WmsLogTag::WMS_LIFE, "[ANI]"); + AniWindowStage* aniWindowStage = reinterpret_cast(nativeObj); + if (aniWindowStage == nullptr || aniWindowStage->GetMainWindow(env) == nullptr) { + TLOGE(WmsLogTag::WMS_LIFE, "[ANI] aniWindowStage is nullptr!"); + return AniWindowUtils::CreateAniUndefined(env); + } + return aniWindowStage->OnGetSubWindow(env); +} + +ani_object AniWindowStage::OnGetSubWindow(ani_env* env) +{ + TLOGI(WmsLogTag::WMS_LIFE, "[ANI]"); + auto windowScene = GetWindowScene().lock(); + if (windowScene == nullptr) { + TLOGE(WmsLogTag::WMS_LIFE, "[ANI] Window scene is nullptr"); + return AniWindowUtils::AniThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY); + } + + std::vector> subWindowVec = windowScene->GetSubWindow(); + TLOGI(WmsLogTag::WMS_LIFE, "Get sub windows, size = %{public}zu", subWindowVec.size()); + return CreateSubWindowArrayObject(env, subWindowVec); +} } // namespace Rosen } // namespace OHOS @@ -518,6 +573,8 @@ ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) reinterpret_cast(AniWindowStage::RegisterWindowCallback)}, ani_native_function {"offSync", nullptr, reinterpret_cast(AniWindowStage::UnregisterWindowCallback)}, + ani_native_function {"getSubWindowSync", "J:Lescompat/Array", + reinterpret_cast(AniWindowStage::GetSubWindow)}, }; for (auto method : methods) { if ((ret = env->Class_BindNativeMethods(cls, &method, 1u)) != ANI_OK) { -- Gitee