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 theme->scrollBarMargin_ = padding; 77 } 78 }; 79 80 ~ScrollBarTheme() override = default; 81 GetNormalWidth()82 const Dimension& GetNormalWidth() const 83 { 84 return normalWidth_; 85 } 86 GetActiveWidth()87 const Dimension& GetActiveWidth() const 88 { 89 return activeWidth_; 90 } 91 GetMinHeight()92 const Dimension& GetMinHeight() const 93 { 94 return minHeight_; 95 } 96 GetMinDynamicHeight()97 const Dimension& GetMinDynamicHeight() const 98 { 99 return minDynamicHeight_; 100 } 101 GetReservedHeight()102 const Dimension& GetReservedHeight() const 103 { 104 return reservedHeight_; 105 } 106 GetTouchWidth()107 const Dimension& GetTouchWidth() const 108 { 109 return touchWidth_; 110 } 111 GetBackgroundColor()112 const Color& GetBackgroundColor() const 113 { 114 return backgroundColor_; 115 } 116 GetForegroundColor()117 const Color& GetForegroundColor() const 118 { 119 return foregroundColor_; 120 } 121 GetShapeMode()122 ShapeMode GetShapeMode() const 123 { 124 return shapeMode_; 125 } 126 GetPadding()127 const Edge& GetPadding() const 128 { 129 return padding_; 130 } 131 GetScrollBarMargin()132 const Dimension& GetScrollBarMargin() const 133 { 134 return scrollBarMargin_; 135 } 136 137 protected: 138 ScrollBarTheme() = default; 139 140 private: 141 ShapeMode shapeMode_ = ShapeMode::DEFAULT; 142 Dimension normalWidth_; 143 Dimension activeWidth_; 144 Dimension minHeight_; 145 Dimension minDynamicHeight_; 146 Dimension reservedHeight_; 147 Dimension touchWidth_; 148 Dimension scrollBarMargin_; 149 Color backgroundColor_; 150 Color foregroundColor_; 151 Edge padding_; 152 }; 153 154 } // namespace OHOS::Ace 155 156 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCROLL_SCROLL_BAR_THEME_H 157