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
22 const std::unordered_map<BlurStyle, std::string> BlurStyleTheme::validBlurStyles_ = {
23 { BlurStyle::THIN, "thin" },
24 { BlurStyle::REGULAR, "regular" },
25 { BlurStyle::THICK, "thick" },
26 { BlurStyle::BACKGROUND_THIN, "background_thin" },
27 { BlurStyle::BACKGROUND_REGULAR, "background_regular" },
28 { BlurStyle::BACKGROUND_THICK, "background_thick" },
29 { BlurStyle::BACKGROUND_ULTRA_THICK, "background_ultra_thick" },
30 { BlurStyle::COMPONENT_ULTRA_THIN, "component_ultra_thin" },
31 { BlurStyle::COMPONENT_THIN, "component_thin" },
32 { BlurStyle::COMPONENT_REGULAR, "component_regular" },
33 { BlurStyle::COMPONENT_THICK, "component_thick" },
34 { BlurStyle::COMPONENT_ULTRA_THICK, "component_ultra_thick" },
35 { BlurStyle::COMPONENT_ULTRA_THICK_WINDOW, "component_ultra_thick_window" }
36 };
37
Build(const RefPtr<ThemeConstants> & themeConstants) const38 RefPtr<BlurStyleTheme> BlurStyleTheme::Builder::Build(const RefPtr<ThemeConstants>& themeConstants) const
39 {
40 RefPtr<BlurStyleTheme> theme = AceType::MakeRefPtr<BlurStyleTheme>();
41 CHECK_NULL_RETURN(themeConstants, theme);
42 RefPtr<ThemeStyle> blurTheme = themeConstants->GetPatternByName(THEME_BLUR_STYLE_COMMON);
43 if (!blurTheme) {
44 TAG_LOGW(AceLogTag::ACE_THEME, "find pattern of blur style fail");
45 return theme;
46 }
47 theme->SetThemeStyle(blurTheme);
48 return theme;
49 }
50
SetThemeStyle(const RefPtr<ThemeStyle> & themeStyle)51 void BlurStyleTheme::SetThemeStyle(const RefPtr<ThemeStyle>& themeStyle)
52 {
53 themeStyle_ = themeStyle;
54 }
55
ParseBlurParam(const RefPtr<ThemeStyle> & themeStyle,const std::string & styleName,bool isDark) const56 BlurParameter BlurStyleTheme::ParseBlurParam(
57 const RefPtr<ThemeStyle>& themeStyle, const std::string& styleName, bool isDark) const
58 {
59 constexpr char prefix[] = "blur_style";
60 constexpr char radiusName[] = "radius";
61 constexpr char saturationName[] = "saturation";
62 constexpr char brightnessName[] = "brightness";
63 constexpr char maskColorName[] = "color";
64 constexpr char darkSuffix[] = "_dark";
65 BlurParameter param;
66 auto radiusFullName = std::string(prefix) + "_" + styleName + "_" + radiusName;
67 auto saturationFullName = std::string(prefix) + "_" + styleName + "_" + saturationName;
68 auto brightnessFullName = std::string(prefix) + "_" + styleName + "_" + brightnessName;
69 auto maskColorFullName = std::string(prefix) + "_" + styleName + "_" + maskColorName;
70 if (isDark) {
71 radiusFullName += darkSuffix;
72 saturationFullName += darkSuffix;
73 brightnessFullName += darkSuffix;
74 maskColorFullName += darkSuffix;
75 }
76 param.radius = themeStyle->GetAttr<double>(radiusFullName, 0.0);
77 param.saturation = themeStyle->GetAttr<double>(saturationFullName, 0.0);
78 param.brightness = themeStyle->GetAttr<double>(brightnessFullName, 0.0);
79 param.maskColor = themeStyle->GetAttr<Color>(maskColorFullName, Color::WHITE);
80 return param;
81 }
82
GetKeyOfBlurStyle(BlurStyle style,ThemeColorMode colorMode)83 uint32_t BlurStyleTheme::GetKeyOfBlurStyle(BlurStyle style, ThemeColorMode colorMode)
84 {
85 return (static_cast<uint32_t>(colorMode) << 8) + static_cast<uint32_t>(style); // can hold 2^8 blurStyle enums
86 }
87
GetBlurParameter(BlurStyle style,ThemeColorMode colorMode)88 std::optional<BlurParameter> BlurStyleTheme::GetBlurParameter(BlurStyle style, ThemeColorMode colorMode)
89 {
90 auto key = GetKeyOfBlurStyle(style, colorMode);
91 auto iter = blurParams_.find(key);
92 if (iter != blurParams_.end()) {
93 return std::optional<BlurParameter>(iter->second);
94 }
95 auto blurIter = validBlurStyles_.find(style);
96 if (blurIter == validBlurStyles_.end() || !themeStyle_) {
97 return std::nullopt;
98 }
99 auto blur = ParseBlurParam(themeStyle_, blurIter->second, colorMode == ThemeColorMode::DARK);
100 blurParams_.emplace(key, blur);
101 return blur;
102 }
103 } // namespace OHOS::Ace
104