• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             theme->height_ = themeConstants->GetDimension(THEME_SEARCH_DEFAULT_HEIGHT);
51             theme->placeholderColor_ = themeConstants->GetColor(THEME_SEARCH_PLACEHOLDER_COLOR);
52             theme->focusPlaceholderColor_ = themeConstants->GetColor(THEME_SEARCH_FOCUS_PLACEHOLDER_COLOR);
53             theme->textColor_ = themeConstants->GetColor(THEME_SEARCH_TEXT_COLOR);
54             theme->focusTextColor_ = themeConstants->GetColor(THEME_SEARCH_FOCUS_TEXT_COLOR);
55             theme->fontSize_ = themeConstants->GetDimension(THEME_SEARCH_FONT_SIZE);
56             theme->iconSize_ = themeConstants->GetDimension(THEME_SEARCH_ICON_SIZE);
57             theme->closeIconSize_ = themeConstants->GetDimension(THEME_SEARCH_CLOSE_ICON_SIZE);
58             theme->closeIconHotZoneSize_ = themeConstants->GetDimension(THEME_SEARCH_CLOSE_ICON_HOT_ZONE_HORIZONTAL);
59             theme->textFieldWidthReserved_ = theme->closeIconHotZoneSize_;
60             theme->leftPadding_ = themeConstants->GetDimension(THEME_SEARCH_TEXT_FIELD_PADDING_LEFT);
61             theme->rightPadding_ = themeConstants->GetDimension(THEME_SEARCH_TEXT_FIELD_PADDING_RIGHT);
62             theme->fontWeight_ = FontWeight(themeConstants->GetInt(THEME_SEARCH_FONT_WEIGHT));
63             theme->borderRadius_ = Radius(themeConstants->GetDimension(THEME_SEARCH_TEXT_FIELD_BORDER_RADIUS));
64             theme->blockRightShade_ = themeConstants->GetInt(THEME_SEARCH_BLOCK_RIGHT_SHADE);
65             ParsePattern(themeConstants->GetThemeStyle(), theme);
66             return theme;
67         }
68 
69     private:
ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<SearchTheme> & theme)70         void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<SearchTheme>& theme) const
71         {
72             if (!themeStyle || !theme) {
73                 return;
74             }
75             auto pattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_SEARCH, nullptr);
76             if (!pattern) {
77                 LOGW("find pattern of search fail");
78                 return;
79             }
80             theme->placeholderColor_ = pattern->GetAttr<Color>("tips_text_color", Color());
81             theme->focusPlaceholderColor_ = pattern->GetAttr<Color>("tips_text_color_focused", Color());
82             theme->textColor_ = pattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color());
83             theme->focusTextColor_ = pattern->GetAttr<Color>(PATTERN_TEXT_COLOR_FOCUSED, Color());
84             theme->fontSize_ = pattern->GetAttr<Dimension>(PATTERN_TEXT_SIZE, 0.0_fp);
85             theme->touchColor_ = pattern->GetAttr<Color>("search_touch_color", Color());
86             theme->hoverColor_ = pattern->GetAttr<Color>("search_hover_color", Color());
87             theme->searchDividerColor_ = pattern->GetAttr<Color>("search_divider_color", Color());
88             theme->searchButtonTextColor_ = pattern->GetAttr<Color>("search_button_text_color", Color());
89             theme->searchIconColor_ = pattern->GetAttr<Color>("search_icon_color", Color());
90             theme->searchButtonTextPadding_ = pattern->GetAttr<Dimension>("search_button_text_padding", Dimension());
91             theme->searchButtonSpace_ = pattern->GetAttr<Dimension>("search_button_space", Dimension());
92             theme->dividerSideSpace_ = pattern->GetAttr<Dimension>("search_divider_side_space", Dimension());
93             theme->iconHeight_ = pattern->GetAttr<Dimension>("search_icon_height", Dimension());
94             theme->searchIconLeftSpace_ = pattern->GetAttr<Dimension>("search_icon_left_space", Dimension());
95             theme->searchIconRightSpace_ = pattern->GetAttr<Dimension>("search_icon_right_space", Dimension());
96         }
97     };
98 
99     ~SearchTheme() override = default;
100 
GetPlaceholderColor()101     const Color& GetPlaceholderColor() const
102     {
103         return placeholderColor_;
104     }
105 
GetFocusPlaceholderColor()106     const Color& GetFocusPlaceholderColor() const
107     {
108         return focusPlaceholderColor_;
109     }
110 
GetTextColor()111     const Color& GetTextColor() const
112     {
113         return textColor_;
114     }
115 
GetFocusTextColor()116     const Color& GetFocusTextColor() const
117     {
118         return focusTextColor_;
119     }
120 
GetTouchColor()121     const Color& GetTouchColor() const
122     {
123         return touchColor_;
124     }
125 
GetHoverColor()126     const Color& GetHoverColor() const
127     {
128         return hoverColor_;
129     }
130 
GetHeight()131     const Dimension& GetHeight() const
132     {
133         return height_;
134     }
135 
GetFontSize()136     const Dimension& GetFontSize() const
137     {
138         return fontSize_;
139     }
140 
GetIconSize()141     const Dimension& GetIconSize() const
142     {
143         return iconSize_;
144     }
145 
GetCloseIconSize()146     const Dimension& GetCloseIconSize() const
147     {
148         return closeIconSize_;
149     }
150 
GetCloseIconHotZoneSize()151     const Dimension& GetCloseIconHotZoneSize() const
152     {
153         return closeIconHotZoneSize_;
154     }
155 
GetTextFieldWidthReserved()156     const Dimension& GetTextFieldWidthReserved() const
157     {
158         return textFieldWidthReserved_;
159     }
160 
GetLeftPadding()161     const Dimension& GetLeftPadding() const
162     {
163         return leftPadding_;
164     }
165 
GetRightPadding()166     const Dimension& GetRightPadding() const
167     {
168         return rightPadding_;
169     }
170 
GetFontWeight()171     const FontWeight& GetFontWeight() const
172     {
173         return fontWeight_;
174     }
175 
GetBorderRadius()176     const Radius& GetBorderRadius() const
177     {
178         return borderRadius_;
179     }
180 
GetBlockRightShade()181     bool GetBlockRightShade() const
182     {
183         return blockRightShade_;
184     }
185 
GetDividerSideSpace()186     const Dimension& GetDividerSideSpace() const
187     {
188         return dividerSideSpace_;
189     }
190 
GetSearchDividerWidth()191     const Dimension& GetSearchDividerWidth() const
192     {
193         return searchDividerWidth_;
194     }
195 
GetSearchButtonTextPadding()196     const Dimension& GetSearchButtonTextPadding() const
197     {
198         return searchButtonTextPadding_;
199     }
200 
GetSearchButtonSpace()201     const Dimension& GetSearchButtonSpace() const
202     {
203         return searchButtonSpace_;
204     }
205 
GetIconHeight()206     const Dimension& GetIconHeight() const
207     {
208         return iconHeight_;
209     }
210 
GetSearchIconLeftSpace()211     const Dimension& GetSearchIconLeftSpace() const
212     {
213         return searchIconLeftSpace_;
214     }
215 
GetSearchIconRightSpace()216     const Dimension& GetSearchIconRightSpace() const
217     {
218         return searchIconRightSpace_;
219     }
220 
GetSearchDividerColor()221     const Color& GetSearchDividerColor() const
222     {
223         return searchDividerColor_;
224     }
225 
GetSearchButtonTextColor()226     const Color& GetSearchButtonTextColor() const
227     {
228         return searchButtonTextColor_;
229     }
230 
GetSearchIconColor()231     const Color& GetSearchIconColor() const
232     {
233         return searchIconColor_;
234     }
235 
GetCancelButtonStyle()236     const CancelButtonStyle& GetCancelButtonStyle() const
237     {
238         return cancelButtonStyle_;
239     }
240 
241 protected:
242     SearchTheme() = default;
243 
244 private:
245     Color placeholderColor_;
246     Color focusPlaceholderColor_;
247     Color textColor_;
248     Color focusTextColor_;
249     Color touchColor_;
250     Color hoverColor_;
251     Dimension height_;
252     Dimension fontSize_;
253     Dimension iconSize_;
254     Dimension closeIconSize_;
255     Dimension closeIconHotZoneSize_;
256     Dimension textFieldWidthReserved_;
257     Dimension leftPadding_;
258     Dimension rightPadding_;
259     FontWeight fontWeight_ = FontWeight::NORMAL;
260     Radius borderRadius_;
261     bool blockRightShade_ = false;
262     Dimension dividerSideSpace_;
263     Dimension searchDividerWidth_ = 1.0_px;
264     Dimension searchButtonTextPadding_;
265     Dimension searchButtonSpace_;
266     Dimension iconHeight_;
267     Dimension searchIconLeftSpace_;
268     Dimension searchIconRightSpace_;
269     Color searchDividerColor_;
270     Color searchButtonTextColor_;
271     Color searchIconColor_;
272     CancelButtonStyle cancelButtonStyle_ = CancelButtonStyle::INPUT;
273 };
274 
275 } // namespace OHOS::Ace
276 
277 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SEARCH_SEARCH_THEME_H
278