Support new features of Tizen.CircularUI (#188)
[platform/core/csapi/xsf.git] / src / XSF / Tizen.Wearable.CircularUI.Forms.Renderer / InformationPopupImplementation.cs
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 Xamarin.Forms;
19 using Xamarin.Forms.Platform.Tizen;
20 using XForms = Xamarin.Forms.Forms;
21
22 [assembly: Dependency(typeof(Tizen.Wearable.CircularUI.Forms.Renderer.InformationPopupImplementation))]
23
24 namespace Tizen.Wearable.CircularUI.Forms.Renderer
25 {
26         public class InformationPopupImplementation : IInformationPopup, IDisposable
27         {
28                 MenuItem _bottomMenuItem;
29                 Color _buttonBgColor;
30
31                 ElmSharp.Popup _popUp;
32                 ElmSharp.ProgressBar _progress;
33                 ElmSharp.Layout _layout;
34                 ElmSharp.Button _bottomButton;
35                 ElmSharp.Box _box;
36                 ElmSharp.Label _progressLabel;
37
38                 string _title;
39                 string _text;
40                 bool _isProgressRunning;
41                 bool _isDisposed;
42
43                 public event EventHandler BackButtonPressed;
44
45                 public InformationPopupImplementation()
46                 {
47                         _popUp = new ElmSharp.Popup(XForms.NativeParent);
48                         _popUp.Style = "circle";
49
50                         _layout = new ElmSharp.Layout(_popUp);
51                         _layout.SetTheme("layout", "popup", "content/circle");
52                         _popUp.SetContent(_layout);
53
54                         _popUp.BackButtonPressed += BackButtonPressedHandler;
55                         _popUp.Dismissed += OnDismissed;
56                 }
57
58                 ~InformationPopupImplementation()
59                 {
60                         Dispose(false);
61                 }
62
63                 public void Dispose()
64                 {
65                         Dispose(true);
66                         GC.SuppressFinalize(this);
67                 }
68
69                 protected virtual void Dispose(bool disposing)
70                 {
71                         if (_isDisposed)
72                                 return;
73
74                         if (disposing)
75                         {
76                                 if (_bottomButton != null)
77                                 {
78                                         _bottomButton.Unrealize();
79                                         _bottomButton = null;
80                                 }
81
82                                 if (_box != null)
83                                 {
84                                         _box.Unrealize();
85                                         _box = null;
86                                 }
87
88                                 if (_progress != null)
89                                 {
90                                         _progress.Unrealize();
91                                         _progress = null;
92                                 }
93
94                                 if (_progressLabel != null)
95                                 {
96                                         _progressLabel.Unrealize();
97                                         _progressLabel = null;
98                                 }
99
100                                 if (_popUp != null)
101                                 {
102                                         _layout.Unrealize();
103                                         _layout = null;
104                                         _popUp.BackButtonPressed -= BackButtonPressedHandler;
105                                         _popUp.Unrealize();
106                                         _popUp = null;
107                                 }
108                         }
109
110                         _isDisposed = true;
111                 }
112
113                 void BackButtonPressedHandler(object sender, EventArgs e)
114                 {
115                         BackButtonPressed?.Invoke(this, EventArgs.Empty);
116                 }
117
118                 public bool IsProgressRunning
119                 {
120                         get
121                         {
122                                 return _isProgressRunning;
123                         }
124                         set
125                         {
126                                 _isProgressRunning = value;
127                                 UpdateProcessVisibility();
128                         }
129                 }
130
131                 public MenuItem BottomButton
132                 {
133                         get
134                         {
135                                 return _bottomMenuItem;
136                         }
137                         set
138                         {
139                                 if (_bottomMenuItem == value) return;
140
141                                 if (value is ColorMenuItem)
142                                 {
143                                         _buttonBgColor = ((ColorMenuItem)value).BackgroundColor;
144                                 }
145                                 else
146                                 {
147                                         _buttonBgColor = Color.Default;
148                                 }
149                                 _bottomMenuItem = value;
150                                 UpdateButton();
151                         }
152                 }
153
154                 public string Title
155                 {
156                         get
157                         {
158                                 return _title;
159                         }
160                         set
161                         {
162                                 if (_title == value) return;
163                                 _title = value;
164                                 UpdateTitle();
165                         }
166                 }
167
168                 public string Text
169                 {
170                         get
171                         {
172                                 return _text;
173                         }
174                         set
175                         {
176                                 if (_text == value) return;
177                                 _text = value;
178                                 UpdateText();
179                         }
180                 }
181
182                 void UpdateProcessVisibility()
183                 {
184                         if (!XForms.IsInitialized)
185                         {
186                                 Log.Debug(FormsCircularUI.Tag, "Tizen Forms is not initialized");
187                                 return;
188                         }
189
190                         if (_isProgressRunning)
191                         {
192                                 _box = new ElmSharp.Box(XForms.NativeParent);
193                                 _box.Show();
194
195                                 _progress = new ElmSharp.ProgressBar(XForms.NativeParent)
196                                 {
197                                         Style = "process/popup/small",
198                                 };
199                                 _progress.Show();
200                                 _progress.PlayPulse();
201                                 _box.PackEnd(_progress);
202
203                                 _layout.SetPartContent("elm.swallow.content", _box, true);
204                         }
205                         else
206                         {
207                                 if (_box != null)
208                                 {
209                                         if (_progress != null)
210                                         {
211                                                 _progress.Unrealize();
212                                                 _progress = null;
213                                         }
214
215                                         if (_progressLabel != null)
216                                         {
217                                                 _progressLabel.Unrealize();
218                                                 _progressLabel = null;
219                                         }
220
221                                         _box.Unrealize();
222                                         _box = null;
223                                 }
224                                 _layout.SetPartContent("elm.swallow.content", null, true);
225                         }
226
227                         UpdateTitle();
228                         UpdateText();
229                 }
230
231                 void UpdateButton()
232                 {
233                         _bottomButton?.Hide();
234
235                         if (BottomButton != null)
236                         {
237                                 _bottomButton = new ElmSharp.Button(_popUp)
238                                 {
239                                         WeightX = 1.0,
240                                         WeightY = 1.0,
241                                         Style = "bottom"
242                                 };
243
244                                 if (!string.IsNullOrEmpty(BottomButton.Text)) _bottomButton.Text = BottomButton.Text;
245
246                                 if (!BottomButton.IconImageSource.IsNullOrEmpty())
247                                 {
248                                         var iconSource = BottomButton.IconImageSource as FileImageSource;
249                                         var buttonImage = new ElmSharp.Image(_bottomButton);
250                                         buttonImage.LoadAsync(ResourcePath.GetPath(iconSource));
251                                         buttonImage.Show();
252                                         _bottomButton.SetPartContent("elm.swallow.content", buttonImage);
253                                 }
254
255                                 _bottomButton.Clicked += (s, e) =>
256                                 {
257                                         ((IMenuItemController)BottomButton).Activate();
258                                 };
259
260                                 if (_buttonBgColor != Color.Default)
261                                 {
262                                         Log.Debug(FormsCircularUI.Tag, $"InformationPopup set button background color:{_buttonBgColor.ToNative()}");
263                                         _bottomButton.BackgroundColor = _buttonBgColor.ToNative();
264                                 }
265                         }
266                         else
267                         {
268                                 _bottomButton.Unrealize();
269                                 _bottomButton = null;
270                         }
271
272                         _popUp.SetPartContent("button1", _bottomButton);
273                 }
274
275                 void UpdateTitle()
276                 {
277                         string title = _title?.Replace("&", "&")
278                                                                   .Replace("<", "&lt;")
279                                                                   .Replace(">", "&gt;")
280                                                                   .Replace(Environment.NewLine, "<br>");
281
282                         if (!_isProgressRunning)
283                         {
284                                 _layout.SetPartText("elm.text.title", title);
285                         }
286                         else
287                         {
288                                 _layout.SetPartText("elm.text.title", null);
289                         }
290                 }
291
292                 void UpdateText()
293                 {
294                         string text = _text?.Replace("&", "&amp;")
295                                                                 .Replace("<", "&lt;")
296                                                                 .Replace(">", "&gt;")
297                                                                 .Replace(Environment.NewLine, "<br>");
298
299                         if (!_isProgressRunning)
300                         {
301                                 _layout.SetPartText("elm.text", text);
302                         }
303                         else
304                         {
305                                 _layout.SetPartText("elm.text", null);
306                                 if (!string.IsNullOrEmpty(text))
307                                 {
308                                         if (_progressLabel == null)
309                                         {
310                                                 _progressLabel = new ElmSharp.Label(XForms.NativeParent)
311                                                 {
312                                                         TextStyle = "DEFAULT ='font=Tizen:style=Light color=#F9F9F9FF font_size=32 align=center valign=top wrap=word'",
313                                                 };
314                                         }
315                                         _progressLabel.Text = text;
316                                         _progressLabel.Show();
317                                         if (_box != null)
318                                         {
319                                                 _box.PackEnd(_progressLabel);
320                                         }
321                                 }
322                         }
323                 }
324
325                 public void Show()
326                 {
327                         if (!XForms.IsInitialized)
328                         {
329                                 throw new InvalidOperationException("When the Application's Platform is not initialized, it can not show the Dialog.");
330                         }
331
332                         if (_popUp != null)
333                         {
334                                 _popUp.Show();
335                         }
336                 }
337
338                 public void Dismiss()
339                 {
340                         _popUp.Hide();
341
342                         if (_bottomButton != null)
343                         {
344                                 _bottomButton.Unrealize();
345                                 _bottomButton = null;
346                                 _popUp.SetPartContent("button1", null);
347                         }
348
349                         if (_box != null)
350                         {
351                                 _box.Unrealize();
352                                 _box = null;
353                         }
354
355                         if (_progress != null)
356                         {
357                                 _progress.Unrealize();
358                                 _progress = null;
359                         }
360
361                         if (_progressLabel != null)
362                         {
363                                 _progressLabel.Unrealize();
364                                 _progressLabel = null;
365                         }
366
367                         if (_popUp != null)
368                         {
369                                 _layout.Unrealize();
370                                 _layout = null;
371                                 _popUp.BackButtonPressed -= BackButtonPressedHandler;
372                                 _popUp.Unrealize();
373                                 _popUp = null;
374                         }
375                 }
376
377                 void OnDismissed(object sender, EventArgs e)
378                 {
379                         Log.Debug(FormsCircularUI.Tag, $"OnDismissed called");
380                         Dispose();
381                 }
382         }
383 }