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_WRAPPER_BUILDER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_LAYOUTS_LAYOUT_WRAPPER_BUILDER_H
18 
19 #include <list>
20 #include <map>
21 #include <optional>
22 #include <unordered_map>
23 
24 #include "base/memory/ace_type.h"
25 #include "base/utils/noncopyable.h"
26 #include "core/components_ng/property/layout_constraint.h"
27 
28 namespace OHOS::Ace::NG {
29 
30 class LayoutWrapper;
31 
32 class LayoutWrapperBuilder : public AceType {
33     DECLARE_ACE_TYPE(LayoutWrapperBuilder, AceType)
34 public:
35     LayoutWrapperBuilder() = default;
36     ~LayoutWrapperBuilder() override = default;
37 
38     RefPtr<LayoutWrapper> GetOrCreateWrapperByIndex(int32_t index);
39 
40     virtual const std::list<RefPtr<LayoutWrapper>>& GetCachedChildLayoutWrapper() = 0;
41 
42     const std::list<RefPtr<LayoutWrapper>>& ExpandAllChildWrappers();
43 
44     void RemoveAllChildInRenderTree();
45 
GetTotalCount()46     int32_t GetTotalCount()
47     {
48         return OnGetTotalCount();
49     }
50 
SetStartIndex(int32_t startIndex)51     void SetStartIndex(int32_t startIndex)
52     {
53         startIndex_ = startIndex;
54     }
55 
GetStartIndex()56     int32_t GetStartIndex() const
57     {
58         return startIndex_;
59     }
60 
61     void SetCacheCount(int32_t cacheCount, const std::optional<LayoutConstraintF>& itemConstraint = std::nullopt)
62     {
63         cacheCount_ = cacheCount < 0 ? 1 : cacheCount;
64         itemConstraint_ = itemConstraint;
65     }
66 
SetLongPredictTask()67     void SetLongPredictTask()
68     {
69         useLongPredictTask_ = true;
70     }
71 
SwapDirtyAndUpdateBuildCache()72     virtual void SwapDirtyAndUpdateBuildCache() {}
73 
AdjustGridOffset()74     virtual void AdjustGridOffset() {}
75 
76 protected:
77     virtual int32_t OnGetTotalCount() = 0;
78     virtual RefPtr<LayoutWrapper> OnGetOrCreateWrapperByIndex(int32_t index) = 0;
79     virtual const std::list<RefPtr<LayoutWrapper>>& OnExpandChildLayoutWrapper() = 0;
80 
81     std::unordered_map<int32_t, RefPtr<LayoutWrapper>> wrapperMap_;
82 
83     std::optional<LayoutConstraintF> itemConstraint_;
84     bool useLongPredictTask_ = false;
85 
86     int32_t startIndex_ = 0;
87     int32_t cacheCount_ = 0;
88 
89     ACE_DISALLOW_COPY_AND_MOVE(LayoutWrapperBuilder);
90 };
91 
92 } // namespace OHOS::Ace::NG
93 
94 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_LAYOUTS_LAYOUT_WRAPPER_BUILDER_H
95