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
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_resource_bridge.h"
17
18 #include <cstdint>
19
20 #include "jsnapi_expo.h"
21
22 #include "base/utils/device_config.h"
23 #include "base/utils/system_properties.h"
24 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_api_bridge.h"
25 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
26 #include "core/common/resource/resource_manager.h"
27 #include "core/components/common/properties/color.h"
28
29 namespace OHOS::Ace::NG {
30 namespace {
MapJsColorModeToColorMode(int32_t jsColorMode)31 ColorMode MapJsColorModeToColorMode(int32_t jsColorMode)
32 {
33 switch (jsColorMode) {
34 case 1: // 1 is the ThemeColorMode.LIGHT
35 return ColorMode::LIGHT;
36 case 2: // 2 is the ThemeColorMode.DARK
37 return ColorMode::DARK;
38 default:
39 return ColorMode::COLOR_MODE_UNDEFINED;
40 }
41 return ColorMode::COLOR_MODE_UNDEFINED;
42 }
43
44 #if defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM)
UpdateColorModeForThemeConstants(const ColorMode & colorMode)45 void UpdateColorModeForThemeConstants(const ColorMode& colorMode)
46 {
47 auto container = Container::Current();
48 CHECK_NULL_VOID(container);
49 auto resConfig = container->GetResourceConfiguration();
50 resConfig.SetColorMode(colorMode);
51
52 auto themeManager = PipelineBase::CurrentThemeManager();
53 CHECK_NULL_VOID(themeManager);
54 auto themeConstants = themeManager->GetThemeConstants();
55 CHECK_NULL_VOID(themeConstants);
56 themeConstants->UpdateConfig(resConfig);
57 }
58 #endif
59 } // namespace
60
UpdateColorMode(ArkUIRuntimeCallInfo * runtimeCallInfo)61 ArkUINativeModuleValue ResourceBridge::UpdateColorMode(ArkUIRuntimeCallInfo* runtimeCallInfo)
62 {
63 EcmaVM* vm = runtimeCallInfo->GetVM();
64 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
65 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
66 ColorMode colorModeValue = ColorMode::COLOR_MODE_UNDEFINED;
67 if (firstArg->IsNumber()) {
68 int32_t firstArgValue = firstArg->Int32Value(vm);
69 colorModeValue = MapJsColorModeToColorMode(firstArgValue);
70 }
71 if (colorModeValue != ColorMode::COLOR_MODE_UNDEFINED) {
72 #if defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM)
73 UpdateColorModeForThemeConstants(colorModeValue);
74 #else
75 ResourceManager::GetInstance().UpdateColorMode(colorModeValue);
76 #endif
77 auto pipelineContext = NG::PipelineContext::GetCurrentContext();
78 CHECK_NULL_RETURN(pipelineContext, panda::JSValueRef::Undefined(vm));
79 pipelineContext->SetLocalColorMode(colorModeValue);
80 }
81 return panda::JSValueRef::Undefined(vm);
82 }
83
Restore(ArkUIRuntimeCallInfo * runtimeCallInfo)84 ArkUINativeModuleValue ResourceBridge::Restore(ArkUIRuntimeCallInfo* runtimeCallInfo)
85 {
86 EcmaVM* vm = runtimeCallInfo->GetVM();
87 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
88
89 auto pipelineContext = NG::PipelineContext::GetCurrentContext();
90 CHECK_NULL_RETURN(pipelineContext, panda::JSValueRef::Undefined(vm));
91 pipelineContext->SetLocalColorMode(ColorMode::COLOR_MODE_UNDEFINED);
92
93 auto colorModeValue = pipelineContext->GetColorMode();
94 #if defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM)
95 UpdateColorModeForThemeConstants(colorModeValue);
96 #else
97 ResourceManager::GetInstance().UpdateColorMode(colorModeValue);
98 #endif
99 return panda::JSValueRef::Undefined(vm);
100 }
101
GetColorValue(ArkUIRuntimeCallInfo * runtimeCallInfo)102 ArkUINativeModuleValue ResourceBridge::GetColorValue(ArkUIRuntimeCallInfo* runtimeCallInfo)
103 {
104 EcmaVM* vm = runtimeCallInfo->GetVM();
105 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
106 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
107 Color color;
108 if (ArkTSUtils::ParseJsColorAlpha(vm, firstArg, color)) {
109 uint32_t colorValue = color.GetValue();
110 return panda::NumberRef::New(vm, colorValue);
111 }
112 return panda::JSValueRef::Undefined(vm);
113 }
114
GetStringValue(ArkUIRuntimeCallInfo * runtimeCallInfo)115 ArkUINativeModuleValue ResourceBridge::GetStringValue(ArkUIRuntimeCallInfo* runtimeCallInfo)
116 {
117 EcmaVM* vm = runtimeCallInfo->GetVM();
118 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
119 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
120 std::string result;
121 if (ArkTSUtils::ParseJsString(vm, firstArg, result)) {
122 return panda::StringRef::NewFromUtf8(vm, result.c_str());
123 }
124 return panda::JSValueRef::Undefined(vm);
125 }
126
GetNumberValue(ArkUIRuntimeCallInfo * runtimeCallInfo)127 ArkUINativeModuleValue ResourceBridge::GetNumberValue(ArkUIRuntimeCallInfo* runtimeCallInfo)
128 {
129 EcmaVM* vm = runtimeCallInfo->GetVM();
130 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
131 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
132 double result;
133 if (ArkTSUtils::ParseJsDouble(vm, firstArg, result)) {
134 return panda::NumberRef::New(vm, result);
135 }
136 return panda::JSValueRef::Undefined(vm);
137 }
138
ClearCache(ArkUIRuntimeCallInfo * runtimeCallInfo)139 ArkUINativeModuleValue ResourceBridge::ClearCache(ArkUIRuntimeCallInfo* runtimeCallInfo)
140 {
141 EcmaVM* vm = runtimeCallInfo->GetVM();
142 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
143 ResourceManager::GetInstance().Reset();
144 return panda::JSValueRef::Undefined(vm);
145 }
146 } // namespace OHOS::Ace::NG
147