1 /*
2 * Copyright (c) 2023 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 "core/components/theme/blur_style_theme.h"
17
18 #include "core/components/common/properties/decoration.h"
19
20 namespace OHOS::Ace {
21
Build(const RefPtr<ThemeConstants> & themeConstants) const22 RefPtr<BlurStyleTheme> BlurStyleTheme::Builder::Build(const RefPtr<ThemeConstants>& themeConstants) const
23 {
24 RefPtr<BlurStyleTheme> theme = AceType::Claim(new BlurStyleTheme());
25 CHECK_NULL_RETURN(themeConstants, theme);
26 RefPtr<ThemeStyle> blurTheme = themeConstants->GetPatternByName(THEME_BLUR_STYLE_COMMON);
27 if (!blurTheme) {
28 TAG_LOGW(AceLogTag::ACE_THEME, "find pattern of blur style fail");
29 return theme;
30 }
31 ParsePattern(blurTheme, theme);
32 return theme;
33 }
34
ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<BlurStyleTheme> & theme) const35 void BlurStyleTheme::Builder::ParsePattern(
36 const RefPtr<ThemeStyle>& themeStyle, const RefPtr<BlurStyleTheme>& theme) const
37 {
38 const static std::pair<std::string, BlurStyle> blurStyles[] = {
39 std::pair<std::string, BlurStyle> { "thin", BlurStyle::THIN },
40 std::pair<std::string, BlurStyle> { "regular", BlurStyle::REGULAR },
41 std::pair<std::string, BlurStyle> { "thick", BlurStyle::THICK },
42 std::pair<std::string, BlurStyle> { "background_thin", BlurStyle::BACKGROUND_THIN },
43 std::pair<std::string, BlurStyle> { "background_regular", BlurStyle::BACKGROUND_REGULAR },
44 std::pair<std::string, BlurStyle> { "background_thick", BlurStyle::BACKGROUND_THICK },
45 std::pair<std::string, BlurStyle> { "background_ultra_thick", BlurStyle::BACKGROUND_ULTRA_THICK },
46 std::pair<std::string, BlurStyle> { "component_ultra_thin", BlurStyle::COMPONENT_ULTRA_THIN },
47 std::pair<std::string, BlurStyle> { "component_thin", BlurStyle::COMPONENT_THIN },
48 std::pair<std::string, BlurStyle> { "component_regular", BlurStyle::COMPONENT_REGULAR },
49 std::pair<std::string, BlurStyle> { "component_thick", BlurStyle::COMPONENT_THICK },
50 std::pair<std::string, BlurStyle> { "component_ultra_thick", BlurStyle::COMPONENT_ULTRA_THICK },
51 std::pair<std::string, BlurStyle> { "component_ultra_thick_window", BlurStyle::COMPONENT_ULTRA_THICK_WINDOW },
52 };
53 const auto length = sizeof(blurStyles) / sizeof(std::pair<std::string, BlurStyle>);
54 for (size_t i = 0; i != length; ++i) {
55 auto blurParamLight = ParseBlurParam(themeStyle, blurStyles[i].first, false);
56 auto blurParamDark = ParseBlurParam(themeStyle, blurStyles[i].first, true);
57 theme->blurParams_.emplace(GetKeyOfBlurStyle(blurStyles[i].second, ThemeColorMode::LIGHT), blurParamLight);
58 theme->blurParams_.emplace(GetKeyOfBlurStyle(blurStyles[i].second, ThemeColorMode::DARK), blurParamDark);
59 }
60 }
61
ParseBlurParam(const RefPtr<ThemeStyle> & themeStyle,const std::string & styleName,bool isDark) const62 BlurParameter BlurStyleTheme::Builder::ParseBlurParam(
63 const RefPtr<ThemeStyle>& themeStyle, const std::string& styleName, bool isDark) const
64 {
65 constexpr static char prefix[] = "blur_style";
66 constexpr static char radiusName[] = "radius";
67 constexpr static char saturationName[] = "saturation";
68 constexpr static char brightnessName[] = "brightness";
69 constexpr static char maskColorName[] = "color";
70 constexpr static char darkSuffix[] = "_dark";
71 BlurParameter param;
72 auto radiusFullName = std::string(prefix) + "_" + styleName + "_" + radiusName;
73 auto saturationFullName = std::string(prefix) + "_" + styleName + "_" + saturationName;
74 auto brightnessFullName = std::string(prefix) + "_" + styleName + "_" + brightnessName;
75 auto maskColorFullName = std::string(prefix) + "_" + styleName + "_" + maskColorName;
76 if (isDark) {
77 radiusFullName += darkSuffix;
78 saturationFullName += darkSuffix;
79 brightnessFullName += darkSuffix;
80 maskColorFullName += darkSuffix;
81 }
82 param.radius = themeStyle->GetAttr<double>(radiusFullName, 0.0);
83 param.saturation = themeStyle->GetAttr<double>(saturationFullName, 0.0);
84 param.brightness = themeStyle->GetAttr<double>(brightnessFullName, 0.0);
85 param.maskColor = themeStyle->GetAttr<Color>(maskColorFullName, Color::WHITE);
86 return param;
87 }
88
GetKeyOfBlurStyle(BlurStyle style,ThemeColorMode colorMode)89 uint32_t BlurStyleTheme::GetKeyOfBlurStyle(BlurStyle style, ThemeColorMode colorMode)
90 {
91 return (static_cast<uint32_t>(colorMode) << 8) + static_cast<uint32_t>(style); // can hold 2^8 blurStyle enums
92 }
93
GetBlurParameter(BlurStyle style,ThemeColorMode colorMode) const94 std::optional<BlurParameter> BlurStyleTheme::GetBlurParameter(BlurStyle style, ThemeColorMode colorMode) const
95 {
96 auto key = GetKeyOfBlurStyle(style, colorMode);
97 auto iter = blurParams_.find(key);
98 return iter != blurParams_.end() ? std::optional<BlurParameter>(iter->second) : std::nullopt;
99 }
100 } // namespace OHOS::Ace
101