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 namespace { 26 inline constexpr double NORMAL_START_ANGLE = -30; 27 inline constexpr double ACTIVE_START_ANGLE = -60; 28 inline constexpr double NORMAL_MAX_OFFSET_ANGLE = 60.0; 29 inline constexpr double ACTIVE_MAX_OFFSET_ANGLE = 120.0; 30 inline constexpr double ARC_SCROLL_BAR_FOREGROUND_OPACITY = 0.66; 31 inline constexpr uint32_t ARC_SCROLL_BAR_BACKGROUND_COLOR = 0x7F7F7F7F; 32 } // namespace 33 34 /** 35 * ScrollBarTheme defines styles of scrollBar. ScrollBarTheme should be built 36 * using ScrollBarTheme::Builder. 37 */ 38 class ScrollBarTheme : public virtual Theme { 39 DECLARE_ACE_TYPE(ScrollBarTheme, Theme); 40 41 public: 42 class Builder { 43 public: 44 Builder() = default; 45 ~Builder() = default; 46 Build(const RefPtr<ThemeConstants> & themeConstants)47 RefPtr<ScrollBarTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 48 { 49 RefPtr<ScrollBarTheme> theme = AceType::MakeRefPtr<ScrollBarTheme>(); 50 if (!themeConstants) { 51 return theme; 52 } 53 ParsePattern(themeConstants, theme); 54 return theme; 55 } 56 57 private: ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<ScrollBarTheme> & theme)58 void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<ScrollBarTheme>& theme) const 59 { 60 RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_SCROLL_BAR); 61 if (!pattern) { 62 LOGW("find pattern of scroll_bar fail"); 63 return; 64 } 65 parseNormalThemeStyle(pattern, theme); 66 theme->arcNormalBackgroundWidth_ = pattern->GetAttr<Dimension>("scroll_bar_normal_background_width", 67 4.0_vp); 68 theme->arcActiveBackgroundWidth_ = pattern->GetAttr<Dimension>("scroll_bar_active_background_width", 69 24.0_vp); 70 theme->arcNormalStartAngle_ = pattern->GetAttr<double>("scroll_bar_normal_start_angle", NORMAL_START_ANGLE); 71 theme->arcActiveStartAngle_ = pattern->GetAttr<double>("scroll_bar_active_start_angle", ACTIVE_START_ANGLE); 72 theme->arcNormalMaxOffsetAngle_ = pattern->GetAttr<double>("scroll_bar_normal_max_offset_angle", 73 NORMAL_MAX_OFFSET_ANGLE); 74 theme->arcActiveMaxOffsetAngle_ = pattern->GetAttr<double>("scroll_bar_active_max_offset_angle", 75 ACTIVE_MAX_OFFSET_ANGLE); 76 theme->arcNormalScrollBarWidth_ = pattern->GetAttr<Dimension>("scroll_bar_normal_scroll_bar_width", 3.0_vp); 77 theme->arcActiveScrollBarWidth_ = pattern->GetAttr<Dimension>("scroll_bar_active_scroll_bar_width", 78 22.0_vp); 79 auto blendOpacity = pattern->GetAttr<double>("arc_scroll_bar_foreground_opacity", 80 ARC_SCROLL_BAR_FOREGROUND_OPACITY); 81 theme->arcForegroundColor_ = pattern->GetAttr<Color>(PATTERN_FG_COLOR, 82 Color::TRANSPARENT).BlendOpacity(blendOpacity); 83 theme->arcBackgroundColor_ = pattern->GetAttr<Color>("arc_scroll_bar_background_color", 84 Color(ARC_SCROLL_BAR_BACKGROUND_COLOR)); 85 } 86 parseNormalThemeStyle(const RefPtr<ThemeStyle> & pattern,const RefPtr<ScrollBarTheme> & theme)87 void parseNormalThemeStyle(const RefPtr<ThemeStyle>& pattern, const RefPtr<ScrollBarTheme>& theme) const 88 { 89 theme->shapeMode_ = static_cast<ShapeMode>(pattern->GetAttr<double>("scroll_bar_shape_mode", 0.0)); 90 theme->normalWidth_ = pattern->GetAttr<Dimension>("scroll_bar_normal_width", 0.0_vp); 91 theme->activeWidth_ = pattern->GetAttr<Dimension>("scroll_bar_active_width", 0.0_vp); 92 theme->minHeight_ = pattern->GetAttr<Dimension>("scroll_bar_min_height", 0.0_vp); 93 theme->minDynamicHeight_ = pattern->GetAttr<Dimension>("scroll_bar_min_dynamic_height", 0.0_vp); 94 theme->reservedHeight_ = pattern->GetAttr<Dimension>("scroll_bar_reserved_height", 0.0_vp); 95 theme->touchWidth_ = pattern->GetAttr<Dimension>("scroll_bar_touch_width", 0.0_vp); 96 auto padding = pattern->GetAttr<Dimension>("scroll_bar_margin", Dimension(4.0, DimensionUnit::VP)); 97 theme->padding_ = Edge(padding.Value(), 0.0, padding.Value(), padding.Value(), padding.Unit()); 98 theme->scrollBarMargin_ = padding; 99 theme->defaultWidth_ = pattern->GetAttr<Dimension>("scroll_bar_default_width", 16.0_vp); 100 theme->defaultHeight_ = pattern->GetAttr<Dimension>("scroll_bar_default_height", 16.0_vp); 101 auto blendOpacity = pattern->GetAttr<double>("scroll_bar_foreground_opacity", 0.4f); 102 theme->foregroundColor_ = pattern->GetAttr<Color>(PATTERN_FG_COLOR, 103 Color::TRANSPARENT).BlendOpacity(blendOpacity); 104 theme->backgroundColor_ = pattern->GetAttr<Color>("scroll_bar_background_color", Color()); 105 106 theme->foregroundHoverBlendColor_ = 107 pattern->GetAttr<Color>("scroll_bar_foreground_hover_blend_color", Color::TRANSPARENT); 108 theme->foregroundPressedBlendColor_ = 109 pattern->GetAttr<Color>("scroll_bar_foreground_pressed_blend_color", PRESSED_BLEND_COLOR); 110 } 111 }; 112 113 ~ScrollBarTheme() override = default; 114 GetNormalWidth()115 const Dimension& GetNormalWidth() const 116 { 117 return normalWidth_; 118 } 119 GetActiveWidth()120 const Dimension& GetActiveWidth() const 121 { 122 return activeWidth_; 123 } 124 GetMinHeight()125 const Dimension& GetMinHeight() const 126 { 127 return minHeight_; 128 } 129 GetMinDynamicHeight()130 const Dimension& GetMinDynamicHeight() const 131 { 132 return minDynamicHeight_; 133 } 134 GetReservedHeight()135 const Dimension& GetReservedHeight() const 136 { 137 return reservedHeight_; 138 } 139 GetTouchWidth()140 const Dimension& GetTouchWidth() const 141 { 142 return touchWidth_; 143 } 144 GetBackgroundColor()145 const Color& GetBackgroundColor() const 146 { 147 return backgroundColor_; 148 } 149 GetForegroundColor()150 const Color& GetForegroundColor() const 151 { 152 return foregroundColor_; 153 } 154 GetForegroundHoverBlendColor()155 const Color& GetForegroundHoverBlendColor() const 156 { 157 return foregroundHoverBlendColor_; 158 } 159 GetForegroundPressedBlendColor()160 const Color& GetForegroundPressedBlendColor() const 161 { 162 return foregroundPressedBlendColor_; 163 } 164 GetShapeMode()165 ShapeMode GetShapeMode() const 166 { 167 return shapeMode_; 168 } 169 GetPadding()170 const Edge& GetPadding() const 171 { 172 return padding_; 173 } 174 GetScrollBarMargin()175 const Dimension& GetScrollBarMargin() const 176 { 177 return scrollBarMargin_; 178 } 179 GetDefaultWidth()180 const Dimension& GetDefaultWidth() const 181 { 182 return defaultWidth_; 183 } 184 GetDefaultHeight()185 const Dimension& GetDefaultHeight() const 186 { 187 return defaultHeight_; 188 } 189 GetArcNormalBackgroundWidth()190 const Dimension& GetArcNormalBackgroundWidth() const 191 { 192 return arcNormalBackgroundWidth_; 193 } GetArcActiveBackgroundWidth()194 const Dimension& GetArcActiveBackgroundWidth() const 195 { 196 return arcActiveBackgroundWidth_; 197 } GetArcNormalStartAngle()198 double GetArcNormalStartAngle() const 199 { 200 return arcNormalStartAngle_; 201 } GetArcActiveStartAngle()202 double GetArcActiveStartAngle() const 203 { 204 return arcActiveStartAngle_; 205 } GetArcNormalMaxOffsetAngle()206 double GetArcNormalMaxOffsetAngle() const 207 { 208 return arcNormalMaxOffsetAngle_; 209 } GetArcActiveMaxOffsetAngle()210 double GetArcActiveMaxOffsetAngle() const 211 { 212 return arcActiveMaxOffsetAngle_; 213 } GetArcNormalScrollBarWidth()214 const Dimension& GetArcNormalScrollBarWidth() const 215 { 216 return arcNormalScrollBarWidth_; 217 } GetArcActiveScrollBarWidth()218 const Dimension& GetArcActiveScrollBarWidth() const 219 { 220 return arcActiveScrollBarWidth_; 221 } GetArcBackgroundColor()222 const Color& GetArcBackgroundColor() const 223 { 224 return arcBackgroundColor_; 225 } GetArcForegroundColor()226 const Color& GetArcForegroundColor() const 227 { 228 return arcForegroundColor_; 229 } 230 231 protected: 232 ScrollBarTheme() = default; 233 234 private: 235 ShapeMode shapeMode_ = ShapeMode::DEFAULT; 236 Dimension normalWidth_; 237 Dimension activeWidth_; 238 Dimension minHeight_; 239 Dimension minDynamicHeight_; 240 Dimension reservedHeight_; 241 Dimension touchWidth_; 242 Dimension scrollBarMargin_; 243 Dimension defaultWidth_; 244 Dimension defaultHeight_; 245 Color backgroundColor_; 246 Color foregroundColor_; 247 Color foregroundHoverBlendColor_; 248 Color foregroundPressedBlendColor_; 249 Edge padding_; 250 Dimension arcNormalBackgroundWidth_; 251 Dimension arcActiveBackgroundWidth_; 252 double arcNormalStartAngle_ = 0.0; 253 double arcActiveStartAngle_ = 0.0; 254 double arcNormalMaxOffsetAngle_ = 0.0; 255 double arcActiveMaxOffsetAngle_ = 0.0; 256 Dimension arcNormalScrollBarWidth_; 257 Dimension arcActiveScrollBarWidth_; 258 Color arcForegroundColor_; 259 Color arcBackgroundColor_; 260 }; 261 262 } // namespace OHOS::Ace 263 264 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCROLL_SCROLL_BAR_THEME_H 265