1 /*
2  * Copyright (c) 2024 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_CONTAINER_MODAL_THEME_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CONTAINER_MODAL_THEME_H
18 
19 #include "core/components/common/layout/constants.h"
20 #include "core/components/common/properties/color.h"
21 #include "core/components/common/properties/text_style.h"
22 #include "core/components/theme/theme.h"
23 #include "core/components/theme/theme_constants.h"
24 #include "core/components/theme/theme_constants_defines.h"
25 #include "core/components_ng/pattern/container_modal/container_modal_view.h"
26 
27 namespace OHOS::Ace::NG {
28 
29 
30 enum ControlBtnColorType {
31     NORMAL,
32     NORMAL_FILL,
33     HOVER,
34     HOVER_FILL,
35     PRESS,
36     PRESS_FILL,
37     FOCUS,
38     FOCUS_FILL,
39     UNFOCUS,
40     UNFOCUS_FILL,
41 };
42 class ContainerModalTheme : public virtual Theme {
43     DECLARE_ACE_TYPE(ContainerModalTheme, Theme);
44 
45 public:
46     class Builder {
47     public:
48         Builder() = default;
49         ~Builder() = default;
50 
Build(const RefPtr<ThemeConstants> & themeConstants)51         RefPtr<ContainerModalTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
52         {
53             RefPtr<ContainerModalTheme> theme = AceType::Claim(new ContainerModalTheme());
54             if (!themeConstants) {
55                 return theme;
56             }
57             ParsePattern(themeConstants->GetThemeStyle(), theme);
58             return theme;
59         }
60 
61     private:
ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<ContainerModalTheme> & theme)62         void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<ContainerModalTheme>& theme) const
63         {
64             if (!themeStyle) {
65                 return;
66             }
67             auto pattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_CONTAINER_MODAL, nullptr);
68             if (!pattern) {
69                 return;
70             }
71             theme->backgroundColor_ = pattern->GetAttr<Color>("container_modal_background", Color());
72             theme->backgroundUnfocusColor_ = pattern->GetAttr<Color>("container_modal_unfocus_background", Color());
73             theme->titleTextColor_ = pattern->GetAttr<Color>("ohos_id_color_primary", Color());
74             theme->normalBtnBackgroundColor_ = pattern->GetAttr<Color>("ohos_id_normalbtn_bg", Color());
75             theme->normalBtnImageFillColor_ = pattern->GetAttr<Color>("ohos_id_color_primary", Color());
76             theme->normalBtnPressedBackgroundColor_ = pattern->GetAttr<Color>("ohos_id_normalbtn_press_bg", Color());
77             theme->normalBtnHoverBackgroundColor_ = pattern->GetAttr<Color>("ohos_id_normalbtn_hover_bg", Color());
78             theme->normalBtnImageHoverFillColor_ = pattern->GetAttr<Color>("ohos_id_color_primary", Color());
79             theme->closeBtnBackgroundColor_  = pattern->GetAttr<Color>("ohos_id_closebtn_focus_bg", Color());
80             theme->closeBtnImageFillColor_ = pattern->GetAttr<Color>("ohos_id_closebtn_color_warning", Color());
81             theme->closeBtnPressBackGroundColor_ = pattern->GetAttr<Color>("ohos_id_closebtn_press_bg", Color());
82             theme->closeBtnImagePressFillColor_ = pattern->GetAttr<Color>("ohos_id_color_foreground_contrary", Color());
83             theme->closeBtnHoverBackGroundColor_  = pattern->GetAttr<Color>("ohos_id_closebtn_color_warning", Color());
84             theme->closeBtnImageHoverFillColor_ = pattern->GetAttr<Color>("ohos_id_color_foreground_contrary", Color());
85             theme->normalBtnUnfocusBackGroundColor_ = pattern->GetAttr<Color>("ohos_id_normalbtn_unfocus_bg", Color());
86             theme->closeBtnUnfocusBackGroundColor_ = pattern->GetAttr<Color>("ohos_id_closebtn_unfocus_bg", Color());
87         }
88     };
89     ContainerModalTheme() = default;
90     ~ContainerModalTheme() override = default;
91 
GetBackGroundColor(bool isFocus)92     Color GetBackGroundColor(bool isFocus)
93     {
94         Color backGroundColor = isFocus ? backgroundColor_ : backgroundUnfocusColor_;
95         return backGroundColor;
96     }
97 
GetControlBtnColor(bool isCloseBtn,ControlBtnColorType type)98     Color GetControlBtnColor(bool isCloseBtn, ControlBtnColorType type)
99     {
100         Color btnColor;
101         switch (type) {
102             case ControlBtnColorType::NORMAL:
103                 btnColor = !isCloseBtn ? normalBtnBackgroundColor_.ChangeOpacity(1.0f)
104                                        : closeBtnBackgroundColor_.ChangeOpacity(1.0f);
105                 break;
106             case ControlBtnColorType::NORMAL_FILL:
107                 btnColor = !isCloseBtn ? normalBtnImageFillColor_.ChangeOpacity(1.0f)
108                                        : closeBtnImageFillColor_.ChangeOpacity(1.0f);
109                 break;
110             case ControlBtnColorType::HOVER:
111                 btnColor = !isCloseBtn ? normalBtnHoverBackgroundColor_.ChangeOpacity(0.05f)
112                                        : closeBtnHoverBackGroundColor_.ChangeOpacity(1.0f);
113                 break;
114             case ControlBtnColorType::HOVER_FILL:
115                 btnColor = !isCloseBtn ? normalBtnImageHoverFillColor_.ChangeOpacity(1.0f)
116                                        : closeBtnImageHoverFillColor_.ChangeOpacity(1.0f);
117                 break;
118             case ControlBtnColorType::PRESS:
119                 btnColor = !isCloseBtn ? normalBtnPressedBackgroundColor_.ChangeOpacity(1.0f)
120                                        : closeBtnPressBackGroundColor_.ChangeOpacity(1.0f);
121                 break;
122             case ControlBtnColorType::PRESS_FILL:
123                 btnColor = !isCloseBtn ? normalBtnImageFillColor_.ChangeOpacity(1.0f)
124                                        : closeBtnImagePressFillColor_.ChangeOpacity(1.0f);
125                 break;
126             case ControlBtnColorType::UNFOCUS:
127                 btnColor = !isCloseBtn ? normalBtnUnfocusBackGroundColor_.ChangeOpacity(0.6f)
128                                        : closeBtnUnfocusBackGroundColor_.ChangeOpacity(0.6f);
129                 break;
130             case ControlBtnColorType::UNFOCUS_FILL:
131                 btnColor = !isCloseBtn ? normalBtnImageFillColor_.ChangeOpacity(0.4f)
132                                        : closeBtnImageFillColor_.ChangeOpacity(0.4f);
133                 break;
134             default:
135                 break;
136         }
137         return btnColor;
138     }
139 
140 private:
141     Color backgroundColor_;
142     Color backgroundUnfocusColor_;
143     Color titleTextColor_;
144     Color normalBtnBackgroundColor_;
145     Color normalBtnImageFillColor_;
146     Color normalBtnPressedBackgroundColor_;
147     Color normalBtnHoverBackgroundColor_;
148     Color closeBtnBackgroundColor_;
149     Color closeBtnImageFillColor_;
150     Color closeBtnPressBackGroundColor_;
151     Color closeBtnImagePressFillColor_;
152     Color closeBtnHoverBackGroundColor_;
153     Color closeBtnImageHoverFillColor_;
154     Color normalBtnUnfocusBackGroundColor_;
155     Color closeBtnUnfocusBackGroundColor_;
156     Color normalBtnImageHoverFillColor_;
157 };
158 
159 } // namespace OHOS::Ace
160 
161 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BUTTON_BUTTON_THEME_H