• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdint>
20 #include "base/utils/device_config.h"
21 #include "base/utils/system_properties.h"
22 #include "base/utils/utils.h"
23 #include "core/components/common/layout/constants.h"
24 #include "core/components/common/properties/color.h"
25 #include "core/components/common/properties/text_style.h"
26 #include "core/components/theme/theme.h"
27 #include "core/components/theme/theme_constants.h"
28 #include "core/components/theme/theme_constants_defines.h"
29 #include "core/components_ng/pattern/container_modal/container_modal_view.h"
30 
31 namespace OHOS::Ace::NG {
32 
33 constexpr uint32_t DARK_MODE_NORMAL_CLOSE_BTN_COLOR = 0xff3d2e2d;
34 constexpr uint32_t HOVER_CLOSE_BTN_COLOR_DARK = 0xff414141;
35 constexpr uint32_t HOVER_CLOSE_BTN_COLOR_LIGHT = 0xffdcdcdc;
36 constexpr uint32_t HOVER_FILL_CLOSE_BTN_COLOR_DARK = 0xffe5e5e5;
37 constexpr uint32_t HOVER_FILL_CLOSE_BTN_COLOR_LIGHT = 0xffffffff;
38 constexpr uint32_t HOVER_FILL_NORMA_BTN_COLOR_LIGHT = 0xff000000;
39 constexpr uint32_t PRESS_CLOSE_BTN_COLOR_DARK = 0xffde6355;
40 constexpr uint32_t PRESS_CLOSE_BTN_COLOR_LIGHT = 0xffd03922;
41 constexpr uint32_t PRESS_NORMAL_BTN_COLOR_DARK = 0xff4b4b4b;
42 constexpr uint32_t PRESS_NORMAL_BTN_COLOR_LIGHT = 0xffd0d0d0;
43 constexpr uint32_t UNFOCUS_CLOSE_BTN_COLOR_DARK = 0xff191312;
44 constexpr uint32_t UNFOCUS_CLOSE_BTN_COLOR_LIGHT = 0xfffef7f6;
45 constexpr uint32_t UNFOCUS_BTN_COLOR_DARK = 0xff121212;
46 constexpr uint32_t UNFOCUS_BTN_COLOR_LIGHT = 0xfff5f5f5;
47 
48 constexpr uint32_t ICON_PRIMARY_LIGHT = 0xE5000000;
49 constexpr uint32_t ICON_PRIMARY_DARK = 0xDBFFFFFF;
50 constexpr uint32_t ICON_ON_PRIMARY_LIGHT = 0xFFFFFF;
51 constexpr uint32_t ICON_ON_PRIMARY_DARK = 0xE5E5E5;
52 constexpr uint32_t INTERACTIVE_HOVER_LIGHT = 0x0C000000;
53 constexpr uint32_t INTERACTIVE_HOVER_DARK = 0x19FFFFFF;
54 constexpr uint32_t INTERACTIVE_CLICK_LIGHT = 0x19000000;
55 constexpr uint32_t INTERACTIVE_CLICK_DARK = 0x26FFFFFF;
56 constexpr uint32_t WARNING_LIGHT = 0xE84026;
57 constexpr uint32_t WARNING_DARK = 0xD94838;
58 constexpr uint32_t CLOSE_BTN_DARK = 0xE6A5A5;
59 
60 enum ControlBtnColorType {
61     NORMAL,
62     NORMAL_FILL,
63     HOVER,
64     HOVER_FILL,
65     PRESS,
66     PRESS_FILL,
67     FOCUS,
68     FOCUS_FILL,
69     UNFOCUS,
70     UNFOCUS_FILL,
71 };
72 class ContainerModalTheme : public virtual Theme {
73     DECLARE_ACE_TYPE(ContainerModalTheme, Theme);
74 
75 public:
76     class Builder {
77     public:
78         Builder() = default;
79         ~Builder() = default;
80 
Build(const RefPtr<ThemeConstants> & themeConstants)81         RefPtr<ContainerModalTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
82         {
83             RefPtr<ContainerModalTheme> theme = AceType::Claim(new ContainerModalTheme());
84             if (!themeConstants) {
85                 return theme;
86             }
87             theme->themeConstants_ = themeConstants;
88             ParsePattern(themeConstants, theme);
89             return theme;
90         }
91 
92     private:
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<ContainerModalTheme> & theme)93         void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<ContainerModalTheme>& theme) const
94         {
95             auto themeStyle = themeConstants->GetThemeStyle();
96             if (!themeStyle) {
97                 return;
98             }
99             auto pattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_CONTAINER_MODAL, nullptr);
100             if (!pattern) {
101                 return;
102             }
103             theme->backgroundColor_ = pattern->GetAttr<Color>("container_modal_background", Color());
104             theme->backgroundUnfocusColor_ = pattern->GetAttr<Color>("container_modal_unfocus_background", Color());
105             theme->titleTextColor_ = pattern->GetAttr<Color>("ohos_id_color_primary", Color());
106 
107             auto resAdapter = themeConstants->GetResourceAdapter();
108             CHECK_NULL_VOID(resAdapter);
109             auto isLightMode = resAdapter->GetResourceColorMode() == ColorMode::LIGHT;
110 
111             theme->iconPrimaryColor_ = pattern->GetAttr<Color>(
112                 "icon_primary",
113                 isLightMode ? Color(ICON_PRIMARY_LIGHT) : Color(ICON_PRIMARY_DARK)
114             );
115             theme->iconOnPrimaryColor_ = pattern->GetAttr<Color>(
116                 "icon_on_primary",
117                 isLightMode ? Color(ICON_ON_PRIMARY_LIGHT) : Color(ICON_ON_PRIMARY_DARK)
118             );
119             theme->interactiveHoverColor_ = pattern->GetAttr<Color>(
120                 "interactive_hover",
121                 isLightMode ? Color(INTERACTIVE_HOVER_LIGHT) : Color(INTERACTIVE_HOVER_DARK)
122             );
123             theme->interactiveClickColor_ = pattern->GetAttr<Color>(
124                 "interactive_click",
125                 isLightMode ? Color(INTERACTIVE_CLICK_LIGHT) : Color(INTERACTIVE_CLICK_DARK)
126             );
127             theme->warningColor_ = pattern->GetAttr<Color>(
128                 "warning",
129                 isLightMode ? Color(WARNING_LIGHT) : Color(WARNING_DARK)
130             );
131         }
132     };
133     ContainerModalTheme() = default;
134     ~ContainerModalTheme() override = default;
135 
GetBackGroundColor(bool isFocus)136     Color GetBackGroundColor(bool isFocus)
137     {
138         Color backGroundColor = isFocus ? backgroundColor_ : backgroundUnfocusColor_;
139         return backGroundColor;
140     }
141 
GetControlBtnColor(bool isCloseBtn,ControlBtnColorType type)142     Color GetControlBtnColor(bool isCloseBtn, ControlBtnColorType type)
143     {
144         auto isLightMode = IsLightMode();
145         auto normalBtnOpacity = isLightMode ? 0.1f : 0.2f;
146         auto normalBtnColor = iconPrimaryColor_;
147         auto closeBtnOpacity = isLightMode ? 0.1f : 0.3f;
148         auto closeBtnColor = isLightMode ? warningColor_ : Color(CLOSE_BTN_DARK);
149         Color btnColor;
150         switch (type) {
151             case ControlBtnColorType::NORMAL:
152                 btnColor = !isCloseBtn
153                     ? normalBtnColor.ChangeOpacity(normalBtnOpacity)
154                     : closeBtnColor.ChangeOpacity(closeBtnOpacity);
155                 break;
156             case ControlBtnColorType::NORMAL_FILL:
157                 btnColor = !isCloseBtn
158                     ? iconPrimaryColor_.ChangeOpacity(1.0f) : warningColor_.ChangeOpacity(1.0f);
159                 break;
160             case ControlBtnColorType::HOVER:
161                 btnColor = !isCloseBtn
162                     ? normalBtnColor.ChangeOpacity(normalBtnOpacity).BlendColor(interactiveHoverColor_)
163                     : warningColor_.ChangeOpacity(1.0f);
164                 break;
165             case ControlBtnColorType::HOVER_FILL:
166                 btnColor = !isCloseBtn
167                     ? iconPrimaryColor_.ChangeOpacity(1.0f) : iconOnPrimaryColor_.ChangeOpacity(1.0f);
168                 break;
169             case ControlBtnColorType::PRESS:
170                 btnColor = !isCloseBtn
171                     ? normalBtnColor.ChangeOpacity(normalBtnOpacity).BlendColor(interactiveClickColor_)
172                     : warningColor_.ChangeOpacity(1.0f).BlendColor(interactiveClickColor_);
173                 break;
174             case ControlBtnColorType::PRESS_FILL:
175                 btnColor = !isCloseBtn
176                     ? iconPrimaryColor_.ChangeOpacity(1.0f) : iconOnPrimaryColor_.ChangeOpacity(1.0f);
177                 break;
178             case ControlBtnColorType::UNFOCUS:
179                 btnColor = !isCloseBtn
180                     ? normalBtnColor.ChangeOpacity(normalBtnOpacity * 0.4f)
181                     : closeBtnColor.ChangeOpacity(closeBtnOpacity * 0.4f);
182                 break;
183             case ControlBtnColorType::UNFOCUS_FILL:
184                 btnColor = !isCloseBtn
185                     ? iconPrimaryColor_.ChangeOpacity(0.4f) : warningColor_.ChangeOpacity(0.4f);
186                 break;
187             default:
188                 break;
189         }
190         return btnColor;
191     }
192 
IsLightMode()193     bool IsLightMode()
194     {
195         CHECK_NULL_RETURN(themeConstants_, true);
196         auto resAdapter = themeConstants_->GetResourceAdapter();
197         CHECK_NULL_RETURN(resAdapter, true);
198         return resAdapter->GetResourceColorMode() == ColorMode::LIGHT;
199     }
200 
201 private:
202     Color backgroundColor_;
203     Color backgroundUnfocusColor_;
204     Color titleTextColor_;
205 
206     Color iconPrimaryColor_;
207     Color iconOnPrimaryColor_;
208     Color interactiveHoverColor_;
209     Color interactiveClickColor_;
210     Color warningColor_;
211     RefPtr<ThemeConstants> themeConstants_ = nullptr;
212 };
213 
214 } // namespace OHOS::Ace
215 
216 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BUTTON_BUTTON_THEME_H