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             ParsePattern(themeConstants, theme);
46             return theme;
47         }
48 
49     private:
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<ScrollBarTheme> & theme)50         void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<ScrollBarTheme>& theme) const
51         {
52             RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_SCROLL_BAR);
53             if (!pattern) {
54                 LOGW("find pattern of scroll_bar fail");
55                 return;
56             }
57             auto blendOpacity = pattern->GetAttr<double>("scroll_bar_foreground_opacity", 0.4f);
58             theme->shapeMode_ = static_cast<ShapeMode>(pattern->GetAttr<double>("scroll_bar_shape_mode", 0.0));
59             theme->normalWidth_ = pattern->GetAttr<Dimension>("scroll_bar_normal_width", 0.0_vp);
60             theme->activeWidth_ = pattern->GetAttr<Dimension>("scroll_bar_active_width", 0.0_vp);
61             theme->minHeight_ = pattern->GetAttr<Dimension>("scroll_bar_min_height", 0.0_vp);
62             theme->minDynamicHeight_ = pattern->GetAttr<Dimension>("scroll_bar_min_dynamic_height", 0.0_vp);
63             theme->reservedHeight_ = pattern->GetAttr<Dimension>("scroll_bar_reserved_height", 0.0_vp);
64             theme->touchWidth_ = pattern->GetAttr<Dimension>("scroll_bar_touch_width", 0.0_vp);
65             theme->backgroundColor_ = pattern->GetAttr<Color>("scroll_bar_background_color", Color());
66             theme->foregroundColor_ = pattern->GetAttr<Color>(PATTERN_FG_COLOR,
67                 Color::TRANSPARENT).BlendOpacity(blendOpacity);
68             auto padding = pattern->GetAttr<Dimension>("scroll_bar_margin", Dimension(4.0, DimensionUnit::VP));
69             theme->padding_ = Edge(padding.Value(), 0.0, padding.Value(), padding.Value(), padding.Unit());
70             theme->scrollBarMargin_ = padding;
71             theme->defaultWidth_ = pattern->GetAttr<Dimension>("scroll_bar_default_width", 16.0_vp);
72             theme->defaultHeight_ = pattern->GetAttr<Dimension>("scroll_bar_default_height", 16.0_vp);
73         }
74     };
75 
76     ~ScrollBarTheme() override = default;
77 
GetNormalWidth()78     const Dimension& GetNormalWidth() const
79     {
80         return normalWidth_;
81     }
82 
GetActiveWidth()83     const Dimension& GetActiveWidth() const
84     {
85         return activeWidth_;
86     }
87 
GetMinHeight()88     const Dimension& GetMinHeight() const
89     {
90         return minHeight_;
91     }
92 
GetMinDynamicHeight()93     const Dimension& GetMinDynamicHeight() const
94     {
95         return minDynamicHeight_;
96     }
97 
GetReservedHeight()98     const Dimension& GetReservedHeight() const
99     {
100         return reservedHeight_;
101     }
102 
GetTouchWidth()103     const Dimension& GetTouchWidth() const
104     {
105         return touchWidth_;
106     }
107 
GetBackgroundColor()108     const Color& GetBackgroundColor() const
109     {
110         return backgroundColor_;
111     }
112 
GetForegroundColor()113     const Color& GetForegroundColor() const
114     {
115         return foregroundColor_;
116     }
117 
GetShapeMode()118     ShapeMode GetShapeMode() const
119     {
120         return shapeMode_;
121     }
122 
GetPadding()123     const Edge& GetPadding() const
124     {
125         return padding_;
126     }
127 
GetScrollBarMargin()128     const Dimension& GetScrollBarMargin() const
129     {
130         return scrollBarMargin_;
131     }
132 
GetDefaultWidth()133     const Dimension& GetDefaultWidth() const
134     {
135         return defaultWidth_;
136     }
137 
GetDefaultHeight()138     const Dimension& GetDefaultHeight() const
139     {
140         return defaultHeight_;
141     }
142 
143 protected:
144     ScrollBarTheme() = default;
145 
146 private:
147     ShapeMode shapeMode_ = ShapeMode::DEFAULT;
148     Dimension normalWidth_;
149     Dimension activeWidth_;
150     Dimension minHeight_;
151     Dimension minDynamicHeight_;
152     Dimension reservedHeight_;
153     Dimension touchWidth_;
154     Dimension scrollBarMargin_;
155     Dimension defaultWidth_;
156     Dimension defaultHeight_;
157     Color backgroundColor_;
158     Color foregroundColor_;
159     Edge padding_;
160 };
161 
162 } // namespace OHOS::Ace
163 
164 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCROLL_SCROLL_BAR_THEME_H
165