• 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 #include "core/components_ng/pattern/scrollable/scrollable_paint_property.h"
17 
18 #include "core/components/scroll/scroll_bar_theme.h"
19 #include "core/pipeline_ng/pipeline_context.h"
20 
21 namespace OHOS::Ace::NG {
ToJsonValue(std::unique_ptr<JsonValue> & json) const22 void ScrollablePaintProperty::ToJsonValue(std::unique_ptr<JsonValue>& json) const
23 {
24     PaintProperty::ToJsonValue(json);
25     json->Put("scrollBar", GetBarStateString().c_str());
26     json->Put("scrollBarColor", GetBarColor().ColorToString().c_str());
27     json->Put("scrollBarWidth", GetBarWidth().ToString().c_str());
28 }
29 
GetBarColor() const30 Color ScrollablePaintProperty::GetBarColor() const
31 {
32     auto context = PipelineContext::GetCurrentContext();
33     CHECK_NULL_RETURN(context, Color::TRANSPARENT);
34     auto themeManager = context->GetThemeManager();
35     CHECK_NULL_RETURN(themeManager, Color::TRANSPARENT);
36     auto scrollBarTheme = themeManager->GetTheme<ScrollBarTheme>();
37     CHECK_NULL_RETURN(scrollBarTheme, Color::TRANSPARENT);
38     auto defaultScrollBarColor = scrollBarTheme->GetForegroundColor();
39     return propScrollBarProperty_ ? propScrollBarProperty_->propScrollBarColor.value_or(defaultScrollBarColor)
40                                   : defaultScrollBarColor;
41 }
42 
GetBarWidth() const43 Dimension ScrollablePaintProperty::GetBarWidth() const
44 {
45     auto context = PipelineContext::GetCurrentContext();
46     CHECK_NULL_RETURN(context, Dimension());
47     auto themeManager = context->GetThemeManager();
48     CHECK_NULL_RETURN(themeManager, Dimension());
49     auto scrollBarTheme = themeManager->GetTheme<ScrollBarTheme>();
50     CHECK_NULL_RETURN(scrollBarTheme, Dimension());
51     auto defaultScrollBarWidth = scrollBarTheme->GetNormalWidth();
52     return propScrollBarProperty_ ? propScrollBarProperty_->propScrollBarWidth.value_or(defaultScrollBarWidth)
53                                   : defaultScrollBarWidth;
54 }
55 
GetBarStateString() const56 std::string ScrollablePaintProperty::GetBarStateString() const
57 {
58     auto mode = propScrollBarProperty_ ? propScrollBarProperty_->propScrollBarMode.value_or(NG::DisplayMode::AUTO)
59                                        : NG::DisplayMode::AUTO;
60     switch (mode) {
61         case NG::DisplayMode::AUTO:
62             return "BarState.Auto";
63         case NG::DisplayMode::ON:
64             return "BarState.On";
65         case NG::DisplayMode::OFF:
66             return "BarState.Off";
67         default:
68             LOGE("mode %{public}d is not valid", mode);
69             break;
70     }
71     return "BarState.Off";
72 }
73 } // namespace OHOS::Ace::NG
74