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