/
ItemsLayouter.cs
executable file
·391 lines (349 loc) · 14.6 KB
/
ItemsLayouter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* Copyright (c) 2021 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using Tizen.NUI.BaseComponents;
using System.Collections.Generic;
using System.ComponentModel;
namespace Tizen.NUI.Components
{
/// <summary>
/// Default layout manager for RecyclerView.
/// Layouting RecyclerViewItem on the scroll ContentContainer
/// which need to be visible on the view by scroll position.
/// </summary>
/// <since_tizen> 9 </since_tizen>
public abstract class ItemsLayouter : ICollectionChangedNotifier, IDisposable
{
private bool disposed = false;
private Extents padding = new Extents(0, 0, 0, 0);
/// <summary>
/// Padding for ContentContainer of RecyclerView.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public Extents Padding {
get
{
return padding;
}
set
{
padding = value;
if (ItemsView?.ContentContainer != null)
{
ItemsView.Padding = padding;
}
}
}
/// <summary>
/// Container which contains ViewItems.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
protected View Container { get; set; }
/// <summary>
/// Parent ItemsView.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
protected RecyclerView ItemsView { get; set; }
/// <summary>
/// The last scrolled position which is calculated by ScrollableBase. The value should be updated in the Recycle() method.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
protected float PrevScrollPosition { get; set; }
/// <summary>
/// First index of visible items.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
protected int FirstVisible { get; set; } = -1;
/// <summary>
/// Last index of visible items.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
protected int LastVisible { get; set; } = -1;
/// <summary>
/// Visible ViewItem.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
protected List<RecyclerViewItem> VisibleItems { get; } = new List<RecyclerViewItem>();
/// <summary>
/// Flag of layouter initialization.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
protected bool IsInitialized { get; set; } = false;
/// <summary>
/// Candidate item step size for scroll size measure.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
protected float StepCandidate { get; set; }
/// <summary>
/// Candidate item's Margin for scroll size measure.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
protected Extents CandidateMargin { get; set; }
/// <summary>
/// Content size of scrollable.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
protected float ScrollContentSize { get; set; }
/// <summary>
/// boolean flag of scrollable horizontal direction.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
protected bool IsHorizontal { get; set; }
/// <summary>
/// Clean up ItemsLayouter.
/// </summary>
/// <param name="view"> ItemsView of layouter.</param>
/// <since_tizen> 9 </since_tizen>
public virtual void Initialize(RecyclerView view)
{
ItemsView = view ?? throw new ArgumentNullException(nameof(view));
Container = view.ContentContainer;
PrevScrollPosition = 0.0f;
IsHorizontal = (view.ScrollingDirection == ScrollableBase.Direction.Horizontal);
IsInitialized = true;
}
/// <summary>
/// This is called to find out where items are lain out according to current scroll position.
/// </summary>
/// <param name="scrollPosition">Scroll position which is calculated by ScrollableBase</param>
/// <param name="force">boolean force flag to layouting forcely.</param>
/// <since_tizen> 9 </since_tizen>
public virtual void RequestLayout(float scrollPosition, bool force = false)
{
// Layouting Items in scrollPosition.
}
/// <summary>
/// Clear the current screen and all properties.
/// </summary>
/// <since_tizen> 9 </since_tizen>
public virtual void Clear()
{
if (VisibleItems != null)
{
foreach (RecyclerViewItem item in VisibleItems)
{
if (ItemsView != null) ItemsView.UnrealizeItem(item, false);
}
VisibleItems.Clear();
}
if (CandidateMargin != null)
{
CandidateMargin.Dispose();
CandidateMargin = null;
}
if (Container)
{
if (ItemsView != null) Container.Size = ItemsView.Size;
Container.Position = new Position(0.0f, 0.0f);
Container = null;
}
ItemsView = null;
}
/// <summary>
/// This is called to find out how much container size can be.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual float CalculateLayoutOrientationSize()
{
return 0.0f;
}
/// <summary>
/// Adjust scrolling position by own scrolling rules.
/// </summary>
/// <param name="scrollPosition">Scroll position which is calculated by ScrollableBase</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual float CalculateCandidateScrollPosition(float scrollPosition)
{
return scrollPosition;
}
/// <summary>
/// Notify the relayout of ViewItem.
/// </summary>
/// <param name="item">updated ViewItem.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual void NotifyItemSizeChanged(RecyclerViewItem item)
{
}
/// <summary>
/// Notify the dataset is Changed.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual void NotifyDataSetChanged()
{
Initialize(ItemsView);
}
/// <summary>
/// Notify the observable item in startIndex is changed.
/// </summary>
/// <param name="source">Dataset source.</param>
/// <param name="startIndex">Changed item index.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual void NotifyItemChanged(IItemSource source, int startIndex)
{
}
/// <summary>
/// Notify the observable item is inserted in dataset.
/// </summary>
/// <param name="source">Dataset source.</param>
/// <param name="startIndex">Inserted item index.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual void NotifyItemInserted(IItemSource source, int startIndex)
{
}
/// <summary>
/// Notify the observable item is moved from fromPosition to ToPosition.
/// </summary>
/// <param name="source">Dataset source.</param>
/// <param name="fromPosition">Previous item position.</param>
/// <param name="toPosition">Moved item position.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual void NotifyItemMoved(IItemSource source, int fromPosition, int toPosition)
{
}
/// <summary>
/// Notify the range of the observable items are moved from fromPosition to ToPosition.
/// </summary>
/// <param name="source"></param>
/// <param name="fromPosition"></param>
/// <param name="toPosition"></param>
/// <param name="count"></param>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual void NotifyItemRangeMoved(IItemSource source, int fromPosition, int toPosition, int count)
{
}
/// <summary>
/// Notify the range of observable items from start to end are changed.
/// </summary>
/// <param name="source">Dataset source.</param>
/// <param name="startRange">Start index of changed items range.</param>
/// <param name="endRange">End index of changed items range.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual void NotifyItemRangeChanged(IItemSource source, int startRange, int endRange)
{
}
/// <summary>
/// Notify the count range of observable items are inserted in startIndex.
/// </summary>
/// <param name="source">Dataset source.</param>
/// <param name="startIndex">Start index of inserted items range.</param>
/// <param name="count">The number of inserted items.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual void NotifyItemRangeInserted(IItemSource source, int startIndex, int count)
{
}
/// <summary>
/// Notify the count range of observable items from the startIndex are removed.
/// </summary>
/// <param name="source">Dataset source.</param>
/// <param name="startIndex">Start index of removed items range.</param>
/// <param name="count">The number of removed items</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual void NotifyItemRangeRemoved(IItemSource source, int startIndex, int count)
{
}
/// <summary>
/// Notify the observable item in startIndex is removed.
/// </summary>
/// <param name="source">Dataset source.</param>
/// <param name="startIndex">Index of removed item.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual void NotifyItemRemoved(IItemSource source, int startIndex)
{
}
/// <summary>
/// Gets the next keyboard focusable view in this control towards the given direction.<br />
/// A control needs to override this function in order to support two dimensional keyboard navigation.<br />
/// </summary>
/// <param name="currentFocusedView">The current focused view.</param>
/// <param name="direction">The direction to move the focus towards.</param>
/// <param name="loopEnabled">Whether the focus movement should be looped within the control.</param>
/// <returns>The next keyboard focusable view in this control or an empty handle if no view can be focused.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual View RequestNextFocusableView(View currentFocusedView, View.FocusDirection direction, bool loopEnabled)
{
return null;
}
/// <summary>
/// Dispose ItemsLayouter and all children on it.
/// </summary>
/// <since_tizen> 9 </since_tizen>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Measure the size of child ViewItem manually.
/// </summary>
/// <param name="parent">Parent ItemsView.</param>
/// <param name="child">Child ViewItem to Measure()</param>
[EditorBrowsable(EditorBrowsableState.Never)]
protected virtual void MeasureChild(RecyclerView parent, RecyclerViewItem child)
{
if (parent == null) throw new ArgumentNullException(nameof(parent));
if (child == null) throw new ArgumentNullException(nameof(child));
if (child.Layout == null) return;
//FIXME: This measure can be restricted size of child to be less than parent size.
// but our parent can be not calculated yet, also some child can be bigger than it's parent size,
// so we use implicit value 9999 as restricted specification.
MeasureSpecification childWidthMeasureSpec = LayoutGroup.GetChildMeasureSpecification(
new MeasureSpecification(new LayoutLength(9999), MeasureSpecification.ModeType.Exactly),
new LayoutLength(0),
new LayoutLength(child.WidthSpecification));
MeasureSpecification childHeightMeasureSpec = LayoutGroup.GetChildMeasureSpecification(
new MeasureSpecification(new LayoutLength(9999), MeasureSpecification.ModeType.Exactly),
new LayoutLength(0),
new LayoutLength(child.HeightSpecification));
child.Layout.Measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
/// <summary>
/// Find consecutive visible items index.
/// </summary>
/// <param name="visibleArea">float turple of visible area start position to end position. </param>
/// <return>int turple of start index to end index</return>
[EditorBrowsable(EditorBrowsableState.Never)]
protected virtual (int start, int end) FindVisibleItems((float X, float Y) visibleArea)
{
return (0, 0);
}
/// <summary>
/// Dispose ItemsLayouter and all children on it.
/// </summary>
/// <param name="disposing">true when it disposed by Dispose(). </param>
[EditorBrowsable(EditorBrowsableState.Never)]
protected virtual void Dispose(bool disposing)
{
if (disposed)
{
return;
}
disposed = true;
if (disposing)
{
Clear();
padding.Dispose();
}
}
internal virtual (float X, float Y) GetItemPosition(int index)
{
return (0, 0);
}
internal virtual (float Width, float Height) GetItemSize(int index)
{
return (0, 0);
}
}
}