From e7c404ac65170c63d4e631e37dab8184967892cd Mon Sep 17 00:00:00 2001 From: zhangxiao150 Date: Mon, 12 Jun 2023 11:23:24 +0000 Subject: [PATCH] =?UTF-8?q?switch=20modifier=E6=9B=BF=E6=8D=A2=E8=80=81?= =?UTF-8?q?=E6=A1=86=E6=9E=B6=E5=8A=A8=E7=94=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangxiao150 Change-Id: If8314c5bb1216ef095ca0dfeb29704d7a3a55427 --- .../components/checkable/checkable_theme.h | 7 + .../pattern/toggle/switch_modifier.h | 53 +++++- .../pattern/toggle/switch_paint_method.cpp | 11 +- .../pattern/toggle/switch_paint_method.h | 15 +- .../pattern/toggle/switch_paint_property.h | 3 +- .../pattern/toggle/switch_pattern.cpp | 159 +++++------------- .../pattern/toggle/switch_pattern.h | 27 ++- .../core/pattern/toggle/toggle_test_ng.cpp | 68 ++------ 8 files changed, 133 insertions(+), 210 deletions(-) diff --git a/frameworks/core/components/checkable/checkable_theme.h b/frameworks/core/components/checkable/checkable_theme.h index 69b4e7bd4d5..758807d59dd 100644 --- a/frameworks/core/components/checkable/checkable_theme.h +++ b/frameworks/core/components/checkable/checkable_theme.h @@ -351,6 +351,7 @@ public: theme->hoverToTouchDuration_ = switchPattern->GetAttr("hover_to_press_animation_duration", 0.0); theme->touchDuration_ = switchPattern->GetAttr("touch_animation_duration", 0.0); theme->colorAnimationDuration_ = switchPattern->GetAttr("color_animation_duration", 0.0); + theme->pointAnimationDuration_ = switchPattern->GetAttr("point_animation_duration", 0.0); if (SystemProperties::GetDeviceType() != DeviceType::CAR) { return; @@ -373,8 +374,14 @@ public: return colorAnimationDuration_; } + double GetPointAnimationDuration() const + { + return pointAnimationDuration_; + } + private: double colorAnimationDuration_ = 0.0; + double pointAnimationDuration_ = 0.0; }; class RadioTheme : public CheckableTheme { diff --git a/frameworks/core/components_ng/pattern/toggle/switch_modifier.h b/frameworks/core/components_ng/pattern/toggle/switch_modifier.h index 3b48bd3cc45..e9ff33243e2 100644 --- a/frameworks/core/components_ng/pattern/toggle/switch_modifier.h +++ b/frameworks/core/components_ng/pattern/toggle/switch_modifier.h @@ -16,6 +16,7 @@ #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWITCH_SWITCH_MODIFIER_H #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWITCH_SWITCH_MODIFIER_H +#include #include #include "base/geometry/ng/offset_t.h" @@ -34,7 +35,7 @@ class SwitchModifier : public ContentModifier { DECLARE_ACE_TYPE(SwitchModifier, ContentModifier); public: - SwitchModifier(bool isSelect, const Color& boardColor, float mainDelta); + SwitchModifier(bool isSelect, const Color& boardColor, float dragOffsetX); ~SwitchModifier() override = default; void onDraw(DrawingContext& context) override @@ -64,12 +65,30 @@ public: default: break; } - AnimationOption option = AnimationOption(); - option.SetDuration(colorAnimationDuration_); - option.SetCurve(Curves::FAST_OUT_SLOW_IN); - AnimationUtils::Animate(option, [&]() { + // Animation is not displayed when created for the first time. + if (isFirstCreated_) { + animatableBoardColor_->Set(isSelect_->Get() ? LinearColor(userActiveColor_) : LinearColor(inactiveColor_)); + pointOffset_->Set(isSelect_->Get() ? size_->Get().Width() - size_->Get().Height() : 0.0f); + isFirstCreated_ = false; + } + AnimationOption colorOption = AnimationOption(); + colorOption.SetDuration(colorAnimationDuration_); + colorOption.SetCurve(Curves::FAST_OUT_SLOW_IN); + AnimationUtils::Animate(colorOption, [&]() { animatableBoardColor_->Set(isSelect_->Get() ? LinearColor(userActiveColor_) : LinearColor(inactiveColor_)); }); + + AnimationOption pointOption = AnimationOption(); + pointOption.SetDuration(pointAnimationDuration_); + pointOption.SetCurve(Curves::FAST_OUT_SLOW_IN); + float newPointOffset = 0.0f; + if (!isDragEvent_) { + newPointOffset = isSelect_->Get() ? size_->Get().Width() - size_->Get().Height() : 0.0f; + } else { + newPointOffset = std::clamp( + dragOffsetX_->Get() - offset_->Get().GetX(), 0.0f, size_->Get().Width() - size_->Get().Height()); + } + AnimationUtils::Animate(pointOption, [&]() { pointOffset_->Set(newPointOffset); }); } void SetBoardColor(LinearColor color, int32_t duratuion, const RefPtr& curve) @@ -143,10 +162,17 @@ public: } } - void SetMainDelta(float mainDelta) + void SetDragOffsetX(float dragOffsetX) { - if (mainDelta_) { - mainDelta_->Set(mainDelta); + if (dragOffsetX_) { + dragOffsetX_->Set(dragOffsetX); + } + } + + void SetPointOffset(float pointOffset) + { + if (pointOffset_) { + pointOffset_->Set(pointOffset); } } @@ -155,6 +181,11 @@ public: touchHoverType_ = touchHoverType; } + void SetIsDragEvent(bool isDragEvent) + { + isDragEvent_ = isDragEvent; + } + private: float actualWidth_ = 0.0f; float actualHeight_ = 0.0f; @@ -170,6 +201,9 @@ private: float hoverToTouchDuration_ = 0.0f; float touchDuration_ = 0.0f; float colorAnimationDuration_ = 0.0f; + float pointAnimationDuration_ = 0.0f; + bool isDragEvent_ = false; + bool isFirstCreated_ = true; OffsetF hotZoneOffset_; SizeF hotZoneSize_; @@ -178,7 +212,8 @@ private: RefPtr animatableBoardColor_; RefPtr animateTouchHoverColor_; RefPtr animatePointColor_; - RefPtr mainDelta_; + RefPtr pointOffset_; + RefPtr dragOffsetX_; RefPtr isSelect_; RefPtr isHover_; RefPtr offset_; diff --git a/frameworks/core/components_ng/pattern/toggle/switch_paint_method.cpp b/frameworks/core/components_ng/pattern/toggle/switch_paint_method.cpp index 8dc3506065c..926eedc1b14 100644 --- a/frameworks/core/components_ng/pattern/toggle/switch_paint_method.cpp +++ b/frameworks/core/components_ng/pattern/toggle/switch_paint_method.cpp @@ -35,7 +35,7 @@ constexpr uint8_t ENABLED_ALPHA = 255; constexpr uint8_t DISABLED_ALPHA = 102; } // namespace -SwitchModifier::SwitchModifier(bool isSelect, const Color& boardColor, float mainDelta) +SwitchModifier::SwitchModifier(bool isSelect, const Color& boardColor, float dragOffsetX) { auto pipeline = PipelineBase::GetCurrentContext(); CHECK_NULL_VOID(pipeline); @@ -44,7 +44,8 @@ SwitchModifier::SwitchModifier(bool isSelect, const Color& boardColor, float mai animatableBoardColor_ = AceType::MakeRefPtr(LinearColor(boardColor)); animateTouchHoverColor_ = AceType::MakeRefPtr(LinearColor(Color::TRANSPARENT)); animatePointColor_ = AceType::MakeRefPtr(LinearColor(switchTheme->GetPointColor())); - mainDelta_ = AceType::MakeRefPtr(mainDelta); + pointOffset_ = AceType::MakeRefPtr(0.0f); + dragOffsetX_ = AceType::MakeRefPtr(dragOffsetX); isSelect_ = AceType::MakeRefPtr(isSelect); isHover_ = AceType::MakeRefPtr(false); offset_ = AceType::MakeRefPtr(OffsetF()); @@ -54,7 +55,8 @@ SwitchModifier::SwitchModifier(bool isSelect, const Color& boardColor, float mai AttachProperty(animatableBoardColor_); AttachProperty(animateTouchHoverColor_); AttachProperty(animatePointColor_); - AttachProperty(mainDelta_); + AttachProperty(pointOffset_); + AttachProperty(dragOffsetX_); AttachProperty(isSelect_); AttachProperty(isHover_); AttachProperty(offset_); @@ -78,6 +80,7 @@ void SwitchModifier::InitializeParam() hoverToTouchDuration_ = switchTheme->GetHoverToTouchDuration(); touchDuration_ = switchTheme->GetTouchDuration(); colorAnimationDuration_ = switchTheme->GetColorAnimationDuration(); + pointAnimationDuration_ = switchTheme->GetPointAnimationDuration(); } void SwitchModifier::PaintSwitch(RSCanvas& canvas, const OffsetF& contentOffset, const SizeF& contentSize) @@ -137,7 +140,7 @@ void SwitchModifier::PaintSwitch(RSCanvas& canvas, const OffsetF& contentOffset, canvas.AttachBrush(brush); RSPoint point; - point.SetX(xOffset + actualGap + pointRadius_ + mainDelta_->Get()); + point.SetX(xOffset + actualGap + pointRadius_ + pointOffset_->Get()); point.SetY(yOffset + radius); canvas.DrawCircle(point, pointRadius_); } diff --git a/frameworks/core/components_ng/pattern/toggle/switch_paint_method.h b/frameworks/core/components_ng/pattern/toggle/switch_paint_method.h index e6b4c0f4b37..59a8c1e37ae 100644 --- a/frameworks/core/components_ng/pattern/toggle/switch_paint_method.h +++ b/frameworks/core/components_ng/pattern/toggle/switch_paint_method.h @@ -56,7 +56,8 @@ public: switchModifier_->SetEnabled(enabled_); switchModifier_->SetIsSelect(isSelect_); switchModifier_->SetTouchHoverAnimationType(touchHoverType_); - switchModifier_->SetMainDelta(mainDelta_); + switchModifier_->SetDragOffsetX(dragOffsetX_); + switchModifier_->SetIsDragEvent(isDragEvent_); switchModifier_->UpdateAnimatableProperty(); auto pipeline = PipelineBase::GetCurrentContext(); CHECK_NULL_VOID(pipeline); @@ -91,9 +92,9 @@ public: enabled_ = enabled; } - void SetMainDelta(float mainDelta) + void SetDragOffsetX(float dragOffsetX) { - mainDelta_ = mainDelta; + dragOffsetX_ = dragOffsetX; } void SetIsSelect(bool isSelect) @@ -111,8 +112,13 @@ public: touchHoverType_ = touchHoverType; } + void SetIsDragEvent(bool isDragEvent) + { + isDragEvent_ = isDragEvent; + } + private: - float mainDelta_ = 0.0f; + float dragOffsetX_ = 0.0f; float hoverPercent_ = 0.0f; const Dimension radiusGap_ = 2.0_vp; bool enabled_ = true; @@ -125,6 +131,7 @@ private: OffsetF hotZoneOffset_; SizeF hotZoneSize_; TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE; + bool isDragEvent_ = false; RefPtr switchModifier_; diff --git a/frameworks/core/components_ng/pattern/toggle/switch_paint_property.h b/frameworks/core/components_ng/pattern/toggle/switch_paint_property.h index f52c6fe2387..2f7c57b1fd8 100644 --- a/frameworks/core/components_ng/pattern/toggle/switch_paint_property.h +++ b/frameworks/core/components_ng/pattern/toggle/switch_paint_property.h @@ -77,7 +77,6 @@ public: ACE_DEFINE_PROPERTY_GROUP(SwitchPaintParagraph, SwitchPaintParagraph); ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP(SwitchPaintParagraph, SelectedColor, Color, PROPERTY_UPDATE_RENDER); ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP(SwitchPaintParagraph, SwitchPointColor, Color, PROPERTY_UPDATE_RENDER); - ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP(SwitchPaintParagraph, CurrentOffset, float, PROPERTY_UPDATE_RENDER); ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(IsOn, bool, PROPERTY_UPDATE_MEASURE); @@ -85,4 +84,4 @@ public: }; } // namespace OHOS::Ace::NG -#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SWITCH_SWITCH_PAINT_PROPERTY_H \ No newline at end of file +#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SWITCH_SWITCH_PAINT_PROPERTY_H diff --git a/frameworks/core/components_ng/pattern/toggle/switch_pattern.cpp b/frameworks/core/components_ng/pattern/toggle/switch_pattern.cpp index de5e8772948..8e17e0af26e 100644 --- a/frameworks/core/components_ng/pattern/toggle/switch_pattern.cpp +++ b/frameworks/core/components_ng/pattern/toggle/switch_pattern.cpp @@ -117,13 +117,12 @@ void SwitchPattern::OnModifyDone() CHECK_NULL_VOID(switchPaintProperty); auto geometryNode = host->GetGeometryNode(); CHECK_NULL_VOID(geometryNode); - if (!isOn_.has_value() || (isOn_.has_value() && NearZero(geometryNode->GetContentSize().Width()) && - NearZero(geometryNode->GetContentSize().Height()))) { - isOn_ = switchPaintProperty->GetIsOnValue(); + if (!isOn_.has_value()) { + isOn_ = switchPaintProperty->GetIsOnValue(false); } - auto isOn = switchPaintProperty->GetIsOnValue(); - isOnBeforeAnimate_ = isOn; - if (isOn != isOn_.value()) { + auto isOn = switchPaintProperty->GetIsOnValue(false); + if (isOn != isOn_.value_or(false)) { + isOn_ = isOn; OnChange(); } InitClickEvent(); @@ -135,70 +134,6 @@ void SwitchPattern::OnModifyDone() InitOnKeyEvent(focusHub); } -void SwitchPattern::UpdateCurrentOffset(float offset) -{ - currentOffset_ = currentOffset_ + offset; - auto host = GetHost(); - CHECK_NULL_VOID(host); - host->MarkDirtyNode(PROPERTY_UPDATE_RENDER); -} - -void SwitchPattern::PlayTranslateAnimation(float startPos, float endPos) -{ - LOGI("Play translate animation startPos: %{public}lf, endPos: %{public}lf", startPos, endPos); - auto host = GetHost(); - CHECK_NULL_VOID(host); - auto curve = GetCurve(); - if (!curve) { - curve = Curves::FAST_OUT_SLOW_IN; - } - - // If animation is still running, stop it before play new animation. - StopTranslateAnimation(); - - auto translate = AceType::MakeRefPtr>(startPos, endPos, curve); - auto weak = AceType::WeakClaim(this); - translate->AddListener(Animation::ValueCallback([weak, startPos, endPos](double value) { - auto switchPattern = weak.Upgrade(); - CHECK_NULL_VOID(switchPattern); - if (!NearEqual(value, startPos) && !NearEqual(value, endPos) && !NearEqual(startPos, endPos)) { - float moveRate = - Curves::EASE_OUT->MoveInternal(static_cast((value - startPos) / (endPos - startPos))); - value = startPos + (endPos - startPos) * moveRate; - } - switchPattern->UpdateCurrentOffset(static_cast(value - switchPattern->currentOffset_)); - })); - - if (!controller_) { - auto pipeline = PipelineBase::GetCurrentContext(); - CHECK_NULL_VOID(pipeline); - controller_ = CREATE_ANIMATOR(pipeline); - } - controller_->ClearStopListeners(); - controller_->ClearInterpolators(); - controller_->AddStopListener([weak]() { - auto switchPattern = weak.Upgrade(); - CHECK_NULL_VOID(switchPattern); - if (!switchPattern->isOn_.value()) { - if (NearEqual(switchPattern->currentOffset_, switchPattern->GetSwitchWidth()) && - switchPattern->changeFlag_) { - switchPattern->isOn_ = true; - switchPattern->UpdateChangeEvent(); - } - } else { - if (NearEqual(switchPattern->currentOffset_, 0) && switchPattern->changeFlag_) { - switchPattern->isOn_ = false; - switchPattern->UpdateChangeEvent(); - } - } - switchPattern->isOnBeforeAnimate_ = switchPattern->isOn_; - switchPattern->GetHost()->MarkDirtyNode(PROPERTY_UPDATE_RENDER); - }); - controller_->SetDuration(GetDuration()); - controller_->AddInterpolator(translate); - controller_->Play(); -} - RefPtr SwitchPattern::GetCurve() const { auto switchPaintProperty = GetPaintProperty(); @@ -213,28 +148,15 @@ int32_t SwitchPattern::GetDuration() const return switchPaintProperty->GetDuration().value_or(DEFAULT_DURATION); } -void SwitchPattern::StopTranslateAnimation() -{ - if (controller_ && !controller_->IsStopped()) { - controller_->Stop(); - } -} - void SwitchPattern::OnChange() { auto host = GetHost(); CHECK_NULL_VOID(host); - auto geometryNode = host->GetGeometryNode(); - CHECK_NULL_VOID(geometryNode); - auto translateOffset = GetSwitchWidth(); - StopTranslateAnimation(); - changeFlag_ = true; - isOnBeforeAnimate_ = !isOn_.value(); - if (!isOn_.value()) { - PlayTranslateAnimation(0.0f, translateOffset); - } else { - PlayTranslateAnimation(translateOffset, 0.0f); - } + auto switchPaintProperty = host->GetPaintProperty(); + CHECK_NULL_VOID(switchPaintProperty); + switchPaintProperty->UpdateIsOn(isOn_.value_or(false)); + UpdateChangeEvent(); + host->MarkDirtyNode(PROPERTY_UPDATE_RENDER); } float SwitchPattern::GetSwitchWidth() const @@ -247,6 +169,15 @@ float SwitchPattern::GetSwitchWidth() const return switchWidth; } +float SwitchPattern::GetSwitchContentOffsetX() const +{ + auto host = GetHost(); + CHECK_NULL_RETURN(host, 0.0f); + auto geometryNode = host->GetGeometryNode(); + CHECK_NULL_RETURN(geometryNode, 0.0f); + return geometryNode->GetContentOffset().GetX(); +} + void SwitchPattern::UpdateChangeEvent() const { auto switchEventHub = GetEventHub(); @@ -256,13 +187,7 @@ void SwitchPattern::UpdateChangeEvent() const void SwitchPattern::OnClick() { - auto host = GetHost(); - CHECK_NULL_VOID(host); - if (controller_ && !controller_->IsStopped()) { - // Clear stop listener before stop, otherwise the previous swipe will be considered complete. - controller_->ClearStopListeners(); - controller_->Stop(); - } + isOn_ = !isOn_.value_or(false); OnChange(); } @@ -305,6 +230,7 @@ void SwitchPattern::InitPanEvent(const RefPtr& gestureHub) if (info.GetInputEventType() == InputEventType::AXIS) { return; } + pattern->HandleDragStart(); }; auto actionUpdateTask = [weak = WeakClaim(this)](const GestureEvent& info) { @@ -468,39 +394,32 @@ void SwitchPattern::HandleMouseEvent(bool isHover) host->MarkDirtyNode(PROPERTY_UPDATE_RENDER); } +void SwitchPattern::HandleDragStart() +{ + isDragEvent_ = true; +} + void SwitchPattern::HandleDragUpdate(const GestureEvent& info) { - auto mainDelta = static_cast(info.GetMainDelta()); - auto isOutOfBoundary = IsOutOfBoundary(mainDelta + currentOffset_); - if (isOutOfBoundary) { - LOGD("Switch has reached boundary, can't drag any more."); - return; - } - UpdateCurrentOffset(static_cast(mainDelta)); + dragOffsetX_ = static_cast(info.GetLocalLocation().GetX()); + auto host = GetHost(); + CHECK_NULL_VOID(host); + host->MarkDirtyNode(PROPERTY_UPDATE_RENDER); } void SwitchPattern::HandleDragEnd() { - LOGD("Drag end currentOffset: %{public}lf", currentOffset_); - // Play translate animation. auto mainSize = GetSwitchWidth(); - if (std::abs(currentOffset_) >= mainSize / 2) { - if (!isOn_.value()) { - changeFlag_ = true; - PlayTranslateAnimation(mainSize, mainSize); - } else { - changeFlag_ = false; - PlayTranslateAnimation(currentOffset_, mainSize); - } - } else if (std::abs(currentOffset_) < mainSize / 2) { - if (isOn_.value()) { - changeFlag_ = true; - PlayTranslateAnimation(0.0f, 0.0f); - } else { - changeFlag_ = false; - PlayTranslateAnimation(currentOffset_, 0.0f); - } + auto contentOffset = GetSwitchContentOffsetX(); + if ((isOn_.value() && dragOffsetX_ - contentOffset < mainSize / 2) || + (!isOn_.value() && dragOffsetX_ - contentOffset >= mainSize / 2)) { + OnClick(); } + isDragEvent_ = false; + dragOffsetX_ = 0.0f; + auto host = GetHost(); + CHECK_NULL_VOID(host); + host->MarkDirtyNode(PROPERTY_UPDATE_RENDER); } bool SwitchPattern::IsOutOfBoundary(double mainOffset) const diff --git a/frameworks/core/components_ng/pattern/toggle/switch_pattern.h b/frameworks/core/components_ng/pattern/toggle/switch_pattern.h index fe7783a7b3e..8b73723380f 100644 --- a/frameworks/core/components_ng/pattern/toggle/switch_pattern.h +++ b/frameworks/core/components_ng/pattern/toggle/switch_pattern.h @@ -51,9 +51,7 @@ public: RefPtr CreatePaintProperty() override { - auto paintProperty = MakeRefPtr(); - paintProperty->UpdateCurrentOffset(currentOffset_); - return paintProperty; + return MakeRefPtr(); } RefPtr CreateNodePaintMethod() override @@ -67,16 +65,17 @@ public: auto isSelect = paintProperty->GetIsOnValue(false); auto boardColor = isSelect ? paintProperty->GetSelectedColorValue(switchTheme->GetActiveColor()) : switchTheme->GetInactivePointColor(); - switchModifier_ = AceType::MakeRefPtr(isSelect, boardColor, currentOffset_); + switchModifier_ = AceType::MakeRefPtr(isSelect, boardColor, dragOffsetX_); } auto paintMethod = MakeRefPtr(switchModifier_); - paintMethod->SetIsSelect(isOnBeforeAnimate_.value_or(false)); + paintMethod->SetIsSelect(isOn_.value_or(false)); auto eventHub = host->GetEventHub(); CHECK_NULL_RETURN(eventHub, nullptr); auto enabled = eventHub->IsEnabled(); paintMethod->SetEnabled(enabled); - paintMethod->SetMainDelta(currentOffset_); + paintMethod->SetDragOffsetX(dragOffsetX_); paintMethod->SetTouchHoverAnimationType(touchHoverType_); + paintMethod->SetIsDragEvent(isDragEvent_); return paintMethod; } @@ -106,24 +105,22 @@ public: } std::string ProvideRestoreInfo() override; - + void OnRestoreInfo(const std::string& restoreInfo) override; - + private: void OnModifyDone() override; - void UpdateCurrentOffset(float offset); void OnAttachToFrameNode() override; bool OnDirtyLayoutWrapperSwap(const RefPtr& dirty, bool skipMeasure, bool skipLayout) override; - void PlayTranslateAnimation(float startPos, float endPos); RefPtr GetCurve() const; int32_t GetDuration() const; - void StopTranslateAnimation(); void UpdateChangeEvent() const; void OnChange(); void OnTouchDown(); void OnTouchUp(); void HandleMouseEvent(bool isHover); float GetSwitchWidth() const; + float GetSwitchContentOffsetX() const; // Init pan recognizer to move items when drag update, play translate animation when drag end. void InitPanEvent(const RefPtr& gestureHub); @@ -136,6 +133,7 @@ private: bool OnKeyEvent(const KeyEvent& event); void GetInnerFocusPaintRect(RoundRect& paintRect); + void HandleDragStart(); void HandleDragUpdate(const GestureEvent& info); void HandleDragEnd(); @@ -146,13 +144,10 @@ private: RefPtr panEvent_; - RefPtr controller_; RefPtr clickListener_; - RefPtr> translate_; std::optional isOn_; - std::optional isOnBeforeAnimate_; - bool changeFlag_ = false; float currentOffset_ = 0.0f; + float dragOffsetX_ = 0.0f; RefPtr touchListener_; RefPtr mouseEvent_; @@ -168,7 +163,7 @@ private: OffsetF hotZoneOffset_; SizeF hotZoneSize_; TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE; - + bool isDragEvent_ = false; RefPtr switchModifier_; ACE_DISALLOW_COPY_AND_MOVE(SwitchPattern); }; diff --git a/test/unittest/core/pattern/toggle/toggle_test_ng.cpp b/test/unittest/core/pattern/toggle/toggle_test_ng.cpp index 479c3b96f6f..3c4347e1258 100644 --- a/test/unittest/core/pattern/toggle/toggle_test_ng.cpp +++ b/test/unittest/core/pattern/toggle/toggle_test_ng.cpp @@ -401,10 +401,8 @@ HWTEST_F(ToggleTestNg, TogglePatternTest008, TestSize.Level1) switchPattern->isOn_ = false; paintProperty->UpdateIsOn(true); switchPattern->OnModifyDone(); - EXPECT_EQ(switchPattern->isOn_, false); + EXPECT_EQ(switchPattern->isOn_, true); EXPECT_EQ(paintProperty->GetIsOnValue(), true); - paintProperty->UpdateCurve(Curves::LINEAR); - switchPattern->PlayTranslateAnimation(0.0f, 1.0f); } /** @@ -564,8 +562,6 @@ HWTEST_F(ToggleTestNg, TogglePatternTest0010, TestSize.Level1) switchPattern->HandleDragEnd(); switchPattern->isOn_ = true; switchPattern->HandleDragEnd(); - switchPattern->controller_ = CREATE_ANIMATOR(); - switchPattern->controller_->status_ = Animator::Status::RUNNING; switchPattern->OnClick(); } @@ -895,6 +891,11 @@ HWTEST_F(ToggleTestNg, TogglePaintTest002, TestSize.Level1) switchModifier->touchHoverType_ = TouchHoverAnimationType::PRESS; switchModifier->UpdateAnimatableProperty(); EXPECT_EQ(switchModifier->animateTouchHoverColor_->Get(), LinearColor(Color::BLUE)); + EXPECT_EQ(switchModifier->isFirstCreated_, false); + switchModifier->isDragEvent_ = true; + switchModifier->SetDragOffsetX(0.0f); + switchModifier->UpdateAnimatableProperty(); + EXPECT_EQ(switchModifier->pointOffset_->Get(), 0.0f); } /** @@ -1205,54 +1206,6 @@ HWTEST_F(ToggleTestNg, ToggleModelTest003, TestSize.Level1) toggleModelNG.Create(TOGGLE_TYPE[2], IS_ON); } -/** - * @tc.name: TogglePatternTest0018 - * @tc.desc: Test toggle PlayTranslateAnimation callback. - * @tc.type: FUNC - */ -HWTEST_F(ToggleTestNg, TogglePatternTest0018, TestSize.Level1) -{ - /** - * @tc.steps: step1. create switch and get frameNode. - */ - ToggleModelNG toggleModelNG; - toggleModelNG.Create(TOGGLE_TYPE[2], IS_ON); - auto switchFrameNode = AceType::DynamicCast(ViewStackProcessor::GetInstance()->Finish()); - ASSERT_NE(switchFrameNode, nullptr); - - auto pattern = switchFrameNode->GetPattern(); - ASSERT_NE(pattern, nullptr); - /** - * @tc.steps: step2. call method PlayTranslateAnimation. - */ - pattern->PlayTranslateAnimation(0.0f, 0.8f); - /** - * @tc.steps: step3. call the translate callback. - */ - auto& interpolators_ = pattern->controller_->interpolators_; - for (auto& interpolator : interpolators_) { - interpolator->OnInitNotify(0.4f, false); - } - /** - * @tc.steps: step4. call the NotifyStopListener. - */ - pattern->controller_->NotifyStopListener(); - /** - * cover changeFlag_ == true branch. - */ - pattern->changeFlag_ = true; - pattern->currentOffset_ = 0.0f; - pattern->controller_->NotifyStopListener(); - /** - * cover isOn_ == false branch. - */ - pattern->isOn_ = false; - pattern->controller_->NotifyStopListener(); - pattern->isOn_ = false; - pattern->changeFlag_ = false; - pattern->controller_->NotifyStopListener(); -} - /** * @tc.name: TogglePatternTest0019 * @tc.desc: Test toggle HandleDragEnd. @@ -1274,14 +1227,19 @@ HWTEST_F(ToggleTestNg, TogglePatternTest0019, TestSize.Level1) /** * @tc.steps: step2. call function HandleDragEnd. */ + pattern->dragOffsetX_ = 0; pattern->HandleDragEnd(); - EXPECT_TRUE(pattern->changeFlag_); + pattern->dragOffsetX_ = SWITCH_WIDTH; + pattern->HandleDragEnd(); + EXPECT_FALSE(pattern->isDragEvent_); /** * cover isOn_ == false branch. */ pattern->isOn_ = false; pattern->HandleDragEnd(); - EXPECT_FALSE(pattern->changeFlag_); + pattern->dragOffsetX_ = 0; + pattern->HandleDragEnd(); + EXPECT_FALSE(pattern->isDragEvent_); } /**