• 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/scrollable/scrollable_paint_property.h"
17 
18 #include "core/pipeline_ng/pipeline_context.h"
19 
20 namespace OHOS::Ace::NG {
ContentClipToStr() const21 std::string ScrollablePaintProperty::ContentClipToStr() const
22 {
23     auto mode = propContentClip_ ? propContentClip_->first : ContentClipMode::DEFAULT;
24     if (mode == ContentClipMode::DEFAULT) {
25         mode = GetDefaultContentClip();
26     }
27     switch (mode) {
28         case ContentClipMode::CONTENT_ONLY:
29             return "ContentClipMode.CONTENT_ONLY";
30         case ContentClipMode::BOUNDARY:
31             return "ContentClipMode.BOUNDARY";
32         case ContentClipMode::CUSTOM:
33             return "RectShape";
34         case ContentClipMode::SAFE_AREA:
35             return "ContentClipMode.SAFE_AREA";
36         default:
37             return "";
38     }
39 }
40 
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter) const41 void ScrollablePaintProperty::ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const
42 {
43     PaintProperty::ToJsonValue(json, filter);
44     /* no fixed attr below, just return */
45     if (filter.IsFastFilter()) {
46         return;
47     }
48     json->PutExtAttr("scrollBar", GetBarStateString().c_str(), filter);
49     json->PutExtAttr("scrollBarColor", GetBarColor().ColorToString().c_str(), filter);
50     json->PutExtAttr("scrollBarWidth", GetBarWidth().ToString().c_str(), filter);
51     json->PutExtAttr("scrollBarMargin",
52         propScrollBarProperty_
53             ? propScrollBarProperty_->propScrollBarMargin.value_or(ScrollBarMargin()).ToString().c_str()
54             : ScrollBarMargin().ToString().c_str(),
55         filter);
56     json->PutExtAttr("fadingEdge",
57         propFadingEdgeProperty_ ? propFadingEdgeProperty_->propFadingEdge.value_or(false) : false, filter);
58     json->PutExtAttr("defaultFadingEdge", false, filter);
59     auto fadingEdgeOption = JsonUtil::Create(true);
60     fadingEdgeOption->Put("fadingEdgeLength",
61         propFadingEdgeProperty_
62             ? propFadingEdgeProperty_->propFadingEdgeLength
63                   .value_or(Dimension(32.0, DimensionUnit::VP)) // 32.0: default fading edge length
64                   .ToString()
65                   .c_str()
66             : Dimension(32.0, DimensionUnit::VP).ToString().c_str()); // 32.0: default fading edge length
67     json->PutExtAttr("fadingEdgeOption", fadingEdgeOption, filter);
68     json->PutExtAttr("clipContent", ContentClipToStr().c_str(), filter);
69 }
70 
GetBarColor() const71 Color ScrollablePaintProperty::GetBarColor() const
72 {
73     auto context = PipelineContext::GetCurrentContextSafelyWithCheck();
74     CHECK_NULL_RETURN(context, Color::TRANSPARENT);
75     auto themeManager = context->GetThemeManager();
76     CHECK_NULL_RETURN(themeManager, Color::TRANSPARENT);
77     auto scrollBarTheme = themeManager->GetTheme<ScrollBarTheme>();
78     CHECK_NULL_RETURN(scrollBarTheme, Color::TRANSPARENT);
79     auto defaultScrollBarColor = scrollBarTheme->GetForegroundColor();
80     return propScrollBarProperty_ ? propScrollBarProperty_->propScrollBarColor.value_or(defaultScrollBarColor)
81                                   : defaultScrollBarColor;
82 }
83 
GetBarWidth() const84 Dimension ScrollablePaintProperty::GetBarWidth() const
85 {
86     auto context = PipelineContext::GetCurrentContextSafelyWithCheck();
87     CHECK_NULL_RETURN(context, Dimension());
88     auto themeManager = context->GetThemeManager();
89     CHECK_NULL_RETURN(themeManager, Dimension());
90     auto scrollBarTheme = themeManager->GetTheme<ScrollBarTheme>();
91     CHECK_NULL_RETURN(scrollBarTheme, Dimension());
92     auto defaultScrollBarWidth = scrollBarTheme->GetNormalWidth();
93     return propScrollBarProperty_ ? propScrollBarProperty_->propScrollBarWidth.value_or(defaultScrollBarWidth)
94                                   : defaultScrollBarWidth;
95 }
96 
GetBarStateString() const97 std::string ScrollablePaintProperty::GetBarStateString() const
98 {
99     auto mode = propScrollBarProperty_ ? propScrollBarProperty_->propScrollBarMode.value_or(DisplayMode::AUTO)
100                                        : DisplayMode::AUTO;
101     switch (mode) {
102         case DisplayMode::AUTO:
103             return "BarState.Auto";
104         case DisplayMode::ON:
105             return "BarState.On";
106         case DisplayMode::OFF:
107             return "BarState.Off";
108         default:
109             break;
110     }
111     return "BarState.Off";
112 }
113 
CloneProps(const ScrollablePaintProperty * src)114 void ScrollablePaintProperty::CloneProps(const ScrollablePaintProperty* src)
115 {
116     UpdatePaintProperty(src);
117     propScrollBarProperty_ = src->CloneScrollBarProperty();
118     propFadingEdgeProperty_ = src->CloneFadingEdgeProperty();
119     propContentClip_ = src->CloneContentClip();
120 }
121 
Clone() const122 RefPtr<PaintProperty> ScrollablePaintProperty::Clone() const
123 {
124     auto paintProperty = MakeRefPtr<ScrollablePaintProperty>();
125     paintProperty->CloneProps(this);
126     return paintProperty;
127 }
128 
Clone() const129 RefPtr<PaintProperty> GridPaintProperty::Clone() const
130 {
131     auto paintProperty = MakeRefPtr<GridPaintProperty>();
132     paintProperty->CloneProps(this);
133     return paintProperty;
134 }
135 
Clone() const136 RefPtr<PaintProperty> ScrollPaintProperty::Clone() const
137 {
138     auto paintProperty = MakeRefPtr<GridPaintProperty>();
139     paintProperty->CloneProps(this);
140     return paintProperty;
141 }
142 } // namespace OHOS::Ace::NG
143