/
FlexLayout.cs
executable file
·538 lines (473 loc) · 21.2 KB
/
FlexLayout.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/* Copyright (c) 2019 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 System.ComponentModel;
using Tizen.NUI.BaseComponents;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace Tizen.NUI
{
/// <summary>
/// [Draft] This class implements a flex layout.
/// The flex layout implementation is based on open source Facebook Yoga layout engine.
/// For more information about the flex layout API and how to use it please refer to https://yogalayout.com/docs/
/// We implement the subset of the API in the class below.
/// </summary>
public class FlexLayout : LayoutGroup, global::System.IDisposable
{
private static bool LayoutDebugFlex = false; // Debug flag
float Flex{ get; set;}
int AlignSelf{get; set;}
private global::System.Runtime.InteropServices.HandleRef swigCPtr;
private bool swigCMemOwn;
private bool disposed;
private bool isDisposeQueued = false;
private MeasureSpecification parentMeasureSpecificationWidth;
private MeasureSpecification parentMeasureSpecificationHeight;
private IntPtr _rootFlex; // Pointer to the unmanged flex layout class.
internal const float FlexUndefined = 10E20F; // Auto setting which is equivalent to WrapContent.
internal struct MeasuredSize
{
public MeasuredSize(float x, float y)
{
width = x;
height = y;
}
float width;
float height;
};
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate MeasuredSize ChildMeasureCallback( global::System.IntPtr child, float width, int measureModeWidth, float height, int measureModeHeight );
event ChildMeasureCallback measureChildDelegate; // Stores a delegate to the child measure callback. Used for all children of this FlexLayout.
internal FlexLayout(global::System.IntPtr cPtr, bool cMemoryOwn)
{
swigCMemOwn = cMemoryOwn;
swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
_rootFlex = Interop.FlexLayout.FlexLayout_New();
measureChildDelegate = new ChildMeasureCallback(measureChild);
}
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(FlexLayout obj)
{
return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
}
/// <summary>
/// Dispose.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public void Dispose()
{
// Throw exception if Dispose() is called in separate thread.
if (!Window.IsInstalled())
{
throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
}
if (isDisposeQueued)
{
Dispose(DisposeTypes.Implicit);
}
else
{
Dispose(DisposeTypes.Explicit);
System.GC.SuppressFinalize(this);
}
}
/// <summary>
/// Dispose.
/// </summary>
/// <since_tizen> 6 </since_tizen>
protected virtual void Dispose(DisposeTypes type)
{
if (disposed)
{
return;
}
if (type == DisposeTypes.Explicit)
{
// Called by User
// Release your own managed resources here.
// You should release all of your own disposable objects here.
}
// Release your own unmanaged resources here.
// You should not access any managed member here except static instance.
// because the execution order of Finalizes is non-deterministic.
if (swigCPtr.Handle != global::System.IntPtr.Zero)
{
if (swigCMemOwn)
{
swigCMemOwn = false;
Interop.FlexLayout.delete_FlexLayout(swigCPtr);
}
swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
}
disposed = true;
}
/// <summary>
/// [Draft] Creates a FlexLayout object.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public FlexLayout() : this(Interop.FlexLayout.FlexLayout_New(), true)
{
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
internal static FlexLayout DownCast(BaseHandle handle)
{
FlexLayout ret = new FlexLayout(Interop.FlexLayout.FlexLayout_DownCast(BaseHandle.getCPtr(handle)), true);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
internal FlexLayout(FlexLayout other) : this(Interop.FlexLayout.new_FlexLayout__SWIG_1(FlexLayout.getCPtr(other)), true)
{
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
internal FlexLayout Assign(FlexLayout other)
{
FlexLayout ret = new FlexLayout(Interop.FlexLayout.FlexLayout_Assign(swigCPtr, FlexLayout.getCPtr(other)), false);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
internal void SetFlexDirection(FlexLayout.FlexDirection flexDirection)
{
Interop.FlexLayout.FlexLayout_SetFlexDirection(swigCPtr, (int)flexDirection);
RequestLayout();
}
internal FlexLayout.FlexDirection GetFlexDirection()
{
FlexLayout.FlexDirection ret = (FlexLayout.FlexDirection)Interop.FlexLayout.FlexLayout_GetFlexDirection(swigCPtr);
return ret;
}
internal void SetFlexJustification(FlexLayout.FlexJustification flexJustification)
{
Interop.FlexLayout.FlexLayout_SetFlexJustification(swigCPtr, (int)flexJustification);
RequestLayout();
}
internal FlexLayout.FlexJustification GetFlexJustification()
{
FlexLayout.FlexJustification ret = (FlexLayout.FlexJustification)Interop.FlexLayout.FlexLayout_GetFlexJustification(swigCPtr);
return ret;
}
internal void SetFlexWrap(FlexLayout.FlexWrapType flexWrap)
{
Interop.FlexLayout.FlexLayout_SetFlexWrap(swigCPtr, (int)flexWrap);
RequestLayout();
}
internal FlexLayout.FlexWrapType GetFlexWrap()
{
FlexLayout.FlexWrapType ret = (FlexLayout.FlexWrapType)Interop.FlexLayout.FlexLayout_GetFlexWrap(swigCPtr);
return ret;
}
internal void SetFlexAlignment(FlexLayout.AlignmentType flexAlignment)
{
Interop.FlexLayout.FlexLayout_SetFlexAlignment(swigCPtr, (int)flexAlignment);
RequestLayout();
}
internal FlexLayout.AlignmentType GetFlexAlignment()
{
FlexLayout.AlignmentType ret = (FlexLayout.AlignmentType)Interop.FlexLayout.FlexLayout_GetFlexAlignment(swigCPtr);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
internal void SetFlexItemsAlignment(FlexLayout.AlignmentType flexAlignment)
{
Interop.FlexLayout.FlexLayout_SetFlexItemsAlignment(swigCPtr, (int)flexAlignment);
RequestLayout();
}
internal FlexLayout.AlignmentType GetFlexItemsAlignment()
{
FlexLayout.AlignmentType ret = (FlexLayout.AlignmentType)Interop.FlexLayout.FlexLayout_GetFlexItemsAlignment(swigCPtr);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
/// <summary>
/// [Draft] Get/Set the flex direction in the layout.
/// The direction of the main-axis which determines the direction that flex items are laid out.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public FlexDirection Direction
{
get
{
return GetFlexDirection();
}
set
{
SetFlexDirection(value);
}
}
/// <summary>
/// [Draft] Get/Set the justification in the layout.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public FlexJustification Justification
{
get
{
return GetFlexJustification();
}
set
{
SetFlexJustification(value);
}
}
/// <summary>
/// [Draft] Get/Set the wrap in the layout.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public FlexWrapType WrapType
{
get
{
return GetFlexWrap();
}
set
{
SetFlexWrap(value);
}
}
/// <summary>
/// [Draft] Get/Set the alignment of the layout content.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public AlignmentType Alignment
{
get
{
return GetFlexAlignment();
}
set
{
SetFlexAlignment(value);
}
}
/// <summary>
/// [Draft] Get/Set the alignment of the layout items.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public AlignmentType ItemsAlignment
{
get
{
return GetFlexItemsAlignment();
}
set
{
SetFlexItemsAlignment(value);
}
}
/// <summary>
/// [Draft] Enumeration for the direction of the main axis in the flex container.
/// This determines the direction that flex items are laid out in the flex container.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public enum FlexDirection
{
/// <summary>
/// The flexible items are displayed vertically as a column
/// </summary>
Column,
/// <summary>
/// The flexible items are displayed vertically as a column, but in reverse order
/// </summary>
ColumnReverse,
/// <summary>
/// The flexible items are displayed horizontally as a row
/// </summary>
Row,
/// <summary>
/// The flexible items are displayed horizontally as a row, but in reverse order
/// </summary>
RowReverse
}
/// <summary>
/// [Draft] Enumeration for the alignment of the flex items when the items do not use all available space on the main-axis.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public enum FlexJustification
{
/// <summary>
/// Items are positioned at the beginning of the container
/// </summary>
FlexStart,
/// <summary>
/// Items are positioned at the center of the container
/// </summary>
Center,
/// <summary>
/// Items are positioned at the end of the container
/// </summary>
FlexEnd,
/// <summary>
/// Items are positioned with equal space between the lines
/// </summary>
SpaceBetween,
/// <summary>
/// Items are positioned with equal space before, between, and after the lines
/// </summary>
SpaceAround
}
/// <summary>
/// [Draft] Enumeration for the wrap type of the flex container when there is no enough room for all the items on one flex line.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public enum FlexWrapType
{
/// <summary>
/// Flex items laid out in single line (shrunk to fit the flex container along the main axis)
/// </summary>
NoWrap,
/// <summary>
/// Flex items laid out in multiple lines if needed
/// </summary>
Wrap
}
/// <summary>
/// [Draft] Enumeration for the alignment of the flex items or lines when the items or lines do not use all the available space on the cross-axis.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public enum AlignmentType
{
/// <summary>
/// Inherits the same alignment from the parent
/// </summary>
Auto,
/// <summary>
/// At the beginning of the container
/// </summary>
FlexStart,
/// <summary>
/// At the center of the container
/// </summary>
Center,
/// <summary>
/// At the end of the container
/// </summary>
FlexEnd,
/// <summary>
/// Stretch to fit the container
/// </summary>
Stretch
}
private MeasuredSize measureChild(global::System.IntPtr childPtr, float width, int measureModeWidth, float height, int measureModeHeight)
{
// We need to measure child layout
View child = Registry.GetManagedBaseHandleFromNativePtr(childPtr) as View;
LayoutItem childLayout = child.Layout;
MeasureSpecification childWidthMeasureSpec = GetChildMeasureSpecification(
new MeasureSpecification(
new LayoutLength(parentMeasureSpecificationWidth.Size - (Padding.Start + Padding.End + child.Margin.Start + child.Margin.End)),
parentMeasureSpecificationWidth.Mode),
new LayoutLength(child.Padding.Start + child.Padding.End),
new LayoutLength(child.WidthSpecification));
MeasureSpecification childHeightMeasureSpec = GetChildMeasureSpecification(
new MeasureSpecification(
new LayoutLength(parentMeasureSpecificationHeight.Size - (Padding.Top + Padding.Bottom + child.Margin.Top + child.Margin.Bottom)),
parentMeasureSpecificationHeight.Mode),
new LayoutLength(Padding.Top + Padding.Bottom),
new LayoutLength(child.HeightSpecification));
childLayout.Measure( childWidthMeasureSpec, childHeightMeasureSpec);
return new MeasuredSize(childLayout.MeasuredWidth.Size.AsRoundedValue(),childLayout.MeasuredHeight.Size.AsRoundedValue());
}
void InsertChild( LayoutItem child )
{
// Store created node for child
Interop.FlexLayout.FlexLayout_AddChildWithMargin(swigCPtr, View.getCPtr(child.Owner), Extents.getCPtr(child.Owner.Margin), measureChildDelegate, LayoutChildren.Count-1);
}
/// <summary>
/// Callback when child is added to container.<br />
/// Derived classes can use this to set their own child properties on the child layout's owner.<br />
/// </summary>
/// <param name="child">The Layout child.</param>
/// <since_tizen> 6 </since_tizen>
protected override void OnChildAdd(LayoutItem child)
{
InsertChild(child);
}
/// <summary>
/// Callback when child is removed from container.<br />
/// </summary>
/// <param name="child">The Layout child.</param>
/// <since_tizen> 6 </since_tizen>
protected override void OnChildRemove(LayoutItem child)
{
// When child View is removed from it's parent View (that is a Layout) then remove it from the layout too.
// FlexLayout refers to the child as a View not LayoutItem.
Interop.FlexLayout.FlexLayout_RemoveChild(swigCPtr, child);
}
/// <summary>
/// Measure the layout and its content to determine the measured width and the measured height.<br />
/// </summary>
/// <param name="widthMeasureSpec">horizontal space requirements as imposed by the parent.</param>
/// <param name="heightMeasureSpec">vertical space requirements as imposed by the parent.</param>
/// <since_tizen> 6 </since_tizen>
protected override void OnMeasure( MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec )
{
bool isLayoutRtl = Owner.LayoutDirection == ViewLayoutDirectionType.RTL;
Extents padding = Owner.Padding;
Extents margin = Owner.Margin;
Interop.FlexLayout.FlexLayout_SetMargin(swigCPtr, Extents.getCPtr(margin));
Interop.FlexLayout.FlexLayout_SetPadding(swigCPtr, Extents.getCPtr(padding));
float width = FlexUndefined; // Behaves as WrapContent (Flex Auto)
float height = FlexUndefined; // Behaves as WrapContent (Flex Auto)
if( widthMeasureSpec.Mode == MeasureSpecification.ModeType.Exactly || widthMeasureSpec.Mode == MeasureSpecification.ModeType.AtMost )
{
width = widthMeasureSpec.Size.AsDecimal();
}
if ( heightMeasureSpec.Mode == MeasureSpecification.ModeType.Exactly || heightMeasureSpec.Mode == MeasureSpecification.ModeType.AtMost )
{
height = heightMeasureSpec.Size.AsDecimal();
}
// Save measureSpec to measure children.
// In other Layout, we can pass it as parameter to measuring child but in FlexLayout we can't
// because measurChild function is called by native callback.
parentMeasureSpecificationWidth = widthMeasureSpec;
parentMeasureSpecificationHeight = heightMeasureSpec;
Interop.FlexLayout.FlexLayout_CalculateLayout( swigCPtr, width, height, isLayoutRtl );
LayoutLength flexLayoutWidth = new LayoutLength( Interop.FlexLayout.FlexLayout_GetWidth(swigCPtr) );
LayoutLength flexLayoutHeight = new LayoutLength( Interop.FlexLayout.FlexLayout_GetHeight(swigCPtr) );
Debug.WriteLineIf( LayoutDebugFlex, "FlexLayout OnMeasure width:" + flexLayoutWidth.AsRoundedValue()
+ " height:" + flexLayoutHeight.AsRoundedValue() );
SetMeasuredDimensions( GetDefaultSize(flexLayoutWidth, widthMeasureSpec ),
GetDefaultSize(flexLayoutHeight, heightMeasureSpec ) );
}
/// <summary>
/// Assign a size and position to each of its children.<br />
/// </summary>
/// <param name="changed">This is a new size or position for this layout.</param>
/// <param name="left">Left position, relative to parent.</param>
/// <param name="top"> Top position, relative to parent.</param>
/// <param name="right">Right position, relative to parent.</param>
/// <param name="bottom">Bottom position, relative to parent.</param>
/// <since_tizen> 6 </since_tizen>
protected override void OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
{
bool isLayoutRtl = Owner.LayoutDirection == ViewLayoutDirectionType.RTL;
LayoutLength width = right - left;
LayoutLength height = bottom - top;
// Call to FlexLayout implementation to calculate layout values for later retrieval.
Interop.FlexLayout.FlexLayout_CalculateLayout( swigCPtr, width.AsDecimal(), height.AsDecimal(), isLayoutRtl );
int count = LayoutChildren.Count;
for( int childIndex = 0; childIndex < count; childIndex++)
{
LayoutItem childLayout = LayoutChildren[childIndex];
if( childLayout != null )
{
// Get the frame for the child, start, top, end, bottom.
Vector4 frame = new Vector4(Interop.FlexLayout.FlexLayout_GetNodeFrame(swigCPtr, childIndex ), true);
childLayout.Layout( new LayoutLength(frame.X), new LayoutLength(frame.Y), new LayoutLength(frame.Z), new LayoutLength(frame.W) );
}
}
}
} // FLexlayout
} // namesspace Tizen.NUI