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