!5777 窗口新增黑白模式接口

Merge pull request !5777 from yuantianzhu1@huawei.com/master
This commit is contained in:
openharmony_ci 2024-04-23 13:55:18 +00:00 committed by Gitee
commit ecfc476744
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
8 changed files with 179 additions and 0 deletions

View File

@ -1927,6 +1927,13 @@ public:
{
return WMError::WM_OK;
}
/**
* @brief Set gray scale of window
* @param grayScale gray scale of window.
* @return WM_OK means set success, others means set failed.
*/
virtual WMError SetGrayScale(float grayScale) { return WMError::WM_ERROR_DEVICE_NOT_SUPPORT; }
};
}
}

View File

@ -50,6 +50,8 @@ namespace {
constexpr size_t INDEX_ZERO = 0;
constexpr size_t INDEX_ONE = 1;
constexpr size_t INDEX_TWO = 2;
constexpr double MIN_GRAY_SCALE = 0.0;
constexpr double MAX_GRAY_SCALE = 1.0;
}
static thread_local std::map<std::string, std::shared_ptr<NativeReference>> g_jsWindowMap;
@ -815,6 +817,13 @@ napi_value JsWindow::SetWindowMask(napi_env env, napi_callback_info info)
return (me != nullptr) ? me->OnSetWindowMask(env, info) : nullptr;
}
napi_value JsWindow::SetWindowGrayScale(napi_env env, napi_callback_info info)
{
WLOGI("[NAPI]SetWindowGrayScale");
JsWindow* me = CheckParamsAndGetThis<JsWindow>(env, info);
return (me != nullptr) ? me->OnSetWindowGrayScale(env, info) : nullptr;
}
static void UpdateSystemBarProperties(std::map<WindowType, SystemBarProperty>& systemBarProperties,
const std::map<WindowType, SystemBarPropertyFlag>& systemBarPropertyFlags, sptr<Window> windowToken)
{
@ -5715,6 +5724,52 @@ napi_value JsWindow::OnSetWindowMask(napi_env env, napi_callback_info info)
return result;
}
napi_value JsWindow::OnSetWindowGrayScale(napi_env env, napi_callback_info info)
{
size_t argc = 4;
napi_value argv[4] = {nullptr};
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
if (argc != 1) { // 1: the param num
WLOGFE("Argc is invalid: %{public}zu", argc);
return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
}
napi_value nativeVal = argv[0];
if (nativeVal == nullptr) {
WLOGFE("Failed to convert parameter to grayScale");
return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
}
double grayScale = 0.0;
napi_get_value_double(env, nativeVal, &grayScale);
constexpr double eps = 1e-6;
if (grayScale < (MIN_GRAY_SCALE - eps) || grayScale > (MAX_GRAY_SCALE + eps)) {
WLOGFE("grayScale should be greater than or equal to 0.0, and should be smaller than or equal to 1.0");
return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
}
wptr<Window> weakToken(windowToken_);
NapiAsyncTask::CompleteCallback complete =
[weakToken, grayScale](napi_env env, NapiAsyncTask& task, int32_t status) {
auto window = weakToken.promote();
if (window == nullptr) {
WLOGFE("window is nullptr");
task.Reject(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
return;
}
WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(window->SetGrayScale(static_cast<float>(grayScale)));
if (ret == WmErrorCode::WM_OK) {
task.Resolve(env, NapiGetUndefined(env));
} else {
task.Reject(env, CreateJsError(env, static_cast<int32_t>(ret), "Set window gray scale failed"));
}
WLOGI("Window [%{public}u, %{public}s] OnSetWindowGrayScale end, grayScale = %{public}f",
window->GetWindowId(), window->GetWindowName().c_str(), grayScale);
};
napi_value result = nullptr;
NapiAsyncTask::Schedule("JsWindow::OnSetWindowGrayScale",
env, CreateAsyncTaskWithLastParam(env, nullptr, nullptr, std::move(complete), &result));
return result;
}
void BindFunctions(napi_env env, napi_value object, const char *moduleName)
{
BindNativeFunction(env, object, "show", moduleName, JsWindow::Show);
@ -5823,6 +5878,7 @@ void BindFunctions(napi_env env, napi_value object, const char *moduleName)
BindNativeFunction(env, object, "getTitleButtonRect", moduleName, JsWindow::GetTitleButtonRect);
BindNativeFunction(env, object, "setTitleButtonVisible", moduleName, JsWindow::SetTitleButtonVisible);
BindNativeFunction(env, object, "setWindowMask", moduleName, JsWindow::SetWindowMask);
BindNativeFunction(env, object, "setWindowGrayScale", moduleName, JsWindow::SetWindowGrayScale);
}
} // namespace Rosen
} // namespace OHOS

