• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_SCROLL_SCROLL_BAR_THEME_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCROLL_SCROLL_BAR_THEME_H
18 
19 #include "core/components/common/properties/scroll_bar.h"
20 #include "core/components/theme/theme.h"
21 #include "core/components/theme/theme_constants.h"
22 #include "core/components/theme/theme_constants_defines.h"
23 
24 namespace OHOS::Ace {
25 
26 /**
27  * ScrollBarTheme defines styles of scrollBar. ScrollBarTheme should be built
28  * using ScrollBarTheme::Builder.
29  */
30 class ScrollBarTheme : public virtual Theme {
31     DECLARE_ACE_TYPE(ScrollBarTheme, Theme);
32 
33 public:
34     class Builder {
35     public:
36         Builder() = default;
37         ~Builder() = default;
38 
Build(const RefPtr<ThemeConstants> & themeConstants)39         RefPtr<ScrollBarTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
40         {
41             RefPtr<ScrollBarTheme> theme = AceType::Claim(new ScrollBarTheme());
42             if (!themeConstants) {
43                 return theme;
44             }
45             theme->shapeMode_ = static_cast<ShapeMode>(themeConstants->GetInt(THEME_SCROLL_BAR_SHAPE_MODE));
46             theme->normalWidth_ = themeConstants->GetDimension(THEME_SCROLL_BAR_NORMAL_WIDTH);
47             theme->activeWidth_ = themeConstants->GetDimension(THEME_SCROLL_BAR_ACTIVE_WIDTH);
48             theme->minHeight_ = themeConstants->GetDimension(THEME_SCROLL_BAR_MIN_HEIGHT);
49             theme->minDynamicHeight_ = themeConstants->GetDimension(THEME_SCROLL_BAR_MIN_DYNAMIC_HEIGHT);
50             theme->reservedHeight_ = themeConstants->GetDimension(THEME_SCROLL_BAR_RESERVED_HEIGHT);
51             theme->touchWidth_ = themeConstants->GetDimension(THEME_SCROLL_BAR_TOUCH_WIDTH);
52             theme->backgroundColor_ = themeConstants->GetColor(THEME_SCROLL_BAR_BACKGROUND_COLOR);
53             theme->foregroundColor_ = themeConstants->GetColor(THEME_SCROLL_BAR_FOREGROUND_COLOR);
54             Dimension paddingRight = themeConstants->GetDimension(THEME_SCROLL_BAR_PADDING_RIGHT);
55             theme->padding_ = Edge(0.0, 0.0, paddingRight.Value(), paddingRight.Value(), paddingRight.Unit());
56             ParsePattern(themeConstants->GetThemeStyle(), theme);
57             return theme;
58         }
59 
60     private:
ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<ScrollBarTheme> & theme)61         void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<ScrollBarTheme>& theme) const
62         {
63             if (!themeStyle) {
64                 LOGI("scroll bar theme style is null");
65                 return;
66             }
67             auto pattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_SCROLL_BAR, nullptr);
68             if (!pattern) {
69                 LOGW("find pattern of scroll_bar fail");
70                 return;
71             }
72             theme->foregroundColor_ = pattern->GetAttr<Color>(PATTERN_FG_COLOR,
73                 Color::TRANSPARENT).BlendOpacity(0.4);
74             auto padding = pattern->GetAttr<Dimension>("scroll_bar_margin", Dimension(4.0, DimensionUnit::VP));
75             theme->padding_ = Edge(0.0, 0.0, padding.Value(), padding.Value(), padding.Unit());
76         }
77     };
78 
79     ~ScrollBarTheme() override = default;
80 
GetNormalWidth()81     const Dimension& GetNormalWidth() const
82     {
83         return normalWidth_;
84     }
85 
GetActiveWidth()86     const Dimension& GetActiveWidth() const
87     {
88         return activeWidth_;
89     }
90 
GetMinHeight()91     const Dimension& GetMinHeight() const
92     {
93         return minHeight_;
94     }
95 
GetMinDynamicHeight()96     const Dimension& GetMinDynamicHeight() const
97     {
98         return minDynamicHeight_;
99     }
100 
GetReservedHeight()101     const Dimension& GetReservedHeight() const
102     {
103         return reservedHeight_;
104     }
105 
GetTouchWidth()106     const Dimension& GetTouchWidth() const
107     {
108         return touchWidth_;
109     }
110 
GetBackgroundColor()111     const Color& GetBackgroundColor() const
112     {
113         return backgroundColor_;
114     }
115 
GetForegroundColor()116     const Color& GetForegroundColor() const
117     {
118         return foregroundColor_;
119     }
120 
GetShapeMode()121     ShapeMode GetShapeMode() const
122     {
123         return shapeMode_;
124     }
125 
GetPadding()126     const Edge& GetPadding() const
127     {
128         return padding_;
129     }
130 
131 protected:
132     ScrollBarTheme() = default;
133 
134 private:
135     ShapeMode shapeMode_ = ShapeMode::DEFAULT;
136     Dimension normalWidth_;
137     Dimension activeWidth_;
138     Dimension minHeight_;
139     Dimension minDynamicHeight_;
140     Dimension reservedHeight_;
141     Dimension touchWidth_;
142     Color backgroundColor_;
143     Color foregroundColor_;
144     Edge padding_;
145 };
146 
147 } // namespace OHOS::Ace
148 
149 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCROLL_SCROLL_BAR_THEME_H
150