1 /*
2 * Copyright (c) 2021 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/common/properties/shadow.h"
17
18 #include "core/animation/evaluator.h"
19 #include "core/common/container.h"
20 #include "core/components/common/properties/shadow_config.h"
21 #include "core/common/resource/resource_parse_utils.h"
22
23 namespace OHOS::Ace {
24
Blend(const Shadow & to,const Shadow & from,float progress)25 Shadow Shadow::Blend(const Shadow& to, const Shadow& from, float progress)
26 {
27 auto offset = from.offset_ + (to.offset_ - from.offset_) * progress;
28 auto blurRadius = from.blurRadius_ + (to.blurRadius_ - from.blurRadius_) * progress;
29 auto spreadRadius = from.spreadRadius_ + (to.spreadRadius_ - from.spreadRadius_) * progress;
30 LinearEvaluator<Color> evaluator;
31 auto color = evaluator.Evaluate(from.color_, to.color_, progress);
32 return Shadow(blurRadius, spreadRadius, offset, color);
33 }
34
RegisterShadowResourceObj(Shadow & shadow,RefPtr<ResourceObject> & radiusObj,RefPtr<ResourceObject> & colorObj,RefPtr<ResourceObject> & offsetXObj,RefPtr<ResourceObject> & offsetYObj)35 void Shadow::RegisterShadowResourceObj(Shadow& shadow,
36 RefPtr<ResourceObject>& radiusObj, RefPtr<ResourceObject>& colorObj,
37 RefPtr<ResourceObject>& offsetXObj, RefPtr<ResourceObject>& offsetYObj)
38 {
39 CHECK_NULL_VOID(SystemProperties::ConfigChangePerform());
40 if (radiusObj) {
41 auto&& updateFunc = [](const RefPtr<ResourceObject>& resObj, Shadow& shadow) {
42 double radius = 0.0;
43 ResourceParseUtils::ParseResDouble(resObj, radius);
44 if (LessNotEqual(radius, 0.0)) {
45 radius = 0.0;
46 }
47 shadow.SetBlurRadius(radius);
48 };
49 shadow.AddResource("shadow.radius", radiusObj, std::move(updateFunc));
50 }
51 if (colorObj) {
52 auto&& updateFunc = [](const RefPtr<ResourceObject>& colorResObj, Shadow& shadow) {
53 Color colorValue;
54 ResourceParseUtils::ParseResColor(colorResObj, colorValue);
55 shadow.SetColor(colorValue);
56 };
57 shadow.AddResource("shadow.colorValue", colorObj, std::move(updateFunc));
58 }
59 if (offsetXObj) {
60 auto&& updateFunc = [](const RefPtr<ResourceObject>& resObj, Shadow& shadow) {
61 CalcDimension offsetX;
62 if (ResourceParseUtils::ParseResResource(resObj, offsetX)) {
63 shadow.SetOffsetX(offsetX.Value());
64 }
65 };
66 shadow.AddResource("shadow.offsetX", offsetXObj, std::move(updateFunc));
67 }
68 if (offsetYObj) {
69 auto&& updateFunc = [](const RefPtr<ResourceObject>& resObj, Shadow& shadow) {
70 CalcDimension offsetY;
71 if (ResourceParseUtils::ParseResResource(resObj, offsetY)) {
72 shadow.SetOffsetY(offsetY.Value());
73 }
74 };
75 shadow.AddResource("shadow.offsetY", offsetYObj, std::move(updateFunc));
76 }
77 }
78
CreateShadow(ShadowStyle style)79 Shadow Shadow::CreateShadow(ShadowStyle style)
80 {
81 auto colorMode = Container::CurrentColorMode();
82 if (colorMode == ColorMode::DARK) {
83 switch (style) {
84 case ShadowStyle::OuterDefaultXS:
85 return ShadowConfig::DefaultShadowXSDark;
86 case ShadowStyle::OuterDefaultSM:
87 return ShadowConfig::DefaultShadowSDark;
88 case ShadowStyle::OuterDefaultMD:
89 return ShadowConfig::DefaultShadowMDark;
90 case ShadowStyle::OuterDefaultLG:
91 return ShadowConfig::DefaultShadowLDark;
92 case ShadowStyle::OuterFloatingSM:
93 return ShadowConfig::FloatingShadowSDark;
94 case ShadowStyle::OuterFloatingMD:
95 return ShadowConfig::FloatingShadowMDark;
96 default:
97 return ShadowConfig::NoneShadow;
98 }
99 }
100 switch (style) {
101 case ShadowStyle::OuterDefaultXS:
102 return ShadowConfig::DefaultShadowXS;
103 case ShadowStyle::OuterDefaultSM:
104 return ShadowConfig::DefaultShadowS;
105 case ShadowStyle::OuterDefaultMD:
106 return ShadowConfig::DefaultShadowM;
107 case ShadowStyle::OuterDefaultLG:
108 return ShadowConfig::DefaultShadowL;
109 case ShadowStyle::OuterFloatingSM:
110 return ShadowConfig::FloatingShadowS;
111 case ShadowStyle::OuterFloatingMD:
112 return ShadowConfig::FloatingShadowM;
113 default:
114 return ShadowConfig::NoneShadow;
115 }
116 return ShadowConfig::NoneShadow;
117 }
118 } // namespace OHOS::Ace