View File

@ -151,6 +151,7 @@ public:
static napi_value GetTitleButtonRect(napi_env env, napi_callback_info info);
static napi_value SetTitleButtonVisible(napi_env env, napi_callback_info info);
static napi_value SetWindowMask(napi_env env, napi_callback_info info);
static napi_value SetWindowGrayScale(napi_env env, napi_callback_info info);
private:
std::string GetWindowName();
@ -269,6 +270,7 @@ private:
napi_value OnSetWaterMarkFlag(napi_env env, napi_callback_info info);
napi_value OnSetWindowMask(napi_env env, napi_callback_info info);
napi_value OnSetHandwritingFlag(napi_env env, napi_callback_info info);
napi_value OnSetWindowGrayScale(napi_env env, napi_callback_info info);
sptr<Window> windowToken_ = nullptr;
std::unique_ptr<JsWindowRegisterManager> registerManager_ = nullptr;

View File

@ -327,6 +327,8 @@ public:
{
return WMError::WM_ERROR_DEVICE_NOT_SUPPORT;
}
virtual WMError SetGrayScale(float grayScale) { return WMError::WM_ERROR_DEVICE_NOT_SUPPORT; }
};
}
}

View File

@ -380,6 +380,10 @@ void CheckWindowImplFunctionsPart1(sptr<Window> window, const uint8_t* data, siz
startPos += GetObject(boolVal, data + startPos, size - startPos);
window->UpdateSurfaceNodeAfterCustomAnimation(boolVal);
float grayScale;
startPos += GetObject(grayScale, data + startPos, size - startPos);
window->SetGrayScale(grayScale);
}
void CheckWindowImplFunctionsPart2(sptr<WindowImpl> window, const uint8_t* data, size_t size)

View File

@ -104,6 +104,7 @@ public:
virtual WMError SetBackdropBlur(float radius) override;
virtual WMError SetBackdropBlurStyle(WindowBlurStyle blurStyle) override;
virtual WMError SetWindowMode(WindowMode mode) override;
virtual WMError SetGrayScale(float grayScale) override;
virtual WMError SetTransparent(bool isTransparent) override;
virtual WMError SetTurnScreenOn(bool turnScreenOn) override;

View File

