Fix controls created by xaml issues (#341)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / XamlBinding / ContentPage.cs
1 /*
2  * Copyright (c) 2017 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;
18 using System.ComponentModel;
19 using Tizen.NUI.Binding;
20 using Tizen.NUI.BaseComponents;
21 using Tizen.NUI.Xaml;
22 using System.Collections.Generic;
23 using System.IO;
24
25 namespace Tizen.NUI
26 {
27     /// <summary>
28     /// The ContentPage class.
29     /// </summary>
30     [ContentProperty("Content")]
31     [EditorBrowsable(EditorBrowsableState.Never)]
32     public class ContentPage : TemplatedPage
33     {
34         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
35         [EditorBrowsable(EditorBrowsableState.Never)]
36         public View Root {get; internal set;}
37         private Window Window;
38
39         internal static readonly BindableProperty ContentProperty = BindableProperty.Create(nameof(Content), typeof(View), typeof(ContentPage), null, propertyChanged: (bindable, oldValue, newValue) =>
40         {
41             // var self = (IControlTemplated)bindable;
42             // var newElement = (Element)newValue;
43             // if (self.ControlTemplate == null)
44             // {
45             //  while (self.InternalChildren.Count > 0)
46             //  {
47             //      self.InternalChildren.RemoveAt(self.InternalChildren.Count - 1);
48             //  }
49
50             //  if (newValue != null)
51             //      self.InternalChildren.Add(newElement);
52             // }
53             // else
54             // {
55             //  if (newElement != null)
56             //  {
57             //      BindableObject.SetInheritedBindingContext(newElement, bindable.BindingContext);
58             //  }
59             // }
60             var self = (ContentPage)bindable;
61             if (newValue != null)
62             {
63                 self.Root.Add((View)newValue);
64             }
65         });
66
67         /// <summary>
68         /// The contents of ContentPage can be added into it.
69         /// </summary>
70         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
71         [EditorBrowsable(EditorBrowsableState.Never)]
72         public View Content
73         {
74             get { return (View)GetValue(ContentProperty); }
75             set { SetValue(ContentProperty, value); }
76         }
77
78         /// <summary>
79         /// Method that is called when the binding content changes.
80         /// </summary>
81         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
82         [EditorBrowsable(EditorBrowsableState.Never)]
83         protected override void OnBindingContextChanged()
84         {
85             base.OnBindingContextChanged();
86
87             View content = Content;
88             ControlTemplate controlTemplate = ControlTemplate;
89             if (content != null && controlTemplate != null)
90             {
91                 SetInheritedBindingContext(content, BindingContext);
92             }
93         }
94
95         internal override void OnControlTemplateChanged(ControlTemplate oldValue, ControlTemplate newValue)
96         {
97             if (oldValue == null)
98                 return;
99
100             base.OnControlTemplateChanged(oldValue, newValue);
101             View content = Content;
102             ControlTemplate controlTemplate = ControlTemplate;
103             if (content != null && controlTemplate != null)
104             {
105                 SetInheritedBindingContext(content, BindingContext);
106             }
107         }
108
109         /// <summary>
110         /// The constructor.
111         /// </summary>
112         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
113         [EditorBrowsable(EditorBrowsableState.Never)]
114         public ContentPage(Window win)
115         {
116             IsCreateByXaml = true;
117
118             Root = new View();
119             Root.WidthResizePolicy = ResizePolicyType.FillToParent;
120             Root.HeightResizePolicy = ResizePolicyType.FillToParent;
121
122             win.Add(Root);
123         }
124
125         /// <summary>
126         /// The Resources property.
127         /// </summary>
128         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
129         [EditorBrowsable(EditorBrowsableState.Never)]
130         public ResourceDictionary XamlResources
131         {
132             get
133             {
134                 return Application.Current.XamlResources;
135             }
136
137             set
138             {
139                 Application.Current.XamlResources = value;
140             }
141         }
142
143         /// <summary>
144         /// To make the ContentPage instance be disposed.
145         /// </summary>
146         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
147         [EditorBrowsable(EditorBrowsableState.Never)]
148         protected override void Dispose(DisposeTypes type)
149         {
150             if (disposed)
151             {
152                 return;
153             }
154
155             if (type == DisposeTypes.Explicit)
156             {
157                 //Called by User
158                 //Release your own managed resources here.
159                 //You should release all of your own disposable objects here.
160             }
161
162             //Release your own unmanaged resources here.
163             //You should not access any managed member here except static instance.
164             //because the execution order of Finalizes is non-deterministic.
165             Window.Instance.Remove(Root);
166             Root?.Dispose();
167             Root = null;
168
169             base.Dispose(type);
170         }
171
172         /// <summary>
173         /// Check whether the content is empty.
174         /// </summary>
175         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
176         [EditorBrowsable(EditorBrowsableState.Never)]
177         public bool IsEmpty
178         {
179             get
180             {
181                 return ( Root.ChildCount == 0 ) ? true : false;
182             }
183         }
184
185         /// <summary>
186         /// Clear all contents from this ContentPage.
187         /// </summary>
188         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
189         [EditorBrowsable(EditorBrowsableState.Never)]
190         public void ClearContent()
191         {
192             if ( Root != null )
193             {
194                 //Remove it from the window
195                 Window.Instance.Remove(Root);
196                 Root.Dispose();
197                 Root = null;
198
199                 //Readd to window
200                 Root = new View();
201                 Root.WidthResizePolicy = ResizePolicyType.FillToParent;
202                 Root.HeightResizePolicy = ResizePolicyType.FillToParent;
203                 Window.Instance.Add(Root);
204
205                 ClearHandler();
206             }
207         }
208
209         private EventHandler _clearEventHandler;
210
211         /// <summary>
212         /// Clear event.
213         /// </summary>
214         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
215         [EditorBrowsable(EditorBrowsableState.Never)]
216         public event EventHandler ClearEvent
217         {
218             add
219             {
220                 _clearEventHandler += value;
221             }
222             remove
223             {
224                 _clearEventHandler -= value;
225             }
226         }
227
228         private void ClearHandler()
229         {
230             if (_clearEventHandler != null)
231             {
232                 _clearEventHandler(this, null);
233             }
234         }
235
236         /// <summary>
237         /// Users can set focus logic codes here.
238         /// </summary>
239         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
240         [EditorBrowsable(EditorBrowsableState.Never)]
241         public virtual void SetFocus() { }
242
243         private Dictionary<string, Transition> transDictionary = new Dictionary<string, Transition>();
244
245         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
246         [EditorBrowsable(EditorBrowsableState.Never)]
247         public Animation CreateAnimation(string animationType)
248         {
249             Animation ani = null;
250             Transition trans = null;
251             transDictionary.TryGetValue(animationType, out trans);
252
253             ani = trans?.CreateAnimation();
254             return ani;
255         }
256
257         private void CreateAnimationFactory()
258         {
259             foreach (string str in transitionType)
260             {
261                 string resourceName = str + ".xaml";
262                 Transition trans = null;
263
264                 string resource = Tizen.Applications.Application.Current.DirectoryInfo.Resource;
265
266                 string likelyResourcePath = resource + "animation/" + resourceName;
267
268                 if (File.Exists(likelyResourcePath))
269                 {
270                     trans = Extensions.LoadTransition(likelyResourcePath);
271                 }
272
273                 transDictionary.Add(trans.Name, trans);
274             }
275         }
276
277         private string[] transitionType;
278
279         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
280         [EditorBrowsable(EditorBrowsableState.Never)]
281         public string[] TransitionType
282         {
283             get
284             {
285                 return transitionType;
286             }
287             set
288             {
289                 transitionType = value;
290                 CreateAnimationFactory();
291             }
292         }
293     }
294 }