Release 4.0.0-preview1-00235
[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         public event EventHandlerWithReturnType<object, TickEventArgs, bool> Tick
102         {
103             add
104             {
105                 if (_timerTickEventHandler == null)
106                 {
107                     _timerTickCallbackDelegate = (OnTick);
108                     TickSignal().Connect(_timerTickCallbackDelegate);
109                 }
110                 _timerTickEventHandler += value;
111             }
112             remove
113             {
114                 _timerTickEventHandler -= value;
115                 if (_timerTickEventHandler == null && TickSignal().Empty() == false)
116                 {
117                     TickSignal().Disconnect(_timerTickCallbackDelegate);
118                 }
119             }
120         }
121
122         private bool OnTick(IntPtr data)
123         {
124             TickEventArgs e = new TickEventArgs();
125
126             if (_timerTickEventHandler != null)
127             {
128                 //here we send all data to user event handlers
129                 return _timerTickEventHandler(this, e);
130             }
131             return false;
132         }
133
134         /// <summary>
135         /// Creates a tick timer that emits periodic signal.
136         /// </summary>
137         /// <param name="milliSec">Interval in milliseconds.</param>
138         /// <returns>A new timer.</returns>
139         public Timer(uint milliSec) : this(NDalicPINVOKE.Timer_New(milliSec), true)
140         {
141             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
142
143         }
144         internal Timer(Timer timer) : this(NDalicPINVOKE.new_Timer__SWIG_1(Timer.getCPtr(timer)), true)
145         {
146             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
147         }
148
149
150         [Obsolete("Please do not use! this will be deprecated")]
151         public static Timer DownCast(BaseHandle handle)
152         {
153             Timer ret =  Registry.GetManagedBaseHandleFromNativePtr(handle) as Timer;
154             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
155             return ret;
156         }
157
158         /// <summary>
159         /// Starts the timer.<br>
160         /// In case a timer is already running, its time is reset and the timer is restarted.<br>
161         /// </summary>
162         public void Start()
163         {
164             NDalicPINVOKE.Timer_Start(swigCPtr);
165             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
166         }
167
168         /// <summary>
169         /// Stops the timer.
170         /// </summary>
171         public void Stop()
172         {
173             NDalicPINVOKE.Timer_Stop(swigCPtr);
174             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
175         }
176
177         /// <summary>
178         /// Gets/Sets the interval of the timer.
179         /// </summary>
180         public uint Interval
181         {
182             get
183             {
184                 return GetInterval();
185             }
186             set
187             {
188                 SetInterval(value);
189             }
190         }
191
192         /// <summary>
193         /// Sets a new interval on the timer and starts the timer.<br>
194         /// Cancels the previous timer.<br>
195         /// </summary>
196         /// <param name="milliSec">MilliSec interval in milliseconds.</param>
197         internal void SetInterval(uint milliSec)
198         {
199             NDalicPINVOKE.Timer_SetInterval(swigCPtr, milliSec);
200             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
201         }
202
203         internal uint GetInterval()
204         {
205             uint ret = NDalicPINVOKE.Timer_GetInterval(swigCPtr);
206             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
207             return ret;
208         }
209
210         /// <summary>
211         /// Tells whether the timer is running.
212         /// </summary>
213         /// <returns>Whether the timer is started or not.</returns>
214         public bool IsRunning()
215         {
216             bool ret = NDalicPINVOKE.Timer_IsRunning(swigCPtr);
217             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
218             return ret;
219         }
220
221         internal TimerSignalType TickSignal()
222         {
223             TimerSignalType ret = new TimerSignalType(NDalicPINVOKE.Timer_TickSignal(swigCPtr), false);
224             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
225             return ret;
226         }
227
228     }
229
230 }