From 6764ff2ce92513bd779daaf79ed580c3f5535115 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=87=E5=AD=9D=E5=9B=BD?= Date: Sat, 17 May 2025 11:28:47 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E7=94=9F=E6=80=81=EF=BC=9A?= =?UTF-8?q?=E7=AA=97=E5=8F=A3=E7=BC=A9=E6=94=BE=E4=B8=8B=E8=BE=93=E5=85=A5?= =?UTF-8?q?=E6=B3=95=E4=BD=8D=E7=BD=AE=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 万孝国 --- .../src/input_method_controller.cpp | 38 +++++++++++++++ .../include/input_method_controller.h | 16 +++++++ .../src/input_method_controller_test.cpp | 47 +++++++++++++++++++ 3 files changed, 101 insertions(+) diff --git a/frameworks/native/inputmethod_controller/src/input_method_controller.cpp b/frameworks/native/inputmethod_controller/src/input_method_controller.cpp index 389a368eb..39ac2a9bf 100644 --- a/frameworks/native/inputmethod_controller/src/input_method_controller.cpp +++ b/frameworks/native/inputmethod_controller/src/input_method_controller.cpp @@ -207,9 +207,15 @@ void InputMethodController::DeactivateClient() void InputMethodController::SaveTextConfig(const TextConfig &textConfig) { IMSA_HILOGD("textConfig: %{public}s.", textConfig.ToString().c_str()); + int32_t x = textConfig.cursorInfo.left; + int32_t y = textConfig.cursorInfo.top; + uint32_t windowId = textConfig.windowId; + GetWindowScaleCoordinate(x, y, windowId); { std::lock_guard lock(textConfigLock_); textConfig_ = textConfig; + textConfig_.cursorInfo.left = x; + textConfig_.cursorInfo.top = y; StringUtils::TruncateUtf16String(textConfig_.inputAttribute.placeholder, MAX_PLACEHOLDER_SIZE); StringUtils::TruncateUtf16String(textConfig_.inputAttribute.abilityName, MAX_ABILITY_NAME_SIZE); } @@ -739,6 +745,16 @@ int32_t InputMethodController::OnCursorUpdate(CursorInfo cursorInfo) IMSA_HILOGD("not editable."); return ErrorCode::ERROR_CLIENT_NOT_EDITABLE; } + int32_t x = cursorInfo.left; + int32_t y = cursorInfo.top; + uint32_t windowId = 0; + { + std::lock_guard lock(textConfigLock_); + windowId = textConfig_.windowId; + } + GetWindowScaleCoordinate(x, y, windowId); + cursorInfo.left = x; + cursorInfo.top = y; { std::lock_guard lock(textConfigLock_); textConfig_.cursorInfo = cursorInfo; @@ -1716,5 +1732,27 @@ int32_t InputMethodController::SendPrivateData(const std::unordered_mapSendPrivateData(value); } + +int32_t InputMethodController::RegisterWindowScaleCallbackHandler(WindowScaleCallback&& callback) +{ + IMSA_HILOGD("isRegister: %{public}d", callback != nullptr); + std::lock_guard lock(windowScaleCallbackMutex_); + windowScaleCallback_ = std::move(callback); + return static_cast(ErrorCode::NO_ERROR); +} + +void InputMethodController::GetWindowScaleCoordinate(int32_t& x, int32_t& y, uint32_t windowId) +{ + WindowScaleCallback handler = nullptr; + { + std::lock_guard lock(windowScaleCallbackMutex_); + handler = windowScaleCallback_; + } + if (handler == nullptr) { + IMSA_HILOGD("handler is nullptr"); + return; + } + handler(x, y, windowId); +} } // namespace MiscServices } // namespace OHOS diff --git a/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h b/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h index 0c36b843a..db253de85 100644 --- a/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h +++ b/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h @@ -118,6 +118,7 @@ public: }; using PrivateDataValue = std::variant; using KeyEventCallback = std::function &keyEvent, bool isConsumed)>; +using WindowScaleCallback = std::function; class InputMethodController : public RefBase, public PrivateCommandInterface { public: /** @@ -929,6 +930,17 @@ public: */ IMF_API int32_t SendPrivateData(const std::unordered_map &privateCommand); + /** + * @brief Registration callbacks are used for coordinate transformation processing in window zooming scenarios. + * + * This function only available special service apply. + * + * @param callback Indicates the window zooming handle callback + * @return Returns 0 for success, others for failure. + * @since 18 + */ + IMF_API int32_t RegisterWindowScaleCallbackHandler(WindowScaleCallback&& callback); + private: InputMethodController(); ~InputMethodController(); @@ -966,6 +978,7 @@ private: int32_t ShowTextInputInner(const AttachOptions &attachOptions, ClientType type); int32_t ShowSoftKeyboardInner(ClientType type); void ReportClientShow(int32_t eventCode, int32_t errCode, ClientType type); + void GetWindowScaleCoordinate(int32_t& x, int32_t& y, uint32_t windowId); std::shared_ptr controllerListener_; std::mutex abilityLock_; @@ -1026,6 +1039,9 @@ private: std::mutex bindImeInfoLock_; std::pair bindImeInfo_{ 0, "" }; // for hiSysEvent std::atomic_uint32_t sessionId_ { 0 }; + + std::mutex windowScaleCallbackMutex_; + WindowScaleCallback windowScaleCallback_ = nullptr; }; } // namespace MiscServices } // namespace OHOS diff --git a/test/unittest/cpp_test/src/input_method_controller_test.cpp b/test/unittest/cpp_test/src/input_method_controller_test.cpp index fe63a538f..9d87db6f5 100644 --- a/test/unittest/cpp_test/src/input_method_controller_test.cpp +++ b/test/unittest/cpp_test/src/input_method_controller_test.cpp @@ -1947,5 +1947,52 @@ HWTEST_F(InputMethodControllerTest, testUpdateTextPreviewState, TestSize.Level0) inputMethodController_->UpdateTextPreviewState(true); EXPECT_TRUE(inputMethodController_->textConfig_.inputAttribute.isTextPreviewSupported); } + +/** + * @tc.name: RegisterWindowScaleCallbackHandler + * @tc.desc: test IMC + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(InputMethodControllerTest, RegisterWindowScaleCallbackHandler, TestSize.Level0) +{ + IMSA_HILOGI("IMC RegisterWindowScaleCallbackHandler Test START"); + ASSERT_NE(inputMethodController_, nullptr); + EXPECT_EQ(inputMethodController_->windowScaleCallback_, nullptr); + auto callback = [] (int32_t& x, int32_t& y, uint32_t windowId) { + return 0; + }; + auto res = inputMethodController_->RegisterWindowScaleCallbackHandler(std::move(callback)); + EXPECT_EQ(res, 0); + EXPECT_NE(inputMethodController_->windowScaleCallback_, nullptr); +} + +/** + * @tc.name: GetWindowScaleCoordinate + * @tc.desc: test IMC + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(InputMethodControllerTest, GetWindowScaleCoordinate, TestSize.Level0) +{ + IMSA_HILOGI("IMC GetWindowScaleCoordinate Test START"); + ASSERT_NE(inputMethodController_, nullptr); + int32_t x = 100; + int32_t y = 100; + uint32_t windowId = 100; + inputMethodController_->GetWindowScaleCoordinate(x, y, windowId); + EXPECT_EQ(x, 100); + EXPECT_EQ(y, 100); + auto callback = [] (int32_t& x, int32_t& y, uint32_t windowId) { + x++; + y++; + return 0; + }; + auto res = inputMethodController_->RegisterWindowScaleCallbackHandler(std::move(callback)); + EXPECT_EQ(res, 0); + inputMethodController_->GetWindowScaleCoordinate(x, y, windowId); + EXPECT_EQ(x, 101); + EXPECT_EQ(y, 101); +} } // namespace MiscServices } // namespace OHOS -- Gitee