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 "ui/view/layout/layout_algorithm.h" 22 23 #include "base/memory/ace_type.h" 24 #include "base/thread/cancelable_callback.h" 25 #include "base/utils/macros.h" 26 #include "base/utils/noncopyable.h" 27 #include "core/components_ng/property/layout_constraint.h" 28 #include "core/pipeline_ng/ui_task_scheduler.h" 29 30 namespace OHOS::Ace::Kit { 31 class LayoutAlgorithm; 32 } 33 34 namespace OHOS::Ace::NG { 35 class LayoutWrapper; 36 37 class ACE_EXPORT LayoutAlgorithm : public virtual AceType { 38 DECLARE_ACE_TYPE(LayoutAlgorithm, AceType); 39 40 public: 41 LayoutAlgorithm() = default; 42 ~LayoutAlgorithm() override = default; 43 Reset()44 void Reset() 45 { 46 OnReset(); 47 } 48 MeasureContent(const LayoutConstraintF &,LayoutWrapper *)49 virtual std::optional<SizeF> MeasureContent( 50 const LayoutConstraintF& /*contentConstraint*/, LayoutWrapper* /*layoutWrapper*/) 51 { 52 return std::nullopt; 53 } 54 Measure(LayoutWrapper * layoutWrapper)55 virtual void Measure(LayoutWrapper* layoutWrapper) {} 56 Layout(LayoutWrapper * layoutWrapper)57 virtual void Layout(LayoutWrapper* layoutWrapper) {} 58 SkipMeasure()59 virtual bool SkipMeasure() 60 { 61 return false; 62 } 63 SkipLayout()64 virtual bool SkipLayout() 65 { 66 return false; 67 } 68 CanRunOnWhichThread()69 virtual TaskThread CanRunOnWhichThread() 70 { 71 return MAIN_TASK; 72 } 73 SetHasMeasured(bool measured)74 void SetHasMeasured(bool measured) 75 { 76 hasMeasured_ = measured; 77 } 78 GetHasMeasured()79 bool GetHasMeasured() 80 { 81 return hasMeasured_; 82 } 83 84 protected: OnReset()85 virtual void OnReset() {} 86 bool hasMeasured_ = false; 87 88 ACE_DISALLOW_COPY_AND_MOVE(LayoutAlgorithm); 89 }; 90 91 class ACE_EXPORT LayoutAlgorithmWrapper : public LayoutAlgorithm { 92 DECLARE_ACE_TYPE(LayoutAlgorithmWrapper, LayoutAlgorithm); 93 94 public: 95 explicit LayoutAlgorithmWrapper( 96 const RefPtr<LayoutAlgorithm>& layoutAlgorithmT, bool skipMeasure = false, bool skipLayout = false); 97 ~LayoutAlgorithmWrapper() override; 98 99 static RefPtr<LayoutAlgorithmWrapper> CreateLayoutAlgorithmWrapper( 100 const RefPtr<Kit::LayoutAlgorithm>& layoutAlgorithm); 101 OnReset()102 void OnReset() override 103 { 104 layoutAlgorithm_.Reset(); 105 skipMeasure_ = false; 106 skipLayout_ = false; 107 } 108 109 std::optional<SizeF> MeasureContent( 110 const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper) override; 111 void Measure(LayoutWrapper* layoutWrapper) override; 112 void Layout(LayoutWrapper* layoutWrapper) override; 113 SetSkipMeasure()114 void SetSkipMeasure() 115 { 116 skipMeasure_ = true; 117 } 118 SetSkipLayout()119 void SetSkipLayout() 120 { 121 skipLayout_ = true; 122 } 123 SetNeedMeasure()124 void SetNeedMeasure() 125 { 126 skipMeasure_ = false; 127 } 128 SetNeedLayout()129 void SetNeedLayout() 130 { 131 skipLayout_ = false; 132 } 133 SkipMeasure()134 bool SkipMeasure() override 135 { 136 return skipMeasure_; 137 } 138 SkipLayout()139 bool SkipLayout() override 140 { 141 return skipLayout_; 142 } 143 GetLayoutAlgorithm()144 const RefPtr<LayoutAlgorithm>& GetLayoutAlgorithm() const 145 { 146 return layoutAlgorithm_; 147 } 148 SetLayoutAlgorithm(RefPtr<LayoutAlgorithm> algorithm)149 void SetLayoutAlgorithm(RefPtr<LayoutAlgorithm> algorithm) 150 { 151 layoutAlgorithm_ = std::move(algorithm); 152 } 153 IsExpire()154 bool IsExpire() const 155 { 156 return frameId != UITaskScheduler::GetFrameId(); 157 } 158 SetPercentHeight(bool value)159 void SetPercentHeight(bool value) 160 { 161 percentHeight_ = value; 162 } 163 SetPercentWidth(bool value)164 void SetPercentWidth(bool value) 165 { 166 percentWidth_ = value; 167 } 168 GetPercentHeight()169 bool GetPercentHeight() const 170 { 171 return percentHeight_; 172 } 173 GetPercentWidth()174 bool GetPercentWidth() const 175 { 176 return percentWidth_; 177 } 178 179 void SetAbsLayoutAlgorithm(const RefPtr<Kit::LayoutAlgorithm>& absLayoutAlgorithm); 180 181 private: 182 RefPtr<LayoutAlgorithm> layoutAlgorithm_; 183 184 bool skipMeasure_ = false; 185 bool skipLayout_ = false; 186 bool percentHeight_ = false; 187 bool percentWidth_ = false; 188 uint64_t frameId = UITaskScheduler::GetFrameId(); 189 RefPtr<Kit::LayoutAlgorithm> absLayoutAlgorithm_; 190 191 ACE_DISALLOW_COPY_AND_MOVE(LayoutAlgorithmWrapper); 192 }; 193 } // namespace OHOS::Ace::NG 194 195 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_LAYOUTS_LAYOUT_ALGORITHM_H 196