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 FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_THEME_JS_SWIPER_THEME_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_THEME_JS_SWIPER_THEME_H
18 
19 #include "bridge/declarative_frontend/ark_theme/theme_apply/js_theme_utils.h"
20 #include "bridge/declarative_frontend/jsview/js_swiper.h"
21 #include "core/components_ng/base/view_stack_model.h"
22 #include "core/components_ng/pattern/swiper/swiper_model.h"
23 
24 namespace OHOS::Ace::Framework {
25 class JSSwiper;
26 class JSSwiperTheme : public JSSwiper {
27 public:
ApplyThemeInConstructor()28     static void ApplyThemeInConstructor()
29     {
30         auto themeColors = JSThemeUtils::GetThemeColors();
31         if (!themeColors) {
32             // no need to apply custom theme colors
33             return;
34         }
35         JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
36         JSRef<JSObject> obj = objectTemplate->NewInstance();
37         SwiperParameters swiperParameters = JSSwiper::GetDotIndicatorInfo(obj);
38         ApplyThemeToDotIndicatorForce(swiperParameters);
39         SwiperModel::GetInstance()->SetDotIndicatorStyle(swiperParameters);
40         SwiperModel::GetInstance()->SetIndicatorType(SwiperIndicatorType::DOT);
41         SwiperModel::GetInstance()->SetShowIndicator(true);
42     }
43 
ApplyThemeToDotIndicator(SwiperParameters & swiperParameters,const JSRef<JSObject> & obj)44     static void ApplyThemeToDotIndicator(SwiperParameters &swiperParameters, const JSRef<JSObject>& obj)
45     {
46         auto themeColors = JSThemeUtils::GetThemeColors();
47         if (!themeColors.has_value()) {
48             return;
49         }
50         Color color;
51         if (!obj->HasProperty("colorValue") || !ParseJsColor(obj->GetProperty("colorValue"), color)) {
52             swiperParameters.colorVal = themeColors->CompBackgroundSecondary();
53         }
54         if (!obj->HasProperty("selectedColorValue") || !ParseJsColor(obj->GetProperty("selectedColorValue"), color)) {
55             swiperParameters.selectedColorVal = themeColors->CompBackgroundEmphasize();
56         }
57     }
58 
ApplyThemeToDotIndicatorForce(SwiperParameters & swiperParameters)59     static void ApplyThemeToDotIndicatorForce(SwiperParameters &swiperParameters)
60     {
61         if (auto themeColors = JSThemeUtils::GetThemeColors(); themeColors.has_value()) {
62             swiperParameters.colorVal = themeColors->CompBackgroundSecondary();
63             swiperParameters.selectedColorVal = themeColors->CompBackgroundEmphasize();
64             return;
65         }
66     }
67 
ApplyThemeToIndicatorStyle(SwiperParameters & swiperParameters,const JSRef<JSObject> & obj)68     static void ApplyThemeToIndicatorStyle(SwiperParameters &swiperParameters, const JSRef<JSObject>& obj)
69     {
70         auto themeColors = JSThemeUtils::GetThemeColors();
71         if (!themeColors.has_value()) {
72             return;
73         }
74         Color color;
75         if (!obj->HasProperty("color") || !ParseJsColor(obj->GetProperty("color"), color)) {
76             swiperParameters.colorVal = themeColors->CompBackgroundSecondary();
77         }
78         if (!obj->HasProperty("selectedColor") || !ParseJsColor(obj->GetProperty("selectedColor"), color)) {
79             swiperParameters.selectedColorVal = themeColors->CompBackgroundEmphasize();
80         }
81     }
82 
ApplyThemeToDigitIndicator(SwiperDigitalParameters & swiperParameters,const JSRef<JSObject> & obj)83     static void ApplyThemeToDigitIndicator(SwiperDigitalParameters &swiperParameters, const JSRef<JSObject>& obj)
84     {
85         auto themeColors = JSThemeUtils::GetThemeColors();
86         if (!themeColors.has_value()) {
87             return;
88         }
89         Color color;
90         if (!obj->HasProperty("fontColorValue") || !ParseJsColor(obj->GetProperty("fontColorValue"), color)) {
91             swiperParameters.fontColor = themeColors->FontPrimary();
92         }
93         if (!obj->HasProperty("selectedColorValue") || !ParseJsColor(obj->GetProperty("selectedColorValue"), color)) {
94             swiperParameters.selectedFontColor = themeColors->FontPrimary();
95         }
96     }
97 
ApplyThemeToDisplayArrow(SwiperArrowParameters & swiperParameters,const JSRef<JSObject> & obj)98     static void ApplyThemeToDisplayArrow(SwiperArrowParameters &swiperParameters, const JSRef<JSObject>& obj)
99     {
100         auto themeColors = JSThemeUtils::GetThemeColors();
101         if (!themeColors.has_value()) {
102             return;
103         }
104         Color color;
105         if (!obj->HasProperty("arrowColorValue") || !ParseJsColor(obj->GetProperty("arrowColorValue"), color)) {
106             swiperParameters.arrowColor = themeColors->IconPrimary();
107         }
108         if (!obj->HasProperty("backgroundColorValue") ||
109                 !ParseJsColor(obj->GetProperty("backgroundColorValue"), color)) {
110             swiperParameters.backgroundColor = themeColors->CompBackgroundSecondary();
111         }
112     }
113 
ApplyThemeToDisplayArrowForce(SwiperArrowParameters & swiperParameters)114     static void ApplyThemeToDisplayArrowForce(SwiperArrowParameters &swiperParameters)
115     {
116         auto themeColors = JSThemeUtils::GetThemeColors();
117         if (themeColors.has_value()) {
118             swiperParameters.arrowColor = themeColors->IconPrimary();
119             swiperParameters.backgroundColor = themeColors->CompBackgroundSecondary();
120         }
121     }
122 };
123 
124 } // namespace OHOS::Ace::Framework
125 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_THEME_JS_SWIPER_THEME_H
126