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_LAYOUTS_LAYOUT_ALGORITHM_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_LAYOUTS_LAYOUT_ALGORITHM_H 18 19 #include <optional> 20 21 #include "base/memory/ace_type.h" 22 #include "base/thread/cancelable_callback.h" 23 #include "base/utils/macros.h" 24 #include "base/utils/noncopyable.h" 25 #include "core/components_ng/property/layout_constraint.h" 26 #include "core/pipeline_ng/ui_task_scheduler.h" 27 28 namespace OHOS::Ace::NG { 29 class LayoutWrapper; 30 31 class ACE_EXPORT LayoutAlgorithm : public virtual AceType { 32 DECLARE_ACE_TYPE(LayoutAlgorithm, AceType); 33 34 public: 35 LayoutAlgorithm() = default; 36 ~LayoutAlgorithm() override = default; 37 Reset()38 void Reset() 39 { 40 OnReset(); 41 } 42 MeasureContent(const LayoutConstraintF &,LayoutWrapper *)43 virtual std::optional<SizeF> MeasureContent( 44 const LayoutConstraintF& /*contentConstraint*/, LayoutWrapper* /*layoutWrapper*/) 45 { 46 return std::nullopt; 47 } 48 Measure(LayoutWrapper * layoutWrapper)49 virtual void Measure(LayoutWrapper* layoutWrapper) {} 50 Layout(LayoutWrapper * layoutWrapper)51 virtual void Layout(LayoutWrapper* layoutWrapper) {} 52 SkipMeasure()53 virtual bool SkipMeasure() 54 { 55 return false; 56 } 57 SkipLayout()58 virtual bool SkipLayout() 59 { 60 return false; 61 } 62 CanRunOnWhichThread()63 virtual TaskThread CanRunOnWhichThread() 64 { 65 return MAIN_TASK; 66 } 67 68 protected: OnReset()69 virtual void OnReset() {} 70 71 ACE_DISALLOW_COPY_AND_MOVE(LayoutAlgorithm); 72 }; 73 74 class ACE_EXPORT LayoutAlgorithmWrapper : public LayoutAlgorithm { 75 DECLARE_ACE_TYPE(LayoutAlgorithmWrapper, LayoutAlgorithm); 76 77 public: 78 explicit LayoutAlgorithmWrapper( 79 const RefPtr<LayoutAlgorithm>& layoutAlgorithmT, bool skipMeasure = false, bool skipLayout = false) layoutAlgorithm_(layoutAlgorithmT)80 : layoutAlgorithm_(layoutAlgorithmT), skipMeasure_(skipMeasure), skipLayout_(skipLayout) 81 {} 82 ~LayoutAlgorithmWrapper() override = default; 83 OnReset()84 void OnReset() override 85 { 86 layoutAlgorithm_.Reset(); 87 skipMeasure_ = false; 88 skipLayout_ = false; 89 } 90 MeasureContent(const LayoutConstraintF & contentConstraint,LayoutWrapper * layoutWrapper)91 std::optional<SizeF> MeasureContent( 92 const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper) override 93 { 94 if (!layoutAlgorithm_) { 95 return std::nullopt; 96 } 97 return layoutAlgorithm_->MeasureContent(contentConstraint, layoutWrapper); 98 } 99 Measure(LayoutWrapper * layoutWrapper)100 void Measure(LayoutWrapper* layoutWrapper) override 101 { 102 if (!layoutAlgorithm_) { 103 return; 104 } 105 layoutAlgorithm_->Measure(layoutWrapper); 106 } 107 Layout(LayoutWrapper * layoutWrapper)108 void Layout(LayoutWrapper* layoutWrapper) override 109 { 110 if (!layoutAlgorithm_) { 111 return; 112 } 113 layoutAlgorithm_->Layout(layoutWrapper); 114 } 115 SetNeedMeasure()116 void SetNeedMeasure() 117 { 118 skipMeasure_ = false; 119 } 120 SetNeedLayout()121 void SetNeedLayout() 122 { 123 skipLayout_ = false; 124 } 125 SkipMeasure()126 bool SkipMeasure() override 127 { 128 return skipMeasure_; 129 } 130 SkipLayout()131 bool SkipLayout() override 132 { 133 return skipLayout_; 134 } 135 GetLayoutAlgorithm()136 const RefPtr<LayoutAlgorithm>& GetLayoutAlgorithm() const 137 { 138 return layoutAlgorithm_; 139 } 140 141 private: 142 RefPtr<LayoutAlgorithm> layoutAlgorithm_; 143 144 bool skipMeasure_ = false; 145 bool skipLayout_ = false; 146 147 ACE_DISALLOW_COPY_AND_MOVE(LayoutAlgorithmWrapper); 148 }; 149 } // namespace OHOS::Ace::NG 150 151 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_LAYOUTS_LAYOUT_ALGORITHM_H 152