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_SEARCH_SEARCH_THEME_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SEARCH_SEARCH_THEME_H 18 19 #include "base/geometry/dimension.h" 20 #include "core/components/common/properties/border.h" 21 #include "core/components/common/properties/color.h" 22 #include "core/components/common/properties/edge.h" 23 #include "core/components/common/properties/radius.h" 24 #include "core/components/theme/theme.h" 25 #include "core/components/theme/theme_constants.h" 26 #include "core/components/theme/theme_constants_defines.h" 27 #include "core/components_ng/pattern/search/search_model.h" 28 29 namespace OHOS::Ace { 30 31 /** 32 * SearchTheme defines color and styles of SearchComponent. SearchTheme should be built 33 * using SearchTheme::Builder. 34 */ 35 class SearchTheme : public virtual Theme { 36 DECLARE_ACE_TYPE(SearchTheme, Theme); 37 38 public: 39 class Builder { 40 public: 41 Builder() = default; 42 ~Builder() = default; 43 Build(const RefPtr<ThemeConstants> & themeConstants)44 RefPtr<SearchTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 45 { 46 RefPtr<SearchTheme> theme = AceType::Claim(new SearchTheme()); 47 if (!themeConstants) { 48 return theme; 49 } 50 ParsePattern(themeConstants, theme); 51 return theme; 52 } 53 54 private: ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<SearchTheme> & theme)55 void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<SearchTheme>& theme) const 56 { 57 if (!theme) { 58 return; 59 } 60 RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_SEARCH); 61 if (!pattern) { 62 LOGW("find pattern of search fail"); 63 return; 64 } 65 theme->height_ = pattern->GetAttr<Dimension>("search_default_height", 0.0_vp); 66 theme->iconSize_ = pattern->GetAttr<Dimension>("search_icon_size", 0.0_vp); 67 theme->closeIconSize_ = pattern->GetAttr<Dimension>("search_close_icon_size", 0.0_vp); 68 theme->closeIconHotZoneSize_ = 69 pattern->GetAttr<Dimension>("search_close_icon_hot_zone_horizontal", 0.0_vp); 70 theme->textFieldWidthReserved_ = theme->closeIconHotZoneSize_; 71 theme->leftPadding_ = pattern->GetAttr<Dimension>("search_text_field_padding_left", 0.0_vp); 72 theme->rightPadding_ = pattern->GetAttr<Dimension>("search_text_field_padding_right", 0.0_vp); 73 theme->fontWeight_ = FontWeight(static_cast<int32_t>(pattern->GetAttr<double>("search_font_weight", 0.0))); 74 theme->borderRadius_ = Radius(pattern->GetAttr<Dimension>("search_text_field_border_radius", 0.0_vp)); 75 theme->blockRightShade_ = static_cast<int32_t>(pattern->GetAttr<double>("search_block_right_shade", 0.0)); 76 theme->placeholderColor_ = pattern->GetAttr<Color>("tips_text_color", Color()); 77 theme->focusPlaceholderColor_ = pattern->GetAttr<Color>("tips_text_color_focused", Color()); 78 theme->textColor_ = pattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color()); 79 theme->focusTextColor_ = pattern->GetAttr<Color>(PATTERN_TEXT_COLOR_FOCUSED, Color()); 80 theme->fontSize_ = pattern->GetAttr<Dimension>(PATTERN_TEXT_SIZE, 0.0_fp); 81 theme->touchColor_ = pattern->GetAttr<Color>("search_touch_color", Color()); 82 theme->hoverColor_ = pattern->GetAttr<Color>("search_hover_color", Color()); 83 theme->searchDividerColor_ = pattern->GetAttr<Color>("search_divider_color", Color()); 84 theme->searchButtonTextColor_ = pattern->GetAttr<Color>("search_button_text_color", Color()); 85 theme->searchIconColor_ = pattern->GetAttr<Color>("search_icon_color", Color()); 86 theme->searchButtonTextPadding_ = pattern->GetAttr<Dimension>("search_button_text_padding", Dimension()); 87 theme->searchButtonSpace_ = pattern->GetAttr<Dimension>("search_button_space", Dimension()); 88 theme->dividerSideSpace_ = pattern->GetAttr<Dimension>("search_divider_side_space", Dimension()); 89 theme->iconHeight_ = pattern->GetAttr<Dimension>("search_icon_height", Dimension()); 90 theme->searchIconLeftSpace_ = pattern->GetAttr<Dimension>("search_icon_left_space", Dimension()); 91 theme->searchIconRightSpace_ = pattern->GetAttr<Dimension>("search_icon_right_space", Dimension()); 92 theme->symbolIconColor_ = pattern->GetAttr<Color>("search_symbol_icon_color", Color()); 93 theme->searchSymbolId_ = themeConstants->GetSymbolByName("sys.symbol.magnifyingglass"); 94 theme->cancelSymbolId_ = themeConstants->GetSymbolByName("sys.symbol.xmark"); 95 } 96 }; 97 98 ~SearchTheme() override = default; 99 GetPlaceholderColor()100 const Color& GetPlaceholderColor() const 101 { 102 return placeholderColor_; 103 } 104 GetFocusPlaceholderColor()105 const Color& GetFocusPlaceholderColor() const 106 { 107 return focusPlaceholderColor_; 108 } 109 GetTextColor()110 const Color& GetTextColor() const 111 { 112 return textColor_; 113 } 114 GetFocusTextColor()115 const Color& GetFocusTextColor() const 116 { 117 return focusTextColor_; 118 } 119 GetTouchColor()120 const Color& GetTouchColor() const 121 { 122 return touchColor_; 123 } 124 GetHoverColor()125 const Color& GetHoverColor() const 126 { 127 return hoverColor_; 128 } 129 GetHeight()130 const Dimension& GetHeight() const 131 { 132 return height_; 133 } 134 GetFontSize()135 const Dimension& GetFontSize() const 136 { 137 return fontSize_; 138 } 139 GetIconSize()140 const Dimension& GetIconSize() const 141 { 142 return iconSize_; 143 } 144 GetCloseIconSize()145 const Dimension& GetCloseIconSize() const 146 { 147 return closeIconSize_; 148 } 149 GetCloseIconHotZoneSize()150 const Dimension& GetCloseIconHotZoneSize() const 151 { 152 return closeIconHotZoneSize_; 153 } 154 GetTextFieldWidthReserved()155 const Dimension& GetTextFieldWidthReserved() const 156 { 157 return textFieldWidthReserved_; 158 } 159 GetLeftPadding()160 const Dimension& GetLeftPadding() const 161 { 162 return leftPadding_; 163 } 164 GetRightPadding()165 const Dimension& GetRightPadding() const 166 { 167 return rightPadding_; 168 } 169 GetFontWeight()170 const FontWeight& GetFontWeight() const 171 { 172 return fontWeight_; 173 } 174 GetBorderRadius()175 const Radius& GetBorderRadius() const 176 { 177 return borderRadius_; 178 } 179 GetBlockRightShade()180 bool GetBlockRightShade() const 181 { 182 return blockRightShade_; 183 } 184 GetDividerSideSpace()185 const Dimension& GetDividerSideSpace() const 186 { 187 return dividerSideSpace_; 188 } 189 GetSearchDividerWidth()190 const Dimension& GetSearchDividerWidth() const 191 { 192 return searchDividerWidth_; 193 } 194 GetSearchButtonTextPadding()195 const Dimension& GetSearchButtonTextPadding() const 196 { 197 return searchButtonTextPadding_; 198 } 199 GetSearchButtonSpace()200 const Dimension& GetSearchButtonSpace() const 201 { 202 return searchButtonSpace_; 203 } 204 GetIconHeight()205 const Dimension& GetIconHeight() const 206 { 207 return iconHeight_; 208 } 209 GetSearchIconLeftSpace()210 const Dimension& GetSearchIconLeftSpace() const 211 { 212 return searchIconLeftSpace_; 213 } 214 GetSearchIconRightSpace()215 const Dimension& GetSearchIconRightSpace() const 216 { 217 return searchIconRightSpace_; 218 } 219 GetSearchDividerColor()220 const Color& GetSearchDividerColor() const 221 { 222 return searchDividerColor_; 223 } 224 GetSearchButtonTextColor()225 const Color& GetSearchButtonTextColor() const 226 { 227 return searchButtonTextColor_; 228 } 229 GetSearchIconColor()230 const Color& GetSearchIconColor() const 231 { 232 return searchIconColor_; 233 } 234 GetCancelButtonStyle()235 const CancelButtonStyle& GetCancelButtonStyle() const 236 { 237 return cancelButtonStyle_; 238 } 239 GetTextStyle()240 TextStyle GetTextStyle() const 241 { 242 return textStyle_; 243 } 244 GetSearchSymbolId()245 uint32_t GetSearchSymbolId() const 246 { 247 return searchSymbolId_; 248 } 249 GetCancelSymbolId()250 uint32_t GetCancelSymbolId() const 251 { 252 return cancelSymbolId_; 253 } 254 GetSymbolIconColor()255 const Color& GetSymbolIconColor() const 256 { 257 return symbolIconColor_; 258 } 259 260 protected: 261 SearchTheme() = default; 262 263 private: 264 Color placeholderColor_; 265 Color focusPlaceholderColor_; 266 Color textColor_; 267 Color focusTextColor_; 268 Color touchColor_; 269 Color hoverColor_; 270 Dimension height_; 271 Dimension fontSize_; 272 Dimension iconSize_; 273 Dimension closeIconSize_; 274 Dimension closeIconHotZoneSize_; 275 Dimension textFieldWidthReserved_; 276 Dimension leftPadding_; 277 Dimension rightPadding_; 278 FontWeight fontWeight_ = FontWeight::NORMAL; 279 Radius borderRadius_; 280 bool blockRightShade_ = false; 281 Dimension dividerSideSpace_; 282 Dimension searchDividerWidth_ = 1.0_px; 283 Dimension searchButtonTextPadding_; 284 Dimension searchButtonSpace_; 285 Dimension iconHeight_; 286 Dimension searchIconLeftSpace_; 287 Dimension searchIconRightSpace_; 288 Color searchDividerColor_; 289 Color searchButtonTextColor_; 290 Color searchIconColor_; 291 CancelButtonStyle cancelButtonStyle_ = CancelButtonStyle::INPUT; 292 TextStyle textStyle_; 293 uint32_t searchSymbolId_ = 0; 294 uint32_t cancelSymbolId_ = 0; 295 Color symbolIconColor_; 296 }; 297 298 } // namespace OHOS::Ace 299 300 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SEARCH_SEARCH_THEME_H 301