• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PATTERNS_PROGRESS_PROGRESS_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_PROGRESS_PROGRESS_PATTERN_H
18 
19 #include <optional>
20 #include <string>
21 
22 #include "base/geometry/dimension.h"
23 #include "core/components/common/properties/color.h"
24 #include "core/components/common/properties/shadow.h"
25 #include "core/components_ng/base/frame_node.h"
26 #include "core/components_ng/base/geometry_node.h"
27 #include "core/components_ng/pattern/pattern.h"
28 #include "core/components_ng/pattern/progress/progress_accessibility_property.h"
29 #include "core/components_ng/pattern/progress/progress_layout_algorithm.h"
30 #include "core/components_ng/pattern/progress/progress_layout_property.h"
31 #include "core/components_ng/pattern/progress/progress_modifier.h"
32 #include "core/components_ng/pattern/progress/progress_paint_method.h"
33 #include "core/components_ng/pattern/progress/progress_paint_property.h"
34 
35 namespace OHOS::Ace::NG {
36 class InspectorFilter;
37 using ProgressMakeCallback = std::function<RefPtr<FrameNode>(const ProgressConfiguration& config)>;
38 // ProgressPattern is the base class for progress render node to perform paint progress.
39 class ProgressPattern : public Pattern {
40     DECLARE_ACE_TYPE(ProgressPattern, Pattern);
41 
42 public:
43     ProgressPattern() = default;
44     ~ProgressPattern() override = default;
45 
CreateNodePaintMethod()46     RefPtr<NodePaintMethod> CreateNodePaintMethod() override
47     {
48         auto progressLayoutProperty = GetLayoutProperty<ProgressLayoutProperty>();
49         CHECK_NULL_RETURN(progressLayoutProperty, nullptr);
50         progressType_ = progressLayoutProperty->GetType().value_or(ProgressType::LINEAR);
51         if (!progressModifier_) {
52             ProgressAnimatableProperty progressAnimatableProperty {};
53             InitAnimatableProperty(progressAnimatableProperty);
54             progressModifier_ = AceType::MakeRefPtr<ProgressModifier>(
55                 GetHost(), progressAnimatableProperty, WeakClaim(this));
56         }
57         bool isRtl = progressLayoutProperty->GetNonAutoLayoutDirection() == TextDirection::RTL;
58         if (isRightToLeft_ != isRtl) {
59             isRightToLeft_ = isRtl;
60         }
61         progressModifier_->SetIsRightToLeft(isRightToLeft_);
62         progressModifier_->SetVisible(visibilityProp_);
63         progressModifier_->SetUseContentModifier(UseContentModifier());
64         if ((progressLayoutProperty->GetType() == ProgressType::RING ||
65                 progressLayoutProperty->GetType() == ProgressType::SCALE) &&
66             progressLayoutProperty->GetPaddingProperty()) {
67             const auto& padding = progressLayoutProperty->GetPaddingProperty();
68             auto leftPadding = padding->left.value_or(CalcLength(0.0_vp)).GetDimension();
69             progressModifier_->SetRingProgressLeftPadding(leftPadding);
70         }
71         return MakeRefPtr<ProgressPaintMethod>(progressType_, strokeWidth_, progressModifier_, isUserInitiatedColor_);
72     }
73 
CreateLayoutProperty()74     RefPtr<LayoutProperty> CreateLayoutProperty() override
75     {
76         return MakeRefPtr<ProgressLayoutProperty>();
77     }
78 
CreateLayoutAlgorithm()79     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
80     {
81         return MakeRefPtr<ProgressLayoutAlgorithm>();
82     }
83 
CreatePaintProperty()84     RefPtr<PaintProperty> CreatePaintProperty() override
85     {
86         return MakeRefPtr<ProgressPaintProperty>();
87     }
88 
CreateAccessibilityProperty()89     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
90     {
91         return MakeRefPtr<ProgressAccessibilityProperty>();
92     }
93 
94     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override;
95 
GetFocusPattern()96     FocusPattern GetFocusPattern() const override
97     {
98         return { FocusType::NODE, true, FocusStyleType::CUSTOM_REGION };
99     }
100 
SetTextFromUser(bool value)101     void SetTextFromUser(bool value)
102     {
103         isTextFromUser_ = value;
104     }
105 
IsTextFromUser()106     bool IsTextFromUser()
107     {
108         return isTextFromUser_;
109     }
110 
111     void OnVisibleChange(bool isVisible) override;
112 
SetBuilderFunc(ProgressMakeCallback && makeFunc)113     void SetBuilderFunc(ProgressMakeCallback&& makeFunc)
114     {
115         if (!makeFunc) {
116             makeFunc_ = std::nullopt;
117             OnModifyDone();
118             return;
119         }
120         makeFunc_ = std::move(makeFunc);
121     }
122 
UseContentModifier()123     bool UseContentModifier() const
124     {
125         return contentModifierNode_ != nullptr;
126     }
127 
GetContentModifierNode()128     RefPtr<FrameNode> GetContentModifierNode()
129     {
130         return contentModifierNode_;
131     }
132 
convertGradient(Color color)133     Gradient convertGradient(Color color)
134     {
135         Gradient gradient;
136         GradientColor gradientColorEnd;
137         GradientColor gradientColorStart;
138         gradientColorEnd.SetLinearColor(LinearColor(color));
139         gradientColorStart.SetLinearColor(LinearColor(color));
140         gradientColorEnd.SetDimension(Dimension(0.0));
141         gradient.AddColor(gradientColorEnd);
142         gradientColorStart.SetDimension(Dimension(1.0));
143         gradient.AddColor(gradientColorStart);
144         return gradient;
145     }
146 
147     void OnAccessibilityEvent();
148 
SetUserInitiatedColor(bool value)149     void SetUserInitiatedColor(bool value)
150     {
151         isUserInitiatedColor_ = value;
152     }
153 
SetUserInitiatedBgColor(bool value)154     void SetUserInitiatedBgColor(bool value)
155     {
156         isUserInitiatedBgColor_ = value;
157     }
158 
IsModifierInitiatedColor(bool value)159     void IsModifierInitiatedColor(bool value)
160     {
161         isModifierInitiatedColor_ = value;
162     }
163 
IsModifierInitiatedBgColor(bool value)164     void IsModifierInitiatedBgColor(bool value)
165     {
166         isModifierInitiatedBgColor_ = value;
167     }
168 
IsEnableMatchParent()169     bool IsEnableMatchParent() override
170     {
171         return true;
172     }
173 
IsEnableFix()174     bool IsEnableFix() override
175     {
176         return true;
177     }
178 
179     bool OnThemeScopeUpdate(int32_t themeScopeId) override;
180     void UpdateGradientColor(const NG::Gradient& gradient, bool isFirstLoad);
181     void UpdateColor(const Color& color, bool isFirstLoad);
182     void OnColorModeChange(uint32_t colorMode) override;
183 
184 private:
185     void InitAnimatableProperty(ProgressAnimatableProperty& progressAnimatableProperty);
186     void InitColorProperty(ProgressAnimatableProperty& progressAnimatableProperty,
187         const RefPtr<ProgressTheme>& progressTheme, const RefPtr<ProgressPaintProperty>& paintProperty);
188     void CalculateStrokeWidth(const SizeF& contentSize);
189     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
190     void OnAttachToFrameNode() override;
191     void OnModifyDone() override;
192     void DumpInfo() override;
193     void DumpInfo(std::unique_ptr<JsonValue>& json) override;
DumpSimplifyInfo(std::shared_ptr<JsonValue> & json)194     void DumpSimplifyInfo(std::shared_ptr<JsonValue>& json) override {}
195     void OnLanguageConfigurationUpdate() override;
196     void InitTouchEvent();
197     void RemoveTouchEvent();
198     void OnPress(const TouchEventInfo& info);
199     void InitFocusEvent();
200     void HandleFocusEvent();
201     void HandleBlurEvent();
202     void SetFocusStyle();
203     void ClearFocusStyle();
204     void AddIsFocusActiveUpdateEvent();
205     void RemoveIsFocusActiveUpdateEvent();
206     void InitHoverEvent();
207     void RemoveHoverEvent();
208     void OnHover(bool isHover);
209     void SetTextColor(const Color& color);
210     void HandleEnabled();
211     void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub);
212     void GetInnerFocusPaintRect(RoundRect& paintRect);
213     void ToJsonValueForRingStyleOptions(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const;
214     void ToJsonValueForLinearStyleOptions(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const;
215     void ToJsonValueForCapsuleStyleOptions(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const;
216     float GetBorderRadiusValues() const;
217     static std::string ConvertProgressStatusToString(const ProgressStatus status);
218     void OnSensitiveStyleChange(bool isSensitive) override;
219     void ObscureText(bool isSensitive);
220     void FireBuilder();
221     void ReportProgressEvent();
222     void OnColorConfigurationUpdate() override;
223     RefPtr<FrameNode> BuildContentModifierNode();
224     std::optional<ProgressMakeCallback> makeFunc_;
225     RefPtr<FrameNode> contentModifierNode_;
226 
227     float strokeWidth_ = Dimension(4.0_vp).ConvertToPx();
228     RefPtr<ProgressModifier> progressModifier_;
229     RefPtr<TouchEventImpl> touchListener_;
230     RefPtr<InputEvent> hoverEvent_;
231     std::function<void(bool)> isFocusActiveUpdateEvent_;
232     float capsuleFocusScale_ = 1.0f;
233     ShadowStyle focusShadowStyle_ = ShadowStyle::None;
234     bool isFocusScaleSet_ = false;
235     bool isFocusTextColorSet_ = false;
236     bool isFocusShadowSet_ = false;
237     Color defaultTextColor_;
238     Color focusedTextColor_;
239     std::optional<Color> backgroundColorOptional_;
240     std::optional<Color> selectColorOptional_;
241     std::optional<Color> borderColorOptional_;
242     Color fontColor_;
243     double value_ = 0.0;
244     bool initFlag_ = false;
245     ProgressType progressType_ = ProgressType::LINEAR;
246     bool isTextFromUser_ = false;
247     bool visibilityProp_ = true;
248     bool isRightToLeft_ = false;
249     bool isUserInitiatedColor_ = false;
250     bool isUserInitiatedBgColor_ = false;
251     bool isModifierInitiatedColor_ = false;
252     bool isModifierInitiatedBgColor_ = false;
253     double reportLastValue_ = 0.0f;
254     ACE_DISALLOW_COPY_AND_MOVE(ProgressPattern);
255 };
256 } // namespace OHOS::Ace::NG
257 
258 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_PROGRESS_PROGRESS_PATTERN_H
259