@ -72,6 +72,8 @@ namespace {
constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowSceneSessionImpl"};
constexpr int32_t WINDOW_DETACH_TIMEOUT = 300;
const std::string PARAM_DUMP_HELP = "-h";
constexpr float MIN_GRAY_SCALE = 0.0f;
constexpr float MAX_GRAY_SCALE = 1.0f;
}
uint32_t WindowSceneSessionImpl::maxFloatingWindowSize_ = 1920;
@ -2011,6 +2013,28 @@ WMError WindowSceneSessionImpl::SetWindowMode(WindowMode mode)
return WMError::WM_OK;
}
WMError WindowSceneSessionImpl::SetGrayScale(float grayScale)
{
if (IsWindowSessionInvalid()) {
WLOGFE("session is invalid");
return WMError::WM_ERROR_INVALID_WINDOW;
}
constexpr float eps = 1e-6f;
if (grayScale < (MIN_GRAY_SCALE - eps) || grayScale > (MAX_GRAY_SCALE + eps)) {
WLOGFE("invalid grayScale value: %{public}f", grayScale);
return WMError::WM_ERROR_INVALID_PARAM;
}
if (uiContent_ == nullptr) {
WLOGFE("uicontent is empty");
return WMError::WM_ERROR_NULLPTR;
}
uiContent_->SetContentNodeGrayScale(grayScale);
WLOGI("Set window gray scale success, grayScale: %{public}f", grayScale);
return WMError::WM_OK;
}
WindowMode WindowSceneSessionImpl::GetMode() const
{
return property_->GetWindowMode();

View File

@ -2931,6 +2931,89 @@ HWTEST_F(WindowSceneSessionImplTest, SyncDestroyAndDisconnectSpecificSession, Fu
GTEST_LOG_(INFO) << "WindowSessionImplTest: SetTitleButtonVisible03 end";
}
/**
* @tc.name: SetGrayScale01
* @tc.desc: SetGrayScale
* @tc.type: FUNC
*/
HWTEST_F(WindowSceneSessionImplTest, SetGrayScale01, Function | SmallTest | Level3)
{
sptr<WindowOption> option = new (std::nothrow) WindowOption();
ASSERT_NE(nullptr, option);
option->SetWindowMode(WindowMode::WINDOW_MODE_PIP);
sptr<WindowSceneSessionImpl> window = new (std::nothrow) WindowSceneSessionImpl(option);
ASSERT_NE(nullptr, window);
constexpr float grayScale = 0.5f;
ASSERT_EQ(WMError::WM_ERROR_INVALID_WINDOW, window->SetGrayScale(grayScale));
}
/**
* @tc.name: SetGrayScale02
* @tc.desc: SetGrayScale
* @tc.type: FUNC
*/
HWTEST_F(WindowSceneSessionImplTest, SetGrayScale02, Function | SmallTest | Level3)
{
sptr<WindowOption> option = new (std::nothrow) WindowOption();
ASSERT_NE(nullptr, option);
sptr<WindowSceneSessionImpl> window = new (std::nothrow) WindowSceneSessionImpl(option);
ASSERT_NE(nullptr, window);
window->state_ = WindowState::STATE_SHOWN;
SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" };
sptr<SessionMocker> session = new (std::nothrow) SessionMocker(sessionInfo);
ASSERT_NE(nullptr, session);
window->property_->SetPersistentId(1);
window->hostSession_ = session;
window->uiContent_ = std::make_unique<Ace::UIContentMocker>();
std::vector<WindowType> types = { WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
WindowType::WINDOW_TYPE_APP_SUB_WINDOW,
WindowType::SYSTEM_WINDOW_BASE };
for (WindowType type : types) {
window->SetWindowType(type);
float grayScale = -0.001f;
ASSERT_EQ(WMError::WM_ERROR_INVALID_PARAM, window->SetGrayScale(grayScale));
grayScale = 1.001f;
ASSERT_EQ(WMError::WM_ERROR_INVALID_PARAM, window->SetGrayScale(grayScale));
}
}
/**
* @tc.name: SetGrayScale03
* @tc.desc: SetGrayScale
* @tc.type: FUNC
*/
HWTEST_F(WindowSceneSessionImplTest, SetGrayScale03, Function | SmallTest | Level3)
{
sptr<WindowOption> option = new (std::nothrow) WindowOption();
ASSERT_NE(nullptr, option);
sptr<WindowSceneSessionImpl> window = new (std::nothrow) WindowSceneSessionImpl(option);
ASSERT_NE(nullptr, window);
window->state_ = WindowState::STATE_SHOWN;
SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" };
sptr<SessionMocker> session = new (std::nothrow) SessionMocker(sessionInfo);
ASSERT_NE(nullptr, session);
window->property_->SetPersistentId(1);
window->hostSession_ = session;
window->uiContent_ = std::make_unique<Ace::UIContentMocker>();
std::vector<WindowType> types = { WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
WindowType::WINDOW_TYPE_APP_SUB_WINDOW,
WindowType::SYSTEM_WINDOW_BASE };
for (WindowType type : types) {
window->SetWindowType(type);
float grayScale = 0.0f;
ASSERT_EQ(WMError::WM_OK, window->SetGrayScale(grayScale));
grayScale = 1.0f;
ASSERT_EQ(WMError::WM_OK, window->SetGrayScale(grayScale));
grayScale = 0.5f;
ASSERT_EQ(WMError::WM_OK, window->SetGrayScale(grayScale));
}
}
/**
* @tc.name: TestGetUIContentWithId
* @tc.desc: Get uicontent with id