mirror of
https://github.com/openharmony/windowmanager.git
synced 2026-07-21 04:25:28 -04:00
c8d4f51e4a
Signed-off-by: qianlf <qianliangfang@huawei.com> Change-Id: I36c0f7a103be4593ccc299bf4b83f61020f7c3fe
1232 lines
56 KiB
C++
1232 lines
56 KiB
C++
/*
|
|
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
#include "js_window.h"
|
|
#include <new>
|
|
#include "window.h"
|
|
#include "window_manager_hilog.h"
|
|
#include "window_option.h"
|
|
namespace OHOS {
|
|
namespace Rosen {
|
|
using namespace AbilityRuntime;
|
|
namespace {
|
|
constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "JsWindow"};
|
|
constexpr Rect g_emptyRect = {0, 0, 0, 0};
|
|
}
|
|
|
|
static std::map<std::string, std::shared_ptr<NativeReference>> g_jsWindowMap;
|
|
std::recursive_mutex g_mutex;
|
|
JsWindow::JsWindow(const sptr<Window>& window, bool isOldApi)
|
|
: windowToken_(window), registerManager_(std::make_unique<JsWindowRegisterManager>()), isOldApi_(isOldApi)
|
|
{
|
|
NotifyNativeWinDestroyFunc func = [](std::string windowName) {
|
|
std::lock_guard<std::recursive_mutex> lock(g_mutex);
|
|
if (windowName.empty() || g_jsWindowMap.count(windowName) == 0) {
|
|
WLOGE("[NAPI]Can not find window %{public}s ", windowName.c_str());
|
|
return;
|
|
}
|
|
g_jsWindowMap.erase(windowName);
|
|
WLOGE("[NAPI]Destroy window %{public}s in js window", windowName.c_str());
|
|
};
|
|
windowToken_->RegisterWindowDestroyedListener(func);
|
|
}
|
|
|
|
JsWindow::~JsWindow()
|
|
{
|
|
WLOGFI("[NAPI]~JsWindow");
|
|
windowToken_ = nullptr;
|
|
}
|
|
|
|
std::string JsWindow::GetWindowName()
|
|
{
|
|
if (windowToken_ == nullptr) {
|
|
return "";
|
|
}
|
|
return windowToken_->GetWindowName();
|
|
}
|
|
|
|
void JsWindow::Finalizer(NativeEngine* engine, void* data, void* hint)
|
|
{
|
|
WLOGFI("[NAPI]Finalizer");
|
|
auto jsWin = std::unique_ptr<JsWindow>(static_cast<JsWindow*>(data));
|
|
if (jsWin == nullptr) {
|
|
WLOGFE("[NAPI]jsWin is nullptr");
|
|
return;
|
|
}
|
|
std::string windowName = jsWin->GetWindowName();
|
|
WLOGFI("[NAPI]Window %{public}s", windowName.c_str());
|
|
std::lock_guard<std::recursive_mutex> lock(g_mutex);
|
|
if (g_jsWindowMap.find(windowName) != g_jsWindowMap.end()) {
|
|
g_jsWindowMap.erase(windowName);
|
|
WLOGFI("[NAPI]Remove window %{public}s from g_jsWindowMap", windowName.c_str());
|
|
}
|
|
}
|
|
|
|
NativeValue* JsWindow::Show(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]Show");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnShow(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::Destroy(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]Destroy");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnDestroy(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::Hide(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]Hide");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnHide(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::MoveTo(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]MoveTo");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnMoveTo(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::Resize(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]Resize");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnResize(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::SetWindowType(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]SetWindowType");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnSetWindowType(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::SetWindowMode(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]SetWindowMode");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnSetWindowMode(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::GetProperties(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]GetProperties");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnGetProperties(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::RegisterWindowCallback(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]RegisterWindowCallback");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnRegisterWindowCallback(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::UnregisterWindowCallback(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]UnregisterWindowCallback");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnUnregisterWindowCallback(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::LoadContent(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]LoadContent");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnLoadContent(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::SetFullScreen(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]SetFullScreen");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnSetFullScreen(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::SetLayoutFullScreen(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]SetLayoutFullScreen");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnSetLayoutFullScreen(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::SetSystemBarEnable(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]SetSystemBarEnable");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnSetSystemBarEnable(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::SetSystemBarProperties(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]SetSystemBarProperties");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnSetSystemBarProperties(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::GetAvoidArea(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]GetAvoidArea");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnGetAvoidArea(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::IsShowing(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]IsShowing");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnIsShowing(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::IsSupportWideGamut(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]IsSupportWideGamut");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnIsSupportWideGamut(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::SetBackgroundColor(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]SetBackgroundColor");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnSetBackgroundColor(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::SetBrightness(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]SetBrightness");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnSetBrightness(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::SetDimBehind(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]SetDimBehind");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnSetDimBehind(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::SetFocusable(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]SetFocusable");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnSetFocusable(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::SetKeepScreenOn(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]SetKeepScreenOn");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnSetKeepScreenOn(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::SetOutsideTouchable(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]SetOutsideTouchable");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnSetOutsideTouchable(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::SetPrivacyMode(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]SetPrivacyMode");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnSetPrivacyMode(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::SetTouchable(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]SetTouchable");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnSetTouchable(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::SetColorSpace(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]SetColorSpace");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnSetColorSpace(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::GetColorSpace(NativeEngine* engine, NativeCallbackInfo* info)
|
|
{
|
|
WLOGFI("[NAPI]GetColorSpace");
|
|
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
|
|
return (me != nullptr) ? me->OnGetColorSpace(*engine, *info) : nullptr;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnShow(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
WMError errCode = WMError::WM_OK;
|
|
if (info.argc > 1) {
|
|
WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
|
|
WLOGFE("[NAPI]window is nullptr or get invalid param");
|
|
return;
|
|
}
|
|
WMError ret = windowToken_->Show();
|
|
if (ret == WMError::WM_OK) {
|
|
task.Resolve(engine, engine.CreateUndefined());
|
|
} else {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window show failed"));
|
|
}
|
|
WLOGFI("[NAPI]Window [%{public}u, %{public}s] show end, ret = %{public}d",
|
|
windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
|
|
};
|
|
|
|
NativeValue* lastParam = (info.argc == 0) ? nullptr :
|
|
(info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnDestroy(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
WMError errCode = WMError::WM_OK;
|
|
if (info.argc > 1) {
|
|
WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
|
|
WLOGFE("[NAPI]window is nullptr or get invalid param");
|
|
return;
|
|
}
|
|
WMError ret = windowToken_->Destroy();
|
|
WLOGFI("[NAPI]Window [%{public}u, %{public}s] destroy end, ret = %{public}d",
|
|
windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
|
|
if (ret != WMError::WM_OK) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window destroy failed"));
|
|
return;
|
|
}
|
|
|
|
windowToken_ = nullptr;
|
|
task.Resolve(engine, engine.CreateUndefined());
|
|
};
|
|
|
|
NativeValue* lastParam = (info.argc == 0) ? nullptr :
|
|
(info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnHide(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
WMError errCode = WMError::WM_OK;
|
|
if (info.argc > 1) {
|
|
WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
|
|
WLOGFE("[NAPI]window is nullptr or get invalid param");
|
|
return;
|
|
}
|
|
WMError ret = windowToken_->Hide();
|
|
if (ret == WMError::WM_OK) {
|
|
task.Resolve(engine, engine.CreateUndefined());
|
|
} else {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window hide failed"));
|
|
}
|
|
WLOGFI("[NAPI]Window [%{public}u, %{public}s] hide end, ret = %{public}d",
|
|
windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
|
|
};
|
|
|
|
NativeValue* lastParam = (info.argc == 0) ? nullptr :
|
|
(info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnMoveTo(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
WMError errCode = WMError::WM_OK;
|
|
if (info.argc < 2 || info.argc > 3) { // 2:minimum param num, 3: maximum param num
|
|
WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
int32_t x = 0;
|
|
if (errCode == WMError::WM_OK && !ConvertFromJsValue(engine, info.argv[0], x)) {
|
|
WLOGFE("[NAPI]Failed to convert parameter to x");
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
|
|
int32_t y = 0;
|
|
if (errCode == WMError::WM_OK && !ConvertFromJsValue(engine, info.argv[1], y)) {
|
|
WLOGFE("[NAPI]Failed to convert parameter to y");
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
|
|
WLOGFE("[NAPI]window is nullptr or get invalid param");
|
|
return;
|
|
}
|
|
WMError ret = windowToken_->MoveTo(x, y);
|
|
if (ret == WMError::WM_OK) {
|
|
task.Resolve(engine, engine.CreateUndefined());
|
|
} else {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window move failed"));
|
|
}
|
|
WLOGFI("[NAPI]Window [%{public}u, %{public}s] move end, ret = %{public}d",
|
|
windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
|
|
};
|
|
// 2: params num; 2: index of callback
|
|
NativeValue* lastParam = (info.argc <= 2) ? nullptr :
|
|
(info.argv[2]->TypeOf() == NATIVE_FUNCTION ? info.argv[2] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnResize(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
WMError errCode = WMError::WM_OK;
|
|
if (info.argc < 2 || info.argc > 3) { // 2: minimum param num, 3: maximum param num
|
|
WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
uint32_t width = 0;
|
|
if (errCode == WMError::WM_OK && !ConvertFromJsValue(engine, info.argv[0], width)) {
|
|
WLOGFE("[NAPI]Failed to convert parameter to width");
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
|
|
uint32_t height = 0;
|
|
if (errCode == WMError::WM_OK && !ConvertFromJsValue(engine, info.argv[1], height)) {
|
|
WLOGFE("[NAPI]Failed to convert parameter to height");
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
|
|
WLOGFE("[NAPI]window is nullptr or get invalid param");
|
|
return;
|
|
}
|
|
WMError ret = windowToken_->Resize(width, height);
|
|
if (ret == WMError::WM_OK) {
|
|
task.Resolve(engine, engine.CreateUndefined());
|
|
} else {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window resize failed"));
|
|
}
|
|
WLOGFI("[NAPI]Window [%{public}u, %{public}s] resize end, ret = %{public}d",
|
|
windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
|
|
};
|
|
// 2: params num; 2: index of callback
|
|
NativeValue* lastParam = (info.argc <= 2) ? nullptr :
|
|
(info.argv[2]->TypeOf() == NATIVE_FUNCTION ? info.argv[2] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnSetWindowType(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
WMError errCode = WMError::WM_OK;
|
|
if (info.argc < 1 || info.argc > 2) { // 2 is max num of argc
|
|
WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
WindowType winType = WindowType::SYSTEM_WINDOW_BASE;
|
|
if (errCode == WMError::WM_OK) {
|
|
NativeNumber* nativeType = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
|
|
if (nativeType == nullptr) {
|
|
WLOGFE("[NAPI]Failed to convert parameter to windowType");
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
} else if (static_cast<uint32_t>(*nativeType) >= static_cast<uint32_t>(WindowType::SYSTEM_WINDOW_BASE)) {
|
|
winType = static_cast<WindowType>(static_cast<uint32_t>(*nativeType)); // adapt to the old version
|
|
} else {
|
|
if (JS_TO_NATIVE_WINDOW_TYPE_MAP.count(
|
|
static_cast<ApiWindowType>(static_cast<uint32_t>(*nativeType))) != 0) {
|
|
winType = JS_TO_NATIVE_WINDOW_TYPE_MAP.at(
|
|
static_cast<ApiWindowType>(static_cast<uint32_t>(*nativeType)));
|
|
} else {
|
|
WLOGFE("[NAPI]Do not surppot this type: %{public}u", static_cast<uint32_t>(*nativeType));
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
}
|
|
}
|
|
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
|
|
WLOGFE("[NAPI]window is nullptr or get invalid param");
|
|
return;
|
|
}
|
|
WMError ret = windowToken_->SetWindowType(winType);
|
|
if (ret == WMError::WM_OK) {
|
|
task.Resolve(engine, engine.CreateUndefined());
|
|
} else {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window set type failed"));
|
|
}
|
|
WLOGFI("[NAPI]Window [%{public}u, %{public}s] set type end, ret = %{public}d",
|
|
windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
|
|
};
|
|
|
|
NativeValue* lastParam = (info.argc <= 1) ? nullptr :
|
|
(info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnSetWindowMode(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
WMError errCode = WMError::WM_OK;
|
|
if (info.argc < 1 || info.argc > 2) { // 2 is max num of argc
|
|
WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
WindowMode winMode = WindowMode::WINDOW_MODE_FULLSCREEN;
|
|
if (errCode == WMError::WM_OK) {
|
|
NativeNumber* nativeMode = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
|
|
if (nativeMode == nullptr) {
|
|
WLOGFE("[NAPI]Failed to convert parameter to windowMode");
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
} else {
|
|
if (static_cast<uint32_t>(*nativeMode) >= static_cast<uint32_t>(WindowMode::WINDOW_MODE_SPLIT_PRIMARY)) {
|
|
winMode = static_cast<WindowMode>(static_cast<uint32_t>(*nativeMode));
|
|
} else if (static_cast<uint32_t>(*nativeMode) >= static_cast<uint32_t>(ApiWindowMode::UNDEFINED) &&
|
|
static_cast<uint32_t>(*nativeMode) <= static_cast<uint32_t>(ApiWindowMode::MODE_END)) {
|
|
winMode = JS_TO_NATIVE_WINDOW_MODE_MAP.at(
|
|
static_cast<ApiWindowMode>(static_cast<uint32_t>(*nativeMode)));
|
|
} else {
|
|
WLOGFE("[NAPI]Do not surppot this mode: %{public}u", static_cast<uint32_t>(*nativeMode));
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
}
|
|
}
|
|
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
|
|
WLOGFE("[NAPI]window is nullptr or get invalid param");
|
|
return;
|
|
}
|
|
WMError ret = windowToken_->SetWindowMode(winMode);
|
|
if (ret == WMError::WM_OK) {
|
|
task.Resolve(engine, engine.CreateUndefined());
|
|
} else {
|
|
task.Reject(engine,
|
|
CreateJsError(engine, static_cast<int32_t>(ret), "Window set mode failed"));
|
|
}
|
|
WLOGFI("[NAPI]Window [%{public}u, %{public}s] set type end, ret = %{public}d",
|
|
windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
|
|
};
|
|
|
|
NativeValue* lastParam = (info.argc == 1) ? nullptr :
|
|
(info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnGetProperties(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
WMError errCode = WMError::WM_OK;
|
|
if (info.argc > 1) {
|
|
WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
|
|
WLOGFE("[NAPI]window is nullptr or get invalid param");
|
|
return;
|
|
}
|
|
auto objValue = CreateJsWindowPropertiesObject(engine, windowToken_);
|
|
if (objValue != nullptr) {
|
|
task.Resolve(engine, objValue);
|
|
} else {
|
|
task.Reject(engine, CreateJsError(engine,
|
|
static_cast<int32_t>(WMError::WM_ERROR_NULLPTR), "Window get properties failed"));
|
|
}
|
|
WLOGFI("[NAPI]Window [%{public}u, %{public}s] get properties end, objValue = %{public}p",
|
|
windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), objValue);
|
|
};
|
|
|
|
NativeValue* lastParam = (info.argc == 0) ? nullptr :
|
|
(info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnRegisterWindowCallback(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
if (windowToken_ == nullptr) {
|
|
WLOGFE("[NAPI]Window is nullptr");
|
|
return engine.CreateUndefined();
|
|
}
|
|
if (info.argc != 2) { // 2: params num
|
|
WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
|
|
return engine.CreateUndefined();
|
|
}
|
|
std::string cbType;
|
|
if (!ConvertFromJsValue(engine, info.argv[0], cbType)) {
|
|
WLOGFE("[NAPI]Failed to convert parameter to callbackType");
|
|
return engine.CreateUndefined();
|
|
}
|
|
NativeValue* value = info.argv[1];
|
|
if (!value->IsCallable()) {
|
|
WLOGFI("[NAPI]Callback(info->argv[1]) is not callable");
|
|
return engine.CreateUndefined();
|
|
}
|
|
registerManager_->RegisterListener(windowToken_, cbType, CaseType::CASE_WINDOW, engine, value);
|
|
WLOGFI("[NAPI]Register end, window [%{public}u, %{public}s], type = %{public}s, callback = %{public}p",
|
|
windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), cbType.c_str(), value);
|
|
return engine.CreateUndefined();
|
|
}
|
|
|
|
NativeValue* JsWindow::OnUnregisterWindowCallback(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
if (windowToken_ == nullptr || info.argc < 1 || info.argc > 2) { // 2: maximum params nums
|
|
WLOGFE("[NAPI]Window is nullptr or argc is invalid: %{public}zu", info.argc);
|
|
return engine.CreateUndefined();
|
|
}
|
|
std::string cbType;
|
|
if (!ConvertFromJsValue(engine, info.argv[0], cbType)) {
|
|
WLOGFE("[NAPI]Failed to convert parameter to callbackType");
|
|
return engine.CreateUndefined();
|
|
}
|
|
|
|
NativeValue* value = nullptr;
|
|
if (info.argc == 1) {
|
|
registerManager_->UnregisterListener(windowToken_, cbType, CaseType::CASE_WINDOW, value);
|
|
} else {
|
|
value = info.argv[1];
|
|
if (!value->IsCallable()) {
|
|
WLOGFI("[NAPI]Callback(info->argv[1]) is not callable");
|
|
return engine.CreateUndefined();
|
|
}
|
|
registerManager_->UnregisterListener(windowToken_, cbType, CaseType::CASE_WINDOW, value);
|
|
}
|
|
WLOGFI("[NAPI]Unregister end, window [%{public}u, %{public}s], type = %{public}s, callback = %{public}p",
|
|
windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), cbType.c_str(), value);
|
|
return engine.CreateUndefined();
|
|
}
|
|
|
|
NativeValue* JsWindow::OnLoadContent(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
WMError errCode = WMError::WM_OK;
|
|
if (info.argc < 1 || info.argc > 3) { // 3 maximum param num
|
|
WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
std::string contextUrl;
|
|
if (errCode == WMError::WM_OK && !ConvertFromJsValue(engine, info.argv[0], contextUrl)) {
|
|
WLOGFE("[NAPI]Failed to convert parameter to context url");
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
NativeValue* storage = nullptr;
|
|
NativeValue* callBack = nullptr;
|
|
if (info.argc == 2) { // 2: num of params
|
|
NativeValue* value = info.argv[1];
|
|
if (value->TypeOf() == NATIVE_FUNCTION) {
|
|
callBack = info.argv[1];
|
|
} else {
|
|
storage = info.argv[1];
|
|
}
|
|
} else if (info.argc == 3) { // 3: num of params
|
|
storage = info.argv[1];
|
|
// 2: index of callback
|
|
callBack = (info.argv[2]->TypeOf() == NATIVE_FUNCTION ? info.argv[2] : nullptr);
|
|
}
|
|
std::shared_ptr<NativeReference> contentStorage = (storage == nullptr) ? nullptr :
|
|
std::shared_ptr<NativeReference>(engine.CreateReference(storage, 1));
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
if (windowToken_ == nullptr || errCode != WMError::WM_OK || isOldApi_) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
|
|
WLOGFE("[NAPI]Window is nullptr or get invalid param or FA mode");
|
|
return;
|
|
}
|
|
NativeValue* nativeStorage = (contentStorage == nullptr) ? nullptr : contentStorage->Get();
|
|
Rosen::WMError ret = windowToken_->SetUIContent(contextUrl, &engine, nativeStorage, false);
|
|
if (ret == WMError::WM_OK) {
|
|
task.Resolve(engine, engine.CreateUndefined());
|
|
} else {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window load content failed"));
|
|
}
|
|
WLOGFI("[NAPI]Window [%{public}u, %{public}s] load content end, ret = %{public}d",
|
|
windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
|
|
};
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, callBack, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnSetFullScreen(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
WMError errCode = WMError::WM_OK;
|
|
if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
|
|
WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
bool isFullScreen = false;
|
|
if (errCode == WMError::WM_OK) {
|
|
NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
|
|
if (nativeVal == nullptr) {
|
|
WLOGFE("[NAPI]Failed to convert parameter to isFullScreen");
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
} else {
|
|
isFullScreen = static_cast<bool>(*nativeVal);
|
|
}
|
|
}
|
|
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params."));
|
|
return;
|
|
}
|
|
WMError ret = windowToken_->SetFullScreen(isFullScreen);
|
|
if (ret == WMError::WM_OK) {
|
|
task.Resolve(engine, engine.CreateUndefined());
|
|
} else {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window SetFullScreen failed."));
|
|
}
|
|
WLOGFI("[NAPI]Window [%{public}u, %{public}s] set full screen end, ret = %{public}d",
|
|
windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
|
|
};
|
|
|
|
NativeValue* lastParam = (info.argc <= 1) ? nullptr :
|
|
(info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnSetLayoutFullScreen(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
WMError errCode = WMError::WM_OK;
|
|
if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
|
|
WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
bool isLayoutFullScreen = false;
|
|
if (errCode == WMError::WM_OK) {
|
|
NativeBoolean* nativeVal = ConvertNativeValueTo<NativeBoolean>(info.argv[0]);
|
|
if (nativeVal == nullptr) {
|
|
WLOGFE("[NAPI]Failed to convert parameter to isLayoutFullScreen");
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
} else {
|
|
isLayoutFullScreen = static_cast<bool>(*nativeVal);
|
|
}
|
|
}
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "Invalidate params."));
|
|
return;
|
|
}
|
|
WMError ret = windowToken_->SetLayoutFullScreen(isLayoutFullScreen);
|
|
if (ret == WMError::WM_OK) {
|
|
task.Resolve(engine, engine.CreateUndefined());
|
|
} else {
|
|
task.Reject(engine, CreateJsError(engine,
|
|
static_cast<int32_t>(ret), "Window OnSetLayoutFullScreen failed."));
|
|
}
|
|
WLOGFI("[NAPI]Window [%{public}u, %{public}s] set layout full screen end, ret = %{public}d",
|
|
windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
|
|
};
|
|
NativeValue* lastParam = (info.argc <= 1) ? nullptr :
|
|
(info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnSetSystemBarEnable(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
WMError errCode = WMError::WM_OK;
|
|
if (info.argc < 1 || info.argc > 2 || windowToken_ == nullptr) { // 2: maximum params num
|
|
WLOGFE("[NAPI]Window is nullptr or argc is invalid: %{public}zu", info.argc);
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
std::map<WindowType, SystemBarProperty> systemBarProperties;
|
|
if (errCode == WMError::WM_OK && !GetSystemBarStatus(systemBarProperties, engine, info, windowToken_)) {
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
|
|
WLOGFE("[NAPI]window is nullptr or get invalid param");
|
|
return;
|
|
}
|
|
WMError ret = windowToken_->SetSystemBarProperty(WindowType::WINDOW_TYPE_STATUS_BAR,
|
|
systemBarProperties.at(WindowType::WINDOW_TYPE_STATUS_BAR));
|
|
ret = windowToken_->SetSystemBarProperty(WindowType::WINDOW_TYPE_NAVIGATION_BAR,
|
|
systemBarProperties.at(WindowType::WINDOW_TYPE_NAVIGATION_BAR));
|
|
if (ret == WMError::WM_OK) {
|
|
task.Resolve(engine, engine.CreateUndefined());
|
|
} else {
|
|
task.Reject(engine, CreateJsError(engine,
|
|
static_cast<int32_t>(ret), "JsWindow::OnSetSystemBarEnable failed"));
|
|
}
|
|
WLOGFI("[NAPI]Window [%{public}u, %{public}s] set system bar enable end, ret = %{public}d",
|
|
windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
|
|
};
|
|
|
|
NativeValue* lastParam = (info.argc <= 1) ? nullptr :
|
|
(info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnSetSystemBarProperties(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
WMError errCode = WMError::WM_OK;
|
|
if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
|
|
WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
std::map<WindowType, SystemBarProperty> systemBarProperties;
|
|
if (errCode == WMError::WM_OK) {
|
|
NativeObject* nativeObj = ConvertNativeValueTo<NativeObject>(info.argv[0]);
|
|
if (nativeObj == nullptr) {
|
|
WLOGFE("[NAPI]Failed to convert object to SystemBarProperties");
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
} else {
|
|
if (!SetSystemBarPropertiesFromJs(engine, nativeObj, systemBarProperties, windowToken_)) {
|
|
WLOGFE("[NAPI]Failed to GetSystemBarProperties From Js Object");
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
}
|
|
}
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
|
|
WLOGFE("[NAPI]window is nullptr or get invalid param");
|
|
}
|
|
WMError ret = windowToken_->SetSystemBarProperty(WindowType::WINDOW_TYPE_STATUS_BAR,
|
|
systemBarProperties.at(WindowType::WINDOW_TYPE_STATUS_BAR));
|
|
ret = windowToken_->SetSystemBarProperty(WindowType::WINDOW_TYPE_NAVIGATION_BAR,
|
|
systemBarProperties.at(WindowType::WINDOW_TYPE_NAVIGATION_BAR));
|
|
if (ret == WMError::WM_OK) {
|
|
task.Resolve(engine, engine.CreateUndefined());
|
|
} else {
|
|
task.Reject(engine, CreateJsError(engine,
|
|
static_cast<int32_t>(WMError::WM_ERROR_NULLPTR), "JsWindow::OnSetSystemBarProperties failed"));
|
|
}
|
|
WLOGFI("[NAPI]Window [%{public}u, %{public}s] set system bar properties end, ret = %{public}d",
|
|
windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
|
|
};
|
|
|
|
NativeValue* lastParam = (info.argc <= 1) ? nullptr :
|
|
(info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnGetAvoidArea(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
WMError errCode = WMError::WM_OK;
|
|
if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
|
|
WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
AvoidAreaType avoidAreaType = AvoidAreaType::TYPE_SYSTEM;
|
|
if (errCode == WMError::WM_OK) {
|
|
// Parse info->argv[0] as AvoidAreaType
|
|
NativeNumber* nativeMode = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
|
|
if (nativeMode == nullptr) {
|
|
WLOGFE("[NAPI]Failed to convert parameter to AvoidAreaType");
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
} else {
|
|
avoidAreaType = static_cast<AvoidAreaType>(static_cast<uint32_t>(*nativeMode));
|
|
}
|
|
}
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
|
|
WLOGFE("[NAPI]window is nullptr or get invalid param");
|
|
return;
|
|
}
|
|
// getAvoidRect by avoidAreaType
|
|
AvoidArea avoidArea;
|
|
WMError ret = windowToken_->GetAvoidAreaByType(avoidAreaType, avoidArea);
|
|
if (ret != WMError::WM_OK) {
|
|
avoidArea = { g_emptyRect, g_emptyRect, g_emptyRect, g_emptyRect }; // left, top, right, bottom
|
|
}
|
|
// native avoidArea -> js avoidArea
|
|
NativeValue* avoidAreaObj = ChangeAvoidAreaToJsValue(engine, avoidArea);
|
|
if (avoidAreaObj != nullptr) {
|
|
task.Resolve(engine, avoidAreaObj);
|
|
} else {
|
|
task.Reject(engine, CreateJsError(engine,
|
|
static_cast<int32_t>(WMError::WM_ERROR_NULLPTR), "JsWindow::OnGetAvoidArea failed"));
|
|
}
|
|
WLOGFI("[NAPI]Window [%{public}u, %{public}s] get avoid area end, ret = %{public}d",
|
|
windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), ret);
|
|
};
|
|
NativeValue* lastParam = (info.argc <= 1) ? nullptr :
|
|
(info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnIsShowing(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
WMError errCode = WMError::WM_OK;
|
|
if (info.argc > 1) {
|
|
WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
|
|
WLOGFE("[NAPI]window is nullptr or get invalid param");
|
|
return;
|
|
}
|
|
bool state = windowToken_->GetShowState();
|
|
task.Resolve(engine, CreateJsValue(engine, state));
|
|
WLOGFI("[NAPI]Window [%{public}u, %{public}s] get show state end, state = %{public}u",
|
|
windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), state);
|
|
};
|
|
|
|
NativeValue* lastParam = (info.argc == 0) ? nullptr :
|
|
(info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnIsSupportWideGamut(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
WMError errCode = WMError::WM_OK;
|
|
if (info.argc > 1) {
|
|
WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
|
|
WLOGFE("[NAPI]window is nullptr or get invalid param");
|
|
return;
|
|
}
|
|
bool flag = windowToken_->IsSupportWideGamut();
|
|
task.Resolve(engine, CreateJsValue(engine, flag));
|
|
WLOGFI("[NAPI]Window [%{public}u, %{public}s] OnIsSupportWideGamut end, ret = %{public}u",
|
|
windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), flag);
|
|
};
|
|
|
|
NativeValue* lastParam = (info.argc == 0) ? nullptr :
|
|
(info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnSetBackgroundColor(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
|
|
};
|
|
|
|
NativeValue* lastParam = (info.argc <= 1) ? nullptr :
|
|
(info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnSetBrightness(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
|
|
};
|
|
|
|
NativeValue* lastParam = (info.argc <= 1) ? nullptr :
|
|
(info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnSetDimBehind(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
|
|
};
|
|
|
|
NativeValue* lastParam = (info.argc <= 1) ? nullptr :
|
|
(info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnSetFocusable(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
|
|
};
|
|
|
|
NativeValue* lastParam = (info.argc <= 1) ? nullptr :
|
|
(info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnSetKeepScreenOn(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
|
|
};
|
|
|
|
NativeValue* lastParam = (info.argc <= 1) ? nullptr :
|
|
(info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnSetOutsideTouchable(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
|
|
};
|
|
|
|
NativeValue* lastParam = (info.argc <= 1) ? nullptr :
|
|
(info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnSetPrivacyMode(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
|
|
};
|
|
|
|
NativeValue* lastParam = (info.argc <= 1) ? nullptr :
|
|
(info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnSetTouchable(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
|
|
};
|
|
|
|
NativeValue* lastParam = (info.argc <= 1) ? nullptr :
|
|
(info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnSetColorSpace(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
WMError errCode = WMError::WM_OK;
|
|
ColorSpace colorSpace = ColorSpace::COLOR_SPACE_DEFAULT;
|
|
if (info.argc < 1 || info.argc > 2) { // 2: maximum params num
|
|
WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
} else {
|
|
NativeNumber* nativeType = ConvertNativeValueTo<NativeNumber>(info.argv[0]);
|
|
if (nativeType == nullptr) {
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
WLOGFE("[NAPI]Failed to convert parameter to ColorSpace");
|
|
} else {
|
|
colorSpace = static_cast<ColorSpace>(static_cast<uint32_t>(*nativeType));
|
|
if (colorSpace > ColorSpace::COLOR_SPACE_WIDE_GAMUT) {
|
|
WLOGFE("[NAPI]ColorSpace %{public}u invalid!", static_cast<uint32_t>(colorSpace));
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
}
|
|
}
|
|
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode), "OnSetColorSpace failed"));
|
|
WLOGFE("[NAPI]window is nullptr or get invalid param");
|
|
return;
|
|
}
|
|
windowToken_->SetColorSpace(colorSpace);
|
|
task.Resolve(engine, engine.CreateUndefined());
|
|
WLOGFI("[NAPI]Window [%{public}u, %{public}s] OnSetColorSpace end, colorSpace = %{public}u",
|
|
windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), static_cast<uint32_t>(colorSpace));
|
|
};
|
|
|
|
NativeValue* lastParam = (info.argc <= 1) ? nullptr :
|
|
(info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
NativeValue* JsWindow::OnGetColorSpace(NativeEngine& engine, NativeCallbackInfo& info)
|
|
{
|
|
WMError errCode = WMError::WM_OK;
|
|
if (info.argc > 1) {
|
|
WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
|
|
errCode = WMError::WM_ERROR_INVALID_PARAM;
|
|
}
|
|
AsyncTask::CompleteCallback complete =
|
|
[=](NativeEngine& engine, AsyncTask& task, int32_t status) {
|
|
if (windowToken_ == nullptr || errCode != WMError::WM_OK) {
|
|
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
|
|
WLOGFE("[NAPI]window is nullptr or get invalid param");
|
|
return;
|
|
}
|
|
ColorSpace colorSpace = windowToken_->GetColorSpace();
|
|
task.Resolve(engine, CreateJsValue(engine, static_cast<uint32_t>(colorSpace)));
|
|
WLOGFI("[NAPI]Window [%{public}u, %{public}s] OnGetColorSpace end, colorSpace = %{public}u",
|
|
windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str(), static_cast<uint32_t>(colorSpace));
|
|
};
|
|
|
|
NativeValue* lastParam = (info.argc == 0) ? nullptr :
|
|
(info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
|
|
NativeValue* result = nullptr;
|
|
AsyncTask::Schedule(
|
|
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
|
|
return result;
|
|
}
|
|
|
|
std::shared_ptr<NativeReference> FindJsWindowObject(std::string windowName)
|
|
{
|
|
WLOGFI("[NAPI]Try to find window %{public}s in g_jsWindowMap", windowName.c_str());
|
|
std::lock_guard<std::recursive_mutex> lock(g_mutex);
|
|
if (g_jsWindowMap.find(windowName) == g_jsWindowMap.end()) {
|
|
WLOGFI("[NAPI]Can not find window %{public}s in g_jsWindowMap", windowName.c_str());
|
|
return nullptr;
|
|
}
|
|
return g_jsWindowMap[windowName];
|
|
}
|
|
|
|
NativeValue* CreateJsWindowObject(NativeEngine& engine, sptr<Window>& window, bool isOldApi)
|
|
{
|
|
WLOGFI("[NAPI]CreateJsWindowObject");
|
|
std::string windowName = window->GetWindowName();
|
|
// avoid repeatedly create js window when getWindow
|
|
std::shared_ptr<NativeReference> jsWindowObj = FindJsWindowObject(windowName);
|
|
if (jsWindowObj != nullptr && jsWindowObj->Get() != nullptr) {
|
|
WLOGFI("[NAPI]FindJsWindowObject %{public}s", windowName.c_str());
|
|
return jsWindowObj->Get();
|
|
}
|
|
NativeValue* objValue = engine.CreateObject();
|
|
NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
|
|
|
|
WLOGFI("[NAPI]CreateJsWindow %{public}s, FA mode: %{public}u", windowName.c_str(), isOldApi);
|
|
std::unique_ptr<JsWindow> jsWindow = std::make_unique<JsWindow>(window, isOldApi);
|
|
object->SetNativePointer(jsWindow.release(), JsWindow::Finalizer, nullptr);
|
|
|
|
BindNativeFunction(engine, *object, "show", JsWindow::Show);
|
|
BindNativeFunction(engine, *object, "destroy", JsWindow::Destroy);
|
|
BindNativeFunction(engine, *object, "hide", JsWindow::Hide);
|
|
BindNativeFunction(engine, *object, "moveTo", JsWindow::MoveTo);
|
|
BindNativeFunction(engine, *object, "resetSize", JsWindow::Resize);
|
|
BindNativeFunction(engine, *object, "setWindowType", JsWindow::SetWindowType);
|
|
BindNativeFunction(engine, *object, "setWindowMode", JsWindow::SetWindowMode);
|
|
BindNativeFunction(engine, *object, "getProperties", JsWindow::GetProperties);
|
|
BindNativeFunction(engine, *object, "on", JsWindow::RegisterWindowCallback);
|
|
BindNativeFunction(engine, *object, "off", JsWindow::UnregisterWindowCallback);
|
|
BindNativeFunction(engine, *object, "loadContent", JsWindow::LoadContent);
|
|
BindNativeFunction(engine, *object, "setFullScreen", JsWindow::SetFullScreen);
|
|
BindNativeFunction(engine, *object, "setLayoutFullScreen", JsWindow::SetLayoutFullScreen);
|
|
BindNativeFunction(engine, *object, "setSystemBarEnable", JsWindow::SetSystemBarEnable);
|
|
BindNativeFunction(engine, *object, "setSystemBarProperties", JsWindow::SetSystemBarProperties);
|
|
BindNativeFunction(engine, *object, "getAvoidArea", JsWindow::GetAvoidArea);
|
|
BindNativeFunction(engine, *object, "isShowing", JsWindow::IsShowing);
|
|
BindNativeFunction(engine, *object, "isSupportWideGamut", JsWindow::IsSupportWideGamut);
|
|
BindNativeFunction(engine, *object, "setColorSpace", JsWindow::SetColorSpace);
|
|
BindNativeFunction(engine, *object, "getColorSpace", JsWindow::GetColorSpace);
|
|
BindNativeFunction(engine, *object, "setBackgroundColor", JsWindow::SetBackgroundColor);
|
|
BindNativeFunction(engine, *object, "setBrightness", JsWindow::SetBrightness);
|
|
BindNativeFunction(engine, *object, "setDimBehind", JsWindow::SetDimBehind);
|
|
BindNativeFunction(engine, *object, "setFocusable", JsWindow::SetFocusable);
|
|
BindNativeFunction(engine, *object, "setKeepScreenOn", JsWindow::SetKeepScreenOn);
|
|
BindNativeFunction(engine, *object, "setOutsideTouchable", JsWindow::SetOutsideTouchable);
|
|
BindNativeFunction(engine, *object, "setPrivacyMode", JsWindow::SetPrivacyMode);
|
|
BindNativeFunction(engine, *object, "setTouchable", JsWindow::SetTouchable);
|
|
std::shared_ptr<NativeReference> jsWindowRef;
|
|
jsWindowRef.reset(engine.CreateReference(objValue, 1));
|
|
std::lock_guard<std::recursive_mutex> lock(g_mutex);
|
|
g_jsWindowMap[windowName] = jsWindowRef;
|
|
return objValue;
|
|
}
|
|
} // namespace Rosen
|
|
} // namespace OHOS
|