From ed11c195321e05dc6be22ba9f005385a55708d99 Mon Sep 17 00:00:00 2001 From: leafly2021 Date: Thu, 10 Feb 2022 10:16:30 +0800 Subject: [PATCH] add setWindowLayoutMode Signed-off-by: leafly2021 Change-Id: If7acbcfb1022a1195e0c6702f5727ccacd44cc0c --- interfaces/innerkits/wm/window_manager.h | 1 + .../napi/window_runtime/api/@ohos.window.d.ts | 4 +- .../window_manager_napi/js_window_manager.cpp | 49 +++++++++++++++++++ .../window_manager_napi/js_window_manager.h | 2 + wm/src/window_manager.cpp | 6 +++ 5 files changed, 60 insertions(+), 2 deletions(-) diff --git a/interfaces/innerkits/wm/window_manager.h b/interfaces/innerkits/wm/window_manager.h index 412ae88f..931d6ef0 100644 --- a/interfaces/innerkits/wm/window_manager.h +++ b/interfaces/innerkits/wm/window_manager.h @@ -79,6 +79,7 @@ public: void RegisterWindowUpdateListener(const sptr& listener); void UnregisterWindowUpdateListener(const sptr& listener); void MinimizeAllAppWindows(DisplayId displayId); + void SetWindowLayoutMode(WindowLayoutMode mode, DisplayId displayId); private: WindowManager(); diff --git a/interfaces/kits/napi/window_runtime/api/@ohos.window.d.ts b/interfaces/kits/napi/window_runtime/api/@ohos.window.d.ts index 8d77144e..cba14165 100644 --- a/interfaces/kits/napi/window_runtime/api/@ohos.window.d.ts +++ b/interfaces/kits/napi/window_runtime/api/@ohos.window.d.ts @@ -202,7 +202,7 @@ declare namespace window { * @systemapi Hide this for inner system use. * @since 8 */ - function setWindowLayoutMode(mode: WindowLayoutMode, callback: AsyncCallback): void; + function setWindowLayoutMode(mode: WindowLayoutMode, displayId: number, callback: AsyncCallback): void; /** * Set the layout mode of a window. @@ -211,7 +211,7 @@ declare namespace window { * @systemapi Hide this for inner system use. * @since 8 */ - function setWindowLayoutMode(mode: WindowLayoutMode): Promise; + function setWindowLayoutMode(mode: WindowLayoutMode, displayId: number): Promise; /** * register the callback of systemBarTintChange diff --git a/interfaces/kits/napi/window_runtime/window_manager_napi/js_window_manager.cpp b/interfaces/kits/napi/window_runtime/window_manager_napi/js_window_manager.cpp index fac457e5..a8e47856 100644 --- a/interfaces/kits/napi/window_runtime/window_manager_napi/js_window_manager.cpp +++ b/interfaces/kits/napi/window_runtime/window_manager_napi/js_window_manager.cpp @@ -71,6 +71,12 @@ NativeValue* JsWindowManager::GetTopWindow(NativeEngine* engine, NativeCallbackI return (me != nullptr) ? me->OnGetTopWindow(*engine, *info) : nullptr; } +NativeValue* JsWindowManager::SetWindowLayoutMode(NativeEngine* engine, NativeCallbackInfo* info) +{ + JsWindowManager* me = CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnSetWindowLayoutMode(*engine, *info) : nullptr; +} + static bool GetAPI7Ability(NativeEngine& engine, AppExecFwk::Ability* &ability) { napi_value global; @@ -477,6 +483,48 @@ NativeValue* JsWindowManager::OnGetTopWindow(NativeEngine& engine, NativeCallbac return result; } +NativeValue* JsWindowManager::OnSetWindowLayoutMode(NativeEngine& engine, NativeCallbackInfo& info) +{ + WLOGFI("JsWindowManager::OnSetWindowLayoutMode is called"); + WMError errCode = WMError::WM_OK; + if (info.argc < ARGC_TWO) { + WLOGFE("JsWindowManager::OnSetWindowLayoutMode params too small"); + errCode = WMError::WM_ERROR_INVALID_PARAM; + } + WindowLayoutMode winLayoutMode = WindowLayoutMode::CASCADE; + int64_t displayId = 0; + if (errCode == WMError::WM_OK) { + NativeNumber* nativeMode = ConvertNativeValueTo(info.argv[0]); + if (nativeMode == nullptr) { + WLOGFE("Failed to convert parameter to windowLayoutMode"); + errCode = WMError::WM_ERROR_INVALID_PARAM; + } else { + winLayoutMode = static_cast(static_cast(*nativeMode)); + } + + if (!ConvertFromJsValue(engine, info.argv[1], displayId)) { + WLOGFE("Failed to convert parameter to displayId"); + errCode = WMError::WM_ERROR_INVALID_PARAM; + } + } + + AsyncTask::CompleteCallback complete = + [=](NativeEngine& engine, AsyncTask& task, int32_t status) { + if (errCode != WMError::WM_OK) { + task.Reject(engine, CreateJsError(engine, static_cast(errCode), "Invalidate params.")); + return; + } + SingletonContainer::Get().SetWindowLayoutMode(winLayoutMode, + static_cast(displayId)); + task.Resolve(engine, engine.CreateUndefined()); + WLOGFI("JsWindowManager::OnSetWindowLayoutMode success"); + }; + NativeValue* lastParam = (info.argc < ARGC_THREE) ? nullptr : info.argv[INDEX_TWO]; + NativeValue* result = nullptr; + AsyncTask::Schedule( + engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); + return result; +} NativeValue* JsWindowManagerInit(NativeEngine* engine, NativeValue* exportObj) { @@ -502,6 +550,7 @@ NativeValue* JsWindowManagerInit(NativeEngine* engine, NativeValue* exportObj) BindNativeFunction(*engine, *object, "off", JsWindowManager::UnregisterWindowMangerCallback); BindNativeFunction(*engine, *object, "getTopWindow", JsWindowManager::GetTopWindow); BindNativeFunction(*engine, *object, "minimizeAll", JsWindowManager::MinimizeAll); + BindNativeFunction(*engine, *object, "setWindowLayoutMode", JsWindowManager::SetWindowLayoutMode); return engine->CreateUndefined(); } } // namespace Rosen diff --git a/interfaces/kits/napi/window_runtime/window_manager_napi/js_window_manager.h b/interfaces/kits/napi/window_runtime/window_manager_napi/js_window_manager.h index f627f92b..8b58e5c3 100644 --- a/interfaces/kits/napi/window_runtime/window_manager_napi/js_window_manager.h +++ b/interfaces/kits/napi/window_runtime/window_manager_napi/js_window_manager.h @@ -36,6 +36,7 @@ public: static NativeValue* RegisterWindowManagerCallback(NativeEngine* engine, NativeCallbackInfo* info); static NativeValue* UnregisterWindowMangerCallback(NativeEngine* engine, NativeCallbackInfo* info); static NativeValue* GetTopWindow(NativeEngine* engine, NativeCallbackInfo* info); + static NativeValue* SetWindowLayoutMode(NativeEngine* engine, NativeCallbackInfo* info); private: bool GetNativeContext(NativeValue* nativeContext); @@ -50,6 +51,7 @@ private: NativeValue* OnRegisterWindowMangerCallback(NativeEngine& engine, NativeCallbackInfo& info); NativeValue* OnUnregisterWindowManagerCallback(NativeEngine& engine, NativeCallbackInfo& info); NativeValue* OnGetTopWindow(NativeEngine& engine, NativeCallbackInfo& info); + NativeValue* OnSetWindowLayoutMode(NativeEngine& engine, NativeCallbackInfo& info); std::weak_ptr context_; std::map, sptr>> jsCbMap_; std::mutex mtx_; diff --git a/wm/src/window_manager.cpp b/wm/src/window_manager.cpp index 402af7da..a45954f1 100644 --- a/wm/src/window_manager.cpp +++ b/wm/src/window_manager.cpp @@ -209,6 +209,12 @@ void WindowManager::MinimizeAllAppWindows(DisplayId displayId) WLOGFI("displayId %{public}" PRIu64"", displayId); SingletonContainer::Get().MinimizeAllAppWindows(displayId); } + +void WindowManager::SetWindowLayoutMode(WindowLayoutMode mode, DisplayId displayId) +{ + WLOGFI("set window layout mode: %{public}d, displayId %{public}" PRIu64"", mode, displayId); +} + void WindowManager::RegisterWindowUpdateListener(const sptr &listener) { if (listener == nullptr) {