[IoTConnectivity] Fix wrong callback management (#1041)
[platform/core/csapi/tizenfx.git] / test / NUITestSample / NUITestSample / examples / layouting / custom-layout-root-layer-test.cs
1 using System;
2 using Tizen.NUI;
3 using Tizen.NUI.BaseComponents;
4
5 namespace CustomLayoutWithoutAbsoluteLayout
6 {
7     public class CustomLayout : LayoutGroup
8     {
9         protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
10         {
11             var accumulatedWidth = new LayoutLength(0);
12             var maxHeight = 0;
13             var measuredWidth = new LayoutLength(0);
14             LayoutLength measuredHeight = new LayoutLength(0);
15             MeasureSpecification.ModeType widthMode = widthMeasureSpec.Mode;
16             MeasureSpecification.ModeType heightMode = heightMeasureSpec.Mode;
17
18             bool isWidthExact = (widthMode == MeasureSpecification.ModeType.EXACTLY);
19             bool isHeightExact = (heightMode == MeasureSpecification.ModeType.EXACTLY);
20
21             // In this layout we will:
22             //  Measuring the layout with the children in a horizontal configuration, one after another
23             //  Set the required width to be the accumulated width of our children
24             //  Set the required height to be the maximum height of any of our children
25
26             for (uint i = 0; i < ChildCount; ++i)
27             {
28                 var childLayout = GetChildAt(i);
29                 if (childLayout)
30                 {
31                     MeasureChild(childLayout, widthMeasureSpec, heightMeasureSpec);
32                     accumulatedWidth += childLayout.MeasuredWidth;
33                     maxHeight = System.Math.Max(childLayout.MeasuredHeight.Value, maxHeight);
34                 }
35             }
36
37             measuredHeight.Value = maxHeight;
38             measuredWidth = accumulatedWidth;
39
40             if (isWidthExact)
41             {
42                 measuredWidth = new LayoutLength(widthMeasureSpec.Size);
43             }
44
45             if (isHeightExact)
46             {
47                 measuredHeight = new LayoutLength(heightMeasureSpec.Size);
48             }
49
50             // Finally, call this method to set the dimensions we would like
51             SetMeasuredDimensions(new MeasuredSize(measuredWidth), new MeasuredSize(measuredHeight));
52         }
53
54         protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
55         {
56             LayoutLength childTop = new LayoutLength(0);
57             LayoutLength childLeft = new LayoutLength(0);
58
59             // We want to vertically align the children to the middle
60             var height = bottom - top;
61             var middle = height / 2;
62
63             // Horizontally align the children to the middle of the space they are given too
64             var width = right - left;
65             uint count = ChildCount;
66             var childIncrement = 0;
67             if (count > 0)
68             {
69                 childIncrement = width.Value / System.Convert.ToInt32(count);
70             }
71             var center = childIncrement / 2;
72
73             // Check layout direction
74             var view = GetOwner();
75             ViewLayoutDirectionType layoutDirection = view.LayoutDirection;
76
77             for (uint i = 0; i < count; i++)
78             {
79                 uint itemIndex;
80                 // If RTL, then layout the last item first
81                 if (layoutDirection == ViewLayoutDirectionType.RTL)
82                 {
83                     itemIndex = count - 1 - i;
84                 }
85                 else
86                 {
87                     itemIndex = i;
88                 }
89
90                 LayoutItem childLayout = GetChildAt(itemIndex);
91                 if (childLayout)
92                 {
93                     var childWidth = childLayout.MeasuredWidth;
94                     var childHeight = childLayout.MeasuredHeight;
95
96                     childTop = middle - (childHeight / 2);
97
98                     var leftPosition = childLeft + center - childWidth / 2;
99
100                     childLayout.Layout(leftPosition, childTop, leftPosition + childWidth, childTop + childHeight);
101                     childLeft += childIncrement;
102                 }
103             }
104         }
105     }
106 }
107
108 namespace CustomLayoutWithoutAbsoluteLayout
109 {
110     static class TestImages
111     {
112         //private const string resources = "./res";
113         public static string resources = Tizen.Applications.Application.Current.DirectoryInfo.Resource;
114         /// Child image filenames
115         public static readonly string[] s_images = new string[]
116         {
117             resources + "/images/application-icon-101.png",
118             resources + "/images/application-icon-102.png",
119             resources + "/images/application-icon-103.png",
120             resources + "/images/application-icon-104.png"
121         };
122     }
123
124     class Example : NUIApplication
125     {
126         protected override void OnCreate()
127         {
128             base.OnCreate();
129             Initialize();
130         }
131
132         private void Initialize()
133         {
134             // Change the background color of Window to White
135             Window window = Window.Instance;
136             window.BackgroundColor = Color.White;
137
138             //Layer layer = new Layer();
139
140             //window.AddLayer(layer);
141
142             // Create a new view
143             View customLayoutView = new View();
144             customLayoutView.Name = "CustomLayoutView";
145             customLayoutView.ParentOrigin = ParentOrigin.Center;
146             customLayoutView.PivotPoint = PivotPoint.Center;
147             customLayoutView.PositionUsesPivotPoint = true;
148             // Set our Custom Layout on the view
149             var layout = new CustomLayout();
150             customLayoutView.Layout = layout;
151             customLayoutView.SetProperty(LayoutItemWrapper.ChildProperty.WIDTH_SPECIFICATION, new PropertyValue(-2));  // -2 WRAP_CONTENT
152             customLayoutView.SetProperty(LayoutItemWrapper.ChildProperty.HEIGHT_SPECIFICATION, new PropertyValue(350));
153             customLayoutView.BackgroundColor = Color.Blue;
154             window.Add(customLayoutView);
155
156             // Add child image-views to the created view
157             foreach (String image in TestImages.s_images)
158             {
159                 customLayoutView.Add(CreateChildImageView(image, new Size2D(100, 100)));
160             }
161         }
162
163         /// <summary>
164         /// Helper function to create ImageViews with given filename and size..<br />
165         /// </summary>
166         /// <param name="filename"> The filename of the image to use.</param>
167         /// <param name="size"> The size that the image should be loaded at.</param>
168         /// <returns>The created ImageView.</returns>
169         ImageView CreateChildImageView(String url, Size2D size)
170         {
171             ImageView imageView = new ImageView();
172             ImageVisual imageVisual = new ImageVisual();
173
174             imageVisual.URL = url;
175             imageVisual.DesiredHeight = size.Height;
176             imageVisual.DesiredWidth = size.Width;
177             imageView.Image = imageVisual.OutputVisualMap;
178
179             imageView.Name = "ImageView";
180             imageView.HeightResizePolicy = ResizePolicyType.Fixed;
181             imageView.WidthResizePolicy = ResizePolicyType.Fixed;
182             return imageView;
183         }
184
185         static void _Main(string[] args)
186         {
187             Example simpleLayout = new Example();
188             simpleLayout.Run(args);
189         }
190     }
191 }
192
193