1 /* 2 * Copyright (c) 2022-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_PROGRESS_PROGRESS_PAINT_METHOD_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_PROGRESS_PROGRESS_PAINT_METHOD_H 18 19 #include <optional> 20 21 #include "base/geometry/dimension.h" 22 #include "base/geometry/ng/offset_t.h" 23 #include "base/geometry/ng/size_t.h" 24 #include "base/utils/utils.h" 25 #include "core/common/container.h" 26 #include "core/components/common/properties/color.h" 27 #include "core/components/progress/progress_theme.h" 28 #include "core/components_ng/pattern/progress/progress_date.h" 29 #include "core/components_ng/pattern/progress/progress_modifier.h" 30 #include "core/components_ng/pattern/progress/progress_paint_property.h" 31 #include "core/components_ng/render/drawing.h" 32 #include "core/components_ng/render/drawing_prop_convertor.h" 33 #include "core/components_ng/render/node_paint_method.h" 34 35 namespace OHOS::Ace::NG { 36 constexpr float DEFAULT_BORDER_WIDTH = 1.0f; 37 38 class ACE_EXPORT ProgressPaintMethod : public NodePaintMethod { 39 DECLARE_ACE_TYPE(ProgressPaintMethod, NodePaintMethod); 40 public: ProgressPaintMethod(ProgressType progressType,float strokeWidth,const RefPtr<ProgressModifier> & progressModifier,bool isUserInitiatedColor)41 explicit ProgressPaintMethod(ProgressType progressType, float strokeWidth, 42 const RefPtr<ProgressModifier>& progressModifier, bool isUserInitiatedColor) 43 : strokeWidth_(strokeWidth), progressType_(progressType), progressModifier_(progressModifier), 44 isUserInitiatedColor_(isUserInitiatedColor) 45 { 46 progressModifier_->SetProgressType(progressType_); 47 } 48 ~ProgressPaintMethod() override = default; 49 GetContentModifier(PaintWrapper * paintWrapper)50 RefPtr<Modifier> GetContentModifier(PaintWrapper* paintWrapper) override 51 { 52 CHECK_NULL_RETURN(progressModifier_, nullptr); 53 return progressModifier_; 54 } 55 UpdateContentModifier(PaintWrapper * paintWrapper)56 void UpdateContentModifier(PaintWrapper* paintWrapper) override 57 { 58 CHECK_NULL_VOID(progressModifier_); 59 GetThemeData(GetThemeScopeId(paintWrapper)); 60 auto paintProperty = DynamicCast<ProgressPaintProperty>(paintWrapper->GetPaintProperty()); 61 CHECK_NULL_VOID(paintProperty); 62 auto isSensitive = paintProperty->GetIsSensitive().value_or(false); 63 if (progressType_ != ProgressType::SCALE) { 64 color_ = isUserInitiatedColor_ ? paintProperty->GetColor().value_or(color_) : color_; 65 } else { 66 color_ = paintProperty->GetColor().value_or(color_); 67 } 68 bgColor_ = paintProperty->GetBackgroundColor().value_or(bgColor_); 69 borderColor_ = paintProperty->GetBorderColor().value_or(borderColor_); 70 maxValue_ = paintProperty->GetMaxValue().value_or(maxValue_); 71 value_ = paintProperty->GetValue().value_or(value_); 72 scaleCount_ = paintProperty->GetScaleCount().value_or(scaleCount_); 73 scaleWidth_ = paintProperty->GetScaleWidth().value_or(Dimension(scaleWidth_)).ConvertToPx(); 74 capsuleBorderWidth_ = paintProperty->GetBorderWidth().value_or(capsuleBorderWidth_); 75 sweepEffect_ = paintProperty->GetEnableScanEffect().value_or(false); 76 ringSweepEffect_ = paintProperty->GetEnableRingScanEffect().value_or(false); 77 linearSweepEffect_ = paintProperty->GetEnableLinearScanEffect().value_or(false); 78 bool paintShadow = paintProperty->GetPaintShadow().value_or(false); 79 ProgressStatus progressStatus = paintProperty->GetProgressStatus().value_or(ProgressStatus::PROGRESSING); 80 progressModifier_->SetSmoothEffect(paintProperty->GetEnableSmoothEffect().value_or(true)); 81 progressModifier_->SetContentOffset(paintWrapper->GetContentOffset()); 82 progressModifier_->SetContentSize(paintWrapper->GetContentSize()); 83 CalculateStrokeWidth(paintWrapper->GetContentSize()); 84 progressModifier_->SetSweepEffect(sweepEffect_); 85 progressModifier_->SetRingSweepEffect(ringSweepEffect_); 86 progressModifier_->SetLinearSweepEffect(linearSweepEffect_); 87 progressModifier_->SetStrokeWidth(strokeWidth_); 88 progressModifier_->SetBorderWidth(capsuleBorderWidth_.ConvertToPx()); 89 progressModifier_->SetColor(LinearColor(color_)); 90 progressModifier_->SetBackgroundColor(LinearColor(bgColor_)); 91 progressModifier_->SetBorderColor(LinearColor(borderColor_)); 92 progressModifier_->SetProgressType(progressType_); 93 progressModifier_->SetProgressStatus(progressStatus); 94 progressModifier_->SetScaleWidth(scaleWidth_); 95 progressModifier_->SetScaleCount(scaleCount_); 96 auto ringProgressColor = GenerateRingProgressColor(paintWrapper); 97 progressModifier_->SetRingProgressColor(ringProgressColor); 98 progressModifier_->SetPaintShadow(paintShadow); 99 isItalic_ = paintProperty->GetItalicFontStyle() != Ace::FontStyle::NORMAL; 100 progressModifier_->SetIsItalic(isItalic_); 101 progressModifier_->SetMaxValue(maxValue_); 102 progressModifier_->SetValue(isSensitive ? 0.0 : value_); 103 auto strokeRadius = static_cast<float>( 104 paintProperty->GetStrokeRadiusValue(Dimension(strokeWidth_ / 2.0f, DimensionUnit::VP)).ConvertToPx()); 105 strokeRadius = std::min(strokeWidth_ / 2, strokeRadius); 106 progressModifier_->SetStrokeRadius(strokeRadius); 107 if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_EIGHTEEN)) { 108 progressModifier_->UpdateProgress(); 109 } 110 SetCapsuleBorderRadius(paintWrapper); 111 UpdateCapsuleProgress(paintWrapper); 112 } 113 114 void GetThemeData(int32_t themeScopeId); 115 void CalculateStrokeWidth(const SizeF& contentSize); 116 117 private: 118 Gradient GenerateRingProgressColor(PaintWrapper* paintWrapper); 119 void SetCapsuleBorderRadius(PaintWrapper* paintWrapper); 120 void UpdateCapsuleProgress(PaintWrapper* paintWrapper); 121 122 Color color_ = Color::BLUE; 123 Color bgColor_ = Color::GRAY; 124 Color borderColor_ = Color::GRAY; 125 float strokeWidth_ = 0.0f; 126 float scaleWidth_ = 0.0f; 127 int32_t scaleCount_ = 0; 128 float maxValue_ = 100.0f; 129 float value_ = 0.0f; 130 Dimension capsuleBorderWidth_ = Dimension(DEFAULT_BORDER_WIDTH, DimensionUnit::VP); 131 Color ringProgressEndSideColor_ = Color::BLUE; 132 Color ringProgressBeginSideColor_ = Color::BLUE; 133 134 Color capsuleBgFocusedColor_; 135 Color capsuleSelectFocusedColor_; 136 Color capsuleInprogressBgColor_; 137 Color capsuleInprogressBorderColor_; 138 Dimension capsuleInprogressBorderWidth_; 139 Color defaultBorderColor_; 140 Dimension defaultBorderWidth_; 141 142 ProgressType progressType_ = ProgressType::LINEAR; 143 RefPtr<ProgressModifier> progressModifier_; 144 bool sweepEffect_ = false; 145 bool ringSweepEffect_ = false; 146 bool linearSweepEffect_ = false; 147 bool isItalic_ = false; 148 bool isUserInitiatedColor_ = false; 149 150 ACE_DISALLOW_COPY_AND_MOVE(ProgressPaintMethod); 151 152 int32_t GetThemeScopeId(PaintWrapper* paintWrapper) const; 153 }; 154 155 } // namespace OHOS::Ace::NG 156 157 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_PROGRESS_PROGRESS_PAINT_METHOD_H 158