新增JS侧调用的UpdateColorMode

Signed-off-by: fangzhiyuan <fangzhiyuan5@huawei.com>
Change-Id: Id674d13e05933044b3d9c4b0a348432ac3041e93
This commit is contained in:
fangzhiyuan 2024-04-22 22:59:09 +08:00 committed by fangzhiyuan
parent 7e0eb9a4a1
commit ca38d5bc3e
18 changed files with 256 additions and 4 deletions

View File

@ -23,9 +23,11 @@
#include "adapter/ohos/osal/resource_convertor.h"
#include "adapter/ohos/osal/resource_theme_style.h"
#include "base/log/log_wrapper.h"
#include "base/utils/device_config.h"
#include "base/utils/system_properties.h"
#include "base/utils/utils.h"
#include "core/components/theme/theme_attributes.h"
#include "core/pipeline_ng/pipeline_context.h"
namespace OHOS::Ace {
namespace {
constexpr uint32_t OHOS_THEME_ID = 125829872; // ohos_theme
@ -104,6 +106,11 @@ RefPtr<ResourceAdapter> ResourceAdapter::CreateNewResourceAdapter(
}
auto resConfig = aceContainer->GetResourceConfiguration();
auto pipelineContext = NG::PipelineContext::GetCurrentContext();
if (pipelineContext && pipelineContext->GetLocalColorMode() != ColorMode::COLOR_MODE_UNDEFINED) {
auto localColorMode = pipelineContext->GetLocalColorMode();
resConfig.SetColorMode(localColorMode);
}
newResourceAdapter->UpdateConfig(resConfig);
return newResourceAdapter;
@ -755,4 +762,15 @@ uint32_t ResourceAdapterImplV2::GetSymbolById(uint32_t resId) const
return result;
}
void ResourceAdapterImplV2::UpdateColorMode(ColorMode colorMode)
{
RefPtr<Container> container = Container::Current();
CHECK_NULL_VOID(container);
auto aceContainer = AceType::DynamicCast<Platform::AceContainer>(container);
CHECK_NULL_VOID(aceContainer);
auto resConfig = aceContainer->GetResourceConfiguration();
resConfig.SetColorMode(colorMode);
UpdateConfig(resConfig, false);
}
} // namespace OHOS::Ace

View File

@ -21,6 +21,7 @@
#include "resource_manager.h"
#include "base/image/pixel_map.h"
#include "base/utils/device_config.h"
#include "core/components/theme/resource_adapter.h"
namespace OHOS::Ace {
@ -77,6 +78,7 @@ public:
uint32_t GetSymbolByName(const char* resName) const override;
uint32_t GetSymbolById(uint32_t resId) const override;
RefPtr<ThemeStyle> GetPatternByName(const std::string& patternName) override;
void UpdateColorMode(ColorMode colorMode) override;
private:
std::string GetActualResourceName(const std::string& resName) const;

View File

@ -76,6 +76,7 @@
OHOS::Ace::UdmfClient::*;
OHOS::Ace::DragEvent::*;
OHOS::Ace::ResourceManager::*;
OHOS::Ace::ResourceWrapper::*;
OHOS::Ace::NG::AppBarView::*;
OHOS::Ace::Color::*;
OHOS::Ace::NG::UIObserverHandler::*;
@ -101,6 +102,7 @@
vtable?for?OHOS::Ace::SubwindowManager;
vtable?for?OHOS::Ace::ThemeConstants;
vtable?for?OHOS::Ace::UIContentImpl;
vtable?for?OHOS::Ace::ResourceWrapper;
VTT?for?OHOS::Ace::Animator;
VTT?for?OHOS::Ace::Container;
@ -109,6 +111,7 @@
VTT?for?OHOS::Ace::PluginComponentManager::UIServiceListener;
VTT?for?OHOS::Ace::Scheduler;
VTT?for?OHOS::Ace::ThemeConstants;
VTT?for?OHOS::Ace::ResourceWrapper;
};
local:
*;

View File

@ -189,6 +189,7 @@ template("declarative_js_engine_ark") {
"nativeModule/arkts_native_rect_bridge.cpp",
"nativeModule/arkts_native_refresh_bridge.cpp",
"nativeModule/arkts_native_render_node_bridge.cpp",
"nativeModule/arkts_native_resource_bridge.cpp",
"nativeModule/arkts_native_rich_editor_bridge.cpp",
"nativeModule/arkts_native_row_bridge.cpp",
"nativeModule/arkts_native_row_split_bridge.cpp",

View File

@ -94,6 +94,7 @@ private:
static void RegisterXComponentAttributes(Local<panda::ObjectRef> object, EcmaVM* vm);
static void RegisterTabContentAttributes(Local<panda::ObjectRef> object, EcmaVM* vm);
static void RegisterRefreshAttributes(Local<panda::ObjectRef> object, EcmaVM* vm);
static void RegisterResourceAttributes(Local<panda::ObjectRef> object, EcmaVM* vm);
};
} // namespace OHOS::Ace::NG
#endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_JSI_NATIVEMODULE_ARKTS_NATIVE_API_BRIDGE_H

