[NUI] Fix Dispose warning error[CA1001] (#2130)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Layouting / LayoutTransition.cs
1 /*
2  * Copyright (c) 2020 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
18 using System.Collections.Generic;
19 using System;
20 using System.ComponentModel;
21
22 namespace Tizen.NUI
23 {
24     /// <summary>
25     /// Define a List of LayoutTransitions
26     /// </summary>
27     /// <since_tizen> 6 </since_tizen>
28     public class TransitionList : List<LayoutTransition> { }
29
30     /// <summary>
31     /// The conditions for transitions.
32     /// </summary>
33     /// <since_tizen> 6 </since_tizen>
34     [FlagsAttribute]
35     public enum TransitionCondition
36     {
37         /// <summary>
38         /// Default when a condition has not been set.
39         /// </summary>
40         /// <since_tizen> 6 </since_tizen>
41         Unspecified = 0,
42         /// <summary>
43         /// Animate changing layout to another layout.
44         /// </summary>
45         /// <since_tizen> 6 </since_tizen>
46
47         LayoutChanged = 1,
48         /// <summary>
49         /// Animate adding item.
50         /// </summary>
51         /// <since_tizen> 6 </since_tizen>
52
53         Add = 2,
54         /// <summary>
55         /// Animate removing item.
56         /// </summary>
57         /// <since_tizen> 6 </since_tizen>
58
59         Remove = 4,
60         /// <summary>
61         /// Animation when an item changes due to a  being added.
62         /// </summary>
63         /// <since_tizen> 6 </since_tizen>
64
65         ChangeOnAdd = 8,
66         /// <summary>
67         /// Animation when an item changes due to a  being removed.
68         /// </summary>
69         /// <since_tizen> 6 </since_tizen>
70
71         ChangeOnRemove = 16
72     }
73
74     /// <summary>
75     /// The properties that can be animated.
76     /// </summary>
77     /// <since_tizen> 6 </since_tizen>
78     public enum AnimatableProperties
79     {
80         /// <summary>
81         /// Position property.
82         /// </summary>
83         /// <since_tizen> 6 </since_tizen>
84
85         Position,
86         /// <summary>
87         /// Size property.
88         /// </summary>
89         /// <since_tizen> 6 </since_tizen>
90
91         Size,
92         /// <summary>
93         /// Opacity property.
94         /// </summary>
95         /// <since_tizen> 6 </since_tizen>
96
97         Opacity
98     }
99
100     /// <summary>
101     /// Parts of the transition that can be configured to provide a custom effect.
102     /// </summary>
103     /// <since_tizen> 6 </since_tizen>
104     public class TransitionComponents : Disposable
105     {
106         /// <summary>
107         /// TransitionComponents default constructor.
108         /// </summary>
109         /// <since_tizen> 6 </since_tizen>
110         public TransitionComponents()
111         {
112             Delay = 0;
113             Duration = 100;
114             AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
115         }
116
117         /// <summary>   
118         /// destructor. This is HiddenAPI. recommended not to use in public.    
119         /// </summary>  
120         [EditorBrowsable(EditorBrowsableState.Never)]
121         ~TransitionComponents()
122         {
123             Dispose();
124         }
125
126         /// <summary>
127         /// TransitionComponents constructor. Stores delay, duration and AlphaFunction.
128         /// </summary>
129         /// <param name="delay">The delay before the animator starts.</param>
130         /// <param name="duration">the duration of the animator.</param>
131         /// <param name="alphaFunction">alpha function to use .</param>
132         /// <since_tizen> 6 </since_tizen>
133
134         public TransitionComponents(int delay, int duration, AlphaFunction alphaFunction)
135         {
136             Delay = delay;
137             Duration = duration;
138             AlphaFunction = alphaFunction;
139         }
140
141         /// <summary>
142         /// Get, Set the time transition should execute for . Milliseconds.
143         /// </summary>
144         /// <since_tizen> 6 </since_tizen>
145         public int Duration;
146         /// <summary>
147         /// Get, Set the delay before the transition executes. Milliseconds.
148         /// </summary>
149         /// <since_tizen> 6 </since_tizen>
150         public int Delay;
151         /// <summary>
152         /// Get, Set the function to alter the transition path over time.
153         /// </summary>
154         /// <since_tizen> 6 </since_tizen>
155         public AlphaFunction AlphaFunction;
156
157         /// <summary>
158         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
159         /// </summary>
160         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
161         [EditorBrowsable(EditorBrowsableState.Never)]
162         protected override void Dispose(DisposeTypes type)
163         {
164             if (disposed)
165             {
166                 return;
167             }
168
169             AlphaFunction.Dispose();
170             base.Dispose();
171         }
172     }
173
174     /// <summary>
175     /// LayoutTransition stores the animation setting for a transition conidition.
176     /// </summary>
177     public class LayoutTransition
178     {
179         /// <summary>
180         /// LayoutTransition default constructor.
181         /// </summary>
182         /// <since_tizen> 6 </since_tizen>
183         public LayoutTransition()
184         {
185             Condition = TransitionCondition.Unspecified;
186             AnimatableProperty = AnimatableProperties.Position;
187             Animator = null;
188             TargetValue = 0;
189         }
190         /// <summary>
191         /// LayoutTransition constructor.
192         /// </summary>
193         /// <param name="condition">The animatable condition.</param>
194         /// <param name="animatableProperty">the property to animate.</param>
195         /// <param name="targetValue">target value of the property.</param>
196         /// <param name="animator">Components to define the animator.</param>
197         /// <since_tizen> 6 </since_tizen>
198         public LayoutTransition(TransitionCondition condition,
199                                  AnimatableProperties animatableProperty,
200                                  object targetValue,
201                                  TransitionComponents animator)
202         {
203             Condition = condition;
204             AnimatableProperty = animatableProperty;
205             Animator = animator;
206             TargetValue = targetValue;
207         }
208
209         /// <summary>
210         /// Condition for this Transition
211         /// </summary>
212         /// <since_tizen> 6 </since_tizen>
213
214         public TransitionCondition Condition { get; set; }
215         /// <summary>
216         /// Property to animate.
217         /// </summary>
218         /// <since_tizen> 6 </since_tizen>
219
220         public AnimatableProperties AnimatableProperty { get; set; }
221         /// <summary>
222         /// Components of the Animator.
223         /// </summary>
224         /// <since_tizen> 6 </since_tizen>
225
226         public TransitionComponents Animator { get; set; }
227         /// <summary>
228         /// Target value to animate to.
229         /// </summary>
230         /// <since_tizen> 6 </since_tizen>
231
232         public object TargetValue { get; set; }
233     }
234
235
236     /// <summary>
237     /// Class to help manage the adding and updating of transitions.
238     /// </summary>
239     internal static class LayoutTransitionsHelper
240     {
241         /// <summary>
242         /// Adds the given transition and condition to a transition list.
243         /// </summary>
244         /// <param name="targetTransitionList">The list to add the transition to.</param>
245         /// <param name="condition">Condition for the transition.</param>
246         /// <param name="transition">The transition to add.</param>
247         /// <param name="explicitlySet">True is set explicitly, false if inherited.</param>
248         static public void AddTransitionForCondition(
249                               Dictionary<TransitionCondition, TransitionList> targetTransitionList,
250                               TransitionCondition condition,
251                               LayoutTransition transition,
252                               bool explicitlySet)
253         {
254             bool replaced = false;
255             bool conditionNotInDictionary = false;
256             TransitionList transitionListMatchingCondition;
257             if (targetTransitionList.TryGetValue(condition, out transitionListMatchingCondition))
258             {
259                 if (transitionListMatchingCondition != null)
260                 {
261                     for (var index = 0; index < transitionListMatchingCondition.Count; index++)
262                     {
263                         if (transitionListMatchingCondition[index].AnimatableProperty == transition.AnimatableProperty)
264                         {
265                             if (explicitlySet)
266                             {
267                                 transitionListMatchingCondition[index] = transition;
268                                 replaced = true;
269                                 continue;
270                             }
271                         }
272                     }
273                 }
274             }
275             else
276             {
277                 conditionNotInDictionary = true;
278             }
279
280             if (replaced == false)
281             {
282                 if (transitionListMatchingCondition == null)
283                 {
284                     transitionListMatchingCondition = new TransitionList();
285                 }
286                 transitionListMatchingCondition.Add(transition);
287                 // Update dictionary with new or replaced entry.
288                 if (conditionNotInDictionary)
289                 {
290                     targetTransitionList.Add(condition, transitionListMatchingCondition); // new entry
291                 }
292                 else
293                 {
294                     targetTransitionList[condition] = transitionListMatchingCondition; // replaced
295                 }
296             }
297         }
298
299         /// <summary>
300         /// Retreive the transition list for the given condition.
301         /// </summary>
302         /// <param name="sourceTransitionCollection">The source collection of transition lists to retrieve.</param>
303         /// <param name="condition">Condition for the transition.</param>
304         /// <param name="transitionsForCondition">transition list to return as out parameter.</param>
305         /// <returns>True if a transition list found for the given condition></returns>
306         static public bool GetTransitionsListForCondition(
307                               Dictionary<TransitionCondition, TransitionList> sourceTransitionCollection,
308                               TransitionCondition condition,
309                               TransitionList transitionsForCondition)
310         {
311             TransitionCondition resolvedCondition = condition;
312             bool matched = false;
313             // LayoutChanged transitions overrides ChangeOnAdd and ChangeOnRemove as siblings will
314             // reposition to the new layout not to the insertion/removal of a sibling.
315             if ((condition & TransitionCondition.LayoutChanged) == TransitionCondition.LayoutChanged)
316             {
317                 resolvedCondition = TransitionCondition.LayoutChanged;
318             }
319
320             if (sourceTransitionCollection.TryGetValue(resolvedCondition, out transitionsForCondition))
321             {
322                 matched = true;
323             }
324
325             return matched;
326         }
327
328         /// <summary>
329         /// Copy the transitions in the source list to the target list
330         /// </summary>
331         /// <param name="sourceTransitionList">The source transition list.</param>
332         /// <param name="targetTransitionList">The target transition list to copy to.</param>
333         static public void CopyTransitions(TransitionList sourceTransitionList,
334                                             TransitionList targetTransitionList)
335         {
336             targetTransitionList.Clear();
337             foreach (LayoutTransition transitionToApply in sourceTransitionList)
338             {
339                 // Overwite existing transitions
340                 targetTransitionList.Add(transitionToApply);
341             }
342
343         }
344     }
345
346 } // namespace Tizen.NUI