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>::InheritAndBind<JSViewAbstract>(globalObj);
31 }
32
RemoveThemeInNative(const JSCallbackInfo & info)33 void JSWithTheme::RemoveThemeInNative(const JSCallbackInfo& info)
34 {
35 auto jsThemeScopeId = info[0];
36 if (!jsThemeScopeId->IsNumber()) {
37 return;
38 }
39 auto themeScopeId = jsThemeScopeId->ToNumber<int32_t>();
40 JSThemeScope::jsThemes.erase(themeScopeId);
41 }
42
SendThemeToNative(const JSCallbackInfo & info)43 void JSWithTheme::SendThemeToNative(const JSCallbackInfo& info)
44 {
45 auto jsColors = info[0];
46 if (!jsColors->IsArray()) {
47 return;
48 }
49 auto jsThemeScopeId = info[1];
50 if (!jsThemeScopeId->IsNumber()) {
51 return;
52 }
53
54 auto jsColorsArray = JSRef<JSArray>::Cast(jsColors);
55 auto themeScopeId = jsThemeScopeId->ToNumber<int32_t>();
56
57 auto colors = JSThemeColors();
58 colors.SetColors(jsColorsArray);
59
60 JSThemeScope::jsThemes[themeScopeId].SetColors(colors);
61 // keep info about WithTheme containers usage
62 if (themeScopeId > 0) {
63 JSThemeScope::jsThemeScopeEnabled = true;
64 }
65 }
66
67 } // namespace OHOS::Ace::Framework