diff --git a/frameworks/core/components_ng/pattern/toggle/switch_modifier.h b/frameworks/core/components_ng/pattern/toggle/switch_modifier.h index e7aa9fc3701..78739cd0e17 100644 --- a/frameworks/core/components_ng/pattern/toggle/switch_modifier.h +++ b/frameworks/core/components_ng/pattern/toggle/switch_modifier.h @@ -80,13 +80,15 @@ public: auto offsetIsRtl = GreatOrEqual(actualSize_.Width(), actualSize_.Height()) ? (isSelect_->Get() ? 0.0f : actualSize_.Width() - actualSize_.Height()) : (isSelect_->Get() ? actualTrackRadius_ : actualSize_.Width() - actualTrackRadius_); - AnimationOption colorOption = AnimationOption(); - colorOption.SetDuration(colorAnimationDuration_); - colorOption.SetCurve(Curves::FAST_OUT_SLOW_IN); - AnimationUtils::Animate(colorOption, [&]() { - animatableBoardColor_->Set(isSelect_->Get() ? LinearColor(userActiveColor_) : LinearColor(inactiveColor_)); - }); - + if (isCancelAnimation_ && !isFocusOrBlur_) { + 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); @@ -119,6 +121,13 @@ public: } } + void SetBoardColor(Color color) + { + if (animatableBoardColor_) { + animatableBoardColor_->Set(LinearColor(color)); + } + } + void InitializeParam(); void PaintSwitch(RSCanvas& canvas, const OffsetF& contentOffset, const SizeF& contentSize); float GetSwitchWidth(const SizeF& contentSize) const; @@ -278,6 +287,11 @@ public: actualTrackRadius_ = borderRadius; } + void SetIsFocusOrBlur(bool isFocusOrBlur) + { + isFocusOrBlur_ = isFocusOrBlur; + } + private: float actualWidth_ = 0.0f; float actualHeight_ = 0.0f; @@ -294,6 +308,7 @@ private: Dimension hoverRadius_ = 8.0_vp; Dimension focusRadius_ = 8.0_vp; bool isUseDiffPointColor_ = false; + bool isCancelAnimation_ = false; float hoverDuration_ = 0.0f; float hoverToTouchDuration_ = 0.0f; float touchDuration_ = 0.0f; @@ -302,6 +317,7 @@ private: bool isDragEvent_ = false; bool isFirstCreated_ = true; bool showHoverEffect_ = true; + bool isFocusOrBlur_ = false; float actualTrackRadius_ = 0.0f; OffsetF hotZoneOffset_; 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 3fc0ff28bf8..43f1afd1709 100644 --- a/frameworks/core/components_ng/pattern/toggle/switch_paint_method.cpp +++ b/frameworks/core/components_ng/pattern/toggle/switch_paint_method.cpp @@ -93,6 +93,7 @@ void SwitchModifier::InitializeParam() pointColorUnchecked_ = switchTheme->GetPointColorUnchecked(); isUseDiffPointColor_ = switchTheme->GetSwitchUseDiffPointColor(); focusBoardColor_ = switchTheme->GetFocusBoardColor(); + isCancelAnimation_ = isUseDiffPointColor_; } float SwitchModifier::CalcActualWidth(float width, float height, double actualGap, double defaultWidthGap) @@ -143,9 +144,9 @@ void SwitchModifier::PaintSwitch(RSCanvas& canvas, const OffsetF& contentOffset, float boardIdealHeight = (switchTheme->GetFocusBoardHeight() - switchTheme->GetHotZoneVerticalPadding() * NUM_TWO).ConvertToPx(); if (animateTrackRadius_->Get() < 0 && idealHeight != 0) { - focusRadius_ = focusRadius_ * height / idealHeight; + focusRadius_ = focusRadius_ * height / idealHeight + switchTheme->GetFocusPaintPadding(); } else if (boardIdealHeight != 0) { - focusRadius_ = focusRadius_ * height / boardIdealHeight; + focusRadius_ = focusRadius_ * height / boardIdealHeight + switchTheme->GetFocusPaintPadding(); } OffsetF hoverBoardOffset; @@ -163,9 +164,8 @@ void SwitchModifier::DrawRectCircle(RSCanvas& canvas, const OffsetF& contentOffs auto xOffset = contentOffset.GetX(); auto yOffset = contentOffset.GetY(); auto height = contentSize.Height(); - auto radius = height / NUM_TWO; auto trackRadius = - (animateTrackRadius_->Get() < 0) ? radius : animateTrackRadius_->Get(); + (animateTrackRadius_->Get() < 0) ? height / NUM_TWO : animateTrackRadius_->Get(); RSRect rect = RSRect(xOffset, yOffset, xOffset + contentSize.Width(), yOffset + height); RSRoundRect roundRect(rect, trackRadius, trackRadius); @@ -175,6 +175,7 @@ void SwitchModifier::DrawRectCircle(RSCanvas& canvas, const OffsetF& contentOffs ToRSColor(animatableBoardColor_->Get().BlendOpacity(static_cast(DISABLED_ALPHA) / ENABLED_ALPHA))); } else { brush.SetColor(ToRSColor(animatableBoardColor_->Get())); + SetIsFocusOrBlur(false); } brush.SetBlendMode(RSBlendMode::SRC_OVER); brush.SetAntiAlias(true); @@ -208,7 +209,7 @@ void SwitchModifier::DrawRectCircle(RSCanvas& canvas, const OffsetF& contentOffs } else { point.SetX(xOffset + pointOffset_->Get()); } - point.SetY(yOffset + radius); + point.SetY(yOffset + height / NUM_TWO); canvas.DrawCircle(point, pointRadius_); canvas.DetachBrush(); } diff --git a/frameworks/core/components_ng/pattern/toggle/switch_pattern.cpp b/frameworks/core/components_ng/pattern/toggle/switch_pattern.cpp index b6b431c0cfc..e5f56d17ef2 100644 --- a/frameworks/core/components_ng/pattern/toggle/switch_pattern.cpp +++ b/frameworks/core/components_ng/pattern/toggle/switch_pattern.cpp @@ -198,6 +198,18 @@ void SwitchPattern::OnIsFocusActiveUpdate(bool isFocusAcitve) CHECK_NULL_VOID(switchModifier); switchModifier->SetIsFocused(isFocusAcitve); isFocus_ = isFocusAcitve; + + CHECK_NULL_VOID(pipeline_); + auto switchTheme = pipeline_->GetTheme(); + CHECK_NULL_VOID(switchTheme); + Color color; + if (isFocus_) { + color = isOn_.value_or(false) ? switchTheme->GetActiveColor() : switchTheme->GetFocusedBGColorUnselected(); + } else { + color = isOn_.value_or(false) ? switchTheme->GetActiveColor() : switchTheme->GetInactiveColor(); + } + switchModifier->SetIsFocusOrBlur(true); + switchModifier->SetBoardColor(color); } void SwitchPattern::UpdateSwitchPaintProperty()