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_REFRESH_REFRESH_THEME_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_REFRESH_REFRESH_THEME_H
18 
19 #include "core/components/theme/theme.h"
20 #include "core/components/theme/theme_constants.h"
21 #include "core/components/theme/theme_constants_defines.h"
22 
23 namespace OHOS::Ace {
24 namespace {
25 constexpr int TEXT_FIELD_FONT_WEIGHT = 3;
26 } // namespace
27 
28 /**
29  * RefreshTheme should be built using RefreshTheme::Builder.
30  */
31 class RefreshTheme : public virtual Theme {
32     DECLARE_ACE_TYPE(RefreshTheme, Theme);
33 
34 public:
35     class Builder {
36     public:
37         Builder() = default;
38         ~Builder() = default;
39 
Build(const RefPtr<ThemeConstants> & themeConstants)40         RefPtr<RefreshTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
41         {
42             RefPtr<RefreshTheme> theme = AceType::Claim(new RefreshTheme());
43             if (!themeConstants) {
44                 return theme;
45             }
46             ParsePattern(themeConstants, theme);
47             return theme;
48         }
49     private:
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<RefreshTheme> & theme)50         void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<RefreshTheme>& theme) const
51         {
52             RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_REFRESH);
53             if (!pattern) {
54                 LOGW("find pattern of refresh fail");
55                 return;
56             }
57             theme->textStyle_.SetFontSize(pattern->GetAttr<Dimension>(PATTERN_TEXT_SIZE, 0.0_vp));
58             theme->textStyle_.SetTextColor(pattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color::BLACK));
59             theme->progressColor_ = pattern->GetAttr<Color>("progress_color", Color::BLACK);
60             theme->backgroundColor_ = pattern->GetAttr<Color>("progress_bg_color", Color::WHITE);
61             theme->loadingDistance_ = pattern->GetAttr<Dimension>("default_loading_distance", 16.0_vp);
62             theme->refreshDistance_ = pattern->GetAttr<Dimension>("default_refreshing_distance", 64.0_vp);
63             theme->maxDistance_ = pattern->GetAttr<Dimension>("default_max_distance", 128.0_vp);
64             theme->progressDistance_ = pattern->GetAttr<Dimension>("default_progress_distance", 16.0_vp);
65             theme->progressDiameter_ = pattern->GetAttr<Dimension>("default_progress_diameter", 32.0_vp);
66             theme->showTimeDistance_ = pattern->GetAttr<Dimension>("default_showtime_distance", 96.0_vp);
67             theme->textStyle_.SetFontWeight(FontWeight(pattern->GetAttr<int>("textfield_font_weight",
68                 TEXT_FIELD_FONT_WEIGHT)));
69         }
70     };
71 
72     ~RefreshTheme() override = default;
73 
GetLoadingDistance()74     const Dimension& GetLoadingDistance() const
75     {
76         return loadingDistance_;
77     }
78 
GetRefreshDistance()79     const Dimension& GetRefreshDistance() const
80     {
81         return refreshDistance_;
82     }
83 
GetProgressDistance()84     const Dimension& GetProgressDistance() const
85     {
86         return progressDistance_;
87     }
88 
GetShowTimeDistance()89     const Dimension& GetShowTimeDistance() const
90     {
91         return showTimeDistance_;
92     }
93 
GetMaxDistance()94     const Dimension& GetMaxDistance() const
95     {
96         return maxDistance_;
97     }
98 
GetProgressDiameter()99     const Dimension& GetProgressDiameter() const
100     {
101         return progressDiameter_;
102     }
103 
GetTextStyle()104     const TextStyle& GetTextStyle() const
105     {
106         return textStyle_;
107     }
108 
GetProgressColor()109     const Color& GetProgressColor() const
110     {
111         return progressColor_;
112     }
113 
GetBackgroundColor()114     const Color& GetBackgroundColor() const
115     {
116         return backgroundColor_;
117     }
118 
119 protected:
120     RefreshTheme() = default;
121 
122 private:
123     Dimension loadingDistance_;
124     Dimension progressDistance_;
125     Dimension refreshDistance_;
126     Dimension maxDistance_;
127     Dimension progressDiameter_;
128     Dimension showTimeDistance_;
129     TextStyle textStyle_;
130     Color progressColor_;
131     Color backgroundColor_;
132 };
133 
134 } // namespace OHOS::Ace
135 
136 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_REFRESH_REFRESH_THEME_H
137