[NUI] Fix scrollableBase focus issue. (#4340)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / ScrollableFocus / ScrollableFocusAllChildrenSample.cs
1 using System;
2 using Tizen.NUI.BaseComponents;
3 using Tizen.NUI;
4 using Tizen.NUI.Components;
5
6
7 namespace Tizen.NUI.Samples
8 {
9     public class ScrollableFocusAllChildrenSample : IExample
10     {
11         int SCROLLMAX = 50;
12         public View root;
13
14         private string GetChildText(View parent)
15         {
16             if (parent != null)
17             {
18                 foreach(View child in parent.Children)
19                 {
20                     if (child is TextLabel label)
21                     {
22                         return $"{label.Text}";
23                     }
24                 }
25             }
26
27             return "";
28         }
29
30         private string GetLabelText(int i)
31         {
32             switch (i)
33             {
34                 case 0:
35                     return "[1st]";
36                 case 1:
37                     return "[2nd]";
38                 case 2:
39                     return "[3rd]";
40                 default:
41                     return $"[{i+1}th]";
42             }
43         }
44
45         public void Activate()
46         {
47
48             Random rnd = new Random();
49             Window window = NUIApplication.GetDefaultWindow();
50
51             root = new View()
52             {
53                 BackgroundColor = Color.White,
54                 WidthSpecification = LayoutParamPolicies.MatchParent,
55                 HeightSpecification = LayoutParamPolicies.MatchParent,
56                 Layout = new LinearLayout()
57                 {
58                     LinearOrientation = LinearLayout.Orientation.Vertical,
59                     HorizontalAlignment = HorizontalAlignment.Center,
60                     CellPadding = new Size(10, 10),
61                 },
62             };
63             window.Add(root);
64
65             var focusInfo = new TextLabel()
66             {
67                 BackgroundColor = Color.Yellow,
68                 TextColor = Color.Red,
69                 Text = "Prev:[N/A] Current:[N/A]"
70             };
71             root.Add(focusInfo);
72
73             FocusManager.Instance.EnableDefaultAlgorithm(true);
74             FocusManager.Instance.FocusChanged += (object s, FocusManager.FocusChangedEventArgs e) =>
75             {
76                 string prev = "[N/A]";
77                 string cur = "[N/A]";
78                 if (e.Previous != null)
79                 {
80                     var prevView = e.Previous;
81                     prev = $"{prevView.Name}[{prevView.ID}]{GetChildText(prevView)}";
82                 }
83
84                 if (e.Current != null)
85                 {
86                     var curView = e.Current;
87                     cur = $"{curView.Name}[{curView.ID}]{GetChildText(curView)}";
88                 }
89
90                 focusInfo.Text = $"Prev:{prev} Current:{cur}";
91                 Console.WriteLine($"Focus Changed Prev:{prev} => Current:{cur}");
92             };
93
94             var top = new Button()
95             {
96                 Focusable = true,
97                 FocusableInTouch = true,
98                 Text = "Top"
99             };
100             root.Add(top);
101
102             var verticalScrollView = new ScrollableBase()
103             {
104                 ScrollingDirection = ScrollableBase.Direction.Vertical,
105                 WidthSpecification = LayoutParamPolicies.MatchParent,
106                 HeightSpecification = LayoutParamPolicies.MatchParent,
107                 BackgroundColor = Color.Gray,
108                 Layout = new LinearLayout
109                 {
110                     LinearOrientation = LinearLayout.Orientation.Vertical,
111                     HorizontalAlignment = HorizontalAlignment.Center,
112                     CellPadding = new Size2D(10, 30),
113                 }
114             };
115             root.Add(verticalScrollView);
116
117             for (int i = 0; i < SCROLLMAX; i++)
118             {
119                 var colorItem = new View()
120                 {
121                     WidthSpecification = LayoutParamPolicies.MatchParent,
122                     HeightSpecification = LayoutParamPolicies.WrapContent,
123                     BackgroundColor = new Color((float)rnd.Next(256)/256f, (float)rnd.Next(256)/256f, (float)rnd.Next(256)/256f, 1),
124                     Focusable = true,
125                     FocusableInTouch = true,
126                     Layout = new LinearLayout
127                     {
128                         LinearOrientation = LinearLayout.Orientation.Horizontal,
129                         VerticalAlignment = VerticalAlignment.Center,
130                         CellPadding = new Size2D(10, 10),
131                     }
132                 };
133                 var label = new TextLabel()
134                 {
135                     Text = GetLabelText(i),
136                     PointSize = 20,
137                 };
138                 colorItem.Add(label);
139                 verticalScrollView.Add(colorItem);
140             }
141
142             var middle = new Button()
143             {
144                 Focusable = true,
145                 FocusableInTouch = true,
146                 Text = "Middle"
147             };
148             root.Add(middle);
149
150             var horizontalLayout = new View()
151             {
152                 BackgroundColor = Color.White,
153                 WidthSpecification = LayoutParamPolicies.MatchParent,
154                 HeightSpecification = LayoutParamPolicies.MatchParent,
155                 Layout = new LinearLayout()
156                 {
157                     LinearOrientation = LinearLayout.Orientation.Horizontal,
158                     VerticalAlignment = VerticalAlignment.Center,
159                     CellPadding = new Size(10, 10),
160                 },
161             };
162             root.Add(horizontalLayout);
163
164             var leftIcon = new RadioButton()
165             {
166                 WidthSpecification = 50,
167                 HeightSpecification = 50,
168                 Focusable = true,
169                 FocusableInTouch = true,
170             };
171             horizontalLayout.Add(leftIcon);
172
173             var horizontalScrollView = new ScrollableBase()
174             {
175                 ScrollingDirection = ScrollableBase.Direction.Horizontal,
176                 WidthSpecification = LayoutParamPolicies.MatchParent,
177                 HeightSpecification = LayoutParamPolicies.MatchParent,
178                 BackgroundColor = Color.Gray,
179                 Layout = new LinearLayout
180                 {
181                     LinearOrientation = LinearLayout.Orientation.Horizontal,
182                     VerticalAlignment = VerticalAlignment.Center,
183                     CellPadding = new Size2D(30, 10),
184                 }
185             };
186             horizontalLayout.Add(horizontalScrollView);
187
188             for (int i = 0; i < SCROLLMAX; i++)
189             {
190                 var colorItem = new View()
191                 {
192                     WidthSpecification = LayoutParamPolicies.WrapContent,
193                     HeightSpecification = LayoutParamPolicies.MatchParent,
194                     BackgroundColor = new Color((float)rnd.Next(256)/256f, (float)rnd.Next(256)/256f, (float)rnd.Next(256)/256f, 1),
195                     Focusable = true,
196                     FocusableInTouch = true,
197                     Layout = new LinearLayout
198                     {
199                         LinearOrientation = LinearLayout.Orientation.Vertical,
200                         HorizontalAlignment = HorizontalAlignment.Center,
201                         CellPadding = new Size2D(10, 10),
202                     }
203                 };
204                 var label = new TextLabel()
205                 {
206                     Text = GetLabelText(i),
207                     PointSize = 20,
208                 };
209                 colorItem.Add(label);
210                 horizontalScrollView.Add(colorItem);
211             }
212
213             var rightIcon = new RadioButton()
214             {
215                 WidthSpecification = 50,
216                 HeightSpecification = 50,
217                 Focusable = true,
218                 FocusableInTouch = true,
219             };
220             horizontalLayout.Add(rightIcon);
221
222             var bottom = new Button()
223             {
224                 Focusable = true,
225                 FocusableInTouch = true,
226                 Text = "Bottom"
227             };
228             root.Add(bottom);
229         }
230
231         public void Deactivate()
232         {
233             if (root != null)
234             {
235                 NUIApplication.GetDefaultWindow().Remove(root);
236                 root.Dispose();
237             }
238         }
239     }
240
241 }