From e8697074b550fd620081c413470f7c78acb69fef Mon Sep 17 00:00:00 2001 From: wangweiyuan Date: Wed, 25 Jun 2025 16:41:59 +0800 Subject: [PATCH] keyboardAvoidance Signed-off-by: wangweiyuan --- .../overlay/sheet_presentation_pattern.cpp | 11 ++++- .../overlay/sheet_presentation_pattern.h | 1 + .../sheet_presentation_test_two_ng.cpp | 48 +++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/frameworks/core/components_ng/pattern/overlay/sheet_presentation_pattern.cpp b/frameworks/core/components_ng/pattern/overlay/sheet_presentation_pattern.cpp index eeaea772f7d..cd1e9db7024 100644 --- a/frameworks/core/components_ng/pattern/overlay/sheet_presentation_pattern.cpp +++ b/frameworks/core/components_ng/pattern/overlay/sheet_presentation_pattern.cpp @@ -2230,7 +2230,7 @@ void SheetPresentationPattern::StartSheetTransitionAnimation( option.GetOnFinishEvent()); SetBottomStyleHotAreaInSubwindow(); } else { - AnimationUtils::StopAnimation(animation_); + StopModifySheetTransition(); animation_ = AnimationUtils::StartAnimation( option, sheetObject_->GetSheetAnimationEvent(isTransitionIn, offset), @@ -2994,6 +2994,13 @@ void SheetPresentationPattern::DumpAdvanceInfo(std::unique_ptr& json) json->Put("IsShouldDismiss", shouldDismiss_ ? "true" : "false"); } +void SheetPresentationPattern::StopModifySheetTransition() +{ + if (isAnimationProcess_ && animation_) { + AnimationUtils::StopAnimation(animation_); + } +} + void SheetPresentationPattern::AvoidKeyboardBySheetMode(bool forceAvoid) { auto host = GetHost(); @@ -3018,7 +3025,7 @@ void SheetPresentationPattern::AvoidKeyboardBySheetMode(bool forceAvoid) "The sheet will disappear, so there's no need to handle canceling keyboard avoidance here."); return; } - + StopModifySheetTransition(); // 1.handle non upward logic: avoidKeyboardMode::RESIZE_ONLY if (AvoidKeyboardBeforeTranslate()) { return; diff --git a/frameworks/core/components_ng/pattern/overlay/sheet_presentation_pattern.h b/frameworks/core/components_ng/pattern/overlay/sheet_presentation_pattern.h index ed466984547..2c2e8e29d06 100644 --- a/frameworks/core/components_ng/pattern/overlay/sheet_presentation_pattern.h +++ b/frameworks/core/components_ng/pattern/overlay/sheet_presentation_pattern.h @@ -1096,6 +1096,7 @@ private: void SetSheetOuterBorderWidth(const RefPtr& sheetTheme, const NG::SheetStyle& sheetStyle); PipelineContext* GetSheetMainPipeline() const; float GetBottomSafeArea(); + void StopModifySheetTransition(); void AvoidKeyboardBySheetMode(bool forceAvoid = false); void DecreaseScrollHeightInSheet(float decreaseHeight); void UpdateSheetWhenSheetTypeChanged(); diff --git a/test/unittest/core/pattern/overlay/sheet_presentation_test_two_ng.cpp b/test/unittest/core/pattern/overlay/sheet_presentation_test_two_ng.cpp index a36788ace48..8a2c2aa0d1b 100644 --- a/test/unittest/core/pattern/overlay/sheet_presentation_test_two_ng.cpp +++ b/test/unittest/core/pattern/overlay/sheet_presentation_test_two_ng.cpp @@ -971,6 +971,54 @@ HWTEST_F(SheetPresentationTestTwoNg, AvoidKeyboardBySheetMode007, TestSize.Level SheetPresentationTestTwoNg::TearDownTestCase(); } +/** + * @tc.name: AvoidKeyboardBySheetMode008 + * @tc.desc: Increase the coverage of SheetPresentationPattern::AvoidKeyboardBySheetMode function. + * @tc.type: FUNC + */ +HWTEST_F(SheetPresentationTestTwoNg, AvoidKeyboardBySheetMode008, TestSize.Level1) +{ + SheetPresentationTestTwoNg::SetUpTestCase(); + auto callback = [](const std::string&) {}; + auto sheetNode = FrameNode::CreateFrameNode(V2::SHEET_ETS_TAG, 101, + AceType::MakeRefPtr(201, V2::TEXT_ETS_TAG, std::move(callback))); + auto sheetPattern = sheetNode->GetPattern(); + sheetPattern->keyboardAvoidMode_ = SheetKeyboardAvoidMode::TRANSLATE_AND_SCROLL; + auto host = sheetPattern->GetHost(); + ASSERT_NE(host, nullptr); + auto pipelineContext = host->GetContext(); + ASSERT_NE(pipelineContext, nullptr); + auto safeAreaManager = pipelineContext->GetSafeAreaManager(); + ASSERT_NE(safeAreaManager, nullptr); + sheetPattern->keyboardHeight_ = safeAreaManager->GetKeyboardInset().Length() + 10.0f; + sheetPattern->isDismissProcess_ = false; + sheetPattern->isScrolling_ = true; + sheetPattern->isAnimationProcess_ = true; + sheetPattern->AvoidKeyboardBySheetMode(); + AnimationOption option; + auto propertyCallback = []() {}; + sheetPattern->animation_ = AnimationUtils::StartAnimation(option, propertyCallback); + auto focusHub = host->GetFocusHub(); + focusHub->currentFocus_ = true; + safeAreaManager->keyboardInset_.end = 1000.0f; + pipelineContext->rootHeight_ = 1800.0f; + sheetPattern->sheetType_ = SheetType::SHEET_BOTTOM; + + EXPECT_NE(sheetPattern->keyboardAvoidMode_, SheetKeyboardAvoidMode::NONE); + EXPECT_NE(sheetPattern->keyboardHeight_, safeAreaManager->GetKeyboardInset().Length()); + EXPECT_FALSE(sheetPattern->isDismissProcess_); + EXPECT_FALSE(sheetPattern->AvoidKeyboardBeforeTranslate()); + EXPECT_TRUE(sheetPattern->isScrolling_); + EXPECT_NE(sheetPattern->GetSheetHeightChange(), 0.0f); + EXPECT_TRUE(focusHub->IsCurrentFocus()); + EXPECT_TRUE(sheetPattern->IsSheetBottomStyle()); + EXPECT_TRUE(sheetPattern->IsSheetBottomStyle()); + sheetPattern->AvoidKeyboardBySheetMode(); + EXPECT_TRUE(sheetPattern->isAnimationProcess_); + EXPECT_FALSE(sheetPattern->isAnimationBreak_); + SheetPresentationTestTwoNg::TearDownTestCase(); +} + /** * @tc.name: IsNeedPlayTransition001 * @tc.desc: Test update detents of sheetStyle. -- Gitee