• 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>(GetHost(), progressAnimatableProperty);
55         }
56         bool isRtl = progressLayoutProperty->GetNonAutoLayoutDirection() == TextDirection::RTL;
57         if (isRightToLeft_ != isRtl) {
58             isRightToLeft_ = isRtl;
59         }
60         progressModifier_->SetIsRightToLeft(isRightToLeft_);
61         progressModifier_->SetVisible(visibilityProp_);
62         progressModifier_->SetUseContentModifier(UseContentModifier());
63         if ((progressLayoutProperty->GetType() == ProgressType::RING ||
64                 progressLayoutProperty->GetType() == ProgressType::SCALE) &&
65                 progressLayoutProperty->GetPaddingProperty()) {
66             const auto& padding = progressLayoutProperty->GetPaddingProperty();
67             auto leftPadding = padding->left.value_or(CalcLength(0.0_vp)).GetDimension();
68             progressModifier_->SetRingProgressLeftPadding(leftPadding);
69         }
70         return MakeRefPtr<ProgressPaintMethod>(progressType_, strokeWidth_, progressModifier_, isUserInitiatedColor_);
71     }
72 
CreateLayoutProperty()73     RefPtr<LayoutProperty> CreateLayoutProperty() override
74     {
75         return MakeRefPtr<ProgressLayoutProperty>();
76     }
77 
CreateLayoutAlgorithm()78     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
79     {
80         return MakeRefPtr<ProgressLayoutAlgorithm>();
81     }
82 
CreatePaintProperty()83     RefPtr<PaintProperty> CreatePaintProperty() override
84     {
85         return MakeRefPtr<ProgressPaintProperty>();
86     }
87 
CreateAccessibilityProperty()88     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
89     {
90         return MakeRefPtr<ProgressAccessibilityProperty>();
91     }
92 
93     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override;
94 
GetFocusPattern()95     FocusPattern GetFocusPattern() const override
96     {
97         return { FocusType::NODE, true, FocusStyleType::CUSTOM_REGION };
98     }
99 
SetTextFromUser(bool value)100     void SetTextFromUser(bool value)
101     {
102         isTextFromUser_ = value;
103     }
104 
IsTextFromUser()105     bool IsTextFromUser()
106     {
107         return isTextFromUser_;
108     }
109 
110     void OnVisibleChange(bool isVisible) override;
111 
SetBuilderFunc(ProgressMakeCallback && makeFunc)112     void SetBuilderFunc(ProgressMakeCallback&& makeFunc)
113     {
114         if (!makeFunc) {
115             makeFunc_ = std::nullopt;
116             OnModifyDone();
117             return;
118         }
119         makeFunc_ = std::move(makeFunc);
120     }
121 
UseContentModifier()122     bool UseContentModifier() const
123     {
124         return contentModifierNode_ != nullptr;
125     }
126 
GetContentModifierNode()127     RefPtr<FrameNode> GetContentModifierNode()
128     {
129         return contentModifierNode_;
130     }
131 
convertGradient(Color color)132     Gradient convertGradient(Color color)
133     {
134         Gradient gradient;
135         GradientColor gradientColorEnd;
136         GradientColor gradientColorStart;
137         gradientColorEnd.SetLinearColor(LinearColor(color));
138         gradientColorStart.SetLinearColor(LinearColor(color));
139         gradientColorEnd.SetDimension(Dimension(0.0));
140         gradient.AddColor(gradientColorEnd);
141         gradientColorStart.SetDimension(Dimension(1.0));
142         gradient.AddColor(gradientColorStart);
143         return gradient;
144     }
145 
146     void OnAccessibilityEvent();
147 
SetUserInitiatedColor(bool value)148     void SetUserInitiatedColor(bool value)
149     {
150         isUserInitiatedColor_ = value;
151     }
152 
SetUserInitiatedBgColor(bool value)153     void SetUserInitiatedBgColor(bool value)
154     {
155         isUserInitiatedBgColor_ = value;
156     }
157 
IsModifierInitiatedColor(bool value)158     void IsModifierInitiatedColor(bool value)
159     {
160         isModifierInitiatedColor_ = value;
161     }
162 
IsModifierInitiatedBgColor(bool value)163     void IsModifierInitiatedBgColor(bool value)
164     {
165         isModifierInitiatedBgColor_ = value;
166     }
167 
168     bool OnThemeScopeUpdate(int32_t themeScopeId) override;
169 
170 private:
171     void InitAnimatableProperty(ProgressAnimatableProperty& progressAnimatableProperty);
172     void InitColorProperty(ProgressAnimatableProperty& progressAnimatableProperty,
173         const RefPtr<ProgressTheme>& progressTheme, const RefPtr<ProgressPaintProperty>& paintProperty);
174     void CalculateStrokeWidth(const SizeF& contentSize);
175     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
176     void OnAttachToFrameNode() override;
177     void OnModifyDone() override;
178     void DumpInfo() override;
179     void DumpInfo(std::unique_ptr<JsonValue>& json) override;
DumpSimplifyInfo(std::unique_ptr<JsonValue> & json)180     void DumpSimplifyInfo(std::unique_ptr<JsonValue>& json) override {}
181     void OnLanguageConfigurationUpdate() override;
182     void InitTouchEvent();
183     void RemoveTouchEvent();
184     void OnPress(const TouchEventInfo& info);
185     void InitFocusEvent();
186     void HandleFocusEvent();
187     void HandleBlurEvent();
188     void SetFocusStyle();
189     void ClearFocusStyle();
190     void AddIsFocusActiveUpdateEvent();
191     void RemoveIsFocusActiveUpdateEvent();
192     void InitHoverEvent();
193     void RemoveHoverEvent();
194     void OnHover(bool isHover);
195     void SetTextColor(const Color& color);
196     void HandleEnabled();
197     void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub);
198     void GetInnerFocusPaintRect(RoundRect& paintRect);
199     void ToJsonValueForRingStyleOptions(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const;
200     void ToJsonValueForLinearStyleOptions(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const;
201     void ToJsonValueForCapsuleStyleOptions(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const;
202     float GetBorderRadiusValues() const;
203     static std::string ConvertProgressStatusToString(const ProgressStatus status);
204     void OnSensitiveStyleChange(bool isSensitive) override;
205     void ObscureText(bool isSensitive);
206     void FireBuilder();
207     RefPtr<FrameNode> BuildContentModifierNode();
208     std::optional<ProgressMakeCallback> makeFunc_;
209     RefPtr<FrameNode> contentModifierNode_;
210 
211     float strokeWidth_ = Dimension(4.0_vp).ConvertToPx();
212     RefPtr<ProgressModifier> progressModifier_;
213     RefPtr<TouchEventImpl> touchListener_;
214     RefPtr<InputEvent> hoverEvent_;
215     std::function<void(bool)> isFocusActiveUpdateEvent_;
216     float capsuleFocusScale_ = 1.0f;
217     ShadowStyle focusShadowStyle_ = ShadowStyle::None;
218     bool isFocusScaleSet_ = false;
219     bool isFocusTextColorSet_ = false;
220     bool isFocusShadowSet_ = false;
221     Color defaultTextColor_;
222     Color focusedTextColor_;
223     std::optional<Color> backgroundColorOptional_;
224     std::optional<Color> selectColorOptional_;
225     std::optional<Color> borderColorOptional_;
226     Color fontColor_;
227     double value_ = 0.0;
228     bool initFlag_ = false;
229     ProgressType progressType_ = ProgressType::LINEAR;
230     bool isTextFromUser_ = false;
231     bool visibilityProp_ = true;
232     bool isRightToLeft_ = false;
233     bool isUserInitiatedColor_ = false;
234     bool isUserInitiatedBgColor_ = false;
235     bool isModifierInitiatedColor_ = false;
236     bool isModifierInitiatedBgColor_ = false;
237     ACE_DISALLOW_COPY_AND_MOVE(ProgressPattern);
238 };
239 } // namespace OHOS::Ace::NG
240 
241 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_PROGRESS_PROGRESS_PATTERN_H
242