1 /*
2  * Copyright (c) 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 #include "core/components_ng/pattern/list/list_content_modifier.h"
17 
18 #include "base/utils/utils.h"
19 #include "core/components_ng/base/modifier.h"
20 #include "core/components_ng/render/divider_painter.h"
21 #include "core/components_ng/render/drawing.h"
22 
23 namespace OHOS::Ace::NG {
ListContentModifier(const OffsetF & clipOffset,const SizeF & clipSize)24 ListContentModifier::ListContentModifier(const OffsetF& clipOffset, const SizeF& clipSize)
25 {
26     color_ = AceType::MakeRefPtr<AnimatablePropertyColor>(LinearColor::TRANSPARENT);
27     clipOffset_ = AceType::MakeRefPtr<AnimatablePropertyOffsetF>(clipOffset);
28     clipSize_ = AceType::MakeRefPtr<AnimatablePropertySizeF>(clipSize);
29     clip_ = AceType::MakeRefPtr<PropertyBool>(true);
30     refDivider_ = AceType::MakeRefPtr<RefDividerMap>();
31     ListDividerMap dividerMap;
32     RefPtr<ListDividerArithmetic> lda = AceType::MakeRefPtr<ListDividerArithmetic>(dividerMap, refDivider_);
33     dividerList_ = AceType::MakeRefPtr<AnimatableArithmeticProperty>(
34         AceType::DynamicCast<CustomAnimatableArithmetic>(lda));
35 
36     AttachProperty(color_);
37     AttachProperty(clipOffset_);
38     AttachProperty(clipSize_);
39     AttachProperty(clip_);
40     AttachProperty(dividerList_);
41 }
42 
onDraw(DrawingContext & context)43 void ListContentModifier::onDraw(DrawingContext& context)
44 {
45     if (clip_->Get()) {
46         auto offset = clipOffset_->Get();
47         auto size = clipSize_->Get();
48         auto clipRect = RSRect(offset.GetX(), offset.GetY(),
49             offset.GetX() + size.Width(), offset.GetY() + size.Height());
50         context.canvas.ClipRect(clipRect, RSClipOp::INTERSECT);
51     }
52 
53     CHECK_NULL_VOID(dividerList_);
54     auto dividerlist = dividerList_->Get();
55     CHECK_NULL_VOID(dividerlist);
56     auto lda = AceType::DynamicCast<ListDividerArithmetic>(dividerlist);
57     CHECK_NULL_VOID(lda);
58     const auto& dividerMap = lda->GetDividerMap();
59     if (!dividerMap.empty()) {
60         DividerPainter dividerPainter(
61             width_, 0, isVertical_, color_->Get().ToColor(), LineCap::SQUARE);
62         for (const auto& child : dividerMap) {
63             if (child.second.length > 0) {
64                 dividerPainter.SetDividerLength(child.second.length);
65                 dividerPainter.DrawLine(context.canvas, child.second.offset);
66             }
67         }
68     }
69 }
70 } // namespace OHOS::Ace::NG
71