1 /*
2  * Copyright (c) 2024 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 #include "core/components_ng/pattern/waterflow/layout/water_flow_layout_info_base.h"
17 
18 #include "core/components_ng/pattern/waterflow/layout/sliding_window/water_flow_layout_info_sw.h"
19 #include "core/components_ng/pattern/waterflow/layout/top_down/water_flow_layout_info.h"
20 #include "core/components_ng/pattern/waterflow/layout/water_flow_layout_algorithm_base.h"
21 #include "core/components_ng/property/calc_length.h"
22 #include "core/components_ng/property/measure_utils.h"
23 namespace OHOS::Ace::NG {
Create(WaterFlowLayoutMode mode)24 RefPtr<WaterFlowLayoutInfoBase> WaterFlowLayoutInfoBase::Create(WaterFlowLayoutMode mode)
25 {
26     switch (mode) {
27         case WaterFlowLayoutMode::SLIDING_WINDOW:
28             return MakeRefPtr<WaterFlowLayoutInfoSW>();
29         default:
30             return MakeRefPtr<WaterFlowLayoutInfo>();
31     }
32 }
33 
GetSegment(int32_t itemIdx) const34 int32_t WaterFlowLayoutInfoBase::GetSegment(int32_t itemIdx) const
35 {
36     if (segmentTails_.empty() || itemIdx < 0) {
37         return 0;
38     }
39     auto cache = segmentCache_.find(itemIdx);
40     if (cache != segmentCache_.end()) {
41         return cache->second;
42     }
43 
44     auto it = std::lower_bound(segmentTails_.begin(), segmentTails_.end(), itemIdx);
45     if (it == segmentTails_.end()) {
46         return static_cast<int32_t>(segmentTails_.size()) - 1;
47     }
48     int32_t idx = it - segmentTails_.begin();
49     segmentCache_[itemIdx] = idx;
50     return idx;
51 }
52 
InitMargins(const std::vector<WaterFlowSections::Section> & sections,const ScaleProperty & scale,float percentWidth)53 void WaterFlowLayoutInfoBase::InitMargins(
54     const std::vector<WaterFlowSections::Section>& sections, const ScaleProperty& scale, float percentWidth)
55 {
56     size_t n = sections.size();
57     if (n == 0) {
58         return;
59     }
60     margins_.resize(n);
61     for (size_t i = 0; i < n; ++i) {
62         if (sections[i].margin) {
63             margins_[i] = ConvertToMarginPropertyF(*sections[i].margin, scale, percentWidth);
64         }
65     }
66 }
67 
UpdateDefaultCachedCount()68 void WaterFlowLayoutInfoBase::UpdateDefaultCachedCount()
69 {
70     static float pageCount = SystemProperties::GetPageCount();
71     if (pageCount <= 0.0f) {
72         return;
73     }
74     int32_t itemCount = endIndex_ - startIndex_ + 1;
75     if (itemCount <= 0) {
76         return;
77     }
78     constexpr int32_t MAX_DEFAULT_CACHED_COUNT = 16;
79     int32_t newCachedCount = static_cast<int32_t>(ceil(pageCount * itemCount));
80     if (newCachedCount > MAX_DEFAULT_CACHED_COUNT) {
81         TAG_LOGI(AceLogTag::ACE_WATERFLOW, "Default cachedCount exceed 16");
82         defCachedCount_ = MAX_DEFAULT_CACHED_COUNT;
83     } else {
84         defCachedCount_ = std::max(newCachedCount, defCachedCount_);
85     }
86 }
87 } // namespace OHOS::Ace::NG
88