1 /* 2 * Copyright (c) 2021-2025 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 protected: 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->rightPaddingWithoutButton_ = 74 pattern->GetAttr<Dimension>("search_text_field_right_padding_without_button", 12.0_vp); 75 theme->fontWeight_ = FontWeight(static_cast<int32_t>(pattern->GetAttr<double>("search_font_weight", 0.0))); 76 theme->borderRadius_ = Radius(pattern->GetAttr<Dimension>("search_text_field_border_radius", 0.0_vp)); 77 theme->blockRightShade_ = static_cast<int32_t>(pattern->GetAttr<double>("search_block_right_shade", 0.0)); 78 theme->placeholderColor_ = pattern->GetAttr<Color>("tips_text_color", Color()); 79 theme->focusPlaceholderColor_ = pattern->GetAttr<Color>("tips_text_color_focused", Color()); 80 theme->textColor_ = pattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color()); 81 theme->focusTextColor_ = pattern->GetAttr<Color>(PATTERN_TEXT_COLOR_FOCUSED, Color()); 82 theme->fontSize_ = pattern->GetAttr<Dimension>(PATTERN_TEXT_SIZE, 0.0_fp); 83 theme->touchColor_ = pattern->GetAttr<Color>("search_touch_color", Color()); 84 theme->hoverColor_ = pattern->GetAttr<Color>("search_hover_color", Color()); 85 theme->searchDividerColor_ = pattern->GetAttr<Color>("search_divider_color", Color()); 86 theme->searchButtonTextColor_ = pattern->GetAttr<Color>("search_button_text_color", Color()); 87 theme->searchButtonTextPadding_ = pattern->GetAttr<Dimension>("search_button_text_padding", Dimension()); 88 theme->searchButtonSpace_ = pattern->GetAttr<Dimension>("search_button_space", Dimension()); 89 theme->dividerSideSpace_ = pattern->GetAttr<Dimension>("search_divider_side_space", Dimension()); 90 theme->searchSymbolId_ = themeConstants->GetSymbolByName("sys.symbol.magnifyingglass"); 91 theme->cancelSymbolId_ = themeConstants->GetSymbolByName("sys.symbol.xmark"); 92 theme->borderColor_ = pattern->GetAttr<Color>("search_border_color", Color()); 93 theme->borderWidth_ = pattern->GetAttr<Dimension>("search_border_width", 0.0_vp); 94 theme->focusBgColor_ = pattern->GetAttr<Color>("search_focus_bg_color", Color()); 95 theme->needFocusBox_ = static_cast<bool>(pattern->GetAttr<double>("search_need_focus_box", 0.0)); 96 theme->cancelButtonStyle_ = static_cast<CancelButtonStyle>( 97 static_cast<int32_t>(pattern->GetAttr<double>("search_cancel_button_style", 2.0f))); 98 theme->searchFocusPadding_ = pattern->GetAttr<Dimension>("search_focus_glow_padding", 0.0_vp); 99 ParsePatternIconTheme(pattern, theme); 100 } 101 ParsePatternIconTheme(const RefPtr<ThemeStyle> & pattern,const RefPtr<SearchTheme> & theme)102 void ParsePatternIconTheme(const RefPtr<ThemeStyle>& pattern, const RefPtr<SearchTheme>& theme) const 103 { 104 theme->searchIconColor_ = pattern->GetAttr<Color>("search_icon_color", Color()); 105 theme->iconHeight_ = pattern->GetAttr<Dimension>("search_icon_height", Dimension()); 106 theme->searchIconLeftSpace_ = pattern->GetAttr<Dimension>("search_icon_left_space", Dimension()); 107 theme->searchIconRightSpace_ = pattern->GetAttr<Dimension>("search_icon_right_space", Dimension()); 108 theme->symbolIconColor_ = pattern->GetAttr<Color>("search_symbol_icon_color", Color()); 109 theme->symbolIconHeight_ = pattern->GetAttr<Dimension>("search_symbol_icon_height", 16.0_fp); 110 theme->focusIconColor_ = pattern->GetAttr<Color>("search_focus_icon_color", Color()); 111 theme->buttonFontSize_ = pattern->GetAttr<Dimension>(PATTERN_TEXT_SIZE, 0.0_fp); 112 if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_EIGHTEEN)) { 113 theme->searchButtonSpace_ = pattern->GetAttr<Dimension>("search_button_space_API16", 4.0_vp); 114 theme->dividerSideSpace_ = pattern->GetAttr<Dimension>("search_button_space_API16", 4.0_vp); 115 theme->searchButtonTextPadding_ = pattern-> 116 GetAttr<Dimension>("search_button_text_padding_API16", 12.0_vp); 117 theme->buttonFontSize_ = pattern->GetAttr<Dimension>("search_font_size_API16", 14.0_fp); 118 } 119 } 120 }; 121 122 ~SearchTheme() override = default; 123 GetPlaceholderColor()124 const Color& GetPlaceholderColor() const 125 { 126 return placeholderColor_; 127 } 128 GetFocusPlaceholderColor()129 const Color& GetFocusPlaceholderColor() const 130 { 131 return focusPlaceholderColor_; 132 } 133 GetTextColor()134 const Color& GetTextColor() const 135 { 136 return textColor_; 137 } 138 GetFocusTextColor()139 const Color& GetFocusTextColor() const 140 { 141 return focusTextColor_; 142 } 143 GetTouchColor()144 const Color& GetTouchColor() const 145 { 146 return touchColor_; 147 } 148 GetHoverColor()149 const Color& GetHoverColor() const 150 { 151 return hoverColor_; 152 } 153 GetHeight()154 const Dimension& GetHeight() const 155 { 156 return height_; 157 } 158 GetFontSize()159 const Dimension& GetFontSize() const 160 { 161 return fontSize_; 162 } 163 GetButtonFontSize()164 const Dimension& GetButtonFontSize() const 165 { 166 return buttonFontSize_; 167 } 168 GetIconSize()169 const Dimension& GetIconSize() const 170 { 171 return iconSize_; 172 } 173 GetCloseIconSize()174 const Dimension& GetCloseIconSize() const 175 { 176 return closeIconSize_; 177 } 178 GetCloseIconHotZoneSize()179 const Dimension& GetCloseIconHotZoneSize() const 180 { 181 return closeIconHotZoneSize_; 182 } 183 GetTextFieldWidthReserved()184 const Dimension& GetTextFieldWidthReserved() const 185 { 186 return textFieldWidthReserved_; 187 } 188 GetLeftPadding()189 const Dimension& GetLeftPadding() const 190 { 191 return leftPadding_; 192 } 193 GetRightPadding()194 const Dimension& GetRightPadding() const 195 { 196 return rightPadding_; 197 } 198 GetFontWeight()199 const FontWeight& GetFontWeight() const 200 { 201 return fontWeight_; 202 } 203 GetBorderRadius()204 const Radius& GetBorderRadius() const 205 { 206 return borderRadius_; 207 } 208 GetBlockRightShade()209 bool GetBlockRightShade() const 210 { 211 return blockRightShade_; 212 } 213 GetDividerSideSpace()214 const Dimension& GetDividerSideSpace() const 215 { 216 return dividerSideSpace_; 217 } 218 GetSearchDividerWidth()219 const Dimension& GetSearchDividerWidth() const 220 { 221 return searchDividerWidth_; 222 } 223 GetSearchButtonTextPadding()224 const Dimension& GetSearchButtonTextPadding() const 225 { 226 return searchButtonTextPadding_; 227 } 228 GetSearchButtonSpace()229 const Dimension& GetSearchButtonSpace() const 230 { 231 return searchButtonSpace_; 232 } 233 GetIconHeight()234 const Dimension& GetIconHeight() const 235 { 236 return iconHeight_; 237 } 238 GetSearchIconLeftSpace()239 const Dimension& GetSearchIconLeftSpace() const 240 { 241 return searchIconLeftSpace_; 242 } 243 GetSearchIconRightSpace()244 const Dimension& GetSearchIconRightSpace() const 245 { 246 return searchIconRightSpace_; 247 } 248 GetSearchDividerColor()249 const Color& GetSearchDividerColor() const 250 { 251 return searchDividerColor_; 252 } 253 GetSearchButtonTextColor()254 const Color& GetSearchButtonTextColor() const 255 { 256 return searchButtonTextColor_; 257 } 258 GetSearchIconColor()259 const Color& GetSearchIconColor() const 260 { 261 return searchIconColor_; 262 } 263 GetCancelButtonStyle()264 const CancelButtonStyle& GetCancelButtonStyle() const 265 { 266 return cancelButtonStyle_; 267 } 268 GetTextStyle()269 TextStyle GetTextStyle() const 270 { 271 return textStyle_; 272 } 273 GetSearchSymbolId()274 uint32_t GetSearchSymbolId() const 275 { 276 return searchSymbolId_; 277 } 278 GetCancelSymbolId()279 uint32_t GetCancelSymbolId() const 280 { 281 return cancelSymbolId_; 282 } 283 GetSymbolIconColor()284 const Color& GetSymbolIconColor() const 285 { 286 return symbolIconColor_; 287 } 288 GetSymbolIconHeight()289 const Dimension& GetSymbolIconHeight() const 290 { 291 return symbolIconHeight_; 292 } 293 GetRightPaddingWithoutButton()294 const Dimension& GetRightPaddingWithoutButton() const 295 { 296 return rightPaddingWithoutButton_; 297 } 298 GetBorderColor()299 const Color& GetBorderColor() const 300 { 301 return borderColor_; 302 } 303 GetBorderWidth()304 const Dimension& GetBorderWidth() const 305 { 306 return borderWidth_; 307 } 308 GetFocusBgColor()309 const Color& GetFocusBgColor() const 310 { 311 return focusBgColor_; 312 } 313 GetFocusIconColor()314 const Color& GetFocusIconColor() const 315 { 316 return focusIconColor_; 317 } 318 NeedFocusBox()319 bool NeedFocusBox() const 320 { 321 return needFocusBox_; 322 } 323 GetSearchFocusPadding()324 const Dimension& GetSearchFocusPadding() const 325 { 326 return searchFocusPadding_; 327 } 328 329 protected: 330 SearchTheme() = default; 331 TextStyle textStyle_; 332 Color textColor_; 333 Color placeholderColor_; 334 Color searchIconColor_; 335 Color searchButtonTextColor_; 336 Color symbolIconColor_; 337 Color focusIconColor_; 338 339 private: 340 Color focusPlaceholderColor_; 341 Color focusTextColor_; 342 Color touchColor_; 343 Color hoverColor_; 344 Dimension height_; 345 Dimension fontSize_; 346 Dimension iconSize_; 347 Dimension closeIconSize_; 348 Dimension closeIconHotZoneSize_; 349 Dimension textFieldWidthReserved_; 350 Dimension leftPadding_; 351 Dimension rightPadding_; 352 FontWeight fontWeight_ = FontWeight::NORMAL; 353 Radius borderRadius_; 354 bool blockRightShade_ = false; 355 Dimension dividerSideSpace_; 356 Dimension searchDividerWidth_ = 1.0_px; 357 Dimension searchButtonTextPadding_; 358 Dimension searchButtonSpace_; 359 Dimension iconHeight_; 360 Dimension searchIconLeftSpace_; 361 Dimension searchIconRightSpace_; 362 Color searchDividerColor_; 363 CancelButtonStyle cancelButtonStyle_ = CancelButtonStyle::INPUT; 364 uint32_t searchSymbolId_ = 0; 365 uint32_t cancelSymbolId_ = 0; 366 Dimension symbolIconHeight_; 367 Dimension rightPaddingWithoutButton_; 368 Color borderColor_; 369 Dimension borderWidth_; 370 Color focusBgColor_; 371 bool needFocusBox_ = false; 372 Dimension searchFocusPadding_; 373 Dimension buttonFontSize_; 374 }; 375 376 } // namespace OHOS::Ace 377 378 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SEARCH_SEARCH_THEME_H 379