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_TOOL_BAR_TOOL_BAR_THEME_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TOOL_BAR_TOOL_BAR_THEME_H 18 19 #include <vector> 20 21 #include "core/components/common/properties/edge.h" 22 #include "core/components/common/properties/text_style.h" 23 #include "core/components/theme/theme.h" 24 #include "core/components/theme/theme_constants.h" 25 #include "core/components/theme/theme_constants_defines.h" 26 #include "core/components/theme/theme_manager.h" 27 #include "frameworks/bridge/common/utils/utils.h" 28 29 namespace OHOS::Ace { 30 31 /** 32 * ToolBarTheme defines color and styles of ToolBar. ToolBarTheme should be built 33 * using ToolBarTheme::Builder. 34 */ 35 class ToolBarTheme : public virtual Theme { 36 DECLARE_ACE_TYPE(ToolBarTheme, Theme); 37 38 public: 39 class Builder { 40 public: 41 Builder() = default; 42 ~Builder() = default; 43 Build(const RefPtr<ThemeConstants> & themeConstants)44 RefPtr<ToolBarTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 45 { 46 RefPtr<ToolBarTheme> theme = AceType::Claim(new ToolBarTheme()); 47 if (!themeConstants) { 48 return theme; 49 } 50 theme->textStyle_.SetFontSize(themeConstants->GetDimension(THEME_TOOL_BAR_TEXT_FONTSIZE)); 51 theme->textStyle_.SetFontFamilies({ themeConstants->GetString(THEME_OHOS_TEXT_FONT_FAMILY_MEDIUM) }); 52 theme->textStyle_.SetAdaptTextSize(themeConstants->GetDimension(THEME_TOOL_BAR_TEXT_FONTSIZE), 53 themeConstants->GetDimension(THEME_TOOL_BAR_TEXT_MIN_FONTSIZE)); 54 theme->iconSize_ = Size(themeConstants->GetDimension(THEME_TOOL_BAR_ITEM_ICON_WIDTH).Value(), 55 themeConstants->GetDimension(THEME_TOOL_BAR_ITEM_ICON_HEIGHT).Value()); 56 theme->imageEdge_ = Edge(themeConstants->GetDimension(THEME_TOOL_BAR_ITEM_PADDING_ICON_LEFT), 57 themeConstants->GetDimension(THEME_TOOL_BAR_ITEM_PADDING_ICON_TOP), 58 themeConstants->GetDimension(THEME_TOOL_BAR_ITEM_PADDING_ICON_RIGHT), 59 themeConstants->GetDimension(THEME_TOOL_BAR_ITEM_PADDING_ICON_BOTTOM)); 60 theme->textEdge_ = Edge(themeConstants->GetDimension(THEME_TOOL_BAR_ITEM_PADDING_TEXT_LEFT), 61 themeConstants->GetDimension(THEME_TOOL_BAR_ITEM_PADDING_TEXT_TOP), 62 themeConstants->GetDimension(THEME_TOOL_BAR_ITEM_PADDING_TEXT_RIGHT), 63 themeConstants->GetDimension(THEME_TOOL_BAR_ITEM_PADDING_TEXT_BOTTOM)); 64 theme->toolBarBgColor_ = themeConstants->GetColor(THEME_TOOL_BAR_BG_COLOR); 65 theme->focusColor_ = themeConstants->GetColor(THEME_TOOL_BAR_ITEM_FOCUS_COLOR); 66 theme->hoverColor_ = themeConstants->GetColor(THEME_TOOL_BAR_ITEM_HOVER_COLOR); 67 theme->toolBarItemBgColor_ = themeConstants->GetColor(THEME_TOOL_BAR_ITEM_BACKGROUND_COLOR); 68 theme->pressColor_ = themeConstants->GetColor(THEME_OHOS_CONTROL_NORMAL); 69 theme->radius_ = themeConstants->GetDimension(THEME_TOOL_BAR_ITEM_RADIUS); 70 theme->iconMoreColor_ = themeConstants->GetColor(THEME_TOOL_BAR_ENDITEM_COLOR); 71 ParsePattern(themeConstants->GetThemeStyle(), theme); 72 return theme; 73 } 74 75 private: ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<ToolBarTheme> & theme)76 void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<ToolBarTheme>& theme) const 77 { 78 if (!themeStyle) { 79 return; 80 } 81 auto toolbarPattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_TOOLBAR, nullptr); 82 if (!toolbarPattern) { 83 LOGI("ToolbarPattern is null"); 84 return; 85 } 86 theme->iconMoreColor_ = toolbarPattern->GetAttr<Color>("more_icon_color", Color()); 87 theme->textStyle_.SetTextColor(toolbarPattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color::BLACK)); 88 theme->iconColor_ = toolbarPattern->GetAttr<Color>("icon_color", Color()); 89 theme->toolBarBgColor_ = toolbarPattern->GetAttr<Color>(PATTERN_BG_COLOR, Color()); 90 theme->toolBarItemBgColor_ = toolbarPattern->GetAttr<Color>("item_bg_color", Color()); 91 } 92 }; 93 94 ~ToolBarTheme() override = default; 95 GetToolBarTextStyle()96 const TextStyle& GetToolBarTextStyle() const 97 { 98 return textStyle_; 99 } 100 GetIconSize()101 const Size& GetIconSize() const 102 { 103 return iconSize_; 104 } 105 GetIconEdge()106 const Edge& GetIconEdge() const 107 { 108 return imageEdge_; 109 } 110 GetTextEdge()111 const Edge& GetTextEdge() const 112 { 113 return textEdge_; 114 } 115 GetToolBarBgColor()116 const Color& GetToolBarBgColor() const 117 { 118 return toolBarBgColor_; 119 } 120 GetItemBackgroundColor()121 const Color& GetItemBackgroundColor() const 122 { 123 return toolBarItemBgColor_; 124 } 125 GetIconColor()126 const Color& GetIconColor() const 127 { 128 return iconColor_; 129 } 130 GetFocusColor()131 const Color& GetFocusColor() const 132 { 133 return focusColor_; 134 } 135 GetHoverColor()136 const Color& GetHoverColor() const 137 { 138 return hoverColor_; 139 } 140 GetPressColor()141 const Color& GetPressColor() const 142 { 143 return pressColor_; 144 } 145 GetRadius()146 const Dimension& GetRadius() const 147 { 148 return radius_; 149 } 150 GetIconMoreColor()151 const Color& GetIconMoreColor() const 152 { 153 return iconMoreColor_; 154 } 155 156 protected: 157 ToolBarTheme() = default; 158 159 private: 160 Edge imageEdge_; 161 Edge textEdge_; 162 TextStyle textStyle_; 163 Size iconSize_; 164 Color toolBarBgColor_; 165 Color iconColor_; 166 Color focusColor_; 167 Color hoverColor_; 168 Color toolBarItemBgColor_; 169 Color pressColor_; 170 Dimension radius_; 171 Color iconMoreColor_; 172 }; 173 174 } // namespace OHOS::Ace 175 176 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TOOL_BAR_TOOL_BAR_THEME_H 177