View File

@ -45,6 +45,7 @@
#include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_node_container_bridge.h"
#include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_pattern_lock_bridge.h"
#include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_progress_bridge.h"
#include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_resource_bridge.h"
#include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_text_area_bridge.h"
#include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_text_clock_bridge.h"
#include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_swiper_bridge.h"
@ -1680,7 +1681,7 @@ ArkUINativeModuleValue ArkUINativeModule::GetArkUINativeModule(ArkUIRuntimeCallI
#if defined(FORM_SUPPORTED) || defined(PREVIEW)
RegisterFormAttributes(object, vm);
#endif
RegisterResourceAttributes(object, vm);
return object;
}
@ -3792,4 +3793,16 @@ void ArkUINativeModule::RegisterXComponentNodeAttributes(Local<panda::ObjectRef>
object->Set(vm, panda::StringRef::NewFromUtf8(vm, "xcomponentNode"), xcomponentNode);
}
#endif
void ArkUINativeModule::RegisterResourceAttributes(Local<panda::ObjectRef> object, EcmaVM* vm)
{
auto resource = panda::ObjectRef::New(vm);
resource->Set(vm, panda::StringRef::NewFromUtf8(vm, "updateColorMode"),
panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), ResourceBridge::UpdateColorMode));
resource->Set(vm, panda::StringRef::NewFromUtf8(vm, "restore"),
panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), ResourceBridge::Restore));
resource->Set(vm, panda::StringRef::NewFromUtf8(vm, "getColorValue"),
panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), ResourceBridge::GetColorValue));
object->Set(vm, panda::StringRef::NewFromUtf8(vm, "resource"), resource);
}
} // namespace OHOS::Ace::NG

View File

@ -0,0 +1,92 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_resource_bridge.h"
#include <cstdint>
#include "jsnapi_expo.h"
#include "base/utils/device_config.h"
#include "base/utils/system_properties.h"
#include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_api_bridge.h"
#include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
#include "core/common/resource/resource_manager.h"
#include "core/components/common/properties/color.h"
#include "core/pipeline_ng/pipeline_context.h"
namespace OHOS::Ace::NG {
namespace {
ColorMode MapJsColorModeToColorMode(int32_t jsColorMode)
{
switch (jsColorMode) {
case 1: // 1 is the ThemeColorMode.LIGHT
return ColorMode::LIGHT;
case 2: // 2 is the ThemeColorMode.DARK
return ColorMode::DARK;
default:
return ColorMode::COLOR_MODE_UNDEFINED;
}
return ColorMode::COLOR_MODE_UNDEFINED;
}
} // namespace
ArkUINativeModuleValue ResourceBridge::UpdateColorMode(ArkUIRuntimeCallInfo* runtimeCallInfo)
{
EcmaVM* vm = runtimeCallInfo->GetVM();
CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
ColorMode colorModeValue = ColorMode::COLOR_MODE_UNDEFINED;
if (firstArg->IsNumber()) {
int32_t firstArgValue = firstArg->Int32Value(vm);
colorModeValue = MapJsColorModeToColorMode(firstArgValue);
}
if (colorModeValue != ColorMode::COLOR_MODE_UNDEFINED) {
ResourceManager::GetInstance().UpdateColorMode(colorModeValue);
auto pipelineContext = NG::PipelineContext::GetCurrentContext();
CHECK_NULL_RETURN(pipelineContext, panda::JSValueRef::Undefined(vm));
pipelineContext->SetLocalColorMode(colorModeValue);
}
return panda::JSValueRef::Undefined(vm);
}
ArkUINativeModuleValue ResourceBridge::Restore(ArkUIRuntimeCallInfo* runtimeCallInfo)
{
EcmaVM* vm = runtimeCallInfo->GetVM();
CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
auto pipelineContext = NG::PipelineContext::GetCurrentContext();
CHECK_NULL_RETURN(pipelineContext, panda::JSValueRef::Undefined(vm));
pipelineContext->SetLocalColorMode(ColorMode::COLOR_MODE_UNDEFINED);
auto colorModeValue = SystemProperties::GetColorMode();
ResourceManager::GetInstance().UpdateColorMode(colorModeValue);
return panda::JSValueRef::Undefined(vm);
}
ArkUINativeModuleValue ResourceBridge::GetColorValue(ArkUIRuntimeCallInfo* runtimeCallInfo)
{
EcmaVM* vm = runtimeCallInfo->GetVM();
CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
Color color;
if (ArkTSUtils::ParseJsColorAlpha(vm, firstArg, color)) {
uint32_t colorValue = color.GetValue();
return panda::NumberRef::New(vm, colorValue);
}
return panda::JSValueRef::Undefined(vm);
}
} // namespace OHOS::Ace::NG

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_JSI_NATIVEMODULE_ARKTS_NATIVE_RESOURCE_BRIDGE_H
#define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_JSI_NATIVEMODULE_ARKTS_NATIVE_RESOURCE_BRIDGE_H
#include <cstdint>
#include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_api_bridge.h"
namespace OHOS::Ace::NG {
class ResourceBridge {
public:
static ArkUINativeModuleValue UpdateColorMode(ArkUIRuntimeCallInfo* runtimeCallInfo);
static ArkUINativeModuleValue Restore(ArkUIRuntimeCallInfo* runtimeCallInfo);
static ArkUINativeModuleValue GetColorValue(ArkUIRuntimeCallInfo* runtimeCallInfo);
};
} // namespace OHOS::Ace::NG
#endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_JSI_NATIVEMODULE_ARKTS_NATIVE_RESOURCE_BRIDGE_H

