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_RENDER_GRID_LAYOUT_ITEM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_GRID_LAYOUT_RENDER_GRID_LAYOUT_ITEM_H
18 
19 #include "core/pipeline/base/render_node.h"
20 
21 namespace OHOS::Ace {
22 
23 class RenderGridLayoutItem : public RenderNode {
24     DECLARE_ACE_TYPE(RenderGridLayoutItem, RenderNode);
25 
26 public:
27     using OnItemLongPressed = std::function<bool(int32_t, const WeakPtr<RenderNode>&)>;
28     using OnSelectFunc = std::function<void(bool)>;
29 
30     static RefPtr<RenderNode> Create();
31 
32     void Update(const RefPtr<Component>& component) override;
33 
34     void PerformLayout() override;
35 
36     void HandleOnFocus();
37 
38     void SetColumnIndex(int32_t columnIndex);
39     void SetRowIndex(int32_t rowIndex);
40     void SetColumnSpan(int32_t columnSpan);
41     void SetRowSpan(int32_t rowSpan);
SetForceRebuild(bool forceRebuild)42     void SetForceRebuild(bool forceRebuild)
43     {
44         forceRebuild_ = forceRebuild;
45     }
46 
GetColumnStart()47     int32_t GetColumnStart() const
48     {
49         return columnIndex_;
50     }
GetRowStart()51     int32_t GetRowStart() const
52     {
53         return rowIndex_;
54     }
GetColumnEnd()55     int32_t GetColumnEnd() const
56     {
57         return columnIndex_ + columnSpan_;
58     }
GetRowEnd()59     int32_t GetRowEnd() const
60     {
61         return rowIndex_ + rowSpan_;
62     }
GetForceRebuild()63     bool GetForceRebuild() const
64     {
65         return forceRebuild_;
66     }
67 
SetBoundary()68     void SetBoundary()
69     {
70         TakeBoundary();
71     }
72 
GetColumnIndex()73     int32_t GetColumnIndex() const
74     {
75         return columnIndex_;
76     }
77 
GetRowIndex()78     int32_t GetRowIndex() const
79     {
80         return rowIndex_;
81     }
82 
GetColumnSpan()83     int32_t GetColumnSpan() const
84     {
85         return columnSpan_;
86     }
87 
GetRowSpan()88     int32_t GetRowSpan() const
89     {
90         return rowSpan_;
91     }
92 
SetIndex(int32_t index)93     void SetIndex(int32_t index)
94     {
95         index_ = index;
96     }
97 
ForceRebuild()98     bool ForceRebuild() const
99     {
100         return forceRebuild_;
101     }
102 
GetIndex()103     int32_t GetIndex() const
104     {
105         return index_;
106     }
107 
108     void OnLongPressEvent();
109 
110     void SetOnItemLongPressed(const OnItemLongPressed& func);
111 
112     RefPtr<Animator> GetAnimationController();
113     bool AnimationAddInterpolator(const RefPtr<Animation<Point>>& animation);
114     void AnimationPlay();
115 
GetSelectable()116     bool GetSelectable() const
117     {
118         return selectable_;
119     }
120 
GetOnSelectId()121     OnSelectFunc GetOnSelectId() const
122     {
123         return onSelectId_;
124     }
125 
MarkIsSelected(bool isSelected)126     void MarkIsSelected(bool isSelected)
127     {
128         isSelected_ = isSelected;
129     }
130 
IsSelected()131     bool IsSelected() const
132     {
133         return isSelected_;
134     }
135 
SetGridItemWidth(const Dimension & width)136     void SetGridItemWidth(const Dimension& width)
137     {
138         itemWidth_ = width;
139     }
140 
GetGridItemWidth()141     const Dimension& GetGridItemWidth() const
142     {
143         return itemWidth_;
144     }
145 
SetGridItemHeight(const Dimension & height)146     void SetGridItemHeight(const Dimension& height)
147     {
148         itemHeight_ = height;
149     }
150 
GetGridItemHeight()151     const Dimension& GetGridItemHeight() const
152     {
153         return itemHeight_;
154     }
155 
156 private:
157     int32_t index_ = -1;
158     int32_t columnIndex_ = -1;
159     int32_t rowIndex_ = -1;
160     int32_t columnSpan_ = 1;
161     int32_t rowSpan_ = 1;
162     bool forceRebuild_ = false;
163     OnItemLongPressed OnItemLongPressed_;
164     RefPtr<Animator> animationController_;
165     void InitAnimationController(const WeakPtr<PipelineContext>& context);
166     OnSelectFunc onSelectId_;
167     bool selectable_ = true;
168     bool isSelected_ = false;
169     Dimension itemWidth_ {-1};
170     Dimension itemHeight_ {-1};
171 };
172 
173 } // namespace OHOS::Ace
174 
175 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_GRID_LAYOUT_RENDER_GRID_LAYOUT_ITEM_H
176