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 #include "core/pipeline_ng/pipeline_context.h"
29
30 namespace OHOS::Ace::NG {
31 namespace {
MapJsColorModeToColorMode(int32_t jsColorMode)32 ColorMode MapJsColorModeToColorMode(int32_t jsColorMode)
33 {
34 switch (jsColorMode) {
35 case 1: // 1 is the ThemeColorMode.LIGHT
36 return ColorMode::LIGHT;
37 case 2: // 2 is the ThemeColorMode.DARK
38 return ColorMode::DARK;
39 default:
40 return ColorMode::COLOR_MODE_UNDEFINED;
41 }
42 return ColorMode::COLOR_MODE_UNDEFINED;
43 }
44
45 #if defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM)
UpdateColorModeForThemeConstants(const ColorMode & colorMode)46 void UpdateColorModeForThemeConstants(const ColorMode& colorMode)
47 {
48 auto container = Container::Current();
49 CHECK_NULL_VOID(container);
50 auto resConfig = container->GetResourceConfiguration();
51 resConfig.SetColorMode(colorMode);
52
53 auto themeManager = PipelineBase::CurrentThemeManager();
54 CHECK_NULL_VOID(themeManager);
55 auto themeConstants = themeManager->GetThemeConstants();
56 CHECK_NULL_VOID(themeConstants);
57 themeConstants->UpdateConfig(resConfig);
58 }
59 #endif
60 } // namespace
61
UpdateColorMode(ArkUIRuntimeCallInfo * runtimeCallInfo)62 ArkUINativeModuleValue ResourceBridge::UpdateColorMode(ArkUIRuntimeCallInfo* runtimeCallInfo)
63 {
64 EcmaVM* vm = runtimeCallInfo->GetVM();
65 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
66 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
67 ColorMode colorModeValue = ColorMode::COLOR_MODE_UNDEFINED;
68 if (firstArg->IsNumber()) {
69 int32_t firstArgValue = firstArg->Int32Value(vm);
70 colorModeValue = MapJsColorModeToColorMode(firstArgValue);
71 }
72 if (colorModeValue != ColorMode::COLOR_MODE_UNDEFINED) {
73 #if defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM)
74 UpdateColorModeForThemeConstants(colorModeValue);
75 #else
76 ResourceManager::GetInstance().UpdateColorMode(colorModeValue);
77 #endif
78 auto pipelineContext = NG::PipelineContext::GetCurrentContext();
79 CHECK_NULL_RETURN(pipelineContext, panda::JSValueRef::Undefined(vm));
80 pipelineContext->SetLocalColorMode(colorModeValue);
81 }
82 return panda::JSValueRef::Undefined(vm);
83 }
84
Restore(ArkUIRuntimeCallInfo * runtimeCallInfo)85 ArkUINativeModuleValue ResourceBridge::Restore(ArkUIRuntimeCallInfo* runtimeCallInfo)
86 {
87 EcmaVM* vm = runtimeCallInfo->GetVM();
88 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
89
90 auto pipelineContext = NG::PipelineContext::GetCurrentContext();
91 CHECK_NULL_RETURN(pipelineContext, panda::JSValueRef::Undefined(vm));
92 pipelineContext->SetLocalColorMode(ColorMode::COLOR_MODE_UNDEFINED);
93
94 auto colorModeValue = SystemProperties::GetColorMode();
95 #if defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM)
96 UpdateColorModeForThemeConstants(colorModeValue);
97 #else
98 ResourceManager::GetInstance().UpdateColorMode(colorModeValue);
99 #endif
100 return panda::JSValueRef::Undefined(vm);
101 }
102
GetColorValue(ArkUIRuntimeCallInfo * runtimeCallInfo)103 ArkUINativeModuleValue ResourceBridge::GetColorValue(ArkUIRuntimeCallInfo* runtimeCallInfo)
104 {
105 EcmaVM* vm = runtimeCallInfo->GetVM();
106 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
107 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
108 Color color;
109 if (ArkTSUtils::ParseJsColorAlpha(vm, firstArg, color)) {
110 uint32_t colorValue = color.GetValue();
111 return panda::NumberRef::New(vm, colorValue);
112 }
113 return panda::JSValueRef::Undefined(vm);
114 }
115
ClearCache(ArkUIRuntimeCallInfo * runtimeCallInfo)116 ArkUINativeModuleValue ResourceBridge::ClearCache(ArkUIRuntimeCallInfo* runtimeCallInfo)
117 {
118 EcmaVM* vm = runtimeCallInfo->GetVM();
119 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
120 ResourceManager::GetInstance().Reset();
121 return panda::JSValueRef::Undefined(vm);
122 }
123 } // namespace OHOS::Ace::NG
124