1 /* 2 * Copyright (c) 2024-2025 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_PATTERNS_LIST_LIST_POSITION_MAP_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_LIST_LIST_POSITION_MAP_H 18 19 20 #include <cstddef> 21 #include <cstdint> 22 #include <functional> 23 #include <optional> 24 #include <tuple> 25 #include <queue> 26 27 #include "base/geometry/dimension.h" 28 #include "base/memory/referenced.h" 29 #include "base/utils/utils.h" 30 #include "core/components_ng/base/ui_node.h" 31 #include "core/components_ng/syntax/lazy_for_each_node.h" 32 #include "core/components_ng/syntax/repeat_virtual_scroll_node.h" 33 #include "core/components_ng/syntax/repeat_virtual_scroll_2_node.h" 34 #include "core/components_ng/pattern/list/list_children_main_size.h" 35 #include "core/components_ng/pattern/list/list_item_group_pattern.h" 36 #include "core/components_ng/property/measure_property.h" 37 38 39 namespace OHOS::Ace::NG { 40 class ListItemGroupPattern; 41 42 struct ListPositionInfo { 43 float mainPos; 44 float mainSize; 45 bool isGroup; 46 }; 47 48 enum class ListPosMapUpdate { 49 NO_CHANGE = 0, 50 UPDATE_ALL_SIZE, 51 RE_CALCULATE, 52 }; 53 54 class ListPositionMap : public virtual AceType { 55 DECLARE_ACE_TYPE(ListPositionMap, AceType); 56 public: 57 ListPositionMap() = default; 58 ~ListPositionMap() override = default; 59 UpdatePos(int32_t index,ListPositionInfo posInfo)60 void UpdatePos(int32_t index, ListPositionInfo posInfo) 61 { 62 posMap_[index] = posInfo; 63 } 64 65 void UpdatePosRange(int32_t startIndex, int32_t endIndex, ListPositionInfo posInfo, float space, int32_t lanes); 66 67 void UpdatePosWithCheck(int32_t index, ListPositionInfo posInfo); 68 ClearPosMap()69 void ClearPosMap() 70 { 71 posMap_.clear(); 72 } 73 MarkDirty(ListChangeFlag flag)74 void MarkDirty(ListChangeFlag flag) 75 { 76 dirty_ = dirty_ | flag; 77 } 78 ClearDirty()79 void ClearDirty() 80 { 81 dirty_ = LIST_NO_CHANGE; 82 } 83 GetTotalHeight()84 float GetTotalHeight() const 85 { 86 return totalHeight_; 87 } 88 GetPrevTotalHeight()89 float GetPrevTotalHeight() const 90 { 91 return prevTotalHeight_; 92 } 93 94 ListPosMapUpdate CheckPosMapUpdateRule(); 95 96 void UpdatePosMapStart(float delta, double& listCurrentPos, float space, 97 int32_t startIndex, float startPos, bool groupAtStart); 98 99 void UpdatePosMapEnd(int32_t prevEndIndex, float space, bool groupAtEnd); 100 101 void CalculateUINode(RefPtr<UINode> node); 102 103 std::optional<bool> GetLazyForEachChildIsGroup(RefPtr<UINode> node); 104 105 void CalculateLazyForEachNode(RefPtr<UINode> node); 106 107 void CalculateFrameNode(RefPtr<FrameNode> frameNode); 108 109 void CalculateListItemNode(); 110 111 void CalculateGroupNode(); 112 113 void PosMapRecalculate(LayoutWrapper* layoutWrapper); 114 115 void GroupPosMapRecalculate(); 116 117 virtual void UpdatePosMap(LayoutWrapper* layoutWrapper, int32_t lanes, float space, 118 RefPtr<ListChildrenMainSize>& childrenSize); 119 120 void UpdateGroupPosMap(int32_t totalCount, int32_t lanes, float space, 121 RefPtr<ListChildrenMainSize>& childrenSize, float headerSize, float footerSize); 122 123 void UpdateTotalCount(int32_t totalCount); 124 125 float GetPos(int32_t index, float offset = 0.0f) 126 { 127 return posMap_[index].mainPos - offset; 128 } 129 GetGroupLayoutOffset(int32_t startIndex,float startPos)130 float GetGroupLayoutOffset(int32_t startIndex, float startPos) 131 { 132 return posMap_[startIndex].mainPos - startPos; 133 } 134 135 ListPositionInfo GetPositionInfo(int32_t index) const; 136 137 std::pair<int32_t, float> GetStartIndexAndPos() const; 138 139 std::pair<int32_t, float> GetEndIndexAndPos() const; 140 141 void OptimizeBeforeMeasure(int32_t& beginIndex, float& beginPos, const float offset, const float contentSize); 142 SetChainOffsetCallback(std::function<float (int32_t)> func)143 void SetChainOffsetCallback(std::function<float(int32_t)> func) 144 { 145 chainOffsetFunc_ = std::move(func); 146 } 147 148 int32_t GetRowStartIndex(const int32_t input); 149 150 int32_t GetRowEndIndex(const int32_t input); 151 152 float GetRowHeight(int32_t input); 153 154 std::pair<int32_t, float> GetRowEndIndexAndHeight(const int32_t input); 155 156 void ReversePosMap(); 157 protected: 158 RefPtr<ListChildrenMainSize> childrenSize_; 159 ListChangeFlag dirty_ = LIST_NO_CHANGE; 160 int32_t totalItemCount_ = 0; 161 float space_ = 0.0f; 162 int32_t lanes_ = -1; 163 164 private: 165 std::map<int32_t, ListPositionInfo> posMap_; 166 std::function<float(int32_t)> chainOffsetFunc_; 167 int32_t curLine_ = 0; 168 int32_t curIndex_ = 0; 169 float totalHeight_ = 0.0f; 170 float prevTotalHeight_ = 0.0f; 171 float curRowHeight_ = 0.0f; 172 float headerSize_ = 0.0f; 173 float footerSize_ = 0.0f; 174 }; 175 176 } // namespace OHOS::Ace::NG 177 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_LIST_LIST_POSITION_MAP_H