2 * Copyright(c) 2019 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using System.ComponentModel;
19 namespace Tizen.NUI.Components
22 /// Layout collection of views in a grid.
24 /// <since_tizen> 6 </since_tizen>
25 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
26 [EditorBrowsable(EditorBrowsableState.Never)]
27 public class GridLayoutManager : LinearLayoutManager
29 private const int DEFAULT_SPAN_COUNT = -1;
31 private int mSpanCount = DEFAULT_SPAN_COUNT;
34 /// Creates a GridLayoutManager with orientation.
36 /// <param name="spanCount">The number of columns or rows in the grid</param>
37 /// <param name="orientation">Layout orientation.Should be HORIZONTAL or VERTICAL</param>
38 /// <since_tizen> 6 </since_tizen>
39 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
40 [EditorBrowsable(EditorBrowsableState.Never)]
41 public GridLayoutManager(int spanCount, int orientation) : base(orientation)
43 mSpanCount = spanCount;
46 internal override void EnsureAnchorReady(FlexibleViewRecycler recycler, AnchorInfo anchorInfo, int itemDirection)
48 bool layingOutInPrimaryDirection = (itemDirection == LayoutState.ITEM_DIRECTION_TAIL);
49 int span = anchorInfo.Position % mSpanCount;
50 if (layingOutInPrimaryDirection)
53 while (span > 0 && anchorInfo.Position > 0)
55 anchorInfo.Position--;
56 span = anchorInfo.Position;
61 // choose the max span we can get. hopefully last one
62 int indexLimit = ChildCount - 1;
63 int pos = anchorInfo.Position;
65 while (pos < indexLimit)
67 int next = (pos + 1) % mSpanCount;
78 anchorInfo.Position = pos;
83 /// Retrieves a position that neighbor to current position by direction.
85 /// <param name="position">The anchor adapter position</param>
86 /// <param name="direction">The direction.</param>
87 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
88 [EditorBrowsable(EditorBrowsableState.Never)]
89 protected override int GetNextPosition(int position, FlexibleViewLayoutManager.Direction direction)
91 if (mOrientation == HORIZONTAL)
95 case FlexibleViewLayoutManager.Direction.Left:
96 if (position >= mSpanCount)
98 return position - mSpanCount;
101 case FlexibleViewLayoutManager.Direction.Right:
102 if (position < ItemCount - mSpanCount)
104 return position + mSpanCount;
107 case FlexibleViewLayoutManager.Direction.Up:
108 if (position % mSpanCount > 0)
113 case FlexibleViewLayoutManager.Direction.Down:
114 if (position < ItemCount - 1 && (position % mSpanCount < mSpanCount - 1))
125 case FlexibleViewLayoutManager.Direction.Left:
126 if (position % mSpanCount > 0)
131 case FlexibleViewLayoutManager.Direction.Right:
132 if (position < ItemCount - 1 && (position % mSpanCount < mSpanCount - 1))
137 case FlexibleViewLayoutManager.Direction.Up:
138 if (position >= mSpanCount)
140 return position - mSpanCount;
143 case FlexibleViewLayoutManager.Direction.Down:
144 if (position < ItemCount - mSpanCount)
146 return position + mSpanCount;
155 internal override void LayoutChunk(FlexibleViewRecycler recycler,
156 LayoutState layoutState, LayoutChunkResult result)
158 bool layingOutInPrimaryDirection =
159 layoutState.ItemDirection == LayoutState.ITEM_DIRECTION_TAIL;
161 int count = mSpanCount;
162 for (int i = 0; i < count; i++)
164 FlexibleViewViewHolder holder = layoutState.Next(recycler);
167 result.Finished = true;
171 if (layingOutInPrimaryDirection)
176 result.Consumed = mOrientationHelper.GetViewHolderMeasurement(holder);
178 float left, top, width, height;
179 if (mOrientation == VERTICAL)
181 width = (Width - PaddingLeft - PaddingRight) / count;
182 height = result.Consumed;
183 if (layoutState.LayoutDirection == LayoutState.LAYOUT_END)
185 left = PaddingLeft + width * i;
186 top = layoutState.Offset;
190 left = PaddingLeft + width * (count - 1 - i);
191 top = layoutState.Offset - height;
193 LayoutChild(holder, left, top, width, height);
197 width = result.Consumed;
198 height = (Height - PaddingTop - PaddingBottom) / count;
199 if (layoutState.LayoutDirection == LayoutState.LAYOUT_END)
201 top = PaddingTop + height * i;
202 left = layoutState.Offset;
206 top = PaddingTop + height * (count - 1 - i);
207 left = layoutState.Offset - width;
209 LayoutChild(holder, left, top, width, height);