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_CUSTOM_PAINT_CUSTOM_PAINT_ELEMENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CUSTOM_PAINT_CUSTOM_PAINT_ELEMENT_H
18 
19 #include "core/components/custom_paint/render_custom_paint.h"
20 #include "core/pipeline/base/render_element.h"
21 
22 namespace OHOS::Ace {
23 
24 class CustomPaintElement : public RenderElement {
25     DECLARE_ACE_TYPE(CustomPaintElement, RenderElement);
26 
27 public:
28     CustomPaintElement() = default;
29     ~CustomPaintElement() override = default;
30 
Update()31     void Update() override
32     {
33         const auto context = renderNode_->GetContext().Upgrade();
34         std::list<TaskFunc> tasks;
35         auto paint = AceType::DynamicCast<RenderCustomPaint>(renderNode_);
36         if (context && !context->GetIsDeclarative()) {
37             if (paint && paint->HasTask()) {
38                 tasks = paint->GetTasks();
39             }
40         }
41         customComponent_ = component_;
42         RenderElement::Update();
43         if (context && !context->GetIsDeclarative() && paint) {
44             paint->SetTasks(tasks);
45         }
46     }
47 
CanUpdate(const RefPtr<Component> & newComponent)48     bool CanUpdate(const RefPtr<Component>& newComponent) override
49     {
50         const auto context = renderNode_->GetContext().Upgrade();
51         if (context && context->GetIsDeclarative()) {
52             return Element::CanUpdate(newComponent);
53         }
54         return (newComponent == customComponent_) && Element::CanUpdate(newComponent);
55     }
56 
57 private:
58     WeakPtr<Component> customComponent_;
59 };
60 } // namespace OHOS::Ace
61 
62 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CUSTOM_PAINT_CUSTOM_PAINT_ELEMENT_H
63