8f04a00783fe14d0b2f4c250d62df09a5e7db06c
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Gadget / Tizen.NUI / NUIGadget.cs
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
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.ComponentModel;
19 using Tizen.Applications;
20 using Tizen.NUI.BaseComponents;
21
22 namespace Tizen.NUI
23 {
24     /// <summary>
25     /// This class represents a NUIGadget controlled lifecycles.
26     /// </summary>
27     /// <since_tizen> 10 </since_tizen>
28     [EditorBrowsable(EditorBrowsableState.Never)]
29     public abstract class NUIGadget
30     {
31         /// <summary>
32         /// Initializes the gadget.
33         /// </summary>
34         /// /// <param name="type">The type of the NUIGadget.</param>
35         /// <since_tizen> 10 </since_tizen>
36         public NUIGadget(NUIGadgetType type)
37         {
38             Type = type;
39             State = NUIGadgetLifecycleState.Initialized;
40         }
41
42         internal event EventHandler<NUIGadgetLifecycleChangedEventArgs> LifecycleChanged;
43
44         /// <summary>
45         /// Gets the class representing information of the current gadget.
46         /// </summary>
47         /// <since_tizen> 10 </since_tizen>
48         public NUIGadgetInfo NUIGadgetInfo
49         {
50             internal set;
51             get;
52         }
53
54         /// <summary>
55         /// Gets the type.
56         /// </summary>
57         /// <since_tizen> 10 </since_tizen>
58         public NUIGadgetType Type
59         {
60             internal set;
61             get;
62         }
63
64         /// <summary>
65         /// Gets the class name.
66         /// </summary>
67         /// <since_tizen> 10 </since_tizen>
68         public string ClassName
69         {
70             internal set;
71             get;
72         }
73
74         /// <summary>
75         /// Gets the main view.
76         /// </summary>
77         /// <since_tizen> 10 </since_tizen>
78         public View MainView
79         {
80             internal set;
81             get;
82         }
83
84         /// <summary>
85         /// Gets the lifecycle state.
86         /// </summary>
87         /// <since_tizen> 10 </since_tizen>
88         public NUIGadgetLifecycleState State
89         {
90             internal set;
91             get;
92         }
93
94         internal bool Create()
95         {
96             MainView = OnCreate();
97             if (MainView == null)
98             {
99                 return false;
100             }
101
102             return true;
103         }
104
105         internal void Resume()
106         {
107             if (State == NUIGadgetLifecycleState.Created || State == NUIGadgetLifecycleState.Paused)
108                 OnResume();
109         }
110
111         internal void Pause()
112         {
113             if (State == NUIGadgetLifecycleState.Resumed)
114                 OnPause();
115         }
116
117         internal void Destroy()
118         {
119             if (State == NUIGadgetLifecycleState.Created || State == NUIGadgetLifecycleState.Paused)
120                 OnDestroy();
121         }
122
123         internal void HandleAppControlReceivedEvent(AppControlReceivedEventArgs args)
124         {
125             OnAppControlReceived(args);
126         }
127
128         internal void HandleEvents(NUIGadgetEventType eventType, EventArgs args)
129         {
130             switch (eventType)
131             {
132                 case NUIGadgetEventType.LocaleChanged:
133                     OnLocaleChanged((LocaleChangedEventArgs)args);
134                     break;
135                 case NUIGadgetEventType.LowMemory:
136                     OnLowMemory((LowMemoryEventArgs)args);
137                     break;
138                 case NUIGadgetEventType.LowBattery:
139                     OnLowBattery((LowBatteryEventArgs)args);
140                     break;
141                 case NUIGadgetEventType.RegionFormatChanged:
142                     OnRegionFormatChanged((RegionFormatChangedEventArgs)args);
143                     break;
144                 case NUIGadgetEventType.DeviceORientationChanged:
145                     OnDeviceOrientationChanged((DeviceOrientationEventArgs)args);
146                     break;
147                 default:
148                     Log.Warn("Unknown Event Type: " + eventType);
149                     break;
150             }
151         }
152
153         private void NotifyLifecycleChanged()
154         {
155             var args = new NUIGadgetLifecycleChangedEventArgs();
156             args.State = State;
157             args.Gadget = this;
158             LifecycleChanged?.Invoke(null, args);
159         }
160
161         /// <summary>
162         /// Overrides this method if want to handle behavior when the gedget is started.
163         /// If 'base.OnCreate()' is not called, the event 'NUIGadgetLifecycleChanged' with  the 'NUIGadgetLifecycleState.Created' state will not be emitted.
164         /// </summary>
165         /// <returns>The main view object.</returns>
166         /// <since_tizen> 10 </since_tizen>
167         protected virtual Tizen.NUI.BaseComponents.View OnCreate()
168         {
169             State = NUIGadgetLifecycleState.Created;
170             NotifyLifecycleChanged();
171             return null;
172         }
173
174         /// <summary>
175         /// Overrides this method if want to handle behavior when the gadget receives the appcontrol message.
176         /// </summary>
177         /// <param name="e">The appcontrol received event argument.</param>
178         /// <since_tizen> 10 </since_tizen>
179         protected virtual void OnAppControlReceived(AppControlReceivedEventArgs e)
180         {
181         }
182
183         /// <summary>
184         /// Overrides this method if want to handle behavior when the gadget is destroyed.
185         /// If 'base.OnDestroy()' is not called. the event 'NUIGadgetLifecycleChanged' with the 'NUIGadgetLifecycleState.Destroyed' state will not be emitted.
186         /// </summary>
187         /// <since_tizen> 10 </since_tizen>
188         protected virtual void OnDestroy()
189         {
190             State = NUIGadgetLifecycleState.Destroyed;
191             NotifyLifecycleChanged();
192         }
193
194         /// <summary>
195         /// Overrides this method if want to handle behavior when the gadget is paused.
196         /// If 'base.OnPause()' is not called. the event 'NUIGadgetLifecycleChanged' with the 'NUIGadgetLifecycleState.Paused' state will not be emitted.
197         /// </summary>
198         /// <since_tizen> 10 </since_tizen>
199         protected virtual void OnPause()
200         {
201             State = NUIGadgetLifecycleState.Paused;
202             NotifyLifecycleChanged();
203         }
204
205         /// <summary>
206         /// Overrides this method if want to handle behavior when the gadget is resumed.
207         /// If 'base.OnResume()' is not called. the event 'NUIGadgetLifecycleChanged' with the 'NUIGadgetLifecycleState.Resumed' state will not be emitted.
208         /// </summary>
209         /// <since_tizen> 10 </since_tizen>
210         protected virtual void OnResume()
211         {
212             State = NUIGadgetLifecycleState.Resumed;
213             NotifyLifecycleChanged();
214         }
215
216         /// <summary>
217         /// Overrides this method if want to handle behavior when the system language is changed.
218         /// </summary>
219         /// <param name="e">The locale changed event argument.</param>
220         /// <since_tizen> 10 </since_tizen>
221         protected virtual void OnLocaleChanged(LocaleChangedEventArgs e)
222         {
223         }
224
225         /// <summary>
226         /// Overrides this method if want to handle behavior when the system battery is low.
227         /// </summary>
228         /// <param name="e">The low batter event argument.</param>
229         /// <since_tizen> 10 </since_tizen>
230         protected virtual void OnLowBattery(LowBatteryEventArgs e)
231         {
232         }
233
234         /// <summary>
235         /// Overrides this method if want to handle behavior when the system memory is low.
236         /// </summary>
237         /// <param name="e">The low memory event argument.</param>
238         /// <since_tizen> 10 </since_tizen>
239         protected virtual void OnLowMemory(LowMemoryEventArgs e)
240         {
241         }
242
243         /// <summary>
244         /// Overrides this method if want to handle behavior when the region format is changed.
245         /// </summary>
246         /// <param name="e">The region format changed event argument.</param>
247         /// <since_tizen> 10 </since_tizen>
248         protected virtual void OnRegionFormatChanged(RegionFormatChangedEventArgs e)
249         {
250         }
251
252         /// <summary>
253         /// Overrides this method if want to handle behavior when the device orientation is changed.
254         /// </summary>
255         /// <param name="e">The device orientation changed event argument.</param>
256         /// <since_tizen> 10 </since_tizen>
257         protected virtual void OnDeviceOrientationChanged(DeviceOrientationEventArgs e)
258         {
259         }
260
261         /// <summary>
262         /// Finishes the gadget.
263         /// </summary>
264         /// <since_tizen> 10 </since_tizen>
265         public void Finish()
266         {
267             Pause();
268             Destroy();
269         }
270     }
271 }