2819f9528f231996f3489501eee0e06449967920
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Timer.cs
1 /** Copyright (c) 2017 Samsung Electronics Co., Ltd.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16
17 namespace Tizen.NUI
18 {
19
20     using System;
21     using System.Runtime.InteropServices;
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     public class Timer : BaseHandle
34     {
35         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
36
37         internal Timer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.Timer_SWIGUpcast(cPtr), cMemoryOwn)
38         {
39             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
40         }
41
42         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Timer obj)
43         {
44             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
45         }
46
47         protected override void Dispose(DisposeTypes type)
48         {
49             if(disposed)
50             {
51                 return;
52             }
53
54             if(type == DisposeTypes.Explicit)
55             {
56                 //Called by User
57                 //Release your own managed resources here.
58                 //You should release all of your own disposable objects here.
59             }
60
61             //Release your own unmanaged resources here.
62             //You should not access any managed member here except static instance.
63             //because the execution order of Finalizes is non-deterministic.
64
65             if (_timerTickCallbackDelegate != null)
66             {
67                 TickSignal().Disconnect(_timerTickCallbackDelegate);
68             }
69
70             if (swigCPtr.Handle != global::System.IntPtr.Zero)
71             {
72                 if (swigCMemOwn)
73                 {
74                     swigCMemOwn = false;
75                     NDalicPINVOKE.delete_Timer(swigCPtr);
76                 }
77                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
78             }
79
80             base.Dispose(type);
81         }
82
83
84         /// <summary>
85         /// Event arguments that passed via the tick event.
86         /// </summary>
87         public class TickEventArgs : EventArgs
88         {
89         }
90
91         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
92         private delegate bool TickCallbackDelegate(IntPtr data);
93         private EventHandlerWithReturnType<object, TickEventArgs, bool> _timerTickEventHandler;
94         private TickCallbackDelegate _timerTickCallbackDelegate;
95
96         /// <summary>
97         /// @brief Event for the ticked signal, which can be used to subscribe or unsubscribe the event handler
98         /// (in the type of TickEventHandler-DaliEventHandlerWithReturnType<object,TickEventArgs,bool>)<br>
99         /// provided by the user. The ticked signal is emitted after specified time interval.<br>
100         /// </summary>
101         /// <since_tizen> 3 </since_tizen>
102         public event EventHandlerWithReturnType<object, TickEventArgs, bool> Tick
103         {
104             add
105             {
106                 if (_timerTickEventHandler == null)
107                 {
108                     _timerTickCallbackDelegate = (OnTick);
109                     TickSignal().Connect(_timerTickCallbackDelegate);
110                 }
111                 _timerTickEventHandler += value;
112             }
113             remove
114             {
115                 _timerTickEventHandler -= value;
116                 if (_timerTickEventHandler == null && TickSignal().Empty() == false)
117                 {
118                     TickSignal().Disconnect(_timerTickCallbackDelegate);
119                 }
120             }
121         }
122
123         private bool OnTick(IntPtr data)
124         {
125             TickEventArgs e = new TickEventArgs();
126
127             if (_timerTickEventHandler != null)
128             {
129                 //here we send all data to user event handlers
130                 return _timerTickEventHandler(this, e);
131             }
132             return false;
133         }
134
135         /// <summary>
136         /// Creates a tick timer that emits periodic signal.
137         /// </summary>
138         /// <param name="milliSec">Interval in milliseconds.</param>
139         /// <returns>A new timer.</returns>
140         /// <since_tizen> 3 </since_tizen>
141         public Timer(uint milliSec) : this(NDalicPINVOKE.Timer_New(milliSec), true)
142         {
143             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
144
145         }
146         internal Timer(Timer timer) : this(NDalicPINVOKE.new_Timer__SWIG_1(Timer.getCPtr(timer)), true)
147         {
148             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
149         }
150
151
152         [Obsolete("Please do not use! this will be deprecated")]
153         public static Timer DownCast(BaseHandle handle)
154         {
155             Timer ret =  Registry.GetManagedBaseHandleFromNativePtr(handle) as Timer;
156             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
157             return ret;
158         }
159
160         /// <summary>
161         /// Starts the timer.<br>
162         /// In case a timer is already running, its time is reset and the timer is restarted.<br>
163         /// </summary>
164         /// <since_tizen> 3 </since_tizen>
165         public void Start()
166         {
167             NDalicPINVOKE.Timer_Start(swigCPtr);
168             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
169         }
170
171         /// <summary>
172         /// Stops the timer.
173         /// </summary>
174         /// <since_tizen> 3 </since_tizen>
175         public void Stop()
176         {
177             NDalicPINVOKE.Timer_Stop(swigCPtr);
178             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
179         }
180
181         /// <summary>
182         /// Gets/Sets the interval of the timer.
183         /// </summary>
184         /// <since_tizen> 4 </since_tizen>
185         public uint Interval
186         {
187             get
188             {
189                 return GetInterval();
190             }
191             set
192             {
193                 SetInterval(value);
194             }
195         }
196
197         /// <summary>
198         /// Sets a new interval on the timer and starts the timer.<br>
199         /// Cancels the previous timer.<br>
200         /// </summary>
201         /// <param name="milliSec">MilliSec interval in milliseconds.</param>
202         internal void SetInterval(uint milliSec)
203         {
204             NDalicPINVOKE.Timer_SetInterval(swigCPtr, milliSec);
205             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
206         }
207
208         internal uint GetInterval()
209         {
210             uint ret = NDalicPINVOKE.Timer_GetInterval(swigCPtr);
211             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
212             return ret;
213         }
214
215         /// <summary>
216         /// Tells whether the timer is running.
217         /// </summary>
218         /// <returns>Whether the timer is started or not.</returns>
219         /// <since_tizen> 3 </since_tizen>
220         public bool IsRunning()
221         {
222             bool ret = NDalicPINVOKE.Timer_IsRunning(swigCPtr);
223             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
224             return ret;
225         }
226
227         internal TimerSignalType TickSignal()
228         {
229             TimerSignalType ret = new TimerSignalType(NDalicPINVOKE.Timer_TickSignal(swigCPtr), false);
230             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
231             return ret;
232         }
233
234     }
235
236 }