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 MeasureInNextFrame()74 virtual bool MeasureInNextFrame() const 75 { 76 return false; 77 } 78 SetHasMeasured(bool measured)79 void SetHasMeasured(bool measured) 80 { 81 hasMeasured_ = measured; 82 } 83 GetHasMeasured()84 bool GetHasMeasured() 85 { 86 return hasMeasured_; 87 } 88 89 void SetNeedPostponeForIgnore(bool needed = true) 90 { 91 postponeForIgnore_ = needed; 92 } 93 GetNeedPostponeForIgnore()94 bool GetNeedPostponeForIgnore() const 95 { 96 return postponeForIgnore_; 97 } 98 99 protected: OnReset()100 virtual void OnReset() {} 101 bool hasMeasured_ = false; 102 bool postponeForIgnore_ = false; 103 104 ACE_DISALLOW_COPY_AND_MOVE(LayoutAlgorithm); 105 }; 106 107 class ACE_EXPORT LayoutAlgorithmWrapper : public LayoutAlgorithm { 108 DECLARE_ACE_TYPE(LayoutAlgorithmWrapper, LayoutAlgorithm); 109 110 public: 111 explicit LayoutAlgorithmWrapper( 112 const RefPtr<LayoutAlgorithm>& layoutAlgorithmT, bool skipMeasure = false, bool skipLayout = false); 113 ~LayoutAlgorithmWrapper() override; 114 115 static RefPtr<LayoutAlgorithmWrapper> CreateLayoutAlgorithmWrapper( 116 const RefPtr<Kit::LayoutAlgorithm>& layoutAlgorithm); 117 OnReset()118 void OnReset() override 119 { 120 layoutAlgorithm_.Reset(); 121 skipMeasure_ = false; 122 skipLayout_ = false; 123 } 124 125 std::optional<SizeF> MeasureContent( 126 const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper) override; 127 void Measure(LayoutWrapper* layoutWrapper) override; 128 void Layout(LayoutWrapper* layoutWrapper) override; 129 SetSkipMeasure()130 void SetSkipMeasure() 131 { 132 skipMeasure_ = true; 133 } 134 SetSkipLayout()135 void SetSkipLayout() 136 { 137 skipLayout_ = true; 138 } 139 SetNeedMeasure()140 void SetNeedMeasure() 141 { 142 skipMeasure_ = false; 143 } 144 SetNeedLayout()145 void SetNeedLayout() 146 { 147 skipLayout_ = false; 148 } 149 SkipMeasure()150 bool SkipMeasure() override 151 { 152 return skipMeasure_; 153 } 154 SkipLayout()155 bool SkipLayout() override 156 { 157 return skipLayout_; 158 } 159 MeasureInNextFrame()160 bool MeasureInNextFrame() const override 161 { 162 return layoutAlgorithm_ && layoutAlgorithm_->MeasureInNextFrame(); 163 } 164 GetLayoutAlgorithm()165 const RefPtr<LayoutAlgorithm>& GetLayoutAlgorithm() const 166 { 167 return layoutAlgorithm_; 168 } 169 SetLayoutAlgorithm(RefPtr<LayoutAlgorithm> algorithm)170 void SetLayoutAlgorithm(RefPtr<LayoutAlgorithm> algorithm) 171 { 172 layoutAlgorithm_ = std::move(algorithm); 173 } 174 IsExpire()175 bool IsExpire() const 176 { 177 return frameId != UITaskScheduler::GetFrameId(); 178 } 179 SetPercentHeight(bool value)180 void SetPercentHeight(bool value) 181 { 182 percentHeight_ = value; 183 } 184 SetPercentWidth(bool value)185 void SetPercentWidth(bool value) 186 { 187 percentWidth_ = value; 188 } 189 GetPercentHeight()190 bool GetPercentHeight() const 191 { 192 return percentHeight_; 193 } 194 GetPercentWidth()195 bool GetPercentWidth() const 196 { 197 return percentWidth_; 198 } 199 200 void SetAbsLayoutAlgorithm(const RefPtr<Kit::LayoutAlgorithm>& absLayoutAlgorithm); 201 202 private: 203 RefPtr<LayoutAlgorithm> layoutAlgorithm_; 204 205 bool skipMeasure_ = false; 206 bool skipLayout_ = false; 207 bool percentHeight_ = false; 208 bool percentWidth_ = false; 209 uint64_t frameId = UITaskScheduler::GetFrameId(); 210 RefPtr<Kit::LayoutAlgorithm> absLayoutAlgorithm_; 211 212 ACE_DISALLOW_COPY_AND_MOVE(LayoutAlgorithmWrapper); 213 }; 214 } // namespace OHOS::Ace::NG 215 216 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_LAYOUTS_LAYOUT_ALGORITHM_H 217