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_PIECE_PIECE_THEME_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PIECE_PIECE_THEME_H
18 
19 #include "base/geometry/dimension.h"
20 #include "core/components/common/properties/color.h"
21 #include "core/components/theme/theme.h"
22 #include "core/components/theme/theme_constants.h"
23 #include "core/components/theme/theme_constants_defines.h"
24 
25 namespace OHOS::Ace {
26 
27 /**
28  * PieceTheme defines default style of DOMPiece,using PieceTheme::Builder to build DOMPiece.
29  */
30 class PieceTheme : public virtual Theme {
31     DECLARE_ACE_TYPE(PieceTheme, Theme);
32 
33 public:
34     class Builder final {
35     public:
36         Builder() = default;
37         ~Builder() = default;
38 
Build(const RefPtr<ThemeConstants> & themeConstants)39         RefPtr<PieceTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
40         {
41             RefPtr<PieceTheme> theme = AceType::Claim(new PieceTheme());
42             if (!themeConstants) {
43                 return theme;
44             }
45             theme->iconResource_ = themeConstants->GetResourceId(THEME_PIECE_ICON_SOURCE);
46             ParsePattern(themeConstants, theme);
47             return theme;
48         }
49 
50     private:
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<PieceTheme> & theme)51         void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<PieceTheme>& theme) const
52         {
53             RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_PIECE);
54             if (!pattern) {
55                 LOGW("find pattern of piece fail");
56                 return;
57             }
58             theme->height_ = pattern->GetAttr<Dimension>("piece_height", 0.0_vp);
59             theme->textStyle_.SetTextOverflow(TextOverflow::ELLIPSIS);
60             auto maxlines = static_cast<int32_t>(pattern->GetAttr<double>("piece_text_lines", 0.0));
61             theme->textStyle_.SetMaxLines(maxlines < 0 ? UINT32_MAX : maxlines);
62             theme->textStyle_.SetFontWeight(
63                 FontWeight(static_cast<int32_t>(pattern->GetAttr<double>("piece_font_weight", 0.0))));
64             theme->paddingHorizontal_ = pattern->GetAttr<Dimension>("piece_padding_horizontal", 0.0_vp);
65             theme->paddingVertical_ = pattern->GetAttr<Dimension>("piece_padding_vertical", 0.0_vp);
66             theme->iconSize_ = pattern->GetAttr<Dimension>("piece_icon_size", 0.0_vp);
67             theme->interval_ = pattern->GetAttr<Dimension>("piece_interval", 0.0_vp);
68             theme->textStyle_.SetTextColor(pattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color()));
69             theme->textStyle_.SetFontSize(pattern->GetAttr<Dimension>(PATTERN_TEXT_SIZE, 0.0_fp));
70             theme->backgroundColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR, Color())
71                 .BlendOpacity(pattern->GetAttr<double>(PATTERN_BG_COLOR_ALPHA, 0.0));
72             theme->hoverColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_HOVERED, Color());
73         }
74     };
75 
76     ~PieceTheme() override = default;
GetHoverColor()77     const Color& GetHoverColor() const
78     {
79         return hoverColor_;
80     }
GetBackgroundColor()81     const Color& GetBackgroundColor() const
82     {
83         return backgroundColor_;
84     }
GetHeight()85     const Dimension& GetHeight() const
86     {
87         return height_;
88     }
GetPaddingVertical()89     const Dimension& GetPaddingVertical() const
90     {
91         return paddingVertical_;
92     }
GetPaddingHorizontal()93     const Dimension& GetPaddingHorizontal() const
94     {
95         return paddingHorizontal_;
96     }
GetInterval()97     const Dimension& GetInterval() const
98     {
99         return interval_;
100     }
GetIconResource()101     InternalResource::ResourceId GetIconResource() const
102     {
103         return iconResource_;
104     }
GetTextStyle()105     const TextStyle& GetTextStyle() const
106     {
107         return textStyle_;
108     }
GetIconSize()109     const Dimension& GetIconSize() const
110     {
111         return iconSize_;
112     }
113 
114 protected:
115     PieceTheme() = default;
116 
117 private:
118     Color hoverColor_;
119     Color backgroundColor_;
120     Dimension height_;
121     Dimension interval_;
122     Dimension iconSize_;
123     Dimension paddingVertical_;
124     Dimension paddingHorizontal_;
125     TextStyle textStyle_;
126     InternalResource::ResourceId iconResource_ = InternalResource::ResourceId::NO_ID;
127 };
128 
129 } // namespace OHOS::Ace
130 
131 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PIECE_PIECE_THEME_H
132