1 /*
2  * Copyright (c) 2022 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_INDEXER_THEME_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_INDEXER_THEME_H
18 
19 #include <string>
20 #include "base/geometry/dimension.h"
21 #include "base/memory/ace_type.h"
22 #include "base/memory/referenced.h"
23 #include "core/common/container.h"
24 #include "core/components/common/properties/color.h"
25 #include "core/components/common/properties/decoration.h"
26 #include "core/components/common/properties/text_style.h"
27 #include "core/components/theme/theme.h"
28 #include "core/components/theme/theme_attributes.h"
29 #include "core/components/theme/theme_constants.h"
30 #include "core/components/theme/theme_constants_defines.h"
31 #include "core/components/theme/theme_style.h"
32 
33 namespace OHOS::Ace {
34 class IndexerTheme : public virtual Theme {
35     DECLARE_ACE_TYPE(IndexerTheme, Theme);
36 
37 public:
38     ~IndexerTheme() = default;
39     class Builder {
40     public:
41         Builder() = default;
42         ~Builder() = default;
Build(const RefPtr<ThemeConstants> & themeConstants)43         RefPtr<IndexerTheme> Build(const RefPtr<ThemeConstants>& themeConstants)
44         {
45             RefPtr<IndexerTheme> theme = AceType::Claim(new IndexerTheme());
46             if (!themeConstants) {
47                 LOGE("Build AppTheme error, themeConstants is null!");
48                 return theme;
49             }
50             ParsePattern(themeConstants, theme);
51             return theme;
52         }
53     };
54 
GetDefaultTextColor()55     const Color& GetDefaultTextColor() const
56     {
57         return defaultTextColor_;
58     }
GetSelectedTextColor()59     const Color& GetSelectedTextColor() const
60     {
61         return selectedTextColor_;
62     }
GetPopupTextColor()63     const Color& GetPopupTextColor() const
64     {
65         return popupTextColor_;
66     }
GetPopupTextSize()67     const Dimension& GetPopupTextSize() const
68     {
69         return popupTextSize_;
70     }
GetPopupBackgroundColor()71     const Color& GetPopupBackgroundColor() const
72     {
73         return popupBackgroundColor_;
74     }
GetPopupSeparateColor()75     const Color& GetPopupSeparateColor() const
76     {
77         return popupSeparateColor_;
78     }
GetSeclectedBackgroundColor()79     const Color& GetSeclectedBackgroundColor() const
80     {
81         return selectedBackgroundColor_;
82     }
GetPopupAreaSize()83     const Dimension& GetPopupAreaSize() const
84     {
85         return popupAreaSize_;
86     }
GetHoverRadiusSize()87     const Dimension& GetHoverRadiusSize() const
88     {
89         return hoverRadiusSize_;
90     }
GetHoverBgAreaSize()91     const Dimension& GetHoverBgAreaSize() const
92     {
93         return hoverBgAreaSize_;
94     }
GetHoverBgAreaColor()95     const Color& GetHoverBgAreaColor() const
96     {
97         return hoverBgAreaColor_;
98     }
GetHoverTextSize()99     const Dimension& GetHoverTextSize() const
100     {
101         return hoverTextSize_;
102     }
GetHoverTextAlpha()103     const Dimension& GetHoverTextAlpha() const
104     {
105         return hoverTextAlpha_;
106     }
GetSlipHoverBackgroundColor()107     const Color& GetSlipHoverBackgroundColor() const
108     {
109         return slipHoverBackgroundColor_;
110     }
GetFocusBgOutlineColor()111     const Color& GetFocusBgOutlineColor() const
112     {
113         return focusBgOutlineColor_;
114     }
GetFocusBgOutlineSize()115     const Dimension& GetFocusBgOutlineSize() const
116     {
117         return focusBgOutlineSize_;
118     }
GetSelectTextStyle()119     const TextStyle& GetSelectTextStyle() const
120     {
121         return seletctTextStyle_;
122     }
GetDefaultTextStyle()123     const TextStyle& GetDefaultTextStyle() const
124     {
125         return defaultTextStyle_;
126     }
GetPopupTextStyle()127     const TextStyle& GetPopupTextStyle() const
128     {
129         return popupTextStyle_;
130     }
GetPopupSelectedTextColor()131     const Color& GetPopupSelectedTextColor() const
132     {
133         return popupSelectedTextColor_;
134     }
GetPopupUnselectedTextColor()135     const Color& GetPopupUnselectedTextColor() const
136     {
137         return popupUnselectedTextColor_;
138     }
GetPressedBgAreaColor()139     const Color& GetPressedBgAreaColor() const
140     {
141         return pressedBgAreaColor_;
142     }
GetSlipPressedBackgroundColor()143     const Color& GetSlipPressedBackgroundColor() const
144     {
145         return slipPressedBackgroundColor_;
146     }
GetPopupClickedBgAreaColor()147     const Color& GetPopupClickedBgAreaColor() const
148     {
149         return popupClickedBgAreaColor_;
150     }
GetPopupTitleBackground()151     const Color& GetPopupTitleBackground() const
152     {
153         return popupTitleBackground_;
154     }
GetPopupUnclickedBgAreaColor()155     const Color& GetPopupUnclickedBgAreaColor() const
156     {
157         return popupUnclickedBgAreaColor_;
158     }
159     static constexpr double SLIP_BACKGROUND_OPACITY = 0.05;
160     static constexpr double SLIP_PRESS_BACKGROUND_OPACITY = 0.1;
161     static constexpr float TEXT_PADDING_LEFT = 12.0f;
162     static constexpr float TEXT_PADDING_TOP = 6.0f;
163 
164 protected:
165     IndexerTheme() = default;
166 
167     Color defaultTextColor_;
168     Color selectedTextColor_;
169     Color popupTextColor_;
170     Dimension popupTextSize_;
171     Color selectedBackgroundColor_;
172     Color popupBackgroundColor_;
173     Color popupSeparateColor_;
174     Color popupSelectedTextColor_;
175     Color popupUnselectedTextColor_;
176     Dimension popupAreaSize_;
177     Dimension hoverRadiusSize_;
178     Dimension hoverBgAreaSize_;
179     Color hoverBgAreaColor_;
180     Dimension hoverTextSize_;
181     Dimension hoverTextAlpha_;
182     Color slipHoverBackgroundColor_;
183     Color focusBgOutlineColor_;
184     Dimension focusBgOutlineSize_;
185     TextStyle seletctTextStyle_;
186     TextStyle defaultTextStyle_;
187     TextStyle popupTextStyle_;
188     Color pressedBgAreaColor_;
189     Color slipPressedBackgroundColor_;
190     Color popupClickedBgAreaColor_;
191     Color popupTitleBackground_;
192     Color popupUnclickedBgAreaColor_;
193 
194 private:
ParseColorAttributes(const RefPtr<ThemeStyle> & indexerPattern,const RefPtr<IndexerTheme> & theme)195     static void ParseColorAttributes(const RefPtr<ThemeStyle>& indexerPattern, const RefPtr<IndexerTheme>& theme)
196     {
197         theme->defaultTextColor_ = indexerPattern->GetAttr<Color>("default_text_color", Color(DEFAULT_TEXT_COLOR));
198         theme->selectedTextColor_ = indexerPattern->GetAttr<Color>("selected_text_color", Color(SELECT_TEXT_COLOR));
199         theme->popupTextColor_ = indexerPattern->GetAttr<Color>("popup_text_color", Color(POPUP_TEXT_COLOR));
200         theme->selectedBackgroundColor_ =
201             indexerPattern->GetAttr<Color>("selected_background_color", Color(SELECT_BACKGROUD_COLOR)).ChangeOpacity(
202                 indexerPattern->GetAttr<double>("selected_background_color_opacity", SELECT_BACKGROUND_OPACITY));
203         theme->popupSeparateColor_ =
204             indexerPattern->GetAttr<Color>("popup_separator_color", Color(POPUP_SEPARATOR_COLOR));
205         theme->popupSelectedTextColor_ =
206             indexerPattern->GetAttr<Color>("popup_selected_text_color", Color(POPUP_SELECTED_TEXT_COLOR));
207         theme->popupUnselectedTextColor_ =
208             indexerPattern->GetAttr<Color>("popup_unselected_text_color", Color(POPUP_UNSELECTED_TEXT_COLOR));
209         theme->hoverBgAreaColor_ = indexerPattern->GetAttr<Color>("hover_bg_area_color", Color(HOVER_BG_AREA_COLOR));
210         theme->slipHoverBackgroundColor_ =
211             indexerPattern->GetAttr<Color>("slip_hover_background_color", Color(SLIP_HOVER_BACKGROUD_COLOR));
212         theme->focusBgOutlineColor_ =
213             indexerPattern->GetAttr<Color>("focus_bg_outline_color", Color(FOCUS_BG_OUTLINE_COLOR));
214         theme->pressedBgAreaColor_ =
215             indexerPattern->GetAttr<Color>("pressed_bg_area_color", Color(PRESSED_BG_AREA_COLOR));
216         theme->slipPressedBackgroundColor_ =
217             indexerPattern->GetAttr<Color>("slip_pressed_background_color", Color(SLIP_PRESSED_BACKGROUD_COLOR));
218         theme->popupClickedBgAreaColor_ =
219             indexerPattern->GetAttr<Color>("popup_clicked_bg_area_color", Color(POPUP_CLICKED_BG_AREA_COLOR));
220         theme->popupTitleBackground_ =
221             indexerPattern->GetAttr<Color>("popup_title_color", Color(POPUP_TITLE_BG_AREA_COLOR));
222         theme->popupUnclickedBgAreaColor_ =
223             indexerPattern->GetAttr<Color>("popup_unclicked_bg_area_color", Color(POPUP_UNCLICKED_BG_AREA_COLOR));
224     }
225 
ParseDimensionAttributes(const RefPtr<ThemeStyle> & indexerPattern,const RefPtr<IndexerTheme> & theme)226     static void ParseDimensionAttributes(const RefPtr<ThemeStyle>& indexerPattern,
227         const RefPtr<IndexerTheme>& theme)
228     {
229         theme->popupTextSize_ =
230             indexerPattern->GetAttr<Dimension>("popup_text_size", Dimension(POPUP_TEXT_SIZE, DimensionUnit::FP));
231         theme->popupAreaSize_ =
232             indexerPattern->GetAttr<Dimension>("popup_area_size", Dimension(POPUP_AREA_SIZE, DimensionUnit::VP));
233         theme->hoverRadiusSize_ =
234             indexerPattern->GetAttr<Dimension>("hover_radius_size", Dimension(HOVER_RADIUS_SIZE, DimensionUnit::VP));
235         theme->hoverBgAreaSize_ =
236             indexerPattern->GetAttr<Dimension>("hover_bg_area_size", Dimension(HOVER_BG_AREA_SIZE, DimensionUnit::VP));
237         theme->hoverTextAlpha_ =
238             indexerPattern->GetAttr<Dimension>("hover_text_alpha", Dimension(HOVER_TEXT_ALPHA, DimensionUnit::PX));
239         theme->focusBgOutlineSize_ = indexerPattern->GetAttr<Dimension>(
240             "focus_bg_outline_size", Dimension(FOCUS_BG_OUTLINE_SIZE, DimensionUnit::VP));
241     }
242 
ParseTextStyleAttributes(const RefPtr<ThemeStyle> & indexerPattern,const RefPtr<IndexerTheme> & theme)243     static void ParseTextStyleAttributes(const RefPtr<ThemeStyle>& indexerPattern,
244         const RefPtr<IndexerTheme>& theme)
245     {
246         std::vector<std::string> fontFamilies;
247         std::string defaultFamily = "HarmonyOS Sans";
248         fontFamilies.emplace_back(defaultFamily);
249         theme->seletctTextStyle_.SetTextColor(
250             indexerPattern->GetAttr<Color>("selected_text_color", Color(SELECT_TEXT_COLOR)));
251         theme->seletctTextStyle_.SetFontStyle(FontStyle::NORMAL);
252         theme->seletctTextStyle_.SetFontFamilies(fontFamilies);
253         theme->defaultTextStyle_.SetTextColor(
254             indexerPattern->GetAttr<Color>("default_text_color", Color(DEFAULT_TEXT_COLOR)));
255         theme->defaultTextStyle_.SetFontStyle(FontStyle::NORMAL);
256         theme->defaultTextStyle_.SetFontFamilies(fontFamilies);
257         theme->popupTextStyle_.SetFontSize(
258             indexerPattern->GetAttr<Dimension>("popup_text_size", Dimension(POPUP_TEXT_SIZE, DimensionUnit::VP)));
259         theme->popupTextStyle_.SetTextColor(
260             indexerPattern->GetAttr<Color>("popup_text_color", Color(POPUP_TEXT_COLOR)));
261         theme->popupTextStyle_.SetFontWeight(FontWeight::MEDIUM);
262         theme->popupTextStyle_.SetFontStyle(FontStyle::NORMAL);
263         theme->popupTextStyle_.SetFontFamilies(fontFamilies);
264     }
265 
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<IndexerTheme> & theme)266     static void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<IndexerTheme>& theme)
267     {
268         RefPtr<ThemeStyle> indexerPattern = themeConstants->GetPatternByName(THEME_PATTERN_INDEXER);
269         if (!indexerPattern) {
270             LOGE("Pattern of indexer is null, please check!");
271             return;
272         }
273         ParseColorAttributes(indexerPattern, theme);
274         ParseDimensionAttributes(indexerPattern, theme);
275         ParseTextStyleAttributes(indexerPattern, theme);
276         if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TWELVE)) {
277             theme->popupBackgroundColor_ = indexerPattern->GetAttr<Color>(
278                 "popup_background_color_api_twelve", Color(POPUP_BACKGROUND_COLOR_API_TWELVE));
279             theme->hoverTextSize_ = indexerPattern->GetAttr<Dimension>(
280                 "hover_text_size_api_twelve", Dimension(HOVER_TEXT_SIZE_API_TWELVE, DimensionUnit::FP));
281             theme->seletctTextStyle_.SetFontSize(indexerPattern->GetAttr<Dimension>(
282                 "hover_text_size_api_twelve", Dimension(HOVER_TEXT_SIZE_API_TWELVE, DimensionUnit::FP)));
283             theme->seletctTextStyle_.SetFontWeight(FontWeight::MEDIUM);
284             theme->defaultTextStyle_.SetFontSize(indexerPattern->GetAttr<Dimension>(
285                 "hover_text_size_api_twelve", Dimension(HOVER_TEXT_SIZE_API_TWELVE, DimensionUnit::FP)));
286             theme->defaultTextStyle_.SetFontWeight(FontWeight::MEDIUM);
287         } else {
288             theme->popupBackgroundColor_ =
289                 indexerPattern->GetAttr<Color>("popup_background_color", Color(POPUP_BACKGROUND_COLOR));
290             theme->hoverTextSize_ =
291                 indexerPattern->GetAttr<Dimension>("hover_text_size", Dimension(HOVER_TEXT_SIZE, DimensionUnit::FP));
292             theme->seletctTextStyle_.SetFontSize(
293                 indexerPattern->GetAttr<Dimension>("hover_text_size", Dimension(HOVER_TEXT_SIZE, DimensionUnit::FP)));
294             theme->seletctTextStyle_.SetFontWeight(FontWeight::REGULAR);
295             theme->defaultTextStyle_.SetFontSize(
296                 indexerPattern->GetAttr<Dimension>("hover_text_size", Dimension(HOVER_TEXT_SIZE, DimensionUnit::FP)));
297             theme->defaultTextStyle_.SetFontWeight(FontWeight::REGULAR);
298         }
299     }
300     static constexpr uint32_t DEFAULT_TEXT_COLOR = 0x99182431;
301     static constexpr uint32_t POPUP_SELECTED_TEXT_COLOR = 0xff182431;
302     static constexpr uint32_t POPUP_UNSELECTED_TEXT_COLOR = 0xff182431;
303     static constexpr uint32_t SELECT_TEXT_COLOR = 0xff007dff;
304     static constexpr uint32_t POPUP_TEXT_COLOR = 0xff007dff;
305     static constexpr float POPUP_TEXT_SIZE = 24.0;
306     static constexpr uint32_t SELECT_BACKGROUD_COLOR = 0x33007dff;
307     static constexpr uint32_t POPUP_BACKGROUND_COLOR = 0xffffffff;
308     static constexpr uint32_t POPUP_SEPARATOR_COLOR = 0x33182431;
309     static constexpr float POPUP_AREA_SIZE = 56.0f;
310     static constexpr float HOVER_RADIUS_SIZE = 4.0f;
311     static constexpr float HOVER_BG_AREA_SIZE = 16.0f;
312     static constexpr uint32_t HOVER_BG_AREA_COLOR = 0x0c182431;
313     static constexpr float HOVER_TEXT_SIZE = 12.0f;
314     static constexpr uint32_t HOVER_TEXT_COLOR = 0xff182431;
315     static constexpr float HOVER_TEXT_ALPHA = 0.6f;
316     static constexpr uint32_t SLIP_HOVER_BACKGROUD_COLOR = 0x000000;
317     static constexpr uint32_t FOCUS_BG_OUTLINE_COLOR = 0xff007dff;
318     static constexpr float FOCUS_BG_OUTLINE_SIZE = 2.0f;
319     static constexpr float TEXT_COLOR_OPACITY = 0.6f;
320     static constexpr float SELECT_BACKGROUND_OPACITY = 0.1f;
321     static constexpr uint32_t PRESSED_BG_AREA_COLOR = 0x19182431;
322     static constexpr uint32_t SLIP_PRESSED_BACKGROUD_COLOR = 0x19182431;
323     static constexpr uint32_t POPUP_CLICKED_BG_AREA_COLOR = 0x0c182431;
324     static constexpr uint32_t POPUP_TITLE_BG_AREA_COLOR = 0x0c182431;
325     static constexpr uint32_t POPUP_UNCLICKED_BG_AREA_COLOR = 0x00000000;
326     static constexpr uint32_t POPUP_BACKGROUND_COLOR_API_TWELVE = 0x66808080;
327     static constexpr float HOVER_TEXT_SIZE_API_TWELVE = 10.0f;
328 };
329 } // namespace OHOS::Ace
330 #endif