|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 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.ContentPopupImplementation))] |
| 23 | + |
| 24 | +namespace Tizen.Wearable.CircularUI.Forms.Renderer |
| 25 | +{ |
| 26 | + public class ContentPopupImplementation : IContentPopup, IDisposable |
| 27 | + { |
| 28 | + View _content; |
| 29 | + StackLayout _contentView; |
| 30 | + ElmSharp.Popup _popUp; |
| 31 | + ElmSharp.EvasObject _nativeContent; |
| 32 | + bool _isDisposed; |
| 33 | + |
| 34 | + public event EventHandler BackButtonPressed; |
| 35 | + |
| 36 | + public ContentPopupImplementation() |
| 37 | + { |
| 38 | + _popUp = new ElmSharp.Popup(XForms.NativeParent); |
| 39 | + _popUp.Style = "circle"; |
| 40 | + |
| 41 | + _popUp.BackButtonPressed += BackButtonPressedHandler; |
| 42 | + _popUp.Dismissed += OnDismissed; |
| 43 | + |
| 44 | + _contentView = new StackLayout(); |
| 45 | + } |
| 46 | + |
| 47 | + ~ContentPopupImplementation() |
| 48 | + { |
| 49 | + Dispose(false); |
| 50 | + } |
| 51 | + |
| 52 | + public void Dispose() |
| 53 | + { |
| 54 | + Dispose(true); |
| 55 | + GC.SuppressFinalize(this); |
| 56 | + } |
| 57 | + |
| 58 | + protected virtual void Dispose(bool disposing) |
| 59 | + { |
| 60 | + if (_isDisposed) |
| 61 | + return; |
| 62 | + |
| 63 | + if (disposing) |
| 64 | + { |
| 65 | + if (_nativeContent != null) |
| 66 | + { |
| 67 | + _nativeContent.Unrealize(); |
| 68 | + _nativeContent = null; |
| 69 | + } |
| 70 | + |
| 71 | + if (_popUp != null) |
| 72 | + { |
| 73 | + _popUp.BackButtonPressed -= BackButtonPressedHandler; |
| 74 | + _popUp.Dismissed -= OnDismissed; |
| 75 | + _popUp.Unrealize(); |
| 76 | + _popUp = null; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + _isDisposed = true; |
| 81 | + } |
| 82 | + |
| 83 | + public View Content |
| 84 | + { |
| 85 | + get |
| 86 | + { |
| 87 | + return _content; |
| 88 | + } |
| 89 | + set |
| 90 | + { |
| 91 | + _content = value; |
| 92 | + UpdateContent(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + void BackButtonPressedHandler(object sender, EventArgs e) |
| 97 | + { |
| 98 | + BackButtonPressed?.Invoke(this, EventArgs.Empty); |
| 99 | + } |
| 100 | + |
| 101 | + void UpdateContent() |
| 102 | + { |
| 103 | + if (!XForms.IsInitialized) |
| 104 | + { |
| 105 | + Log.Debug(FormsCircularUI.Tag, "Tizen Forms is not initialized"); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + _contentView.Children.Clear(); |
| 110 | + if (Content != null) |
| 111 | + { |
| 112 | + _contentView.Children.Add(Content); |
| 113 | + |
| 114 | + var renderer = Platform.GetOrCreateRenderer(_contentView); |
| 115 | + (renderer as LayoutRenderer)?.RegisterOnLayoutUpdated(); |
| 116 | + |
| 117 | + var sizeRequest = _contentView.Measure(XForms.NativeParent.Geometry.Width, XForms.NativeParent.Geometry.Height).Request.ToPixel(); |
| 118 | + _nativeContent = renderer.NativeView; |
| 119 | + _nativeContent.MinimumHeight = sizeRequest.Height; |
| 120 | + |
| 121 | + _popUp.SetContent(_nativeContent, true); |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + if (_nativeContent != null) |
| 126 | + { |
| 127 | + _nativeContent.Unrealize(); |
| 128 | + _nativeContent = null; |
| 129 | + } |
| 130 | + _popUp.SetContent(null, true); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + public void Show() |
| 135 | + { |
| 136 | + if (!XForms.IsInitialized) |
| 137 | + { |
| 138 | + throw new InvalidOperationException("When the Application's Platform is not initialized, it can not show the Dialog."); |
| 139 | + } |
| 140 | + |
| 141 | + if (_popUp != null) |
| 142 | + { |
| 143 | + _popUp.Show(); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + public void Dismiss() |
| 148 | + { |
| 149 | + _popUp.Hide(); |
| 150 | + _popUp.Dismiss(); |
| 151 | + } |
| 152 | + |
| 153 | + void OnDismissed(object sender, EventArgs e) |
| 154 | + { |
| 155 | + Dispose(); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments