2 * Copyright(c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using System.Collections.Generic;
20 using System.ComponentModel;
21 using System.Threading.Tasks;
22 using Tizen.NUI.BaseComponents;
23 using Tizen.NUI.Binding;
25 namespace Tizen.NUI.Components
28 /// The Navigator is a class which navigates pages with stack methods such
31 [EditorBrowsable(EditorBrowsableState.Never)]
32 public class Navigator : Control
34 //This will be replaced with view transition class instance.
35 private Animation _curAnimation = null;
37 //This will be replaced with view transition class instance.
38 private Animation _newAnimation = null;
41 /// Creates a new instance of a Navigator.
43 [EditorBrowsable(EditorBrowsableState.Never)]
44 public Navigator() : base()
49 /// List of pages of Navigator.
51 [EditorBrowsable(EditorBrowsableState.Never)]
52 public List<Page> NavigationPages { get; } = new List<Page>();
55 /// Pushes a page to Navigator.
56 /// If the page is already in Navigator, then it is not pushed.
58 /// <param name="page">The page to push to Navigator.</param>
59 /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
60 [EditorBrowsable(EditorBrowsableState.Never)]
61 public void Push(Page page)
65 throw new ArgumentNullException(nameof(page), "page should not be null.");
68 //Duplicate page is not pushed.
69 if (NavigationPages.Contains(page)) return;
79 NavigationPages.Add(page);
83 page.InvokeAppearing();
84 curTop.InvokeDisappearing();
86 //TODO: The following transition codes will be replaced with view transition.
90 _curAnimation.Clear();
96 _newAnimation.Clear();
99 _curAnimation = new Animation(1000);
100 using (var scaleVec = new Vector3(0.0f, 0.0f, 1.0f))
102 _curAnimation.AnimateTo(curTop, "Scale", scaleVec, 0, 1000);
104 _curAnimation.AnimateTo(curTop, "Opacity", 0.0f, 0, 1000);
105 _curAnimation.EndAction = Animation.EndActions.Discard;
106 _curAnimation.Play();
108 using (var scaleVec = new Vector3(0.0f, 0.0f, 1.0f))
110 using (var scaleProp = new Tizen.NUI.PropertyValue(scaleVec))
112 Tizen.NUI.Object.SetProperty(page.SwigCPtr, Page.Property.SCALE, scaleProp);
115 using (var scaleProp = new Tizen.NUI.PropertyValue(0.0f))
117 Tizen.NUI.Object.SetProperty(page.SwigCPtr, Page.Property.OPACITY, scaleProp);
119 _newAnimation = new Animation(1000);
120 using (var scaleVec = new Vector3(1.0f, 1.0f, 1.0f))
122 _newAnimation.AnimateTo(page, "Scale", scaleVec, 0, 1000);
124 _newAnimation.AnimateTo(page, "Opacity", 1.0f, 0, 1000);
125 _newAnimation.Play();
129 /// Pops the top page from Navigator.
131 /// <returns>The popped page.</returns>
132 /// <exception cref="InvalidOperationException">Thrown when there is no page in Navigator.</exception>
133 [EditorBrowsable(EditorBrowsableState.Never)]
136 if (NavigationPages.Count == 0)
138 throw new InvalidOperationException("There is no page in Navigator.");
143 if (NavigationPages.Count == 1)
149 var newTop = NavigationPages[NavigationPages.Count - 2];
152 newTop.InvokeAppearing();
153 curTop.InvokeDisappearing();
155 //TODO: The following transition codes will be replaced with view transition.
158 _curAnimation.Stop();
159 _curAnimation.Clear();
164 _newAnimation.Stop();
165 _newAnimation.Clear();
168 _curAnimation = new Animation(1000);
169 using (var scaleVec = new Vector3(0.0f, 0.0f, 1.0f))
171 _curAnimation.AnimateTo(curTop, "Scale", scaleVec, 0, 1000);
173 _curAnimation.AnimateTo(curTop, "Opacity", 0.0f, 0, 1000);
174 _curAnimation.Play();
175 _curAnimation.Finished += (object sender, EventArgs e) =>
177 //Removes the current top page after transition is finished.
181 using (var scaleVec = new Vector3(0.0f, 0.0f, 1.0f))
183 using (var scaleProp = new Tizen.NUI.PropertyValue(scaleVec))
185 Tizen.NUI.Object.SetProperty(newTop.SwigCPtr, Page.Property.SCALE, scaleProp);
188 using (var opacityProp = new Tizen.NUI.PropertyValue(0.0f))
190 Tizen.NUI.Object.SetProperty(newTop.SwigCPtr, Page.Property.OPACITY, opacityProp);
192 _newAnimation = new Animation(1000);
193 using (var scaleVec = new Vector3(1.0f, 1.0f, 1.0f))
195 _newAnimation.AnimateTo(newTop, "Scale", scaleVec, 0, 1000);
197 _newAnimation.AnimateTo(newTop, "Opacity", 1.0f, 0, 1000);
198 _newAnimation.Play();
204 /// Inserts a page at the specified index of Navigator.
205 /// If the page is already in Navigator, then it is not inserted.
207 /// <param name="index">The index of Navigator where a page will be inserted.</param>
208 /// <param name="page">The page to insert to Navigator.</param>
209 /// <exception cref="ArgumentOutOfRangeException">Thrown when the argument index is less than 0, or greater than the number of pages.</exception>
210 /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
211 [EditorBrowsable(EditorBrowsableState.Never)]
212 public void Insert(int index, Page page)
214 if ((index < 0) || (index > NavigationPages.Count))
216 throw new ArgumentOutOfRangeException(nameof(index), "index should be greater than or equal to 0, and less than or equal to the number of pages.");
221 throw new ArgumentNullException(nameof(page), "page should not be null.");
224 //Duplicate page is not pushed.
225 if (NavigationPages.Contains(page)) return;
227 NavigationPages.Insert(index, page);
232 /// Inserts a page to Navigator before an existing page.
233 /// If the page is already in Navigator, then it is not inserted.
235 /// <param name="before">The existing page, before which a page will be inserted.</param>
236 /// <param name="page">The page to insert to Navigator.</param>
237 /// <exception cref="ArgumentNullException">Thrown when the argument before is null.</exception>
238 /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
239 /// <exception cref="ArgumentException">Thrown when the argument before does not exist in Navigator.</exception>
240 [EditorBrowsable(EditorBrowsableState.Never)]
241 public void InsertBefore(Page before, Page page)
245 throw new ArgumentNullException(nameof(before), "before should not be null.");
250 throw new ArgumentNullException(nameof(page), "page should not be null.");
253 //Find the index of before page.
254 int beforeIndex = NavigationPages.FindIndex(x => x == before);
256 //before does not exist in Navigator.
257 if (beforeIndex == -1)
259 throw new ArgumentException("before does not exist in Navigator.", nameof(before));
262 Insert(beforeIndex, page);
266 /// Removes a page from Navigator.
268 /// <param name="page">The page to remove from Navigator.</param>
269 /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
270 [EditorBrowsable(EditorBrowsableState.Never)]
271 public void Remove(Page page)
275 throw new ArgumentNullException(nameof(page), "page should not be null.");
278 NavigationPages.Remove(page);
283 /// Removes a page at the specified index of Navigator.
285 /// <param name="index">The index of Navigator where a page will be removed.</param>
286 /// <exception cref="ArgumentOutOfRangeException">Thrown when the index is less than 0, or greater than or equal to the number of pages.</exception>
287 [EditorBrowsable(EditorBrowsableState.Never)]
288 public void RemoveAt(int index)
290 if ((index < 0) || (index >= NavigationPages.Count))
292 throw new ArgumentOutOfRangeException(nameof(index), "index should be greater than or equal to 0, and less than the number of pages.");
295 Remove(NavigationPages[index]);
299 /// Returns the page at the top of Navigator.
301 /// <returns>The page at the top of Navigator.</returns>
302 [EditorBrowsable(EditorBrowsableState.Never)]
305 if (NavigationPages.Count == 0) return null;
307 return NavigationPages[NavigationPages.Count - 1];
310 } //namespace Tizen.NUI