[NUI] Change for Size2D property to own internalSize2D which is cashed to prevent...
[platform/core/csapi/tizenfx.git] / test / NUITizenGallery / Examples / LayoutAddRemoveTest / LayoutAddRemoveTest2Page.xaml.cs
1 /*
2  * Copyright(c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17 using System.Threading;
18 using Tizen.NUI;
19 using Tizen.NUI.BaseComponents;
20 using Tizen.NUI.Components;
21
22 namespace NUITizenGallery
23 {
24     public partial class LayoutAddRemoveTest2Page : ContentPage
25     {
26         float red = 1f;
27         public LayoutAddRemoveTest2Page()
28         {
29             InitializeComponent();
30             addButton.Clicked += OnAddButtonClicked;
31             addTaskButton.Clicked += OnAddTaskButtonClicked;
32             removeButton.Clicked += OnRemoveButtonClicked;
33         }
34
35         private void OnAddButtonClicked(object sender, ClickedEventArgs e)
36         {
37             linear.Add(new View
38             {
39                 WidthSpecification = 240,
40                 HeightSpecification = 240,
41                 BackgroundColor = new Color(red / 255f, 50f / 255f, 50 / 255f, 255f),
42             });
43
44             red = red > 255f ? 1f : red + 50f;
45         }
46
47         private void OnAddTaskButtonClicked(object sender, ClickedEventArgs e)
48         {
49             SynchronizationContext.Current.Post(AddButton, null);
50         }
51
52         private void OnRemoveButtonClicked(object sender, ClickedEventArgs e)
53         {
54             if (linear.ChildCount > 0)
55             {
56                 linear.Remove(linear.Children[0]);
57             }
58         }
59
60         private void AddButton(object state)
61         {
62             linear.Add(new Button()
63             {
64                 Text = "Add",
65                 HeightSpecification = LayoutParamPolicies.MatchParent,
66             });
67         }
68
69         protected override void Dispose(DisposeTypes type)
70         {
71             if (Disposed)
72             {
73                 return;
74             }
75
76             if (type == DisposeTypes.Explicit)
77             {
78                 addButton.Clicked -= OnAddButtonClicked;
79                 addTaskButton.Clicked -= OnAddTaskButtonClicked;
80                 removeButton.Clicked -= OnRemoveButtonClicked;
81                 RemoveAllChildren(true);
82             }
83
84             base.Dispose(type);
85         }
86
87         private void RemoveAllChildren(bool dispose = false)
88         {
89             RecursiveRemoveChildren(this, dispose);
90         }
91
92         private void RecursiveRemoveChildren(View parent, bool dispose)
93         {
94             if (parent == null)
95             {
96                 return;
97             }
98
99             int maxChild = (int)parent.ChildCount;
100             for (int i = maxChild - 1; i >= 0; --i)
101             {
102                 View child = parent.GetChildAt((uint)i);
103                 if (child == null)
104                 {
105                     continue;
106                 }
107                 RecursiveRemoveChildren(child, dispose);
108                 parent.Remove(child);
109                 if (dispose)
110                 {
111                     child.Dispose();
112                 }
113             }
114         }
115     }
116 }