1 /*
2  * Copyright (c) 2021-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_GRID_LAYOUT_GRID_LAYOUT_ITEM_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_GRID_LAYOUT_GRID_LAYOUT_ITEM_COMPONENT_H
18 
19 #include "base/utils/macros.h"
20 #include "core/pipeline/base/sole_child_component.h"
21 
22 namespace OHOS::Ace {
23 
24 using OnSelectFunc = std::function<void(bool)>;
25 using DeepRenderFunc = std::function<RefPtr<Component>()>;
26 
27 class ACE_EXPORT GridLayoutItemComponent : public SoleChildComponent {
28     DECLARE_ACE_TYPE(GridLayoutItemComponent, SoleChildComponent);
29 
30 public:
31     GridLayoutItemComponent() = default;
GridLayoutItemComponent(const RefPtr<Component> & child)32     explicit GridLayoutItemComponent(const RefPtr<Component>& child)
33         : SoleChildComponent(child) {}
34     ~GridLayoutItemComponent() override = default;
35 
36     RefPtr<RenderNode> CreateRenderNode() override;
37 
38     RefPtr<Element> CreateElement() override;
39 
40     void SetColumnIndex(int32_t columnIndex);
41     void SetRowIndex(int32_t rowIndex);
42     void SetColumnSpan(int32_t columnSpan);
43     void SetRowSpan(int32_t rowSpan);
SetForceRebuild(bool forceRebuild)44     void SetForceRebuild(bool forceRebuild)
45     {
46         forceRebuild_ = forceRebuild;
47     }
48 
SetClickedEventId(const EventMarker & eventId)49     void SetClickedEventId(const EventMarker& eventId)
50     {
51         clickEventId_ = eventId;
52     }
53 
GetClickedEventId()54     const EventMarker& GetClickedEventId() const
55     {
56         return clickEventId_;
57     }
58 
GetColumnIndex()59     int32_t GetColumnIndex() const
60     {
61         return columnIndex_;
62     }
63 
GetRowIndex()64     int32_t GetRowIndex() const
65     {
66         return rowIndex_;
67     }
68 
GetColumnSpan()69     int32_t GetColumnSpan() const
70     {
71         return columnSpan_;
72     }
73 
GetRowSpan()74     int32_t GetRowSpan() const
75     {
76         return rowSpan_;
77     }
78 
ForceRebuild()79     bool ForceRebuild() const
80     {
81         return forceRebuild_;
82     }
83 
GetSelectable()84     bool GetSelectable() const
85     {
86         return selectable_;
87     }
88 
SetSelectable(bool selectable)89     void SetSelectable(bool selectable)
90     {
91         selectable_ = selectable;
92     }
93 
GetOnSelectId()94     OnSelectFunc GetOnSelectId() const
95     {
96         return onSelectId_;
97     }
98 
SetOnSelectId(const OnSelectFunc & onSelectId)99     void SetOnSelectId(const OnSelectFunc& onSelectId)
100     {
101         onSelectId_ = onSelectId;
102     }
103 
SetGridItemWidth(const Dimension & width)104     void SetGridItemWidth(const Dimension& width)
105     {
106         itemWidth_ = width;
107     }
108 
GetGridItemWidth()109     const Dimension& GetGridItemWidth() const
110     {
111         return itemWidth_;
112     }
113 
SetGridItemHeight(const Dimension & height)114     void SetGridItemHeight(const Dimension& height)
115     {
116         itemHeight_ = height;
117     }
118 
GetGridItemHeight()119     const Dimension& GetGridItemHeight() const
120     {
121         return itemHeight_;
122     }
123 
124     // transfer ownershop of deepRenderFunc_ to provided GridLayoutItemComponent
MoveDeepRenderFunc(RefPtr<GridLayoutItemComponent> & component)125     void MoveDeepRenderFunc(RefPtr<GridLayoutItemComponent>& component)
126     {
127         component->deepRenderFunc_ = std::move(deepRenderFunc_);
128     }
129 
SetDeepRenderFunc(const DeepRenderFunc & deepRenderFunc)130     void SetDeepRenderFunc(const DeepRenderFunc& deepRenderFunc)
131     {
132         deepRenderFunc_ = deepRenderFunc;
133     }
134 
ExecDeepRender()135     RefPtr<Component> ExecDeepRender() const
136     {
137         return (deepRenderFunc_) ? deepRenderFunc_() : nullptr;
138     }
139 
SetIsLazyCreating(const bool isLazy)140     void SetIsLazyCreating(const bool isLazy)
141     {
142         isLazyCreating_ = isLazy;
143     }
144 
GetIsLazyCreating()145     bool GetIsLazyCreating() const
146     {
147         return isLazyCreating_;
148     }
149 
150 private:
151     EventMarker clickEventId_;
152     int32_t columnIndex_ = -1;
153     int32_t rowIndex_ = -1;
154     int32_t columnSpan_ = 1;
155     int32_t rowSpan_ = 1;
156     bool forceRebuild_ = false;
157     OnSelectFunc onSelectId_;
158     bool selectable_ = true;
159     Dimension itemWidth_ {-1};
160     Dimension itemHeight_ {-1};
161     DeepRenderFunc deepRenderFunc_ = nullptr;
162     bool isLazyCreating_ = false;
163 };
164 
165 } // namespace OHOS::Ace
166 
167 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_GRID_LAYOUT_GRID_LAYOUT_ITEM_COMPONENT_H
168