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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DISPLAY_RENDER_DISPLAY_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DISPLAY_RENDER_DISPLAY_H
18 
19 #include "core/animation/animator.h"
20 #include "core/components/display/display_component.h"
21 #include "core/pipeline/base/render_node.h"
22 
23 namespace OHOS::Ace {
24 
25 class RenderDisplay : public RenderNode {
26     DECLARE_ACE_TYPE(RenderDisplay, RenderNode);
27 
28 public:
29     static RefPtr<RenderNode> Create();
30     void Update(const RefPtr<Component>& component) override;
31     void PerformLayout() override;
32 
33     void Dump() override;
34 
OnAttachContext()35     void OnAttachContext() override
36     {
37         animatableOpacity_.SetContextAndCallback(
38             context_, std::bind(&RenderDisplay::OnOpacityAnimationCallback, this));
39     }
40 
41     void UpdateVisibleType(VisibleType type);
42 
43     void SetVisible(bool visible, bool inRecursion = false) override;
44 
UpdateHidden(bool hidden)45     void UpdateHidden(bool hidden)
46     {
47         if (GetHidden() != hidden) {
48             SetSelfHidden(hidden);
49             MarkNeedLayout();
50         }
51     }
52 
UpdateOpacity()53     void UpdateOpacity()
54     {
55         auto display = displayComponent_.Upgrade();
56         if (display) {
57             display->SetOpacity(transitionOpacity_);
58         }
59     }
60 
UpdateOpacity(uint8_t opacity)61     void UpdateOpacity(uint8_t opacity) override
62     {
63         if (disableLayer_) {
64             for (auto& callback : opacityCallbacks_) {
65                 callback(opacity);
66             }
67             return;
68         }
69         if (opacity_ != opacity) {
70             opacity_ = opacity;
71             MarkNeedRender();
72         }
73     }
74 
GetTransitionOpacity()75     double GetTransitionOpacity() const
76     {
77         return transitionOpacity_;
78     }
79 
GetVisibleType()80     VisibleType GetVisibleType()
81     {
82         return visible_;
83     }
84 
85     void GetOpacityCallbacks();
86 
87     bool GetVisible() const override;
88 
89     void OnStatusStyleChanged(VisualState style) override;
90 
91     void OnTransition(TransitionType type, int32_t id) override;
92 
93     bool HasDisappearingTransition(int32_t nodeId) override;
94 
GetStateAttributes()95     RefPtr<StateAttributes<DisplayStateAttribute>> GetStateAttributes()
96     {
97         if (stateAttributeList_ == nullptr) {
98             stateAttributeList_ = MakeRefPtr<StateAttributes<DisplayStateAttribute>>();
99         }
100         return stateAttributeList_;
101     }
102 
HasStateAttributes()103     bool HasStateAttributes()
104     {
105         return stateAttributeList_ != nullptr;
106     }
107 
HasBackgroundMask()108     bool HasBackgroundMask() const
109     {
110         return backgroundMask_.has_value();
111     }
112 
GetBackgroundMask()113     const Color& GetBackgroundMask() const
114     {
115         return backgroundMask_.value();
116     }
117 
SetBackgroundMask(const Color & backgroundMask)118     void SetBackgroundMask(const Color& backgroundMask)
119     {
120         backgroundMask_ = backgroundMask;
121     }
122 
123 protected:
124     void OnOpacityAnimationCallback();
125     void OnOpacityDisappearingCallback();
126     void ClearRenderObject() override;
127     int32_t GetCardAppearingDuration();
128     void CreateAppearingAnimation(uint8_t opacity, int32_t limit);
129     void ResetAppearingAnimation();
130 
131     int32_t duration_ = 0;
132     double transitionOpacity_ = 0.0;
133     VisibleType visible_ = VisibleType::VISIBLE;
134     AnimatableDouble animatableOpacity_ = AnimatableDouble(1.0);
135     double appearingOpacity_ = 0.0;
136     double disappearingOpacity_ = 0.0;
137     bool hasDisappearTransition_ = false;
138     bool hasAppearTransition_ = false;
139     bool pendingAppearing_ = false;
140     bool disableLayer_ = false;
141     std::list<OpacityCallback> opacityCallbacks_;
142     WeakPtr<DisplayComponent> displayComponent_;
143     RefPtr<CurveAnimation<uint8_t>> appearingAnimation_;
144     RefPtr<Animator> animator_;
145     RefPtr<StateAttributes<DisplayStateAttribute>> stateAttributeList_;
146     std::optional<Color> backgroundMask_;
147 
148 private:
149     void OnVisibleChange(VisibleType type);
150 };
151 
152 } // namespace OHOS::Ace
153 
154 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DISPLAY_RENDER_DISPLAY_H
155