2 * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://floralicense.org/license/
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.
19 using Xamarin.Forms.Platform.Tizen;
20 using XForms = Xamarin.Forms.Forms;
22 [assembly: Dependency(typeof(Tizen.Wearable.CircularUI.Forms.Renderer.TwoButtonPopupImplementation))]
24 namespace Tizen.Wearable.CircularUI.Forms.Renderer
26 public class TwoButtonPopupImplementation : ITwoButtonPopup, IDisposable
29 StackLayout _contentView;
30 MenuItem _firstMenuItem;
31 MenuItem _secondMenuItem;
32 Color _firstButtonBgColor;
33 Color _secondButtonBgColor;
35 ElmSharp.Popup _popUp;
36 ElmSharp.Layout _layout;
37 ElmSharp.Button _firstButton;
38 ElmSharp.Button _secondButton;
39 ElmSharp.EvasObject _nativeContent;
45 public event EventHandler BackButtonPressed;
47 public TwoButtonPopupImplementation()
49 _popUp = new ElmSharp.Popup(XForms.NativeParent);
50 _popUp.Style = "circle";
52 _layout = new ElmSharp.Layout(_popUp);
53 _layout.SetTheme("layout", "popup", "content/circle/buttons2");
54 _popUp.SetContent(_layout);
56 _popUp.BackButtonPressed += BackButtonPressedHandler;
57 _popUp.Dismissed += OnDismissed;
59 _contentView = new StackLayout();
62 ~TwoButtonPopupImplementation()
70 GC.SuppressFinalize(this);
73 protected virtual void Dispose(bool disposing)
80 if (_firstButton != null)
82 _firstButton.Unrealize();
86 if (_secondButton != null)
88 _secondButton.Unrealize();
92 if (_nativeContent != null)
94 _nativeContent.Unrealize();
95 _nativeContent = null;
110 void BackButtonPressedHandler(object sender, EventArgs e)
112 BackButtonPressed?.Invoke(this, EventArgs.Empty);
128 public MenuItem FirstButton
132 return _firstMenuItem;
136 if (_firstMenuItem == value) return;
137 _firstMenuItem = value;
139 if (value is ColorMenuItem)
141 _firstButtonBgColor = ((ColorMenuItem)value).BackgroundColor;
145 _firstButtonBgColor = Color.Default;
152 public MenuItem SecondButton
156 return _secondMenuItem;
160 if (_secondMenuItem == value) return;
161 _secondMenuItem = value;
163 if (value is ColorMenuItem)
165 _secondButtonBgColor = ((ColorMenuItem)value).BackgroundColor;
169 _secondButtonBgColor = Color.Default;
172 UpdateSecondButton();
184 if (_title == value) return;
198 if (_text == value) return;
206 if (!XForms.IsInitialized)
208 Log.Debug(FormsCircularUI.Tag, "Tizen Forms is not initialized");
212 _contentView.Children.Clear();
215 _contentView.Children.Add(Content);
217 var renderer = Xamarin.Forms.Platform.Tizen.Platform.GetOrCreateRenderer(_contentView);
218 (renderer as LayoutRenderer)?.RegisterOnLayoutUpdated();
219 var sizeRequest = _contentView.Measure(XForms.NativeParent.Geometry.Width, XForms.NativeParent.Geometry.Height).Request.ToPixel();
221 _nativeContent = renderer.NativeView;
222 _nativeContent.MinimumHeight = sizeRequest.Height;
224 _layout.SetPartContent("elm.swallow.content", _nativeContent, true);
228 _layout.SetPartContent("elm.swallow.content", null, true);
232 void UpdateFirstButton()
234 _firstButton?.Hide();
236 if (FirstButton != null)
238 _firstButton = new ElmSharp.Button(_popUp)
242 Style = "popup/circle/left"
245 if (!string.IsNullOrEmpty(FirstButton.Text)) _firstButton.Text = FirstButton.Text;
247 if (!FirstButton.IconImageSource.IsNullOrEmpty())
249 var iconSource = FirstButton.IconImageSource as FileImageSource;
250 if (!iconSource.IsNullOrEmpty())
252 var buttonImage = new ElmSharp.Image(_firstButton);
253 buttonImage.LoadAsync(ResourcePath.GetPath(iconSource));
255 _firstButton.SetPartContent("elm.swallow.content", buttonImage);
259 _firstButton.Clicked += (s, e) =>
261 ((IMenuItemController)FirstButton).Activate();
264 if (_firstButtonBgColor != Color.Default)
266 Log.Debug(FormsCircularUI.Tag, $"TwoButtonPopup set first button background color:{_firstButtonBgColor.ToNative()}");
267 _firstButton.BackgroundColor = _firstButtonBgColor.ToNative();
275 _popUp.SetPartContent("button1", _firstButton);
278 void UpdateSecondButton()
280 _secondButton?.Hide();
282 if (SecondButton != null)
284 _secondButton = new ElmSharp.Button(_popUp)
288 Style = "popup/circle/right"
291 if (!string.IsNullOrEmpty(SecondButton.Text)) _secondButton.Text = SecondButton.Text;
293 if (!SecondButton.IconImageSource.IsNullOrEmpty())
295 var iconSource = SecondButton.IconImageSource as FileImageSource;
296 if (!iconSource.IsNullOrEmpty())
298 var buttonImage = new ElmSharp.Image(_secondButton);
299 buttonImage.LoadAsync(ResourcePath.GetPath(iconSource));
301 _secondButton.SetPartContent("elm.swallow.content", buttonImage);
305 _secondButton.Clicked += (s, e) =>
307 ((IMenuItemController)SecondButton).Activate();
310 if (_secondButtonBgColor != Color.Default)
312 Log.Debug(FormsCircularUI.Tag, $"TwoButtonPopup set second button background color:{_secondButtonBgColor.ToNative()}");
313 _secondButton.BackgroundColor = _secondButtonBgColor.ToNative();
316 _popUp.SetPartContent("button2", _secondButton);
320 /* This is workaround fix for Left button occupied whole window when right button is null*/
321 _secondButton = new ElmSharp.Button(_popUp)
325 Style = "popup/circle/right"
327 _popUp.SetPartContent("button2", _secondButton);
328 _secondButton.Unrealize();
329 _secondButton = null;
335 string title = _title?.Replace("&", "&")
336 .Replace("<", "<")
337 .Replace(">", ">")
338 .Replace(Environment.NewLine, "<br>");
339 _layout.SetPartText("elm.text.title", title);
344 string text = _text?.Replace("&", "&")
345 .Replace("<", "<")
346 .Replace(">", ">")
347 .Replace(Environment.NewLine, "<br>");
348 _layout.SetPartText("elm.text", text);
353 if (!XForms.IsInitialized)
355 throw new InvalidOperationException("When the Application's Platform is not initialized, it can not show the Dialog.");
364 public void Dismiss()
366 if (_firstButton != null)
368 _firstButton.Unrealize();
370 _popUp.SetPartContent("button1", null);
373 if (_secondButton != null)
375 _secondButton.Unrealize();
376 _secondButton = null;
377 _popUp.SetPartContent("button2", null);
380 if (_nativeContent != null)
382 _nativeContent.Unrealize();
383 _nativeContent = null;
395 void OnDismissed(object sender, EventArgs e)
397 Log.Debug(FormsCircularUI.Tag, $"OnDismissed called");