• 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_TEXT_FIELD_TEXTFIELD_THEME_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_FIELD_TEXTFIELD_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 
28 namespace OHOS::Ace {
29 
30 /**
31  * TextFieldTheme defines color and styles of SliderComponent. TextFieldTheme should be built
32  * using TextFieldTheme::Builder.
33  */
34 class TextFieldTheme : public virtual Theme {
35     DECLARE_ACE_TYPE(TextFieldTheme, Theme);
36 
37 public:
38     class Builder {
39     public:
40         Builder() = default;
41         ~Builder() = default;
42 
Build(const RefPtr<ThemeConstants> & themeConstants)43         RefPtr<TextFieldTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
44         {
45             RefPtr<TextFieldTheme> theme = AceType::Claim(new TextFieldTheme());
46             if (!themeConstants) {
47                 return theme;
48             }
49             // init theme from global data
50             theme->padding_ = Edge(themeConstants->GetDimension(THEME_TEXTFIELD_PADDING_HORIZONTAL),
51                 themeConstants->GetDimension(THEME_TEXTFIELD_PADDING_VERTICAL),
52                 themeConstants->GetDimension(THEME_TEXTFIELD_PADDING_HORIZONTAL),
53                 themeConstants->GetDimension(THEME_TEXTFIELD_PADDING_VERTICAL));
54             theme->height_ = themeConstants->GetDimension(THEME_TEXTFIELD_HEIGHT);
55             theme->fontSize_ = themeConstants->GetDimension(THEME_TEXTFIELD_FONT_SIZE);
56             theme->fontWeight_ = FontWeight(themeConstants->GetInt(THEME_TEXTFIELD_FONT_WEIGHT));
57             theme->borderRadius_ = Radius(themeConstants->GetDimension(THEME_TEXTFIELD_BORDER_RADIUS));
58 
59             theme->bgColor_ = themeConstants->GetColor(THEME_TEXTFIELD_BACKGROUND_COLOR);
60             theme->placeholderColor_ = themeConstants->GetColor(THEME_TEXTFIELD_PLACEHOLDER_COLOR);
61             theme->textColor_ = themeConstants->GetColor(THEME_TEXTFIELD_TEXT_COLOR);
62             theme->selectedColor_ = themeConstants->GetColor(THEME_TEXTFIELD_TEXT_SELECTED_COLOR);
63             theme->hoverColor_ = themeConstants->GetColor(THEME_TEXTFIELD_HOVER_COLOR);
64             theme->pressColor_ = themeConstants->GetColor(THEME_TEXTFIELD_PRESS_COLOR);
65 
66             theme->focusBgColor_ = themeConstants->GetColor(THEME_TEXTFIELD_FOCUS_BACKGROUND_COLOR);
67             theme->focusPlaceholderColor_ = themeConstants->GetColor(THEME_TEXTFIELD_FOCUS_PLACEHOLDER_COLOR);
68             theme->focusTextColor_ = themeConstants->GetColor(THEME_TEXTFIELD_FOCUS_TEXT_COLOR);
69 
70             theme->disableOpacityRatio_ = themeConstants->GetDouble(THEME_TEXTFIELD_DISABLE_OPACITY_RATIO);
71             theme->disableTextColor_ = themeConstants->GetColor(THEME_TEXTFIELD_TEXT_COLOR_DISABLE);
72 
73             theme->overHideLength_ = themeConstants->GetDimension(THEME_TEXTFIELD_OVER_HIDE_LENGTH);
74 
75             theme->cursorColor_ = themeConstants->GetColor(THEME_TEXTFIELD_CURSOR_COLOR);
76             theme->cursorRadius_ = themeConstants->GetDimension(THEME_TEXTFIELD_CURSOR_RADIUS);
77             theme->needFade_ = static_cast<bool>(themeConstants->GetInt(THEME_TEXTFIELD_NEED_FADE));
78 
79             theme->iconSize_ = themeConstants->GetDimension(THEME_TEXTFIELD_ICON_SIZE);
80             theme->iconHotZoneSize_ = themeConstants->GetDimension(THEME_TEXTFIELD_ICON_HOT_ZONE_SIZE);
81 
82             theme->showEllipsis_ = static_cast<bool>(themeConstants->GetInt(THEME_TEXTFIELD_SHOW_ELLIPSIS));
83 
84             theme->errorSpacing_ = themeConstants->GetDimension(THEME_TEXTFIELD_ERROR_SPACING);
85             theme->errorIsInner_ = static_cast<bool>(themeConstants->GetInt(THEME_TEXTFIELD_ERROR_IS_INNER));
86             theme->errorBorderWidth_ = themeConstants->GetDimension(THEME_TEXTFIELD_ERROR_BORDER_WIDTH);
87             theme->errorBorderColor_ = themeConstants->GetColor(THEME_TEXTFIELD_ERROR_BORDER_COLOR);
88             theme->errorTextStyle_.SetTextColor(themeConstants->GetColor(THEME_TEXTFIELD_ERROR_COLOR));
89             theme->errorTextStyle_.SetFontSize(themeConstants->GetDimension(THEME_TEXTFIELD_ERROR_FONT_SIZE));
90             theme->errorTextStyle_.SetFontWeight(FontWeight(themeConstants->GetInt(THEME_TEXTFIELD_ERROR_FONT_WEIGHT)));
91 
92             theme->countTextStyle_.SetTextColor(themeConstants->GetColor(THEME_TEXTFIELD_COUNT_TEXT_COLOR));
93             theme->countTextStyle_.SetFontSize(themeConstants->GetDimension(THEME_TEXTFIELD_COUNT_FONT_SIZE));
94             theme->countTextStyle_.SetFontWeight(
95                 FontWeight(themeConstants->GetInt(THEME_TEXTFIELD_COUNT_FONT_WEIGHT)));
96             theme->overCountStyle_.SetTextColor(themeConstants->GetColor(THEME_TEXTFIELD_OVER_COUNT_TEXT_COLOR));
97             theme->overCountStyle_.SetFontSize(themeConstants->GetDimension(THEME_TEXTFIELD_OVER_COUNT_FONT_SIZE));
98             theme->overCountStyle_.SetFontWeight(
99                 FontWeight(themeConstants->GetInt(THEME_TEXTFIELD_OVER_COUNT_FONT_WEIGHT)));
100 
101             theme->countTextStyleOuter_.SetTextColor(themeConstants->GetColor(THEME_TEXTFIELD_COUNT_TEXT_COLOR));
102             theme->countTextStyleOuter_.SetFontSize(
103                 themeConstants->GetDimension(THEME_TEXTFIELD_COUNT_FONT_SIZE_OUTER));
104             theme->countTextStyleOuter_.SetFontWeight(
105                 FontWeight(themeConstants->GetInt(THEME_TEXTFIELD_COUNT_FONT_WEIGHT)));
106             theme->overCountStyleOuter_.SetTextColor(themeConstants->GetColor(THEME_TEXTFIELD_OVER_COUNT_TEXT_COLOR));
107             theme->overCountStyleOuter_.SetFontSize(
108                 themeConstants->GetDimension(THEME_TEXTFIELD_COUNT_FONT_SIZE_OUTER));
109             theme->overCountStyleOuter_.SetFontWeight(
110                 FontWeight(themeConstants->GetInt(THEME_TEXTFIELD_OVER_COUNT_FONT_WEIGHT)));
111 
112             ParsePattern(themeConstants->GetThemeStyle(), theme);
113             return theme;
114         }
115 
116     private:
ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<TextFieldTheme> & theme)117         void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<TextFieldTheme>& theme) const
118         {
119             if (!themeStyle || !theme) {
120                 return;
121             }
122             theme->fontSize_ = themeStyle->GetAttr<Dimension>(TEXTFIELD_FONT_SIZE, 0.0_fp);
123             theme->textColor_ = themeStyle->GetAttr<Color>(TEXTFIELD_TEXT_COLOR, Color());
124             theme->focusTextColor_ = themeStyle->GetAttr<Color>(TEXTFIELD_TEXT_COLOR, Color());
125             theme->placeholderColor_ = themeStyle->GetAttr<Color>(TEXTFIELD_HINT_TEXT_COLOR, Color());
126             theme->focusPlaceholderColor_ = themeStyle->GetAttr<Color>(TEXTFIELD_HINT_TEXT_COLOR, Color());
127             theme->bgColor_ = themeStyle->GetAttr<Color>(TEXTFIELD_BACKGROUND_COLOR, Color());
128             theme->focusBgColor_ = themeStyle->GetAttr<Color>(TEXTFIELD_BACKGROUND_COLOR, Color());
129             // color of error border blend 60% opacity
130             theme->errorBorderColor_ = themeStyle->GetAttr<Color>(TEXTFIELD_ERROR_COLOR, Color()).BlendOpacity(0.6);
131             theme->errorTextStyle_.SetTextColor(themeStyle->GetAttr<Color>(TEXTFIELD_ERROR_COLOR, Color()));
132             theme->errorTextStyle_.SetFontSize(themeStyle->GetAttr<Dimension>(TEXTFIELD_ERROR_TEXT_FONT_SIZE, 0.0_fp));
133 
134             theme->countTextStyle_.SetTextColor(themeStyle->GetAttr<Color>(TEXTFIELD_COUNT_TEXT_COLOR, Color()));
135             theme->countTextStyle_.SetFontSize(themeStyle->GetAttr<Dimension>(TEXTFIELD_COUNT_TEXT_FONT_SIZE, 0.0_fp));
136             theme->overCountStyle_.SetTextColor(themeStyle->GetAttr<Color>(TEXTFIELD_ERROR_COLOR, Color()));
137             theme->overCountStyle_.SetFontSize(themeStyle->GetAttr<Dimension>(TEXTFIELD_COUNT_TEXT_FONT_SIZE, 0.0_fp));
138 
139             theme->countTextStyleOuter_.SetTextColor(themeStyle->GetAttr<Color>(TEXTFIELD_COUNT_TEXT_COLOR, Color()));
140             theme->countTextStyleOuter_.SetFontSize(
141                 themeStyle->GetAttr<Dimension>(TEXTFIELD_COUNT_TEXT_FONT_SIZE_OUTER, 0.0_fp));
142             theme->overCountStyleOuter_.SetTextColor(themeStyle->GetAttr<Color>(TEXTFIELD_ERROR_COLOR, Color()));
143             theme->overCountStyleOuter_.SetFontSize(
144                 themeStyle->GetAttr<Dimension>(TEXTFIELD_COUNT_TEXT_FONT_SIZE_OUTER, 0.0_fp));
145 
146             theme->selectedColor_ = themeStyle->GetAttr<Color>(TEXTFIELD_SELECTED_COLOR, Color());
147             theme->disableTextColor_ = themeStyle->GetAttr<Color>(TEXTFIELD_TEXT_COLOR_DISABLE, Color());
148             theme->cursorColor_ = themeStyle->GetAttr<Color>(TEXTFIELD_CURSOR_COLOR, Color());
149             theme->hoverColor_ = themeStyle->GetAttr<Color>(TEXTFIELD_HOVER_COLOR, Color());
150             theme->pressColor_ = themeStyle->GetAttr<Color>(TEXTFIELD_PRESS_COLOR, Color());
151         }
152     };
153 
154     ~TextFieldTheme() override = default;
155 
GetPadding()156     const Edge& GetPadding() const
157     {
158         return padding_;
159     }
160 
GetHeight()161     const Dimension& GetHeight() const
162     {
163         return height_;
164     }
165 
GetFontSize()166     const Dimension& GetFontSize() const
167     {
168         return fontSize_;
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 
GetBgColor()181     const Color& GetBgColor() const
182     {
183         return bgColor_;
184     }
185 
GetPlaceholderColor()186     const Color& GetPlaceholderColor() const
187     {
188         return placeholderColor_;
189     }
190 
GetFocusBgColor()191     const Color& GetFocusBgColor() const
192     {
193         return focusBgColor_;
194     }
195 
GetFocusPlaceholderColor()196     const Color& GetFocusPlaceholderColor() const
197     {
198         return focusPlaceholderColor_;
199     }
200 
GetFocusTextColor()201     const Color& GetFocusTextColor() const
202     {
203         return focusTextColor_;
204     }
205 
GetTextColor()206     const Color& GetTextColor() const
207     {
208         return textColor_;
209     }
210 
GetDisableTextColor()211     const Color& GetDisableTextColor() const
212     {
213         return disableTextColor_;
214     }
215 
GetSelectedColor()216     const Color& GetSelectedColor() const
217     {
218         return selectedColor_;
219     }
220 
GetHoverColor()221     const Color& GetHoverColor() const
222     {
223         return hoverColor_;
224     }
225 
GetPressColor()226     const Color& GetPressColor() const
227     {
228         return pressColor_;
229     }
230 
GetDisableOpacityRatio()231     double GetDisableOpacityRatio() const
232     {
233         return disableOpacityRatio_;
234     }
235 
GetOverHideLength()236     const Dimension& GetOverHideLength() const
237     {
238         return overHideLength_;
239     }
240 
GetCursorColor()241     const Color& GetCursorColor() const
242     {
243         return cursorColor_;
244     }
245 
GetCursorRadius()246     const Dimension& GetCursorRadius() const
247     {
248         return cursorRadius_;
249     }
250 
NeedFade()251     bool NeedFade() const
252     {
253         return needFade_;
254     }
255 
GetIconSize()256     const Dimension& GetIconSize() const
257     {
258         return iconSize_;
259     }
260 
GetIconHotZoneSize()261     const Dimension& GetIconHotZoneSize() const
262     {
263         return iconHotZoneSize_;
264     }
265 
ShowEllipsis()266     bool ShowEllipsis() const
267     {
268         return showEllipsis_;
269     }
270 
GetErrorSpacing()271     const Dimension& GetErrorSpacing() const
272     {
273         return errorSpacing_;
274     }
275 
GetErrorIsInner()276     bool GetErrorIsInner() const
277     {
278         return errorIsInner_;
279     }
280 
GetErrorBorderWidth()281     const Dimension& GetErrorBorderWidth() const
282     {
283         return errorBorderWidth_;
284     }
285 
GetErrorBorderColor()286     const Color& GetErrorBorderColor() const
287     {
288         return errorBorderColor_;
289     }
290 
GetErrorTextStyle()291     const TextStyle& GetErrorTextStyle() const
292     {
293         return errorTextStyle_;
294     }
295 
GetCountTextStyle()296     const TextStyle& GetCountTextStyle() const
297     {
298         return countTextStyle_;
299     }
300 
GetOverCountStyle()301     const TextStyle& GetOverCountStyle() const
302     {
303         return overCountStyle_;
304     }
305 
GetCountTextStyleOuter()306     const TextStyle& GetCountTextStyleOuter() const
307     {
308         return countTextStyleOuter_;
309     }
310 
GetOverCountStyleOuter()311     const TextStyle& GetOverCountStyleOuter() const
312     {
313         return overCountStyleOuter_;
314     }
315 
316 protected:
317     TextFieldTheme() = default;
318 
319 private:
320     Edge padding_;
321     Dimension height_;
322     Dimension fontSize_;
323     FontWeight fontWeight_ = FontWeight::NORMAL;
324     Radius borderRadius_;
325 
326     Color bgColor_;
327     Color placeholderColor_;
328     Color focusBgColor_;
329     Color focusPlaceholderColor_;
330     Color focusTextColor_;
331     Color textColor_;
332     Color disableTextColor_;
333     Color selectedColor_;
334     Color hoverColor_;
335     Color pressColor_;
336     Dimension errorSpacing_;
337     bool errorIsInner_ = false;
338     Dimension errorBorderWidth_;
339     Color errorBorderColor_;
340     TextStyle errorTextStyle_;
341     TextStyle countTextStyle_;
342     TextStyle overCountStyle_;
343     TextStyle countTextStyleOuter_;
344     TextStyle overCountStyleOuter_;
345 
346     // UX::disable state: opacity is set to 38% of the default
347     double disableOpacityRatio_ = 1.0;
348 
349     // UX::over length state
350     Dimension overHideLength_;
351 
352     // UX::cursor state cursor-color=#000000, cursor blur-radius=0.9, cursor-width=2, cursor-height=24, cursor-radius=1
353     Color cursorColor_;
354     Dimension cursorRadius_;
355     bool needFade_ = false;
356 
357     // UX::icon size = 24, hotZoneSize = 36
358     Dimension iconSize_;
359     Dimension iconHotZoneSize_;
360 
361     bool showEllipsis_ = true;
362 };
363 
364 } // namespace OHOS::Ace
365 
366 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_FIELD_TEXTFIELD_THEME_H
367