• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "core/components/theme/shadow_theme.h"
17 
18 namespace OHOS::Ace {
19 const std::unordered_map<ShadowStyle, std::string> ShadowTheme::validShadowStyles_ = {
20     { ShadowStyle::OuterDefaultXS, "default_xs" }, { ShadowStyle::OuterDefaultSM, "default_sm" },
21     { ShadowStyle::OuterDefaultMD, "default_md" }, { ShadowStyle::OuterDefaultLG, "default_lg" },
22     { ShadowStyle::OuterFloatingSM, "floating_sm" }, { ShadowStyle::OuterFloatingMD, "floating_md" }
23 };
24 
Build(const RefPtr<ThemeConstants> & themeConstants)25 RefPtr<ShadowTheme> ShadowTheme::Builder::Build(const RefPtr<ThemeConstants>& themeConstants)
26 {
27     RefPtr<ShadowTheme> theme = AceType::MakeRefPtr<ShadowTheme>();
28     CHECK_NULL_RETURN(themeConstants, theme);
29     RefPtr<ThemeStyle> shadowTheme = themeConstants->GetPatternByName(THEME_PATTERN_SHADOW);
30     if (!shadowTheme) {
31         TAG_LOGW(AceLogTag::ACE_THEME, "find pattern of shadow fail");
32         return theme;
33     }
34     theme->SetThemeStyle(shadowTheme);
35     return theme;
36 }
37 
GetShadow(ShadowStyle style,ColorMode colorMode)38 Shadow ShadowTheme::GetShadow(ShadowStyle style, ColorMode colorMode)
39 {
40     auto key = GetKeyOfShadowStyle(style, colorMode);
41     auto iter = shadowStyles_.find(key);
42     if (iter != shadowStyles_.end()) {
43         return iter->second;
44     }
45     auto shadowIter = validShadowStyles_.find(style);
46     if (shadowIter == validShadowStyles_.end() || !themeStyle_) {
47         return Shadow();
48     }
49     auto shadow = ParseShadowParam(themeStyle_, style, shadowIter->second, colorMode == ColorMode::DARK);
50     shadowStyles_.emplace(key, shadow);
51     return shadow;
52 }
53 
SetThemeStyle(const RefPtr<ThemeStyle> & themeStyle)54 void ShadowTheme::SetThemeStyle(const RefPtr<ThemeStyle>& themeStyle)
55 {
56     themeStyle_ = themeStyle;
57 }
58 
GetKeyOfShadowStyle(ShadowStyle style,ColorMode colorMode)59 uint32_t ShadowTheme::GetKeyOfShadowStyle(ShadowStyle style, ColorMode colorMode)
60 {
61     return (static_cast<uint32_t>(colorMode) << 8) + static_cast<uint32_t>(style); // can hold 2^8 shadowStyle enums
62 }
63 
ParseShadowParam(const RefPtr<ThemeStyle> & themeStyle,ShadowStyle shadowStyle,const std::string & name,bool isDark)64 Shadow ShadowTheme::ParseShadowParam(
65     const RefPtr<ThemeStyle>& themeStyle, ShadowStyle shadowStyle, const std::string& name, bool isDark)
66 {
67     // shadow_style_floating_sm_shadow prefix + name +
68     const std::string prefix = std::string("shadow_style_") + name;
69     const char* attrs[] = { "_shadow", "_offset_x", "_offset_y", "_color", "_radius" };
70     const std::string suffix = isDark ? "_dark" : "";
71 
72     Shadow shadow;
73 
74     auto elevationName = prefix + std::string(attrs[0]) + suffix;
75     auto elevation = themeStyle->GetAttr<double>(elevationName, 0.0);
76 
77     auto offsetXName = prefix + std::string(attrs[1]) + suffix;
78     auto offsetX = themeStyle->GetAttr<double>(offsetXName, 0.0);
79     auto offsetYName = prefix + std::string(attrs[2]) + suffix;
80     auto offsetY = themeStyle->GetAttr<double>(offsetYName, 0.0);
81     Offset offset(offsetX, offsetY);
82 
83     auto colorName = prefix + std::string(attrs[3]) + suffix;
84     auto color = themeStyle->GetAttr<Color>(colorName, Color());
85     auto radiusName =  prefix + std::string(attrs[4]) + suffix;
86     auto radius  =  themeStyle->GetAttr<double>(radiusName, 0.0);
87     if (radius != 0.0) {
88         return Shadow(radius, offset, color, shadowStyle);
89     }
90     return Shadow(static_cast<float>(elevation), offset, color, shadowStyle);
91 }
92 
93 } // namespace OHOS::Ace
94