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_BADGE_BADGE_THEME_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BADGE_BADGE_THEME_H 18 19 #include "core/components/common/layout/constants.h" 20 #include "core/components/common/properties/color.h" 21 #include "core/components/theme/theme.h" 22 #include "core/components/theme/theme_constants.h" 23 #include "core/components/theme/theme_constants_defines.h" 24 #include "core/components/theme/theme_manager.h" 25 26 namespace OHOS::Ace { 27 28 class BadgeTheme : public virtual Theme { 29 DECLARE_ACE_TYPE(BadgeTheme, Theme); 30 31 public: 32 class Builder { 33 public: 34 Builder() = default; 35 ~Builder() = default; 36 Build(const RefPtr<ThemeConstants> & themeConstants)37 RefPtr<BadgeTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 38 { 39 RefPtr<BadgeTheme> theme = AceType::Claim(new BadgeTheme()); 40 if (!themeConstants) { 41 return theme; 42 } 43 theme->badgeColor_ = themeConstants->GetColor(THEME_BADGE_COLOR); 44 theme->messageCount_ = themeConstants->GetInt(THEME_BADGE_MESSAGECOUNT); 45 theme->badgePosition_ = BadgePosition(themeConstants->GetInt(THEME_BADGE_POSITION)); 46 theme->showMessage_ = themeConstants->GetInt(THEME_BADGE_SHOWMESSAGE); 47 theme->badgeTextColor_ = themeConstants->GetColor(THEME_BADGE_TEXT_COLOR); 48 theme->badgeFontSize_ = themeConstants->GetDimension(THEME_BADGE_TEXT_FONT_SIZE); 49 theme->badgeBorderColor_ = themeConstants->GetColor(THEME_BADGE_BORDER_COLOR); 50 theme->badgeBorderWidth_ = themeConstants->GetDimension(THEME_BADGE_BORDER_WIDTH); 51 ParsePattern(themeConstants->GetThemeStyle(), theme); 52 return theme; 53 } 54 55 private: ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<BadgeTheme> & theme)56 void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<BadgeTheme>& theme) const 57 { 58 if (!themeStyle) { 59 return; 60 } 61 auto pattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_BADGE, nullptr); 62 if (!pattern) { 63 LOGW("find pattern of badge fail"); 64 return; 65 } 66 theme->badgeColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR, Color::BLACK); 67 theme->badgeFontSize_ = pattern->GetAttr<Dimension>(PATTERN_TEXT_SIZE, 0.0_vp); 68 theme->badgeTextColor_ = pattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color::BLACK); 69 theme->badgeBorderColor_ = pattern->GetAttr<Color>(BADGE_BORDER_COLOR, Color::BLACK); 70 theme->badgeBorderWidth_ = pattern->GetAttr<Dimension>(BADGE_BORDER_WIDTH, 0.0_vp); 71 } 72 }; 73 74 ~BadgeTheme() override = default; 75 GetBadgeColor()76 const Color& GetBadgeColor() const 77 { 78 return badgeColor_; 79 } 80 GetMessageCount()81 int64_t GetMessageCount() const 82 { 83 return messageCount_; 84 } 85 GetBadgePosition()86 BadgePosition GetBadgePosition() const 87 { 88 return badgePosition_; 89 } 90 GetBadgePositionX()91 const Dimension& GetBadgePositionX() const 92 { 93 return badgePositionX_; 94 } 95 GetBadgePositionY()96 const Dimension& GetBadgePositionY() const 97 { 98 return badgePositionY_; 99 } 100 GetIsPositionXy()101 bool GetIsPositionXy() const 102 { 103 return isPositionXy_; 104 } 105 GetShowMessage()106 bool GetShowMessage() const 107 { 108 return showMessage_; 109 } 110 GetBadgeTextColor()111 const Color& GetBadgeTextColor() const 112 { 113 return badgeTextColor_; 114 } 115 GetBadgeBorderColor()116 const Color& GetBadgeBorderColor() const 117 { 118 return badgeBorderColor_; 119 } 120 GetBadgeFontSize()121 const Dimension& GetBadgeFontSize() const 122 { 123 return badgeFontSize_; 124 } 125 GetBadgeCircleSize()126 const Dimension& GetBadgeCircleSize() 127 { 128 return badgeSize_; 129 } 130 GetLittleBadgeCircleSize()131 const Dimension& GetLittleBadgeCircleSize() 132 { 133 return littleBadgeSize_; 134 } 135 GetMaxCount()136 int GetMaxCount() const 137 { 138 return maxCount_; 139 } 140 GetNumericalBadgePadding()141 const Dimension& GetNumericalBadgePadding() 142 { 143 return numericalBadgePadding_; 144 } 145 GetBadgeBorderWidth()146 const Dimension& GetBadgeBorderWidth() 147 { 148 return badgeBorderWidth_; 149 } 150 151 protected: 152 BadgeTheme() = default; 153 154 private: 155 Color badgeColor_; 156 Color badgeTextColor_; 157 Color badgeBorderColor_; 158 int64_t messageCount_; 159 BadgePosition badgePosition_ = BadgePosition::RIGHT_TOP; 160 Dimension badgePositionX_ = 0.0_vp; 161 Dimension badgePositionY_ = 0.0_vp; 162 bool isPositionXy_ = false; 163 bool showMessage_; 164 Dimension badgeFontSize_; 165 Dimension badgeBorderWidth_; 166 Dimension badgeSize_ = 16.0_vp; 167 Dimension littleBadgeSize_ = 6.0_vp; 168 Dimension numericalBadgePadding_ = 6.0_vp; 169 int maxCount_ = 99; 170 }; 171 172 } // namespace OHOS::Ace 173 174 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BADGE_BADGE_THEME_H 175