View File

@ -88,6 +88,7 @@ template("ace_core_source_set") {
"common/platform_bridge.cpp",
"common/render_boundary_manager.cpp",
"common/resource/resource_manager.cpp",
"common/resource/resource_wrapper.cpp",
"common/sharedata/share_data.cpp",
"common/storage/storage_proxy.cpp",
"common/task_executor_impl.cpp",
@ -656,6 +657,7 @@ template("ace_core_ng_source_set") {
"common/platform_bridge.cpp",
"common/render_boundary_manager.cpp",
"common/resource/resource_manager.cpp",
"common/resource/resource_wrapper.cpp",
"common/sharedata/share_data.cpp",
"common/storage/storage_proxy.cpp",
"common/task_executor_impl.cpp",

View File

@ -124,6 +124,17 @@ public:
cache_.clear();
}
void UpdateColorMode(ColorMode colorMode)
{
std::unique_lock<std::shared_mutex> lock(mutex_);
for (auto iter = resourceAdapters_.begin(); iter != resourceAdapters_.end(); ++iter) {
iter->second->UpdateColorMode(colorMode);
}
for (auto iter = cacheList_.begin(); iter != cacheList_.end(); ++iter) {
iter->cacheObj->UpdateColorMode(colorMode);
}
}
private:
ResourceManager() = default;

View File

@ -0,0 +1,41 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "core/common/resource/resource_wrapper.h"
#include <memory>
#include <string>
#include "base/memory/referenced.h"
#include "base/utils/system_properties.h"
#include "core/components/theme/resource_adapter.h"
namespace OHOS::Ace {
ResourceWrapper::ResourceWrapper(
RefPtr<ThemeConstants>& themeConstants, RefPtr<ResourceAdapter>& resourceAdapter, ColorMode colorMode)
: themeConstants_(themeConstants), resourceAdapter_(resourceAdapter), localColorMode_(colorMode)
{
if (resourceAdapter && colorMode != ColorMode::COLOR_MODE_UNDEFINED) {
resourceAdapter->UpdateColorMode(colorMode);
}
};
ResourceWrapper::~ResourceWrapper()
{
if (localColorMode_ != ColorMode::COLOR_MODE_UNDEFINED) {
resourceAdapter_->UpdateColorMode(SystemProperties::GetColorMode());
}
}
} // namespace OHOS::Ace

View File

@ -24,19 +24,22 @@
#include "base/image/pixel_map.h"
#include "base/memory/ace_type.h"
#include "base/memory/referenced.h"
#include "base/utils/device_config.h"
#include "base/utils/system_properties.h"
#include "core/components/common/properties/color.h"
#include "core/components/theme/resource_adapter.h"
#include "core/components/theme/theme_constants.h"
namespace OHOS::Ace {
class ResourceWrapper : public AceType {
class ACE_FORCE_EXPORT ResourceWrapper : public AceType {
DECLARE_ACE_TYPE(ResourceWrapper, AceType);
public:
ResourceWrapper(RefPtr<ThemeConstants>& themeConstants, RefPtr<ResourceAdapter>& resourceAdapter)
: themeConstants_(themeConstants), resourceAdapter_(resourceAdapter) {};
~ResourceWrapper() = default;
ResourceWrapper(
RefPtr<ThemeConstants>& themeConstants, RefPtr<ResourceAdapter>& resourceAdapter, ColorMode colorMode);
~ResourceWrapper() override;
Color GetColor(uint32_t key) const
{
@ -296,6 +299,7 @@ public:
private:
RefPtr<ThemeConstants> themeConstants_;
RefPtr<ResourceAdapter> resourceAdapter_;
ColorMode localColorMode_ = ColorMode::COLOR_MODE_UNDEFINED;
};
} // namespace OHOS::Ace

View File

@ -223,6 +223,8 @@ public:
static RefPtr<ResourceAdapter> CreateNewResourceAdapter(
const std::string& bundleName, const std::string& moduleName);
virtual void UpdateColorMode(ColorMode colorMode) {}
};
} // namespace OHOS::Ace

View File

@ -647,7 +647,8 @@ std::shared_ptr<RSData> ResourceImageLoader::LoadImageData(
themeConstants = themeManager->GetThemeConstants();
CHECK_NULL_RETURN(themeConstants, nullptr);
}
auto resourceWrapper = AceType::MakeRefPtr<ResourceWrapper>(themeConstants, resourceAdapter);
auto resourceWrapper =
AceType::MakeRefPtr<ResourceWrapper>(themeConstants, resourceAdapter, imageSourceInfo.GetLocalColorMode());
std::unique_ptr<uint8_t[]> data;
size_t dataLen = 0;

View File

@ -19,6 +19,7 @@
#include "base/utils/string_utils.h"
#include "core/common/container.h"
#include "core/pipeline_ng/pipeline_context.h"
namespace OHOS::Ace {
namespace {
@ -146,6 +147,11 @@ ImageSourceInfo::ImageSourceInfo(std::string imageSrc, std::string bundleName, s
TAG_LOGW(AceLogTag::ACE_IMAGE, "ImageSourceInfo: multi image source set, only one will be load.");
}
GenerateCacheKey();
auto pipelineContext = NG::PipelineContext::GetCurrentContext();
if (pipelineContext) {
localColorMode_ = pipelineContext->GetLocalColorMode();
}
}
SrcType ImageSourceInfo::ResolveSrcType() const

View File

@ -23,6 +23,7 @@
#include "base/geometry/size.h"
#include "base/image/pixel_map.h"
#include "base/resource/internal_resource.h"
#include "base/utils/device_config.h"
#include "core/components/common/layout/constants.h"
#include "core/components/common/properties/color.h"
@ -110,6 +111,11 @@ public:
needCache_ = needCache;
}
ColorMode GetLocalColorMode() const
{
return localColorMode_;
}
private:
SrcType ResolveSrcType() const;
void GenerateCacheKey();
@ -133,6 +139,8 @@ private:
// image source type for example:FILE, ASSET, NETWORK, MEMORY, BASE64, INTERNAL, RESOURCE or DATA_ABILITY,
SrcType srcType_ = SrcType::UNSUPPORTED;
ColorMode localColorMode_ = ColorMode::COLOR_MODE_UNDEFINED;
};
} // namespace OHOS::Ace

