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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_WATERFLOW_WATER_FLOW_LAYOUT_BASE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_WATERFLOW_WATER_FLOW_LAYOUT_BASE_H
18 #include "core/components_ng/base/frame_node.h"
19 #include "core/components_ng/layout/layout_algorithm.h"
20 #include "core/components_ng/pattern/waterflow/layout/water_flow_layout_info_base.h"
21 
22 namespace OHOS::Ace::NG {
23 class WaterFlowLayoutBase : public LayoutAlgorithm {
24     DECLARE_ACE_TYPE(WaterFlowLayoutBase, LayoutAlgorithm);
25 
26 public:
27     virtual void SetCanOverScroll(bool canOverScroll) = 0;
28 
StartCacheLayout()29     virtual void StartCacheLayout()
30     { /* callback when cache layout starts */
31     }
32     /**
33      * @brief Measure the cache item and append it to a lane.
34      *
35      * @param itemIdx index of the cache item to add.
36      * @param deadline of the preload task. Return early if deadline is reached.
37      * @return true if the item is actually created and appended in lane.
38      */
39     virtual bool PreloadItem(LayoutWrapper* host, int32_t itemIdx, int64_t deadline) = 0;
40 
EndCacheLayout()41     virtual void EndCacheLayout()
42     { /* callback when cache layout ends */
43     }
44 
45 protected:
46     /**
47      * @brief Register an IdleTask to preload (create/measure/layout) items in cache range.
48      */
49     void PreloadItems(LayoutWrapper* host, const RefPtr<WaterFlowLayoutInfoBase>& info, int32_t cacheCount);
50 
51     /**
52      * @brief immediately create & measure items in cache range.
53      * need sync load to display cache items on screen.
54      */
55     void SyncPreloadItems(LayoutWrapper* host, const RefPtr<WaterFlowLayoutInfoBase>& info, int32_t cacheCount);
56 
57     static int32_t GetUpdateIdx(LayoutWrapper* host, int32_t footerIdx);
58 
59 private:
60     /**
61      * @brief immediately create / measure a cache item during LayoutTask
62      */
63     virtual void SyncPreloadItem(LayoutWrapper* host, int32_t itemIdx) = 0;
64 
65     /**
66      * @param force true if force to preload all items in cache range.
67      */
68     static std::list<int32_t> GeneratePreloadList(
69         const RefPtr<WaterFlowLayoutInfoBase>& info, LayoutWrapper* host, int32_t cacheCount, bool force);
70     static void PostIdleTask(const RefPtr<FrameNode>& frameNode);
71 };
72 
73 enum class WaterFlowLayoutMode { TOP_DOWN = 0, SLIDING_WINDOW = 1 };
74 } // namespace OHOS::Ace::NG
75 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_WATERFLOW_WATER_FLOW_LAYOUT_BASE_H