[NUI] Add Obsolete attributes for EditorBrowsable apis
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Timer.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.Runtime.InteropServices;
19 using System.ComponentModel;
20
21 namespace Tizen.NUI
22 {
23     /// <summary>
24     /// Mechanism to issue simple periodic or one-shot events.<br />
25     /// Timer is provided for application developers to be able to issue
26     /// simple periodic or one-shot events. Please note that the timer
27     /// callback functions should return as soon as possible because they
28     /// block the next SignalTick. Please note that timer signals are not
29     /// in sync with DALi's render timer.<br />
30     /// This class is a handle class so it can be stack allocated and used
31     /// as a member.<br />
32     /// </summary>
33     /// <since_tizen> 3 </since_tizen>
34     public class Timer : BaseHandle
35     {
36         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
37
38         internal Timer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.Timer_SWIGUpcast(cPtr), cMemoryOwn)
39         {
40             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
41         }
42
43         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Timer obj)
44         {
45             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
46         }
47
48         /// <summary>
49         /// Dispose.
50         /// </summary>
51         /// <since_tizen> 3 </since_tizen>
52         protected override void Dispose(DisposeTypes type)
53         {
54             if(disposed)
55             {
56                 return;
57             }
58
59             if(type == DisposeTypes.Explicit)
60             {
61                 //Called by User
62                 //Release your own managed resources here.
63                 //You should release all of your own disposable objects here.
64             }
65
66             //Release your own unmanaged resources here.
67             //You should not access any managed member here except static instance.
68             //because the execution order of Finalizes is non-deterministic.
69
70             if (_timerTickCallbackDelegate != null)
71             {
72                 TickSignal().Disconnect(_timerTickCallbackDelegate);
73             }
74
75             if (swigCPtr.Handle != global::System.IntPtr.Zero)
76             {
77                 if (swigCMemOwn)
78                 {
79                     swigCMemOwn = false;
80                     NDalicPINVOKE.delete_Timer(swigCPtr);
81                 }
82                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
83             }
84
85             base.Dispose(type);
86         }
87
88
89         /// <summary>
90         /// Event arguments that passed via the tick event.
91         /// </summary>
92         /// <since_tizen> 3 </since_tizen>
93         public class TickEventArgs : EventArgs
94         {
95         }
96
97         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
98         private delegate bool TickCallbackDelegate(IntPtr data);
99         private EventHandlerWithReturnType<object, TickEventArgs, bool> _timerTickEventHandler;
100         private TickCallbackDelegate _timerTickCallbackDelegate;
101
102         /// <summary>
103         /// @brief Event for the ticked signal, which can be used to subscribe or unsubscribe the event handler
104         /// provided by the user. The ticked signal is emitted after specified time interval.<br />
105         /// </summary>
106         /// <since_tizen> 3 </since_tizen>
107         public event EventHandlerWithReturnType<object, TickEventArgs, bool> Tick
108         {
109             add
110             {
111                 if (_timerTickEventHandler == null)
112                 {
113                     _timerTickCallbackDelegate = (OnTick);
114                     TickSignal().Connect(_timerTickCallbackDelegate);
115                 }
116                 _timerTickEventHandler += value;
117             }
118             remove
119             {
120                 _timerTickEventHandler -= value;
121                 if (_timerTickEventHandler == null && TickSignal().Empty() == false)
122                 {
123                     TickSignal().Disconnect(_timerTickCallbackDelegate);
124                 }
125             }
126         }
127
128         private bool OnTick(IntPtr data)
129         {
130             TickEventArgs e = new TickEventArgs();
131
132             if (_timerTickEventHandler != null)
133             {
134                 //here we send all data to user event handlers
135                 return _timerTickEventHandler(this, e);
136             }
137             return false;
138         }
139
140         /// <summary>
141         /// Creates a tick timer that emits periodic signal.
142         /// </summary>
143         /// <param name="milliSec">Interval in milliseconds.</param>
144         /// <returns>A new timer.</returns>
145         /// <since_tizen> 3 </since_tizen>
146         public Timer(uint milliSec) : this(NDalicPINVOKE.Timer_New(milliSec), true)
147         {
148             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
149
150         }
151         internal Timer(Timer timer) : this(NDalicPINVOKE.new_Timer__SWIG_1(Timer.getCPtr(timer)), true)
152         {
153             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
154         }
155
156         /// <summary>
157         /// Downcasts a handle to Timer handle.
158         /// </summary>
159         /// <since_tizen> 3 </since_tizen>
160         /// Please do not use! this will be deprecated!
161         /// Instead please use as keyword.
162         [Obsolete("Please do not use! This will be deprecated! Please use as keyword instead!")]
163         [EditorBrowsable(EditorBrowsableState.Never)]
164         public static Timer DownCast(BaseHandle handle)
165         {
166             Timer ret =  Registry.GetManagedBaseHandleFromNativePtr(handle) as Timer;
167             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
168             return ret;
169         }
170
171         /// <summary>
172         /// Starts the timer.<br />
173         /// In case a timer is already running, its time is reset and the timer is restarted.<br />
174         /// </summary>
175         /// <since_tizen> 3 </since_tizen>
176         public void Start()
177         {
178             NDalicPINVOKE.Timer_Start(swigCPtr);
179             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
180         }
181
182         /// <summary>
183         /// Stops the timer.
184         /// </summary>
185         /// <since_tizen> 3 </since_tizen>
186         public void Stop()
187         {
188             NDalicPINVOKE.Timer_Stop(swigCPtr);
189             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
190         }
191
192         /// <summary>
193         /// Gets/Sets the interval of the timer.
194         /// </summary>
195         /// <since_tizen> 4 </since_tizen>
196         public uint Interval
197         {
198             get
199             {
200                 return GetInterval();
201             }
202             set
203             {
204                 SetInterval(value);
205             }
206         }
207
208         /// <summary>
209         /// Sets a new interval on the timer and starts the timer.<br />
210         /// Cancels the previous timer.<br />
211         /// </summary>
212         /// <param name="milliSec">MilliSec interval in milliseconds.</param>
213         internal void SetInterval(uint milliSec)
214         {
215             NDalicPINVOKE.Timer_SetInterval(swigCPtr, milliSec);
216             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
217         }
218
219         internal uint GetInterval()
220         {
221             uint ret = NDalicPINVOKE.Timer_GetInterval(swigCPtr);
222             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
223             return ret;
224         }
225
226         /// <summary>
227         /// Tells whether the timer is running.
228         /// </summary>
229         /// <returns>Whether the timer is started or not.</returns>
230         /// <since_tizen> 3 </since_tizen>
231         public bool IsRunning()
232         {
233             bool ret = NDalicPINVOKE.Timer_IsRunning(swigCPtr);
234             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
235             return ret;
236         }
237
238         internal TimerSignalType TickSignal()
239         {
240             TimerSignalType ret = new TimerSignalType(NDalicPINVOKE.Timer_TickSignal(swigCPtr), false);
241             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
242             return ret;
243         }
244
245     }
246
247 }