• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_SHAPE_RECT_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SHAPE_RECT_PATTERN_H
18 
19 #include "base/memory/referenced.h"
20 #include "base/utils/noncopyable.h"
21 #include "core/components_ng/pattern/pattern.h"
22 #include "core/components_ng/pattern/shape/rect_paint_method.h"
23 #include "core/components_ng/pattern/shape/rect_paint_property.h"
24 #include "core/components_ng/pattern/shape/shape_pattern.h"
25 
26 namespace OHOS::Ace::NG {
27 
28 class RectPattern : public ShapePattern {
29     DECLARE_ACE_TYPE(RectPattern, ShapePattern);
30 
31 public:
32     RectPattern() = default;
33     ~RectPattern() override = default;
CreateNodePaintMethod()34     RefPtr<NodePaintMethod> CreateNodePaintMethod() override
35     {
36         if (!shapeOverlayModifier_) {
37             shapeOverlayModifier_ = MakeRefPtr<ShapeOverlayModifier>();
38         }
39         return MakeRefPtr<RectPaintMethod>(GetAncestorPaintProperty(), shapeOverlayModifier_);
40     }
CreatePaintProperty()41     RefPtr<PaintProperty> CreatePaintProperty() override
42     {
43         return MakeRefPtr<RectPaintProperty>();
44     }
45 
OnPixelRoundFinish(const SizeF & pixelGridRoundSize)46     void OnPixelRoundFinish(const SizeF& pixelGridRoundSize) override
47     {
48         auto host = GetHost();
49         CHECK_NULL_VOID(host);
50         auto geometryNode = host->GetGeometryNode();
51         CHECK_NULL_VOID(geometryNode);
52         auto pixelRoundContentOffset = geometryNode->GetPixelRoundContentOffset();
53         auto pixelRoundContentSize = geometryNode->GetPixelRoundContentSize();
54         auto rectPaintProperty = host->GetPaintProperty<RectPaintProperty>();
55         CHECK_NULL_VOID(rectPaintProperty);
56         rectPaintProperty->SetContentOffset(pixelRoundContentOffset);
57         rectPaintProperty->SetContentSize(pixelRoundContentSize);
58     }
59 
UpdatePropertyImpl(const std::string & key,RefPtr<PropertyValueBase> value)60     void UpdatePropertyImpl(const std::string& key, RefPtr<PropertyValueBase> value) override
61     {
62         ShapePattern::UpdatePropertyImpl(key, value);
63         auto frameNode = GetHost();
64         CHECK_NULL_VOID(frameNode);
65         auto property = frameNode->GetPaintPropertyPtr<RectPaintProperty>();
66         CHECK_NULL_VOID(property);
67         using Handler = std::function<void(RectPaintProperty*, RefPtr<PropertyValueBase>, RefPtr<FrameNode>)>;
68         static const std::unordered_map<std::string, Handler> handlers = {
69             { "RectRadiusWidth",
70                 [](RectPaintProperty* prop, RefPtr<PropertyValueBase> value, RefPtr<FrameNode> frameNode) {
71                     if (auto realValue = std::get_if<CalcDimension>(&(value->GetValue()))) {
72                         Radius radius;
73                         realValue->IsNegative() ? radius.SetX(Dimension(DEFAULT_RADIUS_VALUE))
74                                                    : radius.SetX(*realValue);
75                         radius.SetY(DEFAULT_RADIUS_INVALID);
76                         prop->UpdateRadius(radius);
77                     }
78                 } },
79             { "RectRadiusHeight",
80                 [](RectPaintProperty* prop, RefPtr<PropertyValueBase> value, RefPtr<FrameNode> frameNode) {
81                     if (auto realValue = std::get_if<CalcDimension>(&(value->GetValue()))) {
82                         Radius radius;
83                         realValue->IsNegative() ? radius.SetY(Dimension(DEFAULT_RADIUS_VALUE))
84                                                    : radius.SetY(*realValue);
85                         radius.SetX(DEFAULT_RADIUS_INVALID);
86                         prop->UpdateRadius(radius);
87                     }
88                 } },
89             { "RectRadius",
90                 [](RectPaintProperty* prop, RefPtr<PropertyValueBase> value, RefPtr<FrameNode> frameNode) {
91                     if (auto realValue = std::get_if<CalcDimension>(&(value->GetValue()))) {
92                         auto radiusVal = *realValue;
93                         Radius radius;
94                         if (radiusVal.IsNegative()) {
95                             radius.SetY(Dimension(DEFAULT_RADIUS_VALUE));
96                             radius.SetX(Dimension(DEFAULT_RADIUS_VALUE));
97                         } else {
98                             radius.SetX(radiusVal);
99                             radius.SetY(radiusVal);
100                         }
101                         prop->UpdateRadius(radius);
102                     }
103                 } },
104         };
105         auto it = handlers.find(key);
106         if (it != handlers.end()) {
107             it->second(property, value, frameNode);
108         }
109         if (frameNode->GetRerenderable()) {
110             frameNode->MarkDirtyNode(PROPERTY_UPDATE_RENDER);
111         }
112     }
113 
114 private:
115     ACE_DISALLOW_COPY_AND_MOVE(RectPattern);
116 };
117 
118 } // namespace OHOS::Ace::NG
119 
120 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SHAPE_RECT_PATTERN_H