1 /*
2  * Copyright (c) 2021-2022 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DISPLAY_DISPLAY_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DISPLAY_DISPLAY_COMPONENT_H
18 
19 #include "core/components/common/properties/animatable_double.h"
20 #include "core/components/common/properties/animation_option.h"
21 #include "core/components/display/display_element.h"
22 #include "core/pipeline/base/sole_child_component.h"
23 
24 namespace OHOS::Ace {
25 
26 enum class DisplayStateAttribute {
27     OPACITY,
28 };
29 
30 class ACE_EXPORT DisplayComponent : public SoleChildComponent {
31     DECLARE_ACE_TYPE(DisplayComponent, SoleChildComponent);
32 
33 public:
34     DisplayComponent() = default;
DisplayComponent(const RefPtr<Component> & child)35     explicit DisplayComponent(const RefPtr<Component>& child) : SoleChildComponent(child) {}
36     ~DisplayComponent() override = default;
37 
38     RefPtr<RenderNode> CreateRenderNode() override;
39 
CreateElement()40     RefPtr<Element> CreateElement() override
41     {
42         return AceType::MakeRefPtr<DisplayElement>();
43     }
44 
GetVisible()45     VisibleType GetVisible() const
46     {
47         return visible_;
48     }
49 
GetOpacity()50     double GetOpacity() const
51     {
52         return opacity_.GetValue();
53     }
54 
GetOpacityAnimationOption()55     AnimationOption GetOpacityAnimationOption() const
56     {
57         return opacity_.GetAnimationOption();
58     }
59 
SetVisible(VisibleType visible)60     void SetVisible(VisibleType visible)
61     {
62         visible_ = visible;
63     }
64 
65     void SetOpacity(double opacity, const AnimationOption& animationOption = AnimationOption())
66     {
67         opacity_ = AnimatableDouble(opacity, animationOption);
68     }
69 
DisableLayer(bool disable)70     void DisableLayer(bool disable)
71     {
72         disableLayer_ = disable;
73     }
74 
IsDisableLayer()75     bool IsDisableLayer() const
76     {
77         return disableLayer_;
78     }
79 
SetShadow(const Shadow & shadow)80     void SetShadow(const Shadow& shadow)
81     {
82         shadow_ = shadow;
83     }
84 
GetShadow()85     const Shadow& GetShadow() const
86     {
87         return shadow_;
88     }
89 
SetTransition(TransitionType type,double opacity)90     void SetTransition(TransitionType type, double opacity)
91     {
92         if (type == TransitionType::DISAPPEARING) {
93             hasDisappearTransition_ = true;
94             disappearingOpacity_ = opacity;
95         } else if (type == TransitionType::APPEARING) {
96             hasAppearTransition_ = true;
97             appearingOpacity_ = opacity;
98         } else {
99             hasDisappearTransition_ = true;
100             hasAppearTransition_ = true;
101             disappearingOpacity_ = opacity;
102             appearingOpacity_ = opacity;
103         }
104     }
105 
HasDisappearTransition()106     bool HasDisappearTransition() const
107     {
108         return hasDisappearTransition_;
109     }
110 
HasAppearTransition()111     bool HasAppearTransition() const
112     {
113         return hasAppearTransition_;
114     }
115 
GetAppearingOpacity()116     double GetAppearingOpacity() const
117     {
118         return appearingOpacity_;
119     }
120 
GetDisappearingOpacity()121     double GetDisappearingOpacity() const
122     {
123         return disappearingOpacity_;
124     }
125 
SetAppearingDuration(int32_t duration)126     void SetAppearingDuration(int32_t duration)
127     {
128         duration_ = duration;
129     }
130 
131     // Duration in millisecond.
GetAppearingDuration()132     int32_t GetAppearingDuration() const
133     {
134         return duration_;
135     }
136 
GetStateAttributes()137     RefPtr<StateAttributes<DisplayStateAttribute>> GetStateAttributes()
138     {
139         if (stateAttributeList_ == nullptr) {
140             stateAttributeList_ = MakeRefPtr<StateAttributes<DisplayStateAttribute>>();
141         }
142         return stateAttributeList_;
143     }
144 
HasStateAttributes()145     bool HasStateAttributes()
146     {
147         return stateAttributeList_ != nullptr;
148     }
149 
GetVisibleChangeEvent()150     const EventMarker& GetVisibleChangeEvent() const
151     {
152         return visibleChangeEvent_;
153     }
154 
SetVisibleChangeEvent(const EventMarker & changeEvent)155     void SetVisibleChangeEvent(const EventMarker& changeEvent)
156     {
157         visibleChangeEvent_ = changeEvent;
158     }
159 
GetBackgroundMask()160     const std::optional<Color>& GetBackgroundMask() const
161     {
162         return backgroundMask_;
163     }
164 
SetBackgroundMask(const Color & backgroundMask)165     void SetBackgroundMask(const Color& backgroundMask)
166     {
167         backgroundMask_ = backgroundMask;
168     }
169 
170 private:
171     VisibleType visible_ = VisibleType::VISIBLE;
172     Shadow shadow_;
173     AnimatableDouble opacity_ = AnimatableDouble(1.0);
174     double appearingOpacity_ = 0.0;
175     double disappearingOpacity_ = 0.0;
176     bool hasDisappearTransition_ = false;
177     bool hasAppearTransition_ = false;
178     bool disableLayer_ = false;
179     int32_t duration_ = 0;
180     std::optional<Color> backgroundMask_;
181     RefPtr<StateAttributes<DisplayStateAttribute>> stateAttributeList_ = nullptr;
182     EventMarker visibleChangeEvent_;
183 };
184 
185 } // namespace OHOS::Ace
186 
187 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DISPLAY_DISPLAY_COMPONENT_H
188