View File

@ -28,6 +28,7 @@
#include "base/log/frame_info.h"
#include "base/log/frame_report.h"
#include "base/memory/referenced.h"
#include "base/utils/device_config.h"
#include "base/view_data/view_data_wrap.h"
#include "core/accessibility/accessibility_manager_ng.h"
#include "core/common/frontend.h"
@ -692,6 +693,18 @@ public:
void CheckNeedUpdateBackgroundColor(Color& color);
bool CheckNeedDisableUpdateBackgroundImage();
void SetLocalColorMode(ColorMode colorMode)
{
auto localColorModeValue = static_cast<int32_t>(colorMode);
localColorMode_ = localColorModeValue;
}
ColorMode GetLocalColorMode() const
{
ColorMode colorMode = static_cast<ColorMode>(localColorMode_.load());
return colorMode;
}
protected:
void StartWindowSizeChangeAnimate(int32_t width, int32_t height, WindowSizeChangeReason type,
const std::shared_ptr<Rosen::RSTransaction>& rsTransaction = nullptr);
@ -911,6 +924,7 @@ private:
int32_t preNodeId_ = -1;
RefPtr<NavigationManager> navigationMgr_ = MakeRefPtr<NavigationManager>();
std::atomic<int32_t> localColorMode_ = static_cast<int32_t>(ColorMode::COLOR_MODE_UNDEFINED);
};
} // namespace OHOS::Ace::NG

View File

@ -21,6 +21,7 @@ ace_unittest("common_test") {
"$ace_root/frameworks/core/common/asset_manager_impl.cpp",
"$ace_root/frameworks/core/common/environment/environment_proxy.cpp",
"$ace_root/frameworks/core/common/resource/resource_manager.cpp",
"$ace_root/frameworks/core/common/resource/resource_wrapper.cpp",
"$ace_root/frameworks/core/common/rosen/rosen_asset_manager.cpp",
"$ace_root/frameworks/core/common/rosen/rosen_convert_helper.cpp",
"$ace_root/frameworks/core/common/rosen/rosen_window.cpp",