• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     RefPtr<ListDividerArithmetic> lda = AceType::MakeRefPtr<ListDividerArithmetic>();
31     dividerList_ = AceType::MakeRefPtr<AnimatableArithmeticProperty>(
32         AceType::DynamicCast<CustomAnimatableArithmetic>(lda));
33 
34     AttachProperty(color_);
35     AttachProperty(clipOffset_);
36     AttachProperty(clipSize_);
37     AttachProperty(clip_);
38     AttachProperty(dividerList_);
39 }
40 
onDraw(DrawingContext & context)41 void ListContentModifier::onDraw(DrawingContext& context)
42 {
43     if (clip_->Get()) {
44         auto offset = clipOffset_->Get();
45         auto size = clipSize_->Get();
46         auto clipRect = RSRect(offset.GetX(), offset.GetY(),
47             offset.GetX() + size.Width(), offset.GetY() + size.Height());
48         context.canvas.ClipRect(clipRect, RSClipOp::INTERSECT);
49     }
50 
51     CHECK_NULL_VOID(dividerList_);
52     auto dividerlist = dividerList_->Get();
53     CHECK_NULL_VOID(dividerlist);
54     auto lda = AceType::DynamicCast<ListDividerArithmetic>(dividerlist);
55     CHECK_NULL_VOID(lda);
56     auto dividerMap = lda->GetDividerMap();
57     if (!dividerMap.empty()) {
58         DividerPainter dividerPainter(
59             width_, 0, isVertical_, color_->Get().ToColor(), LineCap::SQUARE);
60         for (auto child : dividerMap) {
61             if (child.second.length > 0) {
62                 dividerPainter.SetDividerLength(child.second.length);
63                 dividerPainter.DrawLine(context.canvas, child.second.offset);
64             }
65         }
66     }
67 }
68 } // namespace OHOS::Ace::NG
69