1 /*
2  * Copyright (c) 2022-2024 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_ng/base/frame_node.h"
25 #include "core/components_ng/base/geometry_node.h"
26 #include "core/components_ng/pattern/pattern.h"
27 #include "core/components_ng/pattern/progress/progress_accessibility_property.h"
28 #include "core/components_ng/pattern/progress/progress_layout_algorithm.h"
29 #include "core/components_ng/pattern/progress/progress_layout_property.h"
30 #include "core/components_ng/pattern/progress/progress_modifier.h"
31 #include "core/components_ng/pattern/progress/progress_paint_method.h"
32 #include "core/components_ng/pattern/progress/progress_paint_property.h"
33 
34 namespace OHOS::Ace::NG {
35 class InspectorFilter;
36 using ProgressMakeCallback = std::function<RefPtr<FrameNode>(const ProgressConfiguration& config)>;
37 // ProgressPattern is the base class for progress render node to perform paint progress.
38 class ProgressPattern : public Pattern {
39     DECLARE_ACE_TYPE(ProgressPattern, Pattern);
40 
41 public:
42     ProgressPattern() = default;
43     ~ProgressPattern() override = default;
44 
CreateNodePaintMethod()45     RefPtr<NodePaintMethod> CreateNodePaintMethod() override
46     {
47         auto progressLayoutProperty = GetLayoutProperty<ProgressLayoutProperty>();
48         CHECK_NULL_RETURN(progressLayoutProperty, nullptr);
49         progressType_ = progressLayoutProperty->GetType().value_or(ProgressType::LINEAR);
50         if (!progressModifier_) {
51             ProgressAnimatableProperty progressAnimatableProperty{};
52             InitAnimatableProperty(progressAnimatableProperty);
53             progressModifier_ = AceType::MakeRefPtr<ProgressModifier>(progressAnimatableProperty);
54         }
55         bool isRtl = progressLayoutProperty->GetNonAutoLayoutDirection() == TextDirection::RTL;
56         if (isRightToLeft_ != isRtl) {
57             isRightToLeft_ = isRtl;
58         }
59         progressModifier_->SetIsRightToLeft(isRightToLeft_);
60         progressModifier_->SetVisible(visibilityProp_);
61         progressModifier_->SetUseContentModifier(UseContentModifier());
62         return MakeRefPtr<ProgressPaintMethod>(progressType_, strokeWidth_, progressModifier_);
63     }
64 
CreateLayoutProperty()65     RefPtr<LayoutProperty> CreateLayoutProperty() override
66     {
67         return MakeRefPtr<ProgressLayoutProperty>();
68     }
69 
CreateLayoutAlgorithm()70     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
71     {
72         return MakeRefPtr<ProgressLayoutAlgorithm>();
73     }
74 
CreatePaintProperty()75     RefPtr<PaintProperty> CreatePaintProperty() override
76     {
77         return MakeRefPtr<ProgressPaintProperty>();
78     }
79 
CreateAccessibilityProperty()80     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
81     {
82         return MakeRefPtr<ProgressAccessibilityProperty>();
83     }
84 
85     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override;
86 
GetFocusPattern()87     FocusPattern GetFocusPattern() const override
88     {
89         return { FocusType::NODE, true, FocusStyleType::CUSTOM_REGION };
90     }
91 
SetTextFromUser(bool value)92     void SetTextFromUser(bool value)
93     {
94         isTextFromUser_ = value;
95     }
96 
IsTextFromUser()97     bool IsTextFromUser()
98     {
99         return isTextFromUser_;
100     }
101 
102     void OnVisibleChange(bool isVisible) override;
103 
SetBuilderFunc(ProgressMakeCallback && makeFunc)104     void SetBuilderFunc(ProgressMakeCallback&& makeFunc)
105     {
106         if (!makeFunc) {
107             makeFunc_ = std::nullopt;
108             OnModifyDone();
109             return;
110         }
111         makeFunc_ = std::move(makeFunc);
112     }
113 
UseContentModifier()114     bool UseContentModifier() const
115     {
116         return contentModifierNode_ != nullptr;
117     }
118 
GetContentModifierNode()119     RefPtr<FrameNode> GetContentModifierNode()
120     {
121         return contentModifierNode_;
122     }
123 
convertGradient(Color color)124     Gradient convertGradient(Color color)
125     {
126         Gradient gradient;
127         GradientColor gradientColorEnd;
128         GradientColor gradientColorStart;
129         gradientColorEnd.SetLinearColor(LinearColor(color));
130         gradientColorStart.SetLinearColor(LinearColor(color));
131         gradientColorEnd.SetDimension(Dimension(0.0));
132         gradient.AddColor(gradientColorEnd);
133         gradientColorStart.SetDimension(Dimension(1.0));
134         gradient.AddColor(gradientColorStart);
135         return gradient;
136     }
137 
138 private:
139     void InitAnimatableProperty(ProgressAnimatableProperty& progressAnimatableProperty);
140     void CalculateStrokeWidth(const SizeF& contentSize);
141     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
142     void OnAttachToFrameNode() override;
143     void OnModifyDone() override;
144     void DumpInfo() override;
145     void OnLanguageConfigurationUpdate() override;
146     void InitTouchEvent();
147     void RemoveTouchEvent();
148     void OnPress(const TouchEventInfo& info);
149     void HandleEnabled();
150     void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub);
151     void GetInnerFocusPaintRect(RoundRect& paintRect);
152     void ToJsonValueForRingStyleOptions(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const;
153     void ToJsonValueForLinearStyleOptions(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const;
154     static std::string ConvertProgressStatusToString(const ProgressStatus status);
155     void OnSensitiveStyleChange(bool isSensitive) override;
156     void ObscureText(bool isSensitive);
157     void FireBuilder();
158     RefPtr<FrameNode> BuildContentModifierNode();
159     std::optional<ProgressMakeCallback> makeFunc_;
160     RefPtr<FrameNode> contentModifierNode_;
161 
162     float strokeWidth_ = Dimension(4.0_vp).ConvertToPx();
163     RefPtr<ProgressModifier> progressModifier_;
164     RefPtr<TouchEventImpl> touchListener_;
165     Color backgroundColor_;
166     Color selectColor_;
167     Color borderColor_;
168     Color fontColor_;
169     ProgressType progressType_ = ProgressType::LINEAR;
170     bool isTextFromUser_ = false;
171     bool visibilityProp_ = true;
172     bool isRightToLeft_ = false;
173     ACE_DISALLOW_COPY_AND_MOVE(ProgressPattern);
174 };
175 } // namespace OHOS::Ace::NG
176 
177 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_PROGRESS_PROGRESS_PATTERN_H
178