• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::MakeRefPtr<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 
GetTextDecoration()274     TextDecoration GetTextDecoration() const
275     {
276         return textStyle_.GetTextDecoration().size() > 0 ?
277             textStyle_.GetTextDecoration()[0] : TextDecoration::NONE;
278     }
279 
GetSearchSymbolId()280     uint32_t GetSearchSymbolId() const
281     {
282         return searchSymbolId_;
283     }
284 
GetCancelSymbolId()285     uint32_t GetCancelSymbolId() const
286     {
287         return cancelSymbolId_;
288     }
289 
GetSymbolIconColor()290     const Color& GetSymbolIconColor() const
291     {
292         return symbolIconColor_;
293     }
294 
GetSymbolIconHeight()295     const Dimension& GetSymbolIconHeight() const
296     {
297         return symbolIconHeight_;
298     }
299 
GetRightPaddingWithoutButton()300     const Dimension& GetRightPaddingWithoutButton() const
301     {
302         return rightPaddingWithoutButton_;
303     }
304 
GetBorderColor()305     const Color& GetBorderColor() const
306     {
307         return borderColor_;
308     }
309 
GetBorderWidth()310     const Dimension& GetBorderWidth() const
311     {
312         return borderWidth_;
313     }
314 
GetFocusBgColor()315     const Color& GetFocusBgColor() const
316     {
317         return focusBgColor_;
318     }
319 
GetFocusIconColor()320     const Color& GetFocusIconColor() const
321     {
322         return focusIconColor_;
323     }
324 
NeedFocusBox()325     bool NeedFocusBox() const
326     {
327         return needFocusBox_;
328     }
329 
GetSearchFocusPadding()330     const Dimension& GetSearchFocusPadding() const
331     {
332         return searchFocusPadding_;
333     }
334 
335 protected:
336     SearchTheme() = default;
337     TextStyle textStyle_;
338     Color textColor_;
339     Color placeholderColor_;
340     Color searchIconColor_;
341     Color searchButtonTextColor_;
342     Color symbolIconColor_;
343     Color focusIconColor_;
344 
345 private:
346     Color focusPlaceholderColor_;
347     Color focusTextColor_;
348     Color touchColor_;
349     Color hoverColor_;
350     Dimension height_;
351     Dimension fontSize_;
352     Dimension iconSize_;
353     Dimension closeIconSize_;
354     Dimension closeIconHotZoneSize_;
355     Dimension textFieldWidthReserved_;
356     Dimension leftPadding_;
357     Dimension rightPadding_;
358     FontWeight fontWeight_ = FontWeight::NORMAL;
359     Radius borderRadius_;
360     bool blockRightShade_ = false;
361     Dimension dividerSideSpace_;
362     Dimension searchDividerWidth_ = 1.0_px;
363     Dimension searchButtonTextPadding_;
364     Dimension searchButtonSpace_;
365     Dimension iconHeight_;
366     Dimension searchIconLeftSpace_;
367     Dimension searchIconRightSpace_;
368     Color searchDividerColor_;
369     CancelButtonStyle cancelButtonStyle_ = CancelButtonStyle::INPUT;
370     uint32_t searchSymbolId_ = 0;
371     uint32_t cancelSymbolId_ = 0;
372     Dimension symbolIconHeight_;
373     Dimension rightPaddingWithoutButton_;
374     Color borderColor_;
375     Dimension borderWidth_;
376     Color focusBgColor_;
377     bool needFocusBox_ = false;
378     Dimension searchFocusPadding_;
379     Dimension buttonFontSize_;
380 };
381 
382 } // namespace OHOS::Ace
383 
384 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SEARCH_SEARCH_THEME_H
385