!41890 Blur与Shadow的Theme按需加载

Merge pull request !41890 from duqinlong/dev_dql0829
This commit is contained in:
openharmony_ci 2024-09-06 04:09:25 +00:00 committed by Gitee
commit f096be1028
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
12 changed files with 151 additions and 107 deletions

View File

@ -348,6 +348,8 @@
OHOS::Ace::NG::ViewStackProcessor::*;
OHOS::Ace::DataAbilityHelperStandard::*;
OHOS::Ace::DataProviderManagerStandard::*;
OHOS::Ace::ShadowTheme::*;
OHOS::Ace::ACE_FORCE_EXPORT::*;
"OHOS::Ace::ElementRegister::GetInstance()";
"OHOS::Ace::ElementRegister::RemoveItemSilently(int)";

View File

@ -839,6 +839,7 @@ template("ace_core_ng_source_set") {
"components/theme/app_theme.cpp",
"components/theme/blur_style_theme.cpp",
"components/theme/icon_theme.cpp",
"components/theme/shadow_theme.cpp",
"components/theme/theme_attributes.cpp",
"components/theme/theme_constants.cpp",
"components/theme/theme_manager_impl.cpp",

View File

@ -44,6 +44,7 @@ template("ace_core_components_theme_set") {
"app_theme.cpp",
"blur_style_theme.cpp",
"icon_theme.cpp",
"shadow_theme.cpp",
"theme_attributes.cpp",
"theme_constants.cpp",
"theme_manager_impl.cpp",

View File

@ -19,46 +19,40 @@
namespace OHOS::Ace {
const std::unordered_map<BlurStyle, std::string> BlurStyleTheme::validBlurStyles_ = {
{ BlurStyle::THIN, "thin" },
{ BlurStyle::REGULAR, "regular" },
{ BlurStyle::THICK, "thick" },
{ BlurStyle::BACKGROUND_THIN, "background_thin" },
{ BlurStyle::BACKGROUND_REGULAR, "background_regular" },
{ BlurStyle::BACKGROUND_THICK, "background_thick" },
{ BlurStyle::BACKGROUND_ULTRA_THICK, "background_ultra_thick" },
{ BlurStyle::COMPONENT_ULTRA_THIN, "component_ultra_thin" },
{ BlurStyle::COMPONENT_THIN, "component_thin" },
{ BlurStyle::COMPONENT_REGULAR, "component_regular" },
{ BlurStyle::COMPONENT_THICK, "component_thick" },
{ BlurStyle::COMPONENT_ULTRA_THICK, "component_ultra_thick" }
};
RefPtr<BlurStyleTheme> BlurStyleTheme::Builder::Build(const RefPtr<ThemeConstants>& themeConstants) const
{
RefPtr<BlurStyleTheme> theme = AceType::Claim(new BlurStyleTheme());
RefPtr<BlurStyleTheme> theme = AceType::MakeRefPtr<BlurStyleTheme>();
CHECK_NULL_RETURN(themeConstants, theme);
RefPtr<ThemeStyle> blurTheme = themeConstants->GetPatternByName(THEME_BLUR_STYLE_COMMON);
if (!blurTheme) {
TAG_LOGW(AceLogTag::ACE_THEME, "find pattern of blur style fail");
return theme;
}
ParsePattern(blurTheme, theme);
theme->SetThemeStyle(blurTheme);
return theme;
}
void BlurStyleTheme::Builder::ParsePattern(
const RefPtr<ThemeStyle>& themeStyle, const RefPtr<BlurStyleTheme>& theme) const
void BlurStyleTheme::SetThemeStyle(const RefPtr<ThemeStyle>& themeStyle)
{
const std::pair<std::string, BlurStyle> blurStyles[] = {
std::pair<std::string, BlurStyle> { "thin", BlurStyle::THIN },
std::pair<std::string, BlurStyle> { "regular", BlurStyle::REGULAR },
std::pair<std::string, BlurStyle> { "thick", BlurStyle::THICK },
std::pair<std::string, BlurStyle> { "background_thin", BlurStyle::BACKGROUND_THIN },
std::pair<std::string, BlurStyle> { "background_regular", BlurStyle::BACKGROUND_REGULAR },
std::pair<std::string, BlurStyle> { "background_thick", BlurStyle::BACKGROUND_THICK },
std::pair<std::string, BlurStyle> { "background_ultra_thick", BlurStyle::BACKGROUND_ULTRA_THICK },
std::pair<std::string, BlurStyle> { "component_ultra_thin", BlurStyle::COMPONENT_ULTRA_THIN },
std::pair<std::string, BlurStyle> { "component_thin", BlurStyle::COMPONENT_THIN },
std::pair<std::string, BlurStyle> { "component_regular", BlurStyle::COMPONENT_REGULAR },
std::pair<std::string, BlurStyle> { "component_thick", BlurStyle::COMPONENT_THICK },
std::pair<std::string, BlurStyle> { "component_ultra_thick", BlurStyle::COMPONENT_ULTRA_THICK },
};
const auto length = sizeof(blurStyles) / sizeof(std::pair<std::string, BlurStyle>);
for (size_t i = 0; i != length; ++i) {
auto blurParamLight = ParseBlurParam(themeStyle, blurStyles[i].first, false);
auto blurParamDark = ParseBlurParam(themeStyle, blurStyles[i].first, true);
theme->blurParams_.emplace(GetKeyOfBlurStyle(blurStyles[i].second, ThemeColorMode::LIGHT), blurParamLight);
theme->blurParams_.emplace(GetKeyOfBlurStyle(blurStyles[i].second, ThemeColorMode::DARK), blurParamDark);
}
themeStyle_ = themeStyle;
}
BlurParameter BlurStyleTheme::Builder::ParseBlurParam(
BlurParameter BlurStyleTheme::ParseBlurParam(
const RefPtr<ThemeStyle>& themeStyle, const std::string& styleName, bool isDark) const
{
constexpr char prefix[] = "blur_style";
@ -90,10 +84,19 @@ uint32_t BlurStyleTheme::GetKeyOfBlurStyle(BlurStyle style, ThemeColorMode color
return (static_cast<uint32_t>(colorMode) << 8) + static_cast<uint32_t>(style); // can hold 2^8 blurStyle enums
}
std::optional<BlurParameter> BlurStyleTheme::GetBlurParameter(BlurStyle style, ThemeColorMode colorMode) const
std::optional<BlurParameter> BlurStyleTheme::GetBlurParameter(BlurStyle style, ThemeColorMode colorMode)
{
auto key = GetKeyOfBlurStyle(style, colorMode);
auto iter = blurParams_.find(key);
return iter != blurParams_.end() ? std::optional<BlurParameter>(iter->second) : std::nullopt;
if (iter != blurParams_.end()) {
return std::optional<BlurParameter>(iter->second);
}
auto blurIter = validBlurStyles_.find(style);
if (blurIter == validBlurStyles_.end()) {
return std::nullopt;
}
auto blur = ParseBlurParam(themeStyle_, blurIter->second, colorMode == ThemeColorMode::DARK);
blurParams_.emplace(key, blur);
return blur;
}
} // namespace OHOS::Ace

View File

@ -34,19 +34,18 @@ public:
Builder() = default;
~Builder() = default;
RefPtr<BlurStyleTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const;
private:
void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<BlurStyleTheme>& theme) const;
BlurParameter ParseBlurParam(const RefPtr<ThemeStyle>& themeStyle, const std::string& name, bool isDark) const;
};
~BlurStyleTheme() override = default;
std::optional<BlurParameter> GetBlurParameter(BlurStyle style, ThemeColorMode colorMode) const;
protected:
BlurStyleTheme() = default;
~BlurStyleTheme() override = default;
std::optional<BlurParameter> GetBlurParameter(BlurStyle style, ThemeColorMode colorMode);
void SetThemeStyle(const RefPtr<ThemeStyle>& themeStyle);
private:
static uint32_t GetKeyOfBlurStyle(BlurStyle style, ThemeColorMode colorMode);
BlurParameter ParseBlurParam(const RefPtr<ThemeStyle>& themeStyle, const std::string& name, bool isDark) const;
const static std::unordered_map<BlurStyle, std::string> validBlurStyles_;
RefPtr<ThemeStyle> themeStyle_;
std::unordered_map<uint32_t, BlurParameter> blurParams_;
};

View File

@ -0,0 +1,93 @@
/*
* 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/components/theme/shadow_theme.h"
namespace OHOS::Ace {
const std::unordered_map<ShadowStyle, std::string> ShadowTheme::validShadowStyles_ = {
{ ShadowStyle::OuterDefaultXS, "default_xs" }, { ShadowStyle::OuterDefaultSM, "default_sm" },
{ ShadowStyle::OuterDefaultMD, "default_md" }, { ShadowStyle::OuterDefaultLG, "default_lg" },
{ ShadowStyle::OuterFloatingSM, "floating_sm" }, { ShadowStyle::OuterFloatingMD, "floating_md" }
};
RefPtr<ShadowTheme> ShadowTheme::Builder::Build(const RefPtr<ThemeConstants>& themeConstants)
{
RefPtr<ShadowTheme> theme = AceType::MakeRefPtr<ShadowTheme>();
CHECK_NULL_RETURN(themeConstants, theme);
RefPtr<ThemeStyle> shadowTheme = themeConstants->GetPatternByName(THEME_PATTERN_SHADOW);
if (!shadowTheme) {
TAG_LOGW(AceLogTag::ACE_THEME, "find pattern of shadow fail");
return theme;
}
theme->SetThemeStyle(shadowTheme);
return theme;
}
Shadow ShadowTheme::GetShadow(ShadowStyle style, ColorMode colorMode)
{
auto key = GetKeyOfShadowStyle(style, colorMode);
auto iter = shadowStyles_.find(key);
if (iter != shadowStyles_.end()) {
return iter->second;
}
auto shadowIter = validShadowStyles_.find(style);
if (shadowIter == validShadowStyles_.end()) {
return Shadow();
}
auto shadow = ParseShadowParam(themeStyle_, style, shadowIter->second, colorMode == ColorMode::DARK);
shadowStyles_.emplace(key, shadow);
return shadow;
}
void ShadowTheme::SetThemeStyle(const RefPtr<ThemeStyle>& themeStyle)
{
themeStyle_ = themeStyle;
}
uint32_t ShadowTheme::GetKeyOfShadowStyle(ShadowStyle style, ColorMode colorMode)
{
return (static_cast<uint32_t>(colorMode) << 8) + static_cast<uint32_t>(style); // can hold 2^8 shadowStyle enums
}
Shadow ShadowTheme::ParseShadowParam(
const RefPtr<ThemeStyle>& themeStyle, ShadowStyle shadowStyle, const std::string& name, bool isDark)
{
// shadow_style_floating_sm_shadow prefix + name +
const std::string prefix = std::string("shadow_style_") + name;
const char* attrs[] = { "_shadow", "_offset_x", "_offset_y", "_color", "_radius" };
const std::string suffix = isDark ? "_dark" : "";
Shadow shadow;
auto elevationName = prefix + std::string(attrs[0]) + suffix;
auto elevation = themeStyle->GetAttr<double>(elevationName, 0.0);
auto offsetXName = prefix + std::string(attrs[1]) + suffix;
auto offsetX = themeStyle->GetAttr<double>(offsetXName, 0.0);
auto offsetYName = prefix + std::string(attrs[2]) + suffix;
auto offsetY = themeStyle->GetAttr<double>(offsetYName, 0.0);
Offset offset(offsetX, offsetY);
auto colorName = prefix + std::string(attrs[3]) + suffix;
auto color = themeStyle->GetAttr<Color>(colorName, Color());
auto radiusName = prefix + std::string(attrs[4]) + suffix;
auto radius = themeStyle->GetAttr<double>(radiusName, 0.0);
if (radius != 0.0) {
return Shadow(radius, offset, color, shadowStyle);
}
return Shadow(static_cast<float>(elevation), offset, color, shadowStyle);
}
} // namespace OHOS::Ace

View File

@ -29,7 +29,7 @@ namespace OHOS::Ace {
enum class BlurStyle;
enum class ThemeColorMode;
class ShadowTheme : public virtual Theme {
class ACE_FORCE_EXPORT ShadowTheme : public virtual Theme {
DECLARE_ACE_TYPE(ShadowTheme, Theme);
public:
@ -37,81 +37,20 @@ public:
public:
Builder() = default;
~Builder() = default;
RefPtr<ShadowTheme> Build(const RefPtr<ThemeConstants>& themeConstants)
{
RefPtr<ShadowTheme> theme = AceType::Claim(new ShadowTheme());
CHECK_NULL_RETURN(themeConstants, theme);
RefPtr<ThemeStyle> shadowTheme = themeConstants->GetPatternByName(THEME_PATTERN_SHADOW);
if (!shadowTheme) {
TAG_LOGW(AceLogTag::ACE_THEME, "find pattern of shadow fail");
return theme;
}
ParsePattern(shadowTheme, theme);
return theme;
}
private:
void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<ShadowTheme>& theme)
{
std::unordered_map<ShadowStyle, std::string> shadowStyles { { ShadowStyle::OuterDefaultXS, "default_xs" },
{ ShadowStyle::OuterDefaultSM, "default_sm" }, { ShadowStyle::OuterDefaultMD, "default_md" },
{ ShadowStyle::OuterDefaultLG, "default_lg" }, { ShadowStyle::OuterFloatingSM, "floating_sm" },
{ ShadowStyle::OuterFloatingMD, "floating_md" } };
for (auto iter = shadowStyles.begin(); iter != shadowStyles.end(); ++iter) {
auto shadowDark = ParseShadowParam(themeStyle, iter->first, iter->second, true);
auto shadowLight = ParseShadowParam(themeStyle, iter->first, iter->second, false);
theme->shadowStyles_.emplace(GetKeyOfShadowStyle(iter->first, ColorMode::DARK), shadowDark);
theme->shadowStyles_.emplace(GetKeyOfShadowStyle(iter->first, ColorMode::LIGHT), shadowLight);
}
}
Shadow ParseShadowParam(
const RefPtr<ThemeStyle>& themeStyle, ShadowStyle shadowStyle, const std::string& name, bool isDark)
{
// shadow_style_floating_sm_shadow prefix + name +
const std::string prefix = std::string("shadow_style_") + name;
const char* attrs[] = { "_shadow", "_offset_x", "_offset_y", "_color", "_radius" };
const std::string suffix = isDark ? "_dark" : "";
Shadow shadow;
auto elevationName = prefix + std::string(attrs[0]) + suffix;
auto elevation = themeStyle->GetAttr<double>(elevationName, 0.0);
auto offsetXName = prefix + std::string(attrs[1]) + suffix;
auto offsetX = themeStyle->GetAttr<double>(offsetXName, 0.0);
auto offsetYName = prefix + std::string(attrs[2]) + suffix;
auto offsetY = themeStyle->GetAttr<double>(offsetYName, 0.0);
Offset offset(offsetX, offsetY);
auto colorName = prefix + std::string(attrs[3]) + suffix;
auto color = themeStyle->GetAttr<Color>(colorName, Color());
auto radiusName = prefix + std::string(attrs[4]) + suffix;
auto radius = themeStyle->GetAttr<double>(radiusName, 0.0);
if (radius != 0.0) {
return Shadow(radius, offset, color, shadowStyle);
}
return Shadow(static_cast<float>(elevation), offset, color, shadowStyle);
}
RefPtr<ShadowTheme> Build(const RefPtr<ThemeConstants>& themeConstants);
};
~ShadowTheme() override = default;
Shadow GetShadow(ShadowStyle style, ColorMode colorMode) const
{
auto key = GetKeyOfShadowStyle(style, colorMode);
auto iter = shadowStyles_.find(key);
return iter != shadowStyles_.end() ? iter->second : Shadow();
}
protected:
ShadowTheme() = default;
~ShadowTheme() override = default;
Shadow GetShadow(ShadowStyle style, ColorMode colorMode);
void SetThemeStyle(const RefPtr<ThemeStyle>& themeStyle);
private:
static uint32_t GetKeyOfShadowStyle(ShadowStyle style, ColorMode colorMode)
{
return (static_cast<uint32_t>(colorMode) << 8) + static_cast<uint32_t>(style); // can hold 2^8 shadowStyle enums
}
static uint32_t GetKeyOfShadowStyle(ShadowStyle style, ColorMode colorMode);
Shadow ParseShadowParam(
const RefPtr<ThemeStyle>& themeStyle, ShadowStyle shadowStyle, const std::string& name, bool isDark);
const static std::unordered_map<ShadowStyle, std::string> validShadowStyles_;
RefPtr<ThemeStyle> themeStyle_;
std::unordered_map<uint32_t, Shadow> shadowStyles_;
};

View File

@ -136,6 +136,7 @@ ohos_source_set("ace_components_base") {
subsystem_name = ace_engine_subsystem
part_name = ace_engine_part
sources = [
"$ace_root/frameworks/core/components/theme/shadow_theme.cpp",
"$ace_root/frameworks/core/components_ng/base/distributed_ui.cpp",
"$ace_root/frameworks/core/components_ng/base/extension_handler.cpp",
"$ace_root/frameworks/core/components_ng/base/frame_node.cpp",

View File

@ -18,5 +18,6 @@ ace_unittest("resource_manager_test") {
"$ace_root/frameworks/core/common/resource/resource_manager.cpp",
"$ace_root/test/mock/core/common/mock_resource_adapter_v2.cpp",
"resource_manager_test.cpp",
"resource_theme_style_test.cpp",
]
}

View File

@ -53,6 +53,7 @@ ohos_unittest("image_provider_test_ng") {
"$ace_root/test/mock/core/common/mock_layout_inspector.cpp",
"$ace_root/test/mock/core/common/mock_motion_path_evaluator.cpp",
"$ace_root/test/mock/core/common/mock_raw_recognizer.cpp",
"$ace_root/test/mock/core/common/mock_theme_utils.cpp",
"$ace_root/test/mock/core/common/mock_udmf.cpp",
"$ace_root/test/mock/core/event/mock_touch_event.cpp",
"$ace_root/test/mock/core/image_provider/mock_image_cache.cpp",

View File

@ -77,6 +77,7 @@ ace_unittest("pipeline_context_test_ng") {
"$ace_root/test/mock/core/common/mock_stylus_detector_default.cpp",
"$ace_root/test/mock/core/common/mock_stylus_detector_mgr.cpp",
"$ace_root/test/mock/core/common/mock_theme_constants.cpp",
"$ace_root/test/mock/core/common/mock_theme_utils.cpp",
"$ace_root/test/mock/core/common/mock_udmf.cpp",
"$ace_root/test/mock/core/common/mock_window.cpp",
"$ace_root/test/mock/core/common/mock_xcollieInterface.cpp",

View File

@ -55,6 +55,7 @@ ohos_source_set("rosen_render_context_test_mock") {
part_name = ace_engine_part
sources = [
"$ace_root/frameworks/core/components_ng/render/image_painter.cpp",
"$ace_root/test/mock/core/common/mock_theme_utils.cpp",
"$ace_root/test/unittest/core/rosen/mock_animated_image.cpp",
"$ace_root/test/unittest/core/rosen/mock_pixmap_image.cpp",
]
@ -223,6 +224,7 @@ ohos_unittest("gradient_style_modifier_test") {
"$ace_root/test/mock/core/common/mock_interaction.cpp",
"$ace_root/test/mock/core/common/mock_layout_inspector.cpp",
"$ace_root/test/mock/core/common/mock_motion_path_evaluator.cpp",
"$ace_root/test/mock/core/common/mock_theme_utils.cpp",
"$ace_root/test/mock/core/common/mock_udmf.cpp",
"$ace_root/test/mock/core/event/mock_touch_event.cpp",
"$ace_root/test/mock/core/image_provider/mock_image_loading_context.cpp",