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_PATTERN_CUSTOM_CUSTOM_NODE_LAYOUT_ALGORITHM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_CUSTOM_NODE_LAYOUT_ALGORITHM_H
18 
19 #include <optional>
20 #include <string>
21 
22 #include "base/utils/noncopyable.h"
23 #include "core/components_ng/base/ui_node.h"
24 #include "core/components_ng/layout/box_layout_algorithm.h"
25 #include "core/components_ng/layout/layout_algorithm.h"
26 #include "core/components_ng/layout/layout_wrapper.h"
27 #include "core/components_ng/pattern/custom/custom_measure_layout_param.h"
28 
29 namespace OHOS::Ace::NG {
30 
31 using RenderFunction = std::function<RefPtr<UINode>()>;
32 
33 // CustomNodeLayoutAlgorithm acts as the underlying @component view.
34 class ACE_EXPORT CustomNodeLayoutAlgorithm : public BoxLayoutAlgorithm {
35     DECLARE_ACE_TYPE(CustomNodeLayoutAlgorithm, BoxLayoutAlgorithm);
36 
37 public:
CustomNodeLayoutAlgorithm(const RenderFunction & renderFunction)38     explicit CustomNodeLayoutAlgorithm(const RenderFunction& renderFunction) : renderFunction_(renderFunction) {}
39 
40     ~CustomNodeLayoutAlgorithm() override = default;
41 
OnReset()42     void OnReset() override
43     {
44         renderFunction_ = nullptr;
45     }
46 
47     void Measure(LayoutWrapper* layoutWrapper) override;
48 
49     void Layout(LayoutWrapper* layoutWrapper) override;
50 
MeasureContent(const LayoutConstraintF &,LayoutWrapper *)51     std::optional<SizeF> MeasureContent(
52         const LayoutConstraintF& /*contentConstraint*/, LayoutWrapper* /*layoutWrapper*/) override
53     {
54         return std::nullopt;
55     }
56 
MoveBuildItem()57     RefPtr<UINode> MoveBuildItem()
58     {
59         return std::move(buildItem_);
60     }
61 
GetMeasureLayoutParam()62     RefPtr<MeasureLayoutParam> GetMeasureLayoutParam()
63     {
64         return children_;
65     }
66 
67 private:
68     RenderFunction renderFunction_;
69     RefPtr<MeasureLayoutParam> children_;
70 
71     RefPtr<UINode> buildItem_;
72 
73     ACE_DISALLOW_COPY_AND_MOVE(CustomNodeLayoutAlgorithm);
74 };
75 } // namespace OHOS::Ace::NG
76 
77 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_CUSTOM_NODE_LAYOUT_ALGORITHM_H
78