!48931 【swiper】swiper arrow bugfix

Merge pull request !48931 from ShihaoWang/1119
This commit is contained in:
openharmony_ci 2024-11-20 07:41:20 +00:00 committed by Gitee
commit a5f0946409
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 26 additions and 22 deletions

View File

@ -39,7 +39,7 @@ void SwiperArrowPattern::OnModifyDone()
InitSwiperChangeEvent(swiperEventHub);
index_ = layoutProperty->GetIndex().value_or(0);
isFirstCreate_ = false;
InitButtonEvent();
InitEvent();
InitOnKeyEvent();
} else {
UpdateArrowContent();
@ -131,14 +131,18 @@ void SwiperArrowPattern::UpdateButtonNode(int32_t index)
symbolNode->MarkModifyDone();
}
void SwiperArrowPattern::InitButtonEvent()
void SwiperArrowPattern::InitEvent()
{
auto host = GetHost();
CHECK_NULL_VOID(host);
auto arrowGestureHub = host->GetOrCreateGestureEventHub();
CHECK_NULL_VOID(arrowGestureHub);
// Set hit test mode transparent to avoid blocking the touch event of child nodes and sibling nodes.
arrowGestureHub->SetHitTestMode(HitTestMode::HTMTRANSPARENT);
auto buttonNode = DynamicCast<FrameNode>(host->GetFirstChild());
CHECK_NULL_VOID(buttonNode);
auto arrowGestureHub = buttonNode->GetOrCreateGestureEventHub();
auto buttonGestureHub = buttonNode->GetOrCreateGestureEventHub();
auto touchCallback = [weak = WeakClaim(this), weakButton = WeakClaim(RawPtr(buttonNode))](
const TouchEventInfo& info) {
auto pattern = weak.Upgrade();
@ -147,9 +151,8 @@ void SwiperArrowPattern::InitButtonEvent()
CHECK_NULL_VOID(buttonNode);
pattern->ButtonTouchEvent(buttonNode, info.GetTouches().front().GetTouchType());
};
buttonTouchListenr_ = MakeRefPtr<TouchEventImpl>(std::move(touchCallback));
arrowGestureHub->AddTouchEvent(buttonTouchListenr_);
arrowGestureHub->SetHitTestMode(HitTestMode::HTMBLOCK);
buttonTouchListener_ = MakeRefPtr<TouchEventImpl>(std::move(touchCallback));
buttonGestureHub->AddTouchEvent(buttonTouchListener_);
auto hoverCallback = [weak = WeakClaim(this), weakButton = WeakClaim(RawPtr(buttonNode))](bool isHovered) {
auto pattern = weak.Upgrade();
@ -158,20 +161,20 @@ void SwiperArrowPattern::InitButtonEvent()
CHECK_NULL_VOID(buttonNode);
pattern->ButtonOnHover(buttonNode, isHovered);
};
buttonOnHoverListenr_ = MakeRefPtr<InputEvent>(std::move(hoverCallback));
buttonOnHoverListener_ = MakeRefPtr<InputEvent>(std::move(hoverCallback));
auto buttonInputHub = buttonNode->GetOrCreateInputEventHub();
buttonInputHub->AddOnHoverEvent(buttonOnHoverListenr_);
buttonInputHub->AddOnHoverEvent(buttonOnHoverListener_);
auto clickCallback = [weak = WeakClaim(this)](const GestureEvent& info) {
auto pattern = weak.Upgrade();
CHECK_NULL_VOID(pattern);
pattern->ButtonClickEvent();
};
if (buttonClickListenr_) {
arrowGestureHub->RemoveClickEvent(buttonClickListenr_);
if (buttonClickListener_) {
buttonGestureHub->RemoveClickEvent(buttonClickListener_);
}
buttonClickListenr_ = MakeRefPtr<ClickEvent>(std::move(clickCallback));
arrowGestureHub->AddClickEvent(buttonClickListenr_);
buttonClickListener_ = MakeRefPtr<ClickEvent>(std::move(clickCallback));
buttonGestureHub->AddClickEvent(buttonClickListener_);
}
void SwiperArrowPattern::ButtonClickEvent()
@ -323,6 +326,8 @@ void SwiperArrowPattern::SetButtonVisible(bool visible)
CHECK_NULL_VOID(host);
auto buttonNode = DynamicCast<FrameNode>(host->GetFirstChild());
CHECK_NULL_VOID(buttonNode);
auto buttonNodeGestureHub = buttonNode->GetOrCreateGestureEventHub();
CHECK_NULL_VOID(buttonNodeGestureHub);
const auto& renderContext = buttonNode->GetRenderContext();
CHECK_NULL_VOID(renderContext);
auto swiperArrowLayoutProperty = GetSwiperArrowLayoutProperty();
@ -332,13 +337,10 @@ void SwiperArrowPattern::SetButtonVisible(bool visible)
CHECK_NULL_VOID(hostFocusHub);
auto swiperPattern = GetSwiperPattern();
CHECK_NULL_VOID(swiperPattern);
auto gestureHub = host->GetOrCreateGestureEventHub();
CHECK_NULL_VOID(gestureHub);
auto displayCount = swiperPattern->GetDisplayCount();
bool leftArrowIsHidden = (index_ == 0);
bool rightArrowIsHidden = (index_ == swiperPattern->TotalCount() - displayCount);
if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_SIXTEEN) &&
swiperPattern->IsSwipeByGroup()) {
if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_SIXTEEN) && swiperPattern->IsSwipeByGroup()) {
leftArrowIsHidden = (index_ < displayCount);
rightArrowIsHidden = (index_ >= swiperPattern->TotalCount() - displayCount);
}
@ -353,7 +355,8 @@ void SwiperArrowPattern::SetButtonVisible(bool visible)
(swiperPattern->RealTotalCount() <= displayCount);
if (needHideArrow) {
renderContext->SetVisible(false);
gestureHub->SetHitTestMode(HitTestMode::HTMTRANSPARENT);
// Set hit test mode NONE to make sure button not respond to the touch events when invisible.
buttonNodeGestureHub->SetHitTestMode(HitTestMode::HTMNONE);
hostFocusHub->SetParentFocusable(false);
hostFocusHub->LostSelfFocus();
return;
@ -366,7 +369,8 @@ void SwiperArrowPattern::SetButtonVisible(bool visible)
visible = true;
}
renderContext->SetVisible(visible);
gestureHub->SetHitTestMode(visible ? HitTestMode::HTMDEFAULT :HitTestMode::HTMTRANSPARENT);
// Set hit test mode BLOCK to make sure button respond to the touch events when visible.
buttonNodeGestureHub->SetHitTestMode(visible ? HitTestMode::HTMBLOCK : HitTestMode::HTMNONE);
}
void SwiperArrowPattern::UpdateArrowContentBySymbol(RefPtr<FrameNode>& buttonNode,

View File

@ -81,7 +81,7 @@ private:
void OnModifyDone() override;
void InitNavigationArrow();
void InitSwiperChangeEvent(const RefPtr<SwiperEventHub>& swiperEventHub);
void InitButtonEvent();
void InitEvent();
void UpdateButtonNode(int32_t index);
void ButtonTouchEvent(RefPtr<FrameNode> buttonNode, TouchType touchType);
void ButtonClickEvent();
@ -92,9 +92,9 @@ private:
int32_t TotalCount() const;
RefPtr<SwiperPattern> GetSwiperPattern() const;
RefPtr<ClickEvent> buttonClickListenr_;
RefPtr<TouchEventImpl> buttonTouchListenr_;
RefPtr<InputEvent> buttonOnHoverListenr_;
RefPtr<ClickEvent> buttonClickListener_;
RefPtr<TouchEventImpl> buttonTouchListener_;
RefPtr<InputEvent> buttonOnHoverListener_;
std::shared_ptr<ChangeEvent> swiperChangeEvent_;
int32_t index_ = 0;