• 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 #include "bridge/declarative_frontend/ark_theme/theme_apply/js_with_theme.h"
17 
18 #include "base/memory/referenced.h"
19 #include "bridge/declarative_frontend/ark_theme/theme_apply/js_theme.h"
20 
21 namespace OHOS::Ace::Framework {
22 
23 std::map<int32_t, JSTheme> JSThemeScope::jsThemes = {};
24 
JSBind(BindingTarget globalObj)25 void JSWithTheme::JSBind(BindingTarget globalObj)
26 {
27     JSClass<JSWithTheme>::Declare("WithTheme");
28     JSClass<JSWithTheme>::StaticMethod("sendThemeToNative", &JSWithTheme::SendThemeToNative, MethodOptions::NONE);
29     JSClass<JSWithTheme>::StaticMethod("removeThemeInNative", &JSWithTheme::RemoveThemeInNative, MethodOptions::NONE);
30     JSClass<JSWithTheme>::StaticMethod("setThemeScopeId", &JSWithTheme::SetThemeScopeId, MethodOptions::NONE);
31     JSClass<JSWithTheme>::InheritAndBind<JSViewAbstract>(globalObj);
32 }
33 
RemoveThemeInNative(const JSCallbackInfo & info)34 void JSWithTheme::RemoveThemeInNative(const JSCallbackInfo& info)
35 {
36     auto jsThemeScopeId = info[0];
37     if (!jsThemeScopeId->IsNumber()) {
38         return;
39     }
40     auto themeScopeId = jsThemeScopeId->ToNumber<int32_t>();
41     JSThemeScope::jsThemes.erase(themeScopeId);
42 }
43 
SendThemeToNative(const JSCallbackInfo & info)44 void JSWithTheme::SendThemeToNative(const JSCallbackInfo& info)
45 {
46     auto jsLightColors = info[0];
47     if (!jsLightColors->IsArray()) {
48         return;
49     }
50     auto jsLightColorsArray = JSRef<JSArray>::Cast(jsLightColors);
51 
52     auto jsThemeScopeId = info[2];
53     if (!jsThemeScopeId->IsNumber()) {
54         return;
55     }
56     auto themeScopeId = jsThemeScopeId->ToNumber<int32_t>();
57 
58     auto jsDarkSetStatus = info[3];
59     if (!jsDarkSetStatus->IsBoolean()) {
60         return;
61     }
62     auto darkSetStatus = jsDarkSetStatus->ToBoolean();
63 
64     auto colors = JSThemeColors();
65     colors.SetColors(jsLightColorsArray);
66 
67     JSThemeScope::jsThemes[themeScopeId].SetColors(colors);
68 
69     if (darkSetStatus) {
70         auto jsDarkColors = info[1];
71         if (!jsDarkColors->IsArray()) {
72             return;
73         }
74         auto jsDarkColorsArray = JSRef<JSArray>::Cast(jsDarkColors);
75 
76         auto darkColors = JSThemeColors();
77         darkColors.SetColors(jsDarkColorsArray);
78 
79         JSThemeScope::jsThemes[themeScopeId].SetDarkColors(darkColors);
80     } else {
81         JSThemeScope::jsThemes[themeScopeId].SetDarkColors(colors);
82     }
83 
84     // save the current theme when Theme was created by WithTheme container
85     if (JSThemeScope::isCurrentThemeDefault || themeScopeId > 0) {
86         std::optional<JSTheme> themeOpt = std::make_optional(JSThemeScope::jsThemes[themeScopeId]);
87         JSThemeScope::jsCurrentTheme.swap(themeOpt);
88     }
89 }
90 
SetThemeScopeId(const JSCallbackInfo & info)91 void JSWithTheme::SetThemeScopeId(const JSCallbackInfo& info)
92 {
93     auto jsThemeScopeId = info[0];
94     if (!jsThemeScopeId->IsNumber()) {
95         return;
96     }
97     auto themeScopeId = jsThemeScopeId->ToNumber<int32_t>();
98     JSThemeScope::isCurrentThemeDefault = themeScopeId == 0;
99     auto theme = JSThemeScope::jsThemes.find(themeScopeId);
100     std::optional<JSTheme> themeOpt = (theme != JSThemeScope::jsThemes.end()) ?
101         std::make_optional(theme->second) : std::nullopt;
102     JSThemeScope::jsCurrentTheme.swap(themeOpt);
103 }
104 
105 } // namespace OHOS::Ace::Framework