switch modifier替换老框架动画

Signed-off-by: zhangxiao150 <zhangxiao150@huawei.com>
Change-Id: If8314c5bb1216ef095ca0dfeb29704d7a3a55427
This commit is contained in:
zhangxiao150 2023-06-12 11:23:24 +00:00
parent 312b6661ac
commit e7c404ac65
8 changed files with 133 additions and 210 deletions

View File

@ -351,6 +351,7 @@ public:
theme->hoverToTouchDuration_ = switchPattern->GetAttr<double>("hover_to_press_animation_duration", 0.0); theme->hoverToTouchDuration_ = switchPattern->GetAttr<double>("hover_to_press_animation_duration", 0.0);
theme->touchDuration_ = switchPattern->GetAttr<double>("touch_animation_duration", 0.0); theme->touchDuration_ = switchPattern->GetAttr<double>("touch_animation_duration", 0.0);
theme->colorAnimationDuration_ = switchPattern->GetAttr<double>("color_animation_duration", 0.0); theme->colorAnimationDuration_ = switchPattern->GetAttr<double>("color_animation_duration", 0.0);
theme->pointAnimationDuration_ = switchPattern->GetAttr<double>("point_animation_duration", 0.0);
if (SystemProperties::GetDeviceType() != DeviceType::CAR) { if (SystemProperties::GetDeviceType() != DeviceType::CAR) {
return; return;
@ -373,8 +374,14 @@ public:
return colorAnimationDuration_; return colorAnimationDuration_;
} }
double GetPointAnimationDuration() const
{
return pointAnimationDuration_;
}
private: private:
double colorAnimationDuration_ = 0.0; double colorAnimationDuration_ = 0.0;
double pointAnimationDuration_ = 0.0;
}; };
class RadioTheme : public CheckableTheme { class RadioTheme : public CheckableTheme {

View File

@ -16,6 +16,7 @@
#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWITCH_SWITCH_MODIFIER_H #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWITCH_SWITCH_MODIFIER_H
#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWITCH_SWITCH_MODIFIER_H #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWITCH_SWITCH_MODIFIER_H
#include <algorithm>
#include <vector> #include <vector>
#include "base/geometry/ng/offset_t.h" #include "base/geometry/ng/offset_t.h"
@ -34,7 +35,7 @@ class SwitchModifier : public ContentModifier {
DECLARE_ACE_TYPE(SwitchModifier, ContentModifier); DECLARE_ACE_TYPE(SwitchModifier, ContentModifier);
public: public:
SwitchModifier(bool isSelect, const Color& boardColor, float mainDelta); SwitchModifier(bool isSelect, const Color& boardColor, float dragOffsetX);
~SwitchModifier() override = default; ~SwitchModifier() override = default;
void onDraw(DrawingContext& context) override void onDraw(DrawingContext& context) override
@ -64,12 +65,30 @@ public:
default: default:
break; break;
} }
AnimationOption option = AnimationOption(); // Animation is not displayed when created for the first time.
option.SetDuration(colorAnimationDuration_); if (isFirstCreated_) {
option.SetCurve(Curves::FAST_OUT_SLOW_IN); animatableBoardColor_->Set(isSelect_->Get() ? LinearColor(userActiveColor_) : LinearColor(inactiveColor_));
AnimationUtils::Animate(option, [&]() { 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_)); 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<CubicCurve>& curve) void SetBoardColor(LinearColor color, int32_t duratuion, const RefPtr<CubicCurve>& curve)
@ -143,10 +162,17 @@ public:
} }
} }
void SetMainDelta(float mainDelta) void SetDragOffsetX(float dragOffsetX)
{ {
if (mainDelta_) { if (dragOffsetX_) {
mainDelta_->Set(mainDelta); dragOffsetX_->Set(dragOffsetX);
}
}
void SetPointOffset(float pointOffset)
{
if (pointOffset_) {
pointOffset_->Set(pointOffset);
} }
} }
@ -155,6 +181,11 @@ public:
touchHoverType_ = touchHoverType; touchHoverType_ = touchHoverType;
} }
void SetIsDragEvent(bool isDragEvent)
{
isDragEvent_ = isDragEvent;
}
private: private:
float actualWidth_ = 0.0f; float actualWidth_ = 0.0f;
float actualHeight_ = 0.0f; float actualHeight_ = 0.0f;
@ -170,6 +201,9 @@ private:
float hoverToTouchDuration_ = 0.0f; float hoverToTouchDuration_ = 0.0f;
float touchDuration_ = 0.0f; float touchDuration_ = 0.0f;
float colorAnimationDuration_ = 0.0f; float colorAnimationDuration_ = 0.0f;
float pointAnimationDuration_ = 0.0f;
bool isDragEvent_ = false;
bool isFirstCreated_ = true;
OffsetF hotZoneOffset_; OffsetF hotZoneOffset_;
SizeF hotZoneSize_; SizeF hotZoneSize_;
@ -178,7 +212,8 @@ private:
RefPtr<AnimatablePropertyColor> animatableBoardColor_; RefPtr<AnimatablePropertyColor> animatableBoardColor_;
RefPtr<AnimatablePropertyColor> animateTouchHoverColor_; RefPtr<AnimatablePropertyColor> animateTouchHoverColor_;
RefPtr<AnimatablePropertyColor> animatePointColor_; RefPtr<AnimatablePropertyColor> animatePointColor_;
RefPtr<PropertyFloat> mainDelta_; RefPtr<AnimatablePropertyFloat> pointOffset_;
RefPtr<PropertyFloat> dragOffsetX_;
RefPtr<PropertyBool> isSelect_; RefPtr<PropertyBool> isSelect_;
RefPtr<PropertyBool> isHover_; RefPtr<PropertyBool> isHover_;
RefPtr<AnimatablePropertyOffsetF> offset_; RefPtr<AnimatablePropertyOffsetF> offset_;

