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 ParsePattern(themeConstants, theme); 44 return theme; 45 } 46 47 private: ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<BadgeTheme> & theme)48 void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<BadgeTheme>& theme) const 49 { 50 RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_BADGE); 51 if (!pattern) { 52 LOGW("find pattern of badge fail"); 53 return; 54 } 55 theme->messageCount_ = static_cast<int32_t>(pattern->GetAttr<double>("badge_message_count", 0.0)); 56 theme->badgePosition_ = 57 BadgePosition(static_cast<int32_t>(pattern->GetAttr<double>("badge_position", 0.0))); 58 theme->showMessage_ = static_cast<int32_t>(pattern->GetAttr<double>("badge_show_message", 0.0)); 59 theme->badgeColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR, Color::BLACK); 60 theme->badgeFontSize_ = pattern->GetAttr<Dimension>(PATTERN_TEXT_SIZE, 0.0_vp); 61 theme->badgeAgeFontSize_ = pattern->GetAttr<Dimension>(BADGE_AGE_FONT_SIZE, 16.0_vp); 62 theme->badgeAgeSize_ = pattern->GetAttr<Dimension>(BADGE_AGE_SIZE, 23.0_vp); 63 theme->badgeAgeAddPadding_ = pattern->GetAttr<Dimension>(BADGE_AFE_ADD_PADDING, 2.0_vp); 64 theme->badgeTextColor_ = pattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color::BLACK); 65 theme->badgeBorderColor_ = pattern->GetAttr<Color>(BADGE_BORDER_COLOR, Color::BLACK); 66 theme->badgeBorderWidth_ = pattern->GetAttr<Dimension>(BADGE_BORDER_WIDTH, 0.0_vp); 67 } 68 }; 69 70 ~BadgeTheme() override = default; 71 GetBadgeColor()72 const Color& GetBadgeColor() const 73 { 74 return badgeColor_; 75 } 76 GetMessageCount()77 int64_t GetMessageCount() const 78 { 79 return messageCount_; 80 } 81 GetBadgePosition()82 BadgePosition GetBadgePosition() const 83 { 84 return badgePosition_; 85 } 86 GetBadgePositionX()87 const Dimension& GetBadgePositionX() const 88 { 89 return badgePositionX_; 90 } 91 GetBadgePositionY()92 const Dimension& GetBadgePositionY() const 93 { 94 return badgePositionY_; 95 } 96 GetIsPositionXy()97 bool GetIsPositionXy() const 98 { 99 return isPositionXy_; 100 } 101 GetShowMessage()102 bool GetShowMessage() const 103 { 104 return showMessage_; 105 } 106 GetBadgeTextColor()107 const Color& GetBadgeTextColor() const 108 { 109 return badgeTextColor_; 110 } 111 GetBadgeBorderColor()112 const Color& GetBadgeBorderColor() const 113 { 114 return badgeBorderColor_; 115 } 116 GetBadgeFontSize()117 const Dimension& GetBadgeFontSize() const 118 { 119 return badgeFontSize_; 120 } 121 GetBadgeAgeFontSize()122 const Dimension& GetBadgeAgeFontSize() const 123 { 124 return badgeAgeFontSize_; 125 } 126 GetBadgeCircleSize()127 const Dimension& GetBadgeCircleSize() 128 { 129 return badgeSize_; 130 } 131 GetBadgeAgeCircleSize()132 const Dimension& GetBadgeAgeCircleSize() const 133 { 134 return badgeAgeSize_; 135 } 136 GetBadgeAgeAddPadding()137 const Dimension& GetBadgeAgeAddPadding() const 138 { 139 return badgeAgeAddPadding_; 140 } 141 GetLittleBadgeCircleSize()142 const Dimension& GetLittleBadgeCircleSize() 143 { 144 return littleBadgeSize_; 145 } 146 GetMaxCount()147 int GetMaxCount() const 148 { 149 return maxCount_; 150 } 151 GetNumericalBadgePadding()152 const Dimension& GetNumericalBadgePadding() 153 { 154 return numericalBadgePadding_; 155 } 156 GetBadgeBorderWidth()157 const Dimension& GetBadgeBorderWidth() 158 { 159 return badgeBorderWidth_; 160 } 161 162 protected: 163 BadgeTheme() = default; 164 165 private: 166 Color badgeColor_; 167 Color badgeTextColor_; 168 Color badgeBorderColor_; 169 int64_t messageCount_; 170 BadgePosition badgePosition_ = BadgePosition::RIGHT_TOP; 171 Dimension badgePositionX_ = 0.0_vp; 172 Dimension badgePositionY_ = 0.0_vp; 173 bool isPositionXy_ = false; 174 bool showMessage_; 175 Dimension badgeFontSize_; 176 Dimension badgeAgeFontSize_; 177 Dimension badgeBorderWidth_; 178 Dimension badgeSize_ = 16.0_vp; 179 Dimension badgeAgeSize_; 180 Dimension badgeAgeAddPadding_; 181 Dimension littleBadgeSize_ = 6.0_vp; 182 Dimension numericalBadgePadding_ = 6.0_vp; 183 int maxCount_ = 99; 184 }; 185 186 } // namespace OHOS::Ace 187 188 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BADGE_BADGE_THEME_H 189