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