View File

@ -35,7 +35,7 @@ constexpr uint8_t ENABLED_ALPHA = 255;
constexpr uint8_t DISABLED_ALPHA = 102; constexpr uint8_t DISABLED_ALPHA = 102;
} // namespace } // namespace
SwitchModifier::SwitchModifier(bool isSelect, const Color& boardColor, float mainDelta) SwitchModifier::SwitchModifier(bool isSelect, const Color& boardColor, float dragOffsetX)
{ {
auto pipeline = PipelineBase::GetCurrentContext(); auto pipeline = PipelineBase::GetCurrentContext();
CHECK_NULL_VOID(pipeline); CHECK_NULL_VOID(pipeline);
@ -44,7 +44,8 @@ SwitchModifier::SwitchModifier(bool isSelect, const Color& boardColor, float mai
animatableBoardColor_ = AceType::MakeRefPtr<AnimatablePropertyColor>(LinearColor(boardColor)); animatableBoardColor_ = AceType::MakeRefPtr<AnimatablePropertyColor>(LinearColor(boardColor));
animateTouchHoverColor_ = AceType::MakeRefPtr<AnimatablePropertyColor>(LinearColor(Color::TRANSPARENT)); animateTouchHoverColor_ = AceType::MakeRefPtr<AnimatablePropertyColor>(LinearColor(Color::TRANSPARENT));
animatePointColor_ = AceType::MakeRefPtr<AnimatablePropertyColor>(LinearColor(switchTheme->GetPointColor())); animatePointColor_ = AceType::MakeRefPtr<AnimatablePropertyColor>(LinearColor(switchTheme->GetPointColor()));
mainDelta_ = AceType::MakeRefPtr<PropertyFloat>(mainDelta); pointOffset_ = AceType::MakeRefPtr<AnimatablePropertyFloat>(0.0f);
dragOffsetX_ = AceType::MakeRefPtr<PropertyFloat>(dragOffsetX);
isSelect_ = AceType::MakeRefPtr<PropertyBool>(isSelect); isSelect_ = AceType::MakeRefPtr<PropertyBool>(isSelect);
isHover_ = AceType::MakeRefPtr<PropertyBool>(false); isHover_ = AceType::MakeRefPtr<PropertyBool>(false);
offset_ = AceType::MakeRefPtr<AnimatablePropertyOffsetF>(OffsetF()); offset_ = AceType::MakeRefPtr<AnimatablePropertyOffsetF>(OffsetF());
@ -54,7 +55,8 @@ SwitchModifier::SwitchModifier(bool isSelect, const Color& boardColor, float mai
AttachProperty(animatableBoardColor_); AttachProperty(animatableBoardColor_);
AttachProperty(animateTouchHoverColor_); AttachProperty(animateTouchHoverColor_);
AttachProperty(animatePointColor_); AttachProperty(animatePointColor_);
AttachProperty(mainDelta_); AttachProperty(pointOffset_);
AttachProperty(dragOffsetX_);
AttachProperty(isSelect_); AttachProperty(isSelect_);
AttachProperty(isHover_); AttachProperty(isHover_);
AttachProperty(offset_); AttachProperty(offset_);
@ -78,6 +80,7 @@ void SwitchModifier::InitializeParam()
hoverToTouchDuration_ = switchTheme->GetHoverToTouchDuration(); hoverToTouchDuration_ = switchTheme->GetHoverToTouchDuration();
touchDuration_ = switchTheme->GetTouchDuration(); touchDuration_ = switchTheme->GetTouchDuration();
colorAnimationDuration_ = switchTheme->GetColorAnimationDuration(); colorAnimationDuration_ = switchTheme->GetColorAnimationDuration();
pointAnimationDuration_ = switchTheme->GetPointAnimationDuration();
} }
void SwitchModifier::PaintSwitch(RSCanvas& canvas, const OffsetF& contentOffset, const SizeF& contentSize) 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); canvas.AttachBrush(brush);
RSPoint point; RSPoint point;
point.SetX(xOffset + actualGap + pointRadius_ + mainDelta_->Get()); point.SetX(xOffset + actualGap + pointRadius_ + pointOffset_->Get());
point.SetY(yOffset + radius); point.SetY(yOffset + radius);
canvas.DrawCircle(point, pointRadius_); canvas.DrawCircle(point, pointRadius_);
} }

