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_PATTERN_DIVIDER_DIVIDER_PAINT_METHOD_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_DIVIDER_DIVIDER_PAINT_METHOD_H
18 
19 #include "core/components/divider/divider_theme.h"
20 #include "core/components_ng/pattern/divider/divider_render_property.h"
21 #include "core/components_ng/pattern/divider/divider_modifier.h"
22 #include "core/components_ng/render/node_paint_method.h"
23 #include "core/pipeline/pipeline_base.h"
24 
25 namespace OHOS::Ace::NG {
26 class ACE_EXPORT DividerPaintMethod : public NodePaintMethod {
DECLARE_ACE_TYPE(DividerPaintMethod,NodePaintMethod)27     DECLARE_ACE_TYPE(DividerPaintMethod, NodePaintMethod)
28 public:
29     DividerPaintMethod(float constrainStrokeWidth, float dividerLength, bool vertical, bool strokeWidthLimitation,
30         RefPtr<DividerModifier> dividerModifier)
31         : constrainStrokeWidth_(constrainStrokeWidth), dividerLength_(dividerLength), vertical_(vertical),
32           strokeWidthLimitation_(strokeWidthLimitation), dividerModifier_(dividerModifier)
33     {}
34     ~DividerPaintMethod() override = default;
35 
GetContentModifier(PaintWrapper * paintWrapper)36     RefPtr<Modifier> GetContentModifier(PaintWrapper* paintWrapper) override
37     {
38         CHECK_NULL_RETURN(dividerModifier_, nullptr);
39         return dividerModifier_;
40     }
41 
UpdateContentModifier(PaintWrapper * paintWrapper)42     void UpdateContentModifier(PaintWrapper* paintWrapper) override
43     {
44         CHECK_NULL_VOID(dividerModifier_);
45         auto dividerRenderProperty = DynamicCast<DividerRenderProperty>(paintWrapper->GetPaintProperty());
46         CHECK_NULL_VOID(dividerRenderProperty);
47         auto pipeline = PipelineBase::GetCurrentContext();
48         CHECK_NULL_VOID(pipeline);
49         auto theme = pipeline->GetTheme<DividerTheme>();
50         CHECK_NULL_VOID(theme);
51         dividerColor_ = dividerRenderProperty->GetDividerColor().value_or(theme->GetColor());
52         lineCap_ = dividerRenderProperty->GetLineCap().value_or(LineCap::BUTT);
53         offset_ = paintWrapper->GetContentOffset();
54         if (lineCap_ == LineCap::SQUARE || lineCap_ == LineCap::ROUND) {
55             float boundsRectWidth = 0.0f;
56             float boundsRectHeight = 0.0f;
57             dividerLength_ += constrainStrokeWidth_;
58             if (vertical_) {
59                 auto offsetY = offset_.GetY();
60                 offset_.SetY(offsetY - constrainStrokeWidth_ / 2);
61                 boundsRectWidth = constrainStrokeWidth_;
62                 boundsRectHeight = dividerLength_;
63             } else {
64                 auto offsetX = offset_.GetX();
65                 offset_.SetX(offsetX - constrainStrokeWidth_ / 2);
66                 boundsRectWidth = dividerLength_;
67                 boundsRectHeight = constrainStrokeWidth_;
68             }
69             RectF boundsRect(offset_.GetX(), offset_.GetY(), boundsRectWidth, boundsRectHeight);
70             dividerModifier_->SetBoundsRect(boundsRect);
71         }
72         dividerModifier_->SetStrokeWidth(constrainStrokeWidth_);
73         dividerModifier_->SetDividerLength(dividerLength_);
74         dividerModifier_->SetVertical(vertical_);
75         dividerModifier_->SetOffset(offset_);
76         dividerModifier_->SetColor(LinearColor(dividerColor_));
77         dividerModifier_->SetLineCap(lineCap_);
78         dividerModifier_->SetStrokeWidthLimitation(strokeWidthLimitation_);
79     }
80 
81 private:
82     float constrainStrokeWidth_;
83     float dividerLength_;
84     bool vertical_ = false;
85     bool strokeWidthLimitation_ = true;
86 
87     OffsetF offset_;
88     Color dividerColor_;
89     LineCap lineCap_;
90     RefPtr<DividerModifier> dividerModifier_;
91     ACE_DISALLOW_COPY_AND_MOVE(DividerPaintMethod);
92 };
93 } // namespace OHOS::Ace::NG
94 
95 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_DIVIDER_DIVIDER_PAINT_METHOD_H
96