[NUI] Fix NUITizenGallery to run on Ubuntu VS-Code debugging
[platform/core/csapi/tizenfx.git] / test / NUITizenGallery / Examples / LinearLayoutTest / LinearLayoutTest4Page.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 Tizen.NUI;
18 using Tizen.NUI.BaseComponents;
19 using Tizen.NUI.Components;
20
21 namespace NUITizenGallery
22 {
23     public partial class LinearLayoutTest4Page : ContentPage
24     {
25         private int i = 0;
26
27         public LinearLayoutTest4Page()
28         {
29             InitializeComponent();
30             addButton.Clicked += OnAddButtonClicked;
31         }
32
33         private void OnAddButtonClicked(object sender, ClickedEventArgs e)
34         {
35             i++;
36             layout.Add(new TextLabel
37             {
38                 Text = $"Label #{i}"
39             });
40         }
41
42         protected override void Dispose(DisposeTypes type)
43         {
44             if (Disposed)
45             {
46                 return;
47             }
48
49             if (type == DisposeTypes.Explicit)
50             {
51                 addButton.Clicked -= OnAddButtonClicked;
52                 RemoveAllChildren(true);
53             }
54
55             base.Dispose(type);
56         }
57
58         private void RemoveAllChildren(bool dispose = false)
59         {
60             RecursiveRemoveChildren(this, dispose);
61         }
62
63         private void RecursiveRemoveChildren(View parent, bool dispose)
64         {
65             if (parent == null)
66             {
67                 return;
68             }
69
70             int maxChild = (int)parent.ChildCount;
71             for (int i = maxChild - 1; i >= 0; --i)
72             {
73                 View child = parent.GetChildAt((uint)i);
74                 if (child == null)
75                 {
76                     continue;
77                 }
78
79                 RecursiveRemoveChildren(child, dispose);
80                 parent.Remove(child);
81                 if (dispose)
82                 {
83                     child.Dispose();
84                 }
85             }
86         }
87     }
88 }