View File

@ -56,7 +56,8 @@ public:
switchModifier_->SetEnabled(enabled_); switchModifier_->SetEnabled(enabled_);
switchModifier_->SetIsSelect(isSelect_); switchModifier_->SetIsSelect(isSelect_);
switchModifier_->SetTouchHoverAnimationType(touchHoverType_); switchModifier_->SetTouchHoverAnimationType(touchHoverType_);
switchModifier_->SetMainDelta(mainDelta_); switchModifier_->SetDragOffsetX(dragOffsetX_);
switchModifier_->SetIsDragEvent(isDragEvent_);
switchModifier_->UpdateAnimatableProperty(); switchModifier_->UpdateAnimatableProperty();
auto pipeline = PipelineBase::GetCurrentContext(); auto pipeline = PipelineBase::GetCurrentContext();
CHECK_NULL_VOID(pipeline); CHECK_NULL_VOID(pipeline);
@ -91,9 +92,9 @@ public:
enabled_ = enabled; enabled_ = enabled;
} }
void SetMainDelta(float mainDelta) void SetDragOffsetX(float dragOffsetX)
{ {
mainDelta_ = mainDelta; dragOffsetX_ = dragOffsetX;
} }
void SetIsSelect(bool isSelect) void SetIsSelect(bool isSelect)
@ -111,8 +112,13 @@ public:
touchHoverType_ = touchHoverType; touchHoverType_ = touchHoverType;
} }
void SetIsDragEvent(bool isDragEvent)
{
isDragEvent_ = isDragEvent;
}
private: private:
float mainDelta_ = 0.0f; float dragOffsetX_ = 0.0f;
float hoverPercent_ = 0.0f; float hoverPercent_ = 0.0f;
const Dimension radiusGap_ = 2.0_vp; const Dimension radiusGap_ = 2.0_vp;
bool enabled_ = true; bool enabled_ = true;
@ -125,6 +131,7 @@ private:
OffsetF hotZoneOffset_; OffsetF hotZoneOffset_;
SizeF hotZoneSize_; SizeF hotZoneSize_;
TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE; TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE;
bool isDragEvent_ = false;
RefPtr<SwitchModifier> switchModifier_; RefPtr<SwitchModifier> switchModifier_;

