1 /*
2  * Copyright (c) 2022-2023 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_GAUGE_GAUGE_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_GAUGE_GAUGE_PATTERN_H
18 
19 #include "core/common/container.h"
20 #include "core/components_ng/image_provider/image_loading_context.h"
21 #include "core/components_ng/pattern/gauge/gauge_accessibility_property.h"
22 #include "core/components_ng/pattern/gauge/gauge_layout_algorithm.h"
23 #include "core/components_ng/pattern/gauge/gauge_layout_property.h"
24 #include "core/components_ng/pattern/gauge/gauge_modifier.h"
25 #include "core/components_ng/pattern/gauge/gauge_model_ng.h"
26 #include "core/components_ng/pattern/gauge/gauge_paint_method.h"
27 #include "core/components_ng/pattern/gauge/gauge_paint_property.h"
28 #include "core/components_ng/pattern/pattern.h"
29 
30 namespace OHOS::Ace::NG {
31 class GaugePattern : public Pattern {
32     DECLARE_ACE_TYPE(GaugePattern, Pattern);
33 
34 public:
35     GaugePattern() = default;
36     ~GaugePattern() override = default;
37 
IsAtomicNode()38     bool IsAtomicNode() const override
39     {
40         return Container::LessThanAPIVersion(PlatformVersion::VERSION_ELEVEN) ? true : false;
41     }
42 
CreateNodePaintMethod()43     RefPtr<NodePaintMethod> CreateNodePaintMethod() override
44     {
45         if (!gaugeModifier_) {
46             gaugeModifier_ = AceType::MakeRefPtr<GaugeModifier>(WeakClaim(this));
47         }
48         gaugeModifier_->SetUseContentModifier(UseContentModifier());
49         return MakeRefPtr<GaugePaintMethod>(WeakClaim(this), gaugeModifier_);
50     }
51 
GetContentModifier(PaintWrapper * paintWrapper)52     RefPtr<GaugeModifier> GetContentModifier(PaintWrapper* paintWrapper)
53     {
54         if (!gaugeModifier_) {
55             gaugeModifier_ = AceType::MakeRefPtr<GaugeModifier>(WeakClaim(this));
56         }
57         return gaugeModifier_;
58     }
59 
CreatePaintProperty()60     RefPtr<PaintProperty> CreatePaintProperty() override
61     {
62         return MakeRefPtr<GaugePaintProperty>();
63     }
64 
CreateLayoutProperty()65     RefPtr<LayoutProperty> CreateLayoutProperty() override
66     {
67         return MakeRefPtr<GaugeLayoutProperty>();
68     }
69 
CreateLayoutAlgorithm()70     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
71     {
72         return MakeRefPtr<GaugeLayoutAlgorithm>(indicatorIconLoadingCtx_);
73     }
74 
CreateAccessibilityProperty()75     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
76     {
77         return MakeRefPtr<GaugeAccessibilityProperty>();
78     }
79 
GetFocusPattern()80     FocusPattern GetFocusPattern() const override
81     {
82         return { FocusType::NODE, false, FocusStyleType::OUTER_BORDER };
83     }
84 
HasDescriptionNode()85     bool HasDescriptionNode() const
86     {
87         return descriptionNodeId_.has_value();
88     }
89 
HasMinValueTextNode()90     bool HasMinValueTextNode() const
91     {
92         return minValueTextId_.has_value();
93     }
94 
HasMaxValueTextNode()95     bool HasMaxValueTextNode() const
96     {
97         return maxValueTextId_.has_value();
98     }
99 
HasTitleChildNode()100     bool HasTitleChildNode() const
101     {
102         return titleChildId_.has_value();
103     }
104 
GetDescriptionNodeId()105     int32_t GetDescriptionNodeId()
106     {
107         if (!descriptionNodeId_.has_value()) {
108             descriptionNodeId_ = ElementRegister::GetInstance()->MakeUniqueId();
109         }
110         return descriptionNodeId_.value();
111     }
112 
GetMinValueTextId()113     int32_t GetMinValueTextId()
114     {
115         if (!minValueTextId_.has_value()) {
116             minValueTextId_ = ElementRegister::GetInstance()->MakeUniqueId();
117         }
118         return minValueTextId_.value();
119     }
120 
GetMaxValueTextId()121     int32_t GetMaxValueTextId()
122     {
123         if (!maxValueTextId_.has_value()) {
124             maxValueTextId_ = ElementRegister::GetInstance()->MakeUniqueId();
125         }
126         return maxValueTextId_.value();
127     }
128 
GetTitleChildId()129     int32_t GetTitleChildId()
130     {
131         if (!titleChildId_.has_value()) {
132             titleChildId_ = ElementRegister::GetInstance()->MakeUniqueId();
133         }
134         return titleChildId_.value();
135     }
136 
SetDescriptionNode(const RefPtr<UINode> & descriptionNode)137     void SetDescriptionNode(const RefPtr<UINode>& descriptionNode)
138     {
139         descriptionNode_ = descriptionNode;
140     }
141 
GetIndicatorIconCanvasImage()142     const RefPtr<CanvasImage>& GetIndicatorIconCanvasImage() const
143     {
144         return indicatorIconCanvasImage_;
145     }
146 
147     void OnModifyDone() override;
148     void OnSensitiveStyleChange(bool isSensitive) override;
149 
SetBuilderFunc(GaugeMakeCallback && makeFunc)150     void SetBuilderFunc(GaugeMakeCallback&& makeFunc)
151     {
152         if (makeFunc == nullptr) {
153             makeFunc_ = std::nullopt;
154             OnModifyDone();
155             return;
156         }
157         makeFunc_ = std::move(makeFunc);
158     }
159 
GetContentModifierNode()160     const RefPtr<FrameNode>& GetContentModifierNode() const
161     {
162         return contentModifierNode_;
163     }
164 
UseContentModifier()165     bool UseContentModifier() const
166     {
167         return contentModifierNode_ != nullptr;
168     }
169 
170 
171 private:
172     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, bool skipMeasure, bool skipLayout) override;
173     void InitDescriptionNode();
174     void InitLimitValueText(int32_t valueTextId, bool isMin);
175     void HideLimitValueText(int32_t valueTextId, bool isMin);
176     void ObscureText(int32_t valueTextId, bool isSensitive);
177     void ObscureLimitValueText(bool isSensitive);
178     void InitIndicatorImage();
179     void InitTitleContent();
180     void FireBuilder();
181     RefPtr<FrameNode> BuildContentModifierNode();
182     std::optional<GaugeMakeCallback> makeFunc_;
183     RefPtr<FrameNode> contentModifierNode_;
184 
185     LoadSuccessNotifyTask CreateLoadSuccessCallback();
186     DataReadyNotifyTask CreateDataReadyCallback();
187     LoadFailNotifyTask CreateLoadFailCallback();
188     void OnImageDataReady();
189     void OnImageLoadSuccess();
190     void OnImageLoadFail();
191 
192     Color GetMaxValueColor(const RefPtr<GaugePaintProperty>& gaugePaintProperty) const;
193     std::optional<int32_t> descriptionNodeId_;
194     std::optional<int32_t> minValueTextId_;
195     std::optional<int32_t> maxValueTextId_;
196     std::optional<int32_t> titleChildId_;
197     RefPtr<UINode> descriptionNode_;
198 
199     RefPtr<ImageLoadingContext> indicatorIconLoadingCtx_;
200     RefPtr<CanvasImage> indicatorIconCanvasImage_;
201     RefPtr<GaugeModifier> gaugeModifier_;
202 
203     ACE_DISALLOW_COPY_AND_MOVE(GaugePattern);
204 };
205 
206 } // namespace OHOS::Ace::NG
207 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_GAUGE_GAUGE_PATTERN_H
208