1 /*
2  * Copyright (c) 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_NG_BASE_CUSTOM_MEASURE_LAYOUT_NODE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_CUSTOM_MEASURE_LAYOUT_NODE_H
18 
19 #include <functional>
20 #include <string>
21 
22 #include "base/utils/macros.h"
23 #include "core/components_ng/base/frame_node.h"
24 #include "core/components_ng/pattern/custom/custom_measure_layout_param.h"
25 #include "core/components_ng/pattern/custom/custom_node.h"
26 #include "core/components_ng/pattern/custom/custom_node_base.h"
27 #include "core/components_ng/pattern/custom/custom_node_pattern.h"
28 
29 namespace OHOS::Ace::NG {
30 
31 // CustomMeasureLayoutNode is the frame node of @Component struct.
32 class ACE_EXPORT CustomMeasureLayoutNode : public FrameNode, public CustomNodeBase {
33     DECLARE_ACE_TYPE(CustomMeasureLayoutNode, FrameNode, CustomNodeBase);
34 
35 public:
36     static RefPtr<CustomMeasureLayoutNode> CreateCustomMeasureLayoutNode(int32_t nodeId, const std::string& viewKey);
37 
38     CustomMeasureLayoutNode(int32_t nodeId, const std::string& viewKey);
39     ~CustomMeasureLayoutNode() override = default;
40 
SetRenderFunction(const RenderFunction & renderFunction)41     void SetRenderFunction(const RenderFunction& renderFunction) override
42     {
43         auto pattern = DynamicCast<CustomNodePattern>(GetPattern());
44         if (pattern) {
45             pattern->SetRenderFunction(renderFunction);
46         }
47     }
48 
49     bool FireOnMeasure(LayoutWrapper* layoutWrapper);
50 
51     bool FireOnLayout(LayoutWrapper* layoutWrapper);
52 
SetLayoutFunction(std::function<void (LayoutWrapper * layoutWrapper)> && layoutFunc)53     void SetLayoutFunction(std::function<void(LayoutWrapper* layoutWrapper)>&& layoutFunc)
54     {
55         layoutFunc_ = std::move(layoutFunc);
56     }
57 
SetMeasureFunction(std::function<void (LayoutWrapper * layoutWrapper)> && measureFunc)58     void SetMeasureFunction(std::function<void(LayoutWrapper* layoutWrapper)>&& measureFunc)
59     {
60         measureFunc_ = std::move(measureFunc);
61     }
SetCompleteReloadFunc(RenderFunction && func)62     void SetCompleteReloadFunc(RenderFunction&& func) override {}
63 
GetMeasureLayoutParam()64     RefPtr<MeasureLayoutParam> GetMeasureLayoutParam() const
65     {
66         return measureLayoutParam_;
67     }
SetMeasureLayoutParam(RefPtr<MeasureLayoutParam> param)68     void SetMeasureLayoutParam(RefPtr<MeasureLayoutParam> param)
69     {
70         measureLayoutParam_ = param;
71     }
SetUpdateParamFunc(std::function<void (LayoutWrapper * layoutWrapper)> && updateParamFunc)72     void SetUpdateParamFunc(std::function<void(LayoutWrapper* layoutWrapper)>&& updateParamFunc)
73     {
74         updateParamFunc_ = updateParamFunc;
75     }
76     bool FireOnUpdateParam(NG::LayoutWrapper* layoutWrapper);
UpdateSize(int32_t index,SizeF size)77     bool UpdateSize(int32_t index, SizeF size)
78     {
79         CHECK_NULL_RETURN(measureLayoutParam_, false);
80         CHECK_NULL_RETURN(measureLayoutParam_->ChildIndexInRange(index), false);
81         auto childParam = measureLayoutParam_->Get(index);
82         childParam.UpdateSize(size);
83         return true;
84     }
85 
86 private:
87     void BuildChildren(const RefPtr<FrameNode>& child);
88     std::function<void(LayoutWrapper* layoutWrapper)> layoutFunc_;
89     std::function<void(LayoutWrapper* layoutWrapper)> measureFunc_;
90     std::function<void(LayoutWrapper* layoutWrapper)> updateParamFunc_;
91     std::string viewKey_;
92     RefPtr<MeasureLayoutParam> measureLayoutParam_;
93 };
94 } // namespace OHOS::Ace::NG
95 
96 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_CUSTOM_NODE_H
97