View File

@ -77,7 +77,6 @@ public:
ACE_DEFINE_PROPERTY_GROUP(SwitchPaintParagraph, SwitchPaintParagraph); 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, SelectedColor, Color, PROPERTY_UPDATE_RENDER);
ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP(SwitchPaintParagraph, SwitchPointColor, 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); ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(IsOn, bool, PROPERTY_UPDATE_MEASURE);
@ -85,4 +84,4 @@ public:
}; };
} // namespace OHOS::Ace::NG } // namespace OHOS::Ace::NG
#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SWITCH_SWITCH_PAINT_PROPERTY_H #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SWITCH_SWITCH_PAINT_PROPERTY_H

View File

@ -117,13 +117,12 @@ void SwitchPattern::OnModifyDone()
CHECK_NULL_VOID(switchPaintProperty); CHECK_NULL_VOID(switchPaintProperty);
auto geometryNode = host->GetGeometryNode(); auto geometryNode = host->GetGeometryNode();
CHECK_NULL_VOID(geometryNode); CHECK_NULL_VOID(geometryNode);
if (!isOn_.has_value() || (isOn_.has_value() && NearZero(geometryNode->GetContentSize().Width()) && if (!isOn_.has_value()) {
NearZero(geometryNode->GetContentSize().Height()))) { isOn_ = switchPaintProperty->GetIsOnValue(false);
isOn_ = switchPaintProperty->GetIsOnValue();
} }
auto isOn = switchPaintProperty->GetIsOnValue(); auto isOn = switchPaintProperty->GetIsOnValue(false);
isOnBeforeAnimate_ = isOn; if (isOn != isOn_.value_or(false)) {
if (isOn != isOn_.value()) { isOn_ = isOn;
OnChange(); OnChange();
} }
InitClickEvent(); InitClickEvent();
@ -135,70 +134,6 @@ void SwitchPattern::OnModifyDone()
InitOnKeyEvent(focusHub); 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<CurveAnimation<double>>(startPos, endPos, curve);
auto weak = AceType::WeakClaim(this);
translate->AddListener(Animation<double>::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<float>((value - startPos) / (endPos - startPos)));
value = startPos + (endPos - startPos) * moveRate;
}
switchPattern->UpdateCurrentOffset(static_cast<float>(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<Curve> SwitchPattern::GetCurve() const RefPtr<Curve> SwitchPattern::GetCurve() const
{ {
auto switchPaintProperty = GetPaintProperty<SwitchPaintProperty>(); auto switchPaintProperty = GetPaintProperty<SwitchPaintProperty>();
@ -213,28 +148,15 @@ int32_t SwitchPattern::GetDuration() const
return switchPaintProperty->GetDuration().value_or(DEFAULT_DURATION); return switchPaintProperty->GetDuration().value_or(DEFAULT_DURATION);
} }
void SwitchPattern::StopTranslateAnimation()
{
if (controller_ && !controller_->IsStopped()) {
controller_->Stop();
}
}
void SwitchPattern::OnChange() void SwitchPattern::OnChange()
{ {
auto host = GetHost(); auto host = GetHost();
CHECK_NULL_VOID(host); CHECK_NULL_VOID(host);
auto geometryNode = host->GetGeometryNode(); auto switchPaintProperty = host->GetPaintProperty<SwitchPaintProperty>();
CHECK_NULL_VOID(geometryNode); CHECK_NULL_VOID(switchPaintProperty);
auto translateOffset = GetSwitchWidth(); switchPaintProperty->UpdateIsOn(isOn_.value_or(false));
StopTranslateAnimation(); UpdateChangeEvent();
changeFlag_ = true; host->MarkDirtyNode(PROPERTY_UPDATE_RENDER);
isOnBeforeAnimate_ = !isOn_.value();
if (!isOn_.value()) {
PlayTranslateAnimation(0.0f, translateOffset);
} else {
PlayTranslateAnimation(translateOffset, 0.0f);
}
} }
float SwitchPattern::GetSwitchWidth() const float SwitchPattern::GetSwitchWidth() const
@ -247,6 +169,15 @@ float SwitchPattern::GetSwitchWidth() const
return switchWidth; 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 void SwitchPattern::UpdateChangeEvent() const
{ {
auto switchEventHub = GetEventHub<SwitchEventHub>(); auto switchEventHub = GetEventHub<SwitchEventHub>();
@ -256,13 +187,7 @@ void SwitchPattern::UpdateChangeEvent() const
void SwitchPattern::OnClick() void SwitchPattern::OnClick()
{ {
auto host = GetHost(); isOn_ = !isOn_.value_or(false);
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();
}
OnChange(); OnChange();
} }
@ -305,6 +230,7 @@ void SwitchPattern::InitPanEvent(const RefPtr<GestureEventHub>& gestureHub)
if (info.GetInputEventType() == InputEventType::AXIS) { if (info.GetInputEventType() == InputEventType::AXIS) {
return; return;
} }
pattern->HandleDragStart();
}; };
auto actionUpdateTask = [weak = WeakClaim(this)](const GestureEvent& info) { auto actionUpdateTask = [weak = WeakClaim(this)](const GestureEvent& info) {
@ -468,39 +394,32 @@ void SwitchPattern::HandleMouseEvent(bool isHover)
host->MarkDirtyNode(PROPERTY_UPDATE_RENDER); host->MarkDirtyNode(PROPERTY_UPDATE_RENDER);
} }
void SwitchPattern::HandleDragStart()
{
isDragEvent_ = true;
}
void SwitchPattern::HandleDragUpdate(const GestureEvent& info) void SwitchPattern::HandleDragUpdate(const GestureEvent& info)
{ {
auto mainDelta = static_cast<float>(info.GetMainDelta()); dragOffsetX_ = static_cast<float>(info.GetLocalLocation().GetX());
auto isOutOfBoundary = IsOutOfBoundary(mainDelta + currentOffset_); auto host = GetHost();
if (isOutOfBoundary) { CHECK_NULL_VOID(host);
LOGD("Switch has reached boundary, can't drag any more."); host->MarkDirtyNode(PROPERTY_UPDATE_RENDER);
return;
}
UpdateCurrentOffset(static_cast<float>(mainDelta));
} }
void SwitchPattern::HandleDragEnd() void SwitchPattern::HandleDragEnd()
{ {
LOGD("Drag end currentOffset: %{public}lf", currentOffset_);
// Play translate animation.
auto mainSize = GetSwitchWidth(); auto mainSize = GetSwitchWidth();
if (std::abs(currentOffset_) >= mainSize / 2) { auto contentOffset = GetSwitchContentOffsetX();
if (!isOn_.value()) { if ((isOn_.value() && dragOffsetX_ - contentOffset < mainSize / 2) ||
changeFlag_ = true; (!isOn_.value() && dragOffsetX_ - contentOffset >= mainSize / 2)) {
PlayTranslateAnimation(mainSize, mainSize); OnClick();
} 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);
}
} }
isDragEvent_ = false;
dragOffsetX_ = 0.0f;
auto host = GetHost();
CHECK_NULL_VOID(host);
host->MarkDirtyNode(PROPERTY_UPDATE_RENDER);
} }
bool SwitchPattern::IsOutOfBoundary(double mainOffset) const bool SwitchPattern::IsOutOfBoundary(double mainOffset) const

