Release 4.0.0-preview1-00051
[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 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 (swigCPtr.Handle != global::System.IntPtr.Zero)
66             {
67                 if (swigCMemOwn)
68                 {
69                     swigCMemOwn = false;
70                     NDalicPINVOKE.delete_Timer(swigCPtr);
71                 }
72                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
73             }
74
75             base.Dispose(type);
76         }
77
78
79         /// <summary>
80         /// Event arguments that passed via Tick event.
81         /// </summary>
82         public class TickEventArgs : EventArgs
83         {
84         }
85
86         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
87         private delegate bool TickCallbackDelegate(IntPtr data);
88         private EventHandlerWithReturnType<object, TickEventArgs, bool> _timerTickEventHandler;
89         private TickCallbackDelegate _timerTickCallbackDelegate;
90
91         /// <summary>
92         /// brief Event for Ticked signal which can be used to subscribe/unsubscribe the event handler
93         /// (in the type of TickEventHandler-DaliEventHandlerWithReturnType<object,TickEventArgs,bool>).<br>
94         /// provided by the user. Ticked signal is emitted after specified time interval.<br>
95         /// </summary>
96         public event EventHandlerWithReturnType<object, TickEventArgs, bool> Tick
97         {
98             add
99             {
100                 if (_timerTickEventHandler == null)
101                 {
102                     _timerTickCallbackDelegate = (OnTick);
103                     TickSignal().Connect(_timerTickCallbackDelegate);
104                 }
105                 _timerTickEventHandler += value;
106             }
107             remove
108             {
109                 _timerTickEventHandler -= value;
110                 if (_timerTickEventHandler == null && TickSignal().Empty() == false)
111                 {
112                     TickSignal().Disconnect(_timerTickCallbackDelegate);
113                 }
114             }
115         }
116
117         private bool OnTick(IntPtr data)
118         {
119             TickEventArgs e = new TickEventArgs();
120
121             if (_timerTickEventHandler != null)
122             {
123                 //here we send all data to user event handlers
124                 return _timerTickEventHandler(this, e);
125             }
126             return false;
127         }
128
129         /// <summary>
130         /// Creates a tick Timer that emits periodic signal.
131         /// </summary>
132         /// <param name="milliSec">Interval in milliseconds</param>
133         /// <returns>A new timer</returns>
134         public Timer(uint milliSec) : this(NDalicPINVOKE.Timer_New(milliSec), true)
135         {
136             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
137
138         }
139         internal Timer(Timer timer) : this(NDalicPINVOKE.new_Timer__SWIG_1(Timer.getCPtr(timer)), true)
140         {
141             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
142         }
143
144
145         [Obsolete("Please do not use! this will be deprecated")]
146         public static Timer DownCast(BaseHandle handle)
147         {
148             Timer ret =  Registry.GetManagedBaseHandleFromNativePtr(handle) as Timer;
149             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
150             return ret;
151         }
152
153         /// <summary>
154         /// Starts timer.<br>
155         /// In case a Timer is already running, its time is reset and timer is restarted.<br>
156         /// </summary>
157         public void Start()
158         {
159             NDalicPINVOKE.Timer_Start(swigCPtr);
160             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
161         }
162
163         /// <summary>
164         /// Stops timer.
165         /// </summary>
166         public void Stop()
167         {
168             NDalicPINVOKE.Timer_Stop(swigCPtr);
169             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
170         }
171
172         /// <summary>
173         /// Sets a new interval on the timer and starts the timer.<br>
174         /// Cancels the previous timer.<br>
175         /// </summary>
176         /// <param name="milliSec">milliSec Interval in milliseconds</param>
177         internal void SetInterval(uint milliSec)
178         {
179             NDalicPINVOKE.Timer_SetInterval(swigCPtr, milliSec);
180             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
181         }
182
183         internal uint GetInterval()
184         {
185             uint ret = NDalicPINVOKE.Timer_GetInterval(swigCPtr);
186             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
187             return ret;
188         }
189
190         /// <summary>
191         /// Tells whether timer is running.
192         /// </summary>
193         /// <returns>Whether Timer is started or not</returns>
194         public bool IsRunning()
195         {
196             bool ret = NDalicPINVOKE.Timer_IsRunning(swigCPtr);
197             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
198             return ret;
199         }
200
201         internal TimerSignalType TickSignal()
202         {
203             TimerSignalType ret = new TimerSignalType(NDalicPINVOKE.Timer_TickSignal(swigCPtr), false);
204             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
205             return ret;
206         }
207
208     }
209
210 }