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 28 namespace OHOS::Ace::NG { 29 30 using RenderFunction = std::function<RefPtr<UINode>()>; 31 32 // CustomNodeLayoutAlgorithm acts as the underlying @component view. 33 class ACE_EXPORT CustomNodeLayoutAlgorithm : public BoxLayoutAlgorithm { 34 DECLARE_ACE_TYPE(CustomNodeLayoutAlgorithm, BoxLayoutAlgorithm); 35 36 public: CustomNodeLayoutAlgorithm(const RenderFunction & renderFunction)37 explicit CustomNodeLayoutAlgorithm(const RenderFunction& renderFunction) : renderFunction_(renderFunction) {} 38 39 ~CustomNodeLayoutAlgorithm() override = default; 40 OnReset()41 void OnReset() override 42 { 43 renderFunction_ = nullptr; 44 } 45 46 void Measure(LayoutWrapper* layoutWrapper) override; 47 48 void Layout(LayoutWrapper* layoutWrapper) override; 49 MeasureContent(const LayoutConstraintF &,LayoutWrapper *)50 std::optional<SizeF> MeasureContent( 51 const LayoutConstraintF& /*contentConstraint*/, LayoutWrapper* /*layoutWrapper*/) override 52 { 53 return std::nullopt; 54 } 55 MoveBuildItem()56 RefPtr<UINode> MoveBuildItem() 57 { 58 return std::move(buildItem_); 59 } 60 61 private: 62 RenderFunction renderFunction_; 63 RefPtr<UINode> buildItem_; 64 65 ACE_DISALLOW_COPY_AND_MOVE(CustomNodeLayoutAlgorithm); 66 }; 67 } // namespace OHOS::Ace::NG 68 69 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_CUSTOM_NODE_LAYOUT_ALGORITHM_H 70