View File

@ -51,9 +51,7 @@ public:
RefPtr<PaintProperty> CreatePaintProperty() override RefPtr<PaintProperty> CreatePaintProperty() override
{ {
auto paintProperty = MakeRefPtr<SwitchPaintProperty>(); return MakeRefPtr<SwitchPaintProperty>();
paintProperty->UpdateCurrentOffset(currentOffset_);
return paintProperty;
} }
RefPtr<NodePaintMethod> CreateNodePaintMethod() override RefPtr<NodePaintMethod> CreateNodePaintMethod() override
@ -67,16 +65,17 @@ public:
auto isSelect = paintProperty->GetIsOnValue(false); auto isSelect = paintProperty->GetIsOnValue(false);
auto boardColor = isSelect ? paintProperty->GetSelectedColorValue(switchTheme->GetActiveColor()) auto boardColor = isSelect ? paintProperty->GetSelectedColorValue(switchTheme->GetActiveColor())
: switchTheme->GetInactivePointColor(); : switchTheme->GetInactivePointColor();
switchModifier_ = AceType::MakeRefPtr<SwitchModifier>(isSelect, boardColor, currentOffset_); switchModifier_ = AceType::MakeRefPtr<SwitchModifier>(isSelect, boardColor, dragOffsetX_);
} }
auto paintMethod = MakeRefPtr<SwitchPaintMethod>(switchModifier_); auto paintMethod = MakeRefPtr<SwitchPaintMethod>(switchModifier_);
paintMethod->SetIsSelect(isOnBeforeAnimate_.value_or(false)); paintMethod->SetIsSelect(isOn_.value_or(false));
auto eventHub = host->GetEventHub<EventHub>(); auto eventHub = host->GetEventHub<EventHub>();
CHECK_NULL_RETURN(eventHub, nullptr); CHECK_NULL_RETURN(eventHub, nullptr);
auto enabled = eventHub->IsEnabled(); auto enabled = eventHub->IsEnabled();
paintMethod->SetEnabled(enabled); paintMethod->SetEnabled(enabled);
paintMethod->SetMainDelta(currentOffset_); paintMethod->SetDragOffsetX(dragOffsetX_);
paintMethod->SetTouchHoverAnimationType(touchHoverType_); paintMethod->SetTouchHoverAnimationType(touchHoverType_);
paintMethod->SetIsDragEvent(isDragEvent_);
return paintMethod; return paintMethod;
} }
@ -106,24 +105,22 @@ public:
} }
std::string ProvideRestoreInfo() override; std::string ProvideRestoreInfo() override;
void OnRestoreInfo(const std::string& restoreInfo) override; void OnRestoreInfo(const std::string& restoreInfo) override;
private: private:
void OnModifyDone() override; void OnModifyDone() override;
void UpdateCurrentOffset(float offset);
void OnAttachToFrameNode() override; void OnAttachToFrameNode() override;
bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, bool skipMeasure, bool skipLayout) override; bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, bool skipMeasure, bool skipLayout) override;
void PlayTranslateAnimation(float startPos, float endPos);
RefPtr<Curve> GetCurve() const; RefPtr<Curve> GetCurve() const;
int32_t GetDuration() const; int32_t GetDuration() const;
void StopTranslateAnimation();
void UpdateChangeEvent() const; void UpdateChangeEvent() const;
void OnChange(); void OnChange();
void OnTouchDown(); void OnTouchDown();
void OnTouchUp(); void OnTouchUp();
void HandleMouseEvent(bool isHover); void HandleMouseEvent(bool isHover);
float GetSwitchWidth() const; float GetSwitchWidth() const;
float GetSwitchContentOffsetX() const;
// Init pan recognizer to move items when drag update, play translate animation when drag end. // Init pan recognizer to move items when drag update, play translate animation when drag end.
void InitPanEvent(const RefPtr<GestureEventHub>& gestureHub); void InitPanEvent(const RefPtr<GestureEventHub>& gestureHub);
@ -136,6 +133,7 @@ private:
bool OnKeyEvent(const KeyEvent& event); bool OnKeyEvent(const KeyEvent& event);
void GetInnerFocusPaintRect(RoundRect& paintRect); void GetInnerFocusPaintRect(RoundRect& paintRect);
void HandleDragStart();
void HandleDragUpdate(const GestureEvent& info); void HandleDragUpdate(const GestureEvent& info);
void HandleDragEnd(); void HandleDragEnd();
@ -146,13 +144,10 @@ private:
RefPtr<PanEvent> panEvent_; RefPtr<PanEvent> panEvent_;
RefPtr<Animator> controller_;
RefPtr<ClickEvent> clickListener_; RefPtr<ClickEvent> clickListener_;
RefPtr<CurveAnimation<double>> translate_;
std::optional<bool> isOn_; std::optional<bool> isOn_;
std::optional<bool> isOnBeforeAnimate_;
bool changeFlag_ = false;
float currentOffset_ = 0.0f; float currentOffset_ = 0.0f;
float dragOffsetX_ = 0.0f;
RefPtr<TouchEventImpl> touchListener_; RefPtr<TouchEventImpl> touchListener_;
RefPtr<InputEvent> mouseEvent_; RefPtr<InputEvent> mouseEvent_;
@ -168,7 +163,7 @@ private:
OffsetF hotZoneOffset_; OffsetF hotZoneOffset_;
SizeF hotZoneSize_; SizeF hotZoneSize_;
TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE; TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE;
bool isDragEvent_ = false;
RefPtr<SwitchModifier> switchModifier_; RefPtr<SwitchModifier> switchModifier_;
ACE_DISALLOW_COPY_AND_MOVE(SwitchPattern); ACE_DISALLOW_COPY_AND_MOVE(SwitchPattern);
}; };

