• 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 jsColors = info[0];
47     if (!jsColors->IsArray()) {
48         return;
49     }
50     auto jsThemeScopeId = info[1];
51     if (!jsThemeScopeId->IsNumber()) {
52         return;
53     }
54 
55     auto jsColorsArray = JSRef<JSArray>::Cast(jsColors);
56     auto themeScopeId = jsThemeScopeId->ToNumber<int32_t>();
57 
58     auto colors = JSThemeColors();
59     colors.SetColors(jsColorsArray);
60 
61     JSThemeScope::jsThemes[themeScopeId].SetColors(colors);
62     // save the current theme when Theme was created by WithTheme container
63     if (JSThemeScope::isCurrentThemeDefault || themeScopeId > 0) {
64         std::optional<JSTheme> themeOpt = std::make_optional(JSThemeScope::jsThemes[themeScopeId]);
65         JSThemeScope::jsCurrentTheme.swap(themeOpt);
66     }
67 }
68 
SetThemeScopeId(const JSCallbackInfo & info)69 void JSWithTheme::SetThemeScopeId(const JSCallbackInfo& info)
70 {
71     auto jsThemeScopeId = info[0];
72     if (!jsThemeScopeId->IsNumber()) {
73         return;
74     }
75     auto themeScopeId = jsThemeScopeId->ToNumber<int32_t>();
76     JSThemeScope::isCurrentThemeDefault = themeScopeId == 0;
77     auto theme = JSThemeScope::jsThemes.find(themeScopeId);
78     std::optional<JSTheme> themeOpt = (theme != JSThemeScope::jsThemes.end()) ?
79         std::make_optional(theme->second) : std::nullopt;
80     JSThemeScope::jsCurrentTheme.swap(themeOpt);
81 }
82 
83 } // namespace OHOS::Ace::Framework