From be9a1bd1839f12879b426b75ade0390823e48a3b Mon Sep 17 00:00:00 2001 From: duanshaofei Date: Tue, 9 Sep 2025 09:50:28 +0800 Subject: [PATCH] scrollbarmode Signed-off-by: duanshaofei Change-Id: I4e462665b39690356005355069f118cfaaaf41ca --- .../napi_webview_controller.cpp | 52 +++++++++++++++++++ .../napi_webview_controller.h | 2 + ohos_interface/include/ohos_nweb/nweb.h | 5 ++ .../include/ohos_nweb/nweb_engine.h | 2 + .../bridge/webcore/ark_web_engine_impl.cpp | 5 ++ .../bridge/webcore/ark_web_engine_impl.h | 2 + .../bridge/webview/ark_web_engine_wrapper.cpp | 5 ++ .../bridge/webview/ark_web_engine_wrapper.h | 2 + .../ohos_nweb/include/ark_web_engine.h | 3 ++ ohos_nweb/include/nweb_helper.h | 2 + ohos_nweb/src/nweb_helper.cpp | 9 ++++ .../nweb_helper_test/nweb_helper_ex_test.cpp | 2 +- .../nweb_helper_test/nweb_helper_test.cpp | 2 + 13 files changed, 92 insertions(+), 1 deletion(-) diff --git a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp index fd8f413fe..c67fea0a0 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp +++ b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.cpp @@ -831,6 +831,7 @@ napi_value NapiWebviewController::Init(napi_env env, napi_value exports) DECLARE_NAPI_STATIC_FUNCTION("setAutoPreconnect", NapiWebviewController::SetAutoPreconnect), DECLARE_NAPI_STATIC_FUNCTION("isAutoPreconnectEnabled", NapiWebviewController::IsAutoPreconnectEnabled), DECLARE_NAPI_STATIC_FUNCTION("setSocketIdleTimeout", NapiWebviewController::SetSocketIdleTimeout), + DECLARE_NAPI_STATIC_FUNCTION("setScrollbarMode", NapiWebviewController::SetScrollbarMode), }; napi_value constructor = nullptr; napi_define_class(env, WEBVIEW_CONTROLLER_CLASS_NAME.c_str(), WEBVIEW_CONTROLLER_CLASS_NAME.length(), @@ -1109,6 +1110,21 @@ napi_value NapiWebviewController::Init(napi_env env, napi_value exports) sizeof(siteIsolationModeProperties[0]), siteIsolationModeProperties, &siteIsolationModeEnum); napi_set_named_property(env, exports, WEB_SITE_ISOLATION_MODE_ENUM_NAME.c_str(), siteIsolationModeEnum); + napi_value scrollbarModeEnum = nullptr; + napi_property_descriptor scrollbarModeProperties[] = { + DECLARE_NAPI_STATIC_PROPERTY( + "OVERLAY_LAYOUT_SCROLLBAR", + NapiParseUtils::ToInt32Value(env, static_cast(ScrollbarMode::OVERLAY_LAYOUT_SCROLLBAR))), + DECLARE_NAPI_STATIC_PROPERTY( + "FORCE_DISPLAY_SCROLLBAR", + NapiParseUtils::ToInt32Value(env, static_cast(ScrollbarMode::FORCE_DISPLAY_SCROLLBAR))), + }; + napi_define_class(env, WEB_SCROLLBAR_MODE_ENUM_NAME.c_str(), WEB_SCROLLBAR_MODE_ENUM_NAME.length(), + NapiParseUtils::CreateEnumConstructor, nullptr, + sizeof(scrollbarModeProperties) / sizeof(scrollbarModeProperties[0]), scrollbarModeProperties, + &scrollbarModeEnum); + napi_set_named_property(env, exports, WEB_SCROLLBAR_MODE_ENUM_NAME.c_str(), scrollbarModeEnum); + WebviewJavaScriptExecuteCallback::InitJSExcute(env, exports); WebviewCreatePDFExecuteCallback::InitJSExcute(env, exports); return exports; @@ -7960,5 +7976,41 @@ napi_value NapiWebviewController::SetSocketIdleTimeout(napi_env env, napi_callba NWebHelper::Instance().SetSocketIdleTimeout(socketIdleTimeout); return result; } + +napi_value NapiWebviewController::SetScrollbarMode(napi_env env, napi_callback_info info) +{ + if (IS_CALLING_FROM_M114()) { + WVLOG_W("SetScrollbarMode unsupported engine version: M114"); + return nullptr; + } + napi_value thisVar = nullptr; + napi_value result = nullptr; + size_t argc = INTEGER_ONE; + napi_value argv[INTEGER_ONE] = { 0 }; + napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); + + if (argc != INTEGER_ONE) { + BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, + NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_NUMBERS_ERROR_ONE, "one")); + return result; + } + + int32_t scrollbarMode = false; + if (!NapiParseUtils::ParseInt32(env, argv[INTEGER_ZERO], scrollbarMode)) { + BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, + NWebError::FormatString(ParamCheckErrorMsgTemplate::TYPE_ERROR, "mode", "ScrollbarMode")); + return result; + } + + if (scrollbarMode < static_cast(ScrollbarMode::OVERLAY_LAYOUT_SCROLLBAR) || + scrollbarMode > static_cast(ScrollbarMode::FORCE_DISPLAY_SCROLLBAR)) { + BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR, + NWebError::FormatString(ParamCheckErrorMsgTemplate::PARAM_TYPE_INVALID, "mode")); + return result; + } + + NWebHelper::Instance().SetScrollbarMode(static_cast(scrollbarMode)); + return result; +} } // namespace NWeb } // namespace OHOS diff --git a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.h b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.h index d34fd7227..547661626 100644 --- a/interfaces/kits/napi/webviewcontroller/napi_webview_controller.h +++ b/interfaces/kits/napi/webviewcontroller/napi_webview_controller.h @@ -47,6 +47,7 @@ const std::string WEB_CONTROLLER_ATTACHSTATE_ENUM_NAME = "ControllerAttachState" const std::string WEB_BLANKLESS_ERROR_CODE_ENUM_NAME = "WebBlanklessErrorCode"; const std::string WEB_DESTROY_MODE_ENUM_NAME = "WebDestroyMode"; const std::string WEB_SITE_ISOLATION_MODE_ENUM_NAME = "SiteIsolationMode"; +const std::string WEB_SCROLLBAR_MODE_ENUM_NAME = "ScrollbarMode"; constexpr double TEN_MILLIMETER_TO_INCH = 0.39; struct Scheme { @@ -425,6 +426,7 @@ private: static napi_value SetAutoPreconnect(napi_env env, napi_callback_info info); static napi_value IsAutoPreconnectEnabled(napi_env env, napi_callback_info info); static napi_value SetSocketIdleTimeout(napi_env env, napi_callback_info info); + static napi_value SetScrollbarMode(napi_env env, napi_callback_info info); }; class ArkWebTransfer { diff --git a/ohos_interface/include/ohos_nweb/nweb.h b/ohos_interface/include/ohos_nweb/nweb.h index 5b303a9e4..46ead6103 100644 --- a/ohos_interface/include/ohos_nweb/nweb.h +++ b/ohos_interface/include/ohos_nweb/nweb.h @@ -292,6 +292,11 @@ enum class WebDestroyMode { FAST_MODE }; +enum class ScrollbarMode { + OVERLAY_LAYOUT_SCROLLBAR, + FORCE_DISPLAY_SCROLLBAR +}; + class OHOS_NWEB_EXPORT NWebMouseEvent { public: virtual ~NWebMouseEvent() = default; diff --git a/ohos_interface/include/ohos_nweb/nweb_engine.h b/ohos_interface/include/ohos_nweb/nweb_engine.h index 5b23cc06b..5427fe942 100644 --- a/ohos_interface/include/ohos_nweb/nweb_engine.h +++ b/ohos_interface/include/ohos_nweb/nweb_engine.h @@ -139,6 +139,8 @@ public: virtual void SetWebDestroyMode(WebDestroyMode mode) {} virtual void SetSocketIdleTimeout(int32_t timeout) {} + + virtual void SetScrollbarMode(ScrollbarMode mode) {} }; } // namespace OHOS::NWeb diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.cpp b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.cpp index a7abe5dd8..9a1cb91f4 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.cpp +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.cpp @@ -332,4 +332,9 @@ void ArkWebEngineImpl::SetSocketIdleTimeout(int32_t timeout) nweb_engine_->SetSocketIdleTimeout(timeout); } +void ArkWebEngineImpl::SetScrollbarMode(int32_t mode) +{ + nweb_engine_->SetScrollbarMode(static_cast(mode)); +} + } // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.h b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.h index 0db19f26e..7c64b45b4 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.h +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webcore/ark_web_engine_impl.h @@ -126,6 +126,8 @@ public: void SetSocketIdleTimeout(int32_t timeout) override; + void SetScrollbarMode(int32_t mode) override; + private: std::shared_ptr nweb_engine_; }; diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.cpp b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.cpp index 3951a4e9c..d14064e1e 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.cpp +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.cpp @@ -388,4 +388,9 @@ void ArkWebEngineWrapper::SetSocketIdleTimeout(int32_t timeout) ark_web_engine_->SetSocketIdleTimeout(timeout); } +void ArkWebEngineWrapper::SetScrollbarMode(OHOS::NWeb::ScrollbarMode mode) +{ + ark_web_engine_->SetScrollbarMode(static_cast(mode)); +} + } // namespace OHOS::ArkWeb diff --git a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.h b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.h index 926124fa4..0db634c00 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.h +++ b/ohos_interface/ohos_glue/ohos_nweb/bridge/webview/ark_web_engine_wrapper.h @@ -127,6 +127,8 @@ public: void SetSocketIdleTimeout(int32_t timeout) override; + void SetScrollbarMode(OHOS::NWeb::ScrollbarMode mode) override; + private: ArkWebRefPtr ark_web_engine_; }; diff --git a/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_engine.h b/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_engine.h index b8b84ea1c..6c9e8d6da 100644 --- a/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_engine.h +++ b/ohos_interface/ohos_glue/ohos_nweb/include/ark_web_engine.h @@ -203,6 +203,9 @@ public: /*--ark web()--*/ virtual void SetSocketIdleTimeout(int32_t timeout) = 0; + + /*--ark web()--*/ + virtual void SetScrollbarMode(int32_t mode) = 0; }; } // namespace OHOS::ArkWeb diff --git a/ohos_nweb/include/nweb_helper.h b/ohos_nweb/include/nweb_helper.h index 8219dd7a7..f76379c8e 100644 --- a/ohos_nweb/include/nweb_helper.h +++ b/ohos_nweb/include/nweb_helper.h @@ -166,6 +166,8 @@ public: int32_t GetSocketIdleTimeout(); + void SetScrollbarMode(ScrollbarMode mode); + private: NWebHelper() = default; bool GetWebEngine(bool fromArk); diff --git a/ohos_nweb/src/nweb_helper.cpp b/ohos_nweb/src/nweb_helper.cpp index d4c1716df..d320d061d 100644 --- a/ohos_nweb/src/nweb_helper.cpp +++ b/ohos_nweb/src/nweb_helper.cpp @@ -1407,4 +1407,13 @@ int32_t NWebHelper::GetSocketIdleTimeout() return socketIdleTimeout_; } +void NWebHelper::SetScrollbarMode(ScrollbarMode mode) +{ + if (nwebEngine_ == nullptr) { + WVLOG_E("web engine is nullptr"); + return; + } + nwebEngine_->SetScrollbarMode(mode); +} + } // namespace OHOS::NWeb diff --git a/test/unittest/nweb_helper_test/nweb_helper_ex_test.cpp b/test/unittest/nweb_helper_test/nweb_helper_ex_test.cpp index 37483f0be..3577f1bf4 100644 --- a/test/unittest/nweb_helper_test/nweb_helper_ex_test.cpp +++ b/test/unittest/nweb_helper_test/nweb_helper_ex_test.cpp @@ -438,7 +438,7 @@ HWTEST_F(NwebHelperTest, NWebHelper_LoadWebEngine_008, TestSize.Level1) NWebHelper::Instance().PauseAllTimers(); NWebHelper::Instance().ResumeAllTimers(); NWebHelper::Instance().SetWebDestroyMode(WebDestroyMode::NORMAL_MODE); - + NWebHelper::Instance().SetScrollbarMode(ScrollbarMode::OVERLAY_LAYOUT_SCROLLBAR); auto nwebEngineMock = std::make_shared(); NWebHelper::Instance().nwebEngine_ = nwebEngineMock; NWebHelper::Instance().SetHttpDns(config); diff --git a/test/unittest/nweb_helper_test/nweb_helper_test.cpp b/test/unittest/nweb_helper_test/nweb_helper_test.cpp index 32e9f454f..23d30f211 100644 --- a/test/unittest/nweb_helper_test/nweb_helper_test.cpp +++ b/test/unittest/nweb_helper_test/nweb_helper_test.cpp @@ -488,6 +488,7 @@ HWTEST_F(NwebHelperTest, NWebHelper_LoadWebEngine_008, TestSize.Level1) NWebHelper::Instance().PauseAllTimers(); NWebHelper::Instance().ResumeAllTimers(); NWebHelper::Instance().SetWebDestroyMode(WebDestroyMode::NORMAL_MODE); + NWebHelper::Instance().SetScrollbarMode(ScrollbarMode::OVERLAY_LAYOUT_SCROLLBAR); EXPECT_NE(NWebHelper::Instance().nwebEngine_, nullptr); NWebHelper::Instance().LoadWebEngine(true, false); bool result = NWebHelper::Instance().GetWebEngine(true); @@ -510,6 +511,7 @@ HWTEST_F(NwebHelperTest, NWebHelper_LoadWebEngine_008, TestSize.Level1) NWebHelper::Instance().PauseAllTimers(); NWebHelper::Instance().ResumeAllTimers(); NWebHelper::Instance().SetWebDestroyMode(WebDestroyMode::NORMAL_MODE); + NWebHelper::Instance().SetScrollbarMode(ScrollbarMode::OVERLAY_LAYOUT_SCROLLBAR); NWebHelper::Instance().SetWebDebuggingAccess(true); NWebHelper::Instance().SetWebDebuggingAccessAndPort(true, 80); NWebHelper::Instance().SaveSchemeVector("web_test", 8080); -- Gitee