View File

@ -401,10 +401,8 @@ HWTEST_F(ToggleTestNg, TogglePatternTest008, TestSize.Level1)
switchPattern->isOn_ = false; switchPattern->isOn_ = false;
paintProperty->UpdateIsOn(true); paintProperty->UpdateIsOn(true);
switchPattern->OnModifyDone(); switchPattern->OnModifyDone();
EXPECT_EQ(switchPattern->isOn_, false); EXPECT_EQ(switchPattern->isOn_, true);
EXPECT_EQ(paintProperty->GetIsOnValue(), 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->HandleDragEnd();
switchPattern->isOn_ = true; switchPattern->isOn_ = true;
switchPattern->HandleDragEnd(); switchPattern->HandleDragEnd();
switchPattern->controller_ = CREATE_ANIMATOR();
switchPattern->controller_->status_ = Animator::Status::RUNNING;
switchPattern->OnClick(); switchPattern->OnClick();
} }
@ -895,6 +891,11 @@ HWTEST_F(ToggleTestNg, TogglePaintTest002, TestSize.Level1)
switchModifier->touchHoverType_ = TouchHoverAnimationType::PRESS; switchModifier->touchHoverType_ = TouchHoverAnimationType::PRESS;
switchModifier->UpdateAnimatableProperty(); switchModifier->UpdateAnimatableProperty();
EXPECT_EQ(switchModifier->animateTouchHoverColor_->Get(), LinearColor(Color::BLUE)); 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); 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<FrameNode>(ViewStackProcessor::GetInstance()->Finish());
ASSERT_NE(switchFrameNode, nullptr);
auto pattern = switchFrameNode->GetPattern<SwitchPattern>();
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.name: TogglePatternTest0019
* @tc.desc: Test toggle HandleDragEnd. * @tc.desc: Test toggle HandleDragEnd.
@ -1274,14 +1227,19 @@ HWTEST_F(ToggleTestNg, TogglePatternTest0019, TestSize.Level1)
/** /**
* @tc.steps: step2. call function HandleDragEnd. * @tc.steps: step2. call function HandleDragEnd.
*/ */
pattern->dragOffsetX_ = 0;
pattern->HandleDragEnd(); pattern->HandleDragEnd();
EXPECT_TRUE(pattern->changeFlag_); pattern->dragOffsetX_ = SWITCH_WIDTH;
pattern->HandleDragEnd();
EXPECT_FALSE(pattern->isDragEvent_);
/** /**
* cover isOn_ == false branch. * cover isOn_ == false branch.
*/ */
pattern->isOn_ = false; pattern->isOn_ = false;
pattern->HandleDragEnd(); pattern->HandleDragEnd();
EXPECT_FALSE(pattern->changeFlag_); pattern->dragOffsetX_ = 0;
pattern->HandleDragEnd();
EXPECT_FALSE(pattern->isDragEvent_);
} }
/** /**