Enhance ContextPopup based on developer requests 65/124065/13
authorSidharth Gupta <sid92.gupta@samsung.com>
Fri, 14 Apr 2017 06:15:20 +0000 (15:15 +0900)
committerSidharth Gupta <sid92.gupta@samsung.com>
Tue, 18 Apr 2017 05:13:26 +0000 (14:13 +0900)
This is related to RFC 28.

- Add Show(anchor, x, y) method to allow ContextPopups to be positioned.
- Add ItemSelected event. The existing SelectedIndexChanged event would
  only be invoked when a new item is selected.

- Verify and improve minor bugs.

Signed-off-by: Sidharth Gupta <sid92.gupta@samsung.com>
Change-Id: Iec96c064572fdc6c38341948d8605e4302a1b95e

Tizen.Xamarin.Forms.Extension.Renderer/ContextPopupImplementation.cs
Tizen.Xamarin.Forms.Extension/ContextPopup.cs
Tizen.Xamarin.Forms.Extension/IContextPopup.cs

index fe5da77..d89ba58 100644 (file)
@@ -22,6 +22,7 @@ namespace Tizen.Xamarin.Forms.Extension.Renderer
             new ContextPopupDirectionPriorities(ContextPopupDirection.Up, ContextPopupDirection.Left, ContextPopupDirection.Right, ContextPopupDirection.Down);
 
         ContextPopupItem _selectedItem = null;
+
         bool _isDisposed;
 
         public ContextPopupImplementation()
@@ -50,7 +51,7 @@ namespace Tizen.Xamarin.Forms.Extension.Renderer
 
         public event PropertyChangedEventHandler PropertyChanged;
 
-        public event EventHandler SelectedIndexChanged;
+        public event EventHandler ItemSelected;
 
         public event EventHandler Dismissed;
 
@@ -129,9 +130,12 @@ namespace Tizen.Xamarin.Forms.Extension.Renderer
         {
             foreach (var item in items)
             {
-                var nativeItem = _items[item];
-                nativeItem.Delete();
-                _items.Remove(item);
+                if (_items.ContainsKey(item))
+                {
+                    var nativeItem = _items[item];
+                    nativeItem.Delete();
+                    _items.Remove(item);
+                }
             }
         }
 
@@ -141,10 +145,10 @@ namespace Tizen.Xamarin.Forms.Extension.Renderer
             _popup.Clear();
         }
 
-        public void Show(XForms.View anchor)
+        public void Show(XForms.View anchor, int xAnchorOffset, int yAnchorOffset)
         {
             var geometry = XFPlatformTizen.Platform.GetRenderer(anchor).NativeView.Geometry;
-            _popup.Move(geometry.X, geometry.Y);
+            _popup.Move(geometry.X + xAnchorOffset, geometry.Y + yAnchorOffset);
             _popup.Show();
             KeyGrab();
         }
@@ -263,16 +267,8 @@ namespace Tizen.Xamarin.Forms.Extension.Renderer
 
             nativeItem.Selected += (s, e) =>
             {
-                if (SelectedItem != item)
-                {
-                    //When SelectedItem is changed, SelectedIndexChanged is invoked.
-                    SelectedItem = item;
-                }
-                else
-                {
-                    //If not a newly selected item, invoke SelectedIndexChanged.
-                    SelectedIndexChanged?.Invoke(this, EventArgs.Empty);
-                };
+                SelectedItem = item; // This will invoke SelectedIndexChanged if the index has changed
+                ItemSelected?.Invoke(this, EventArgs.Empty);
             };
         }
 
index d885bf3..09d4ee0 100644 (file)
@@ -37,6 +37,7 @@ namespace Tizen.Xamarin.Forms.Extension
     public class ContextPopup : BindableObject
     {
         IContextPopup _contextPopup;
+
         ObservableCollection<ContextPopupItem> _items;
 
         static ContextPopupDirectionPriorities _priorities =
@@ -77,11 +78,15 @@ namespace Tizen.Xamarin.Forms.Extension
         public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(nameof(ItemsSource), typeof(IList), typeof(ContextPopup), default(IList),
             propertyChanged: OnItemsSourceChanged);
 
+        /// <summary>
+        /// The constructor which creates a new ContextPopup instance.
+        /// </summary>
         public ContextPopup()
         {
             _contextPopup = DependencyService.Get<IContextPopup>(DependencyFetchTarget.NewInstance);
+
             _contextPopup.Dismissed += (s, e) => Dismissed?.Invoke(this, EventArgs.Empty);
-            _contextPopup.SelectedIndexChanged += (s, e) => SelectedIndexChanged?.Invoke(this, EventArgs.Empty);
+            _contextPopup.ItemSelected += (s, e) => ItemSelected?.Invoke(this, EventArgs.Empty);
 
             _items = new ObservableCollection<ContextPopupItem>();
             _items.CollectionChanged += ItemsCollectionChanged;
@@ -98,11 +103,16 @@ namespace Tizen.Xamarin.Forms.Extension
         public event EventHandler Dismissed;
 
         /// <summary>
-        /// Occurs when an item is selected.
+        /// Occurs when the index of the selected ContextPopupItem changes.
         /// </summary>
         public event EventHandler SelectedIndexChanged;
 
         /// <summary>
+        /// Occurs when a ContextPopupItem is selected.
+        /// </summary>
+        public event EventHandler ItemSelected;
+
+        /// <summary>
         /// Gets or sets the orientation of the ContextPopup.
         /// The default value is ContextPopupOrientation.Vertical.
         /// </summary>
@@ -167,12 +177,34 @@ namespace Tizen.Xamarin.Forms.Extension
         }
 
         /// <summary>
-        /// Shows the ContextPopup.
+        /// Shows the ContextPopup. The ContextPopup is positioned at the horizontal and vertical position of a specific anchor.
         /// </summary>
         /// <param name="anchor">The View to which the popup should be anchored.</param>
         public void Show(View anchor)
         {
-            _contextPopup.Show(anchor);
+            Show(anchor, 0, 0);
+        }
+
+        /// <summary>
+        /// Shows the ContextPopup. The ContextPopup is positioned at the horizontal and vertical position of a specific anchor with offsets.
+        /// </summary>
+        /// <param name="anchor">The View to which the popup should be anchored.</param>
+        /// <param name="xOffset">The horizontal offset from the anchor.</param>
+        /// <param name="yOffset">The vertical offset from the anchor.</param>
+        public void Show(View anchor, int xOffset, int yOffset)
+        {
+            _contextPopup.Show(anchor, xOffset, yOffset);
+        }
+
+        /// <summary>
+        /// Shows the ContextPopup. The ContextPopup is positioned at the horizontal and vertical position of a specific anchor with offsets.
+        /// </summary>
+        /// <param name="anchor">The View to which the popup should be anchored.</param>
+        /// <param name="xOffset">The horizontal offset from the anchor.</param>
+        /// <param name="yOffset">The vertical offset from the anchor.</param>
+        public void Show(View anchor, double xOffset, double yOffset)
+        {
+            Show(anchor, (int)xOffset, (int)yOffset);
         }
 
         /// <summary>
@@ -240,6 +272,10 @@ namespace Tizen.Xamarin.Forms.Extension
                 var item = ConvertObjectToContextPopupItem(oldItem);
                 item.PropertyChanged -= ContextPopupItemPropertyChanged;
                 items.Add(item);
+                if (_items.Contains(item))
+                {
+                    _items.Remove(item);
+                }
             }
 
             _contextPopup.RemoveItems(items);
@@ -254,6 +290,10 @@ namespace Tizen.Xamarin.Forms.Extension
                 var item = ConvertObjectToContextPopupItem(newItem);
                 item.PropertyChanged += ContextPopupItemPropertyChanged;
                 items.Add(item);
+                if (!_items.Contains(item))
+                {
+                    _items.Add(item);
+                }
             }
 
             _contextPopup.AddItems(items);
index 0b0388f..a54a426 100644 (file)
@@ -6,7 +6,7 @@ namespace Tizen.Xamarin.Forms.Extension
 {
     internal interface IContextPopup
     {
-        event EventHandler SelectedIndexChanged;
+        event EventHandler ItemSelected;
 
         event EventHandler Dismissed;
 
@@ -24,7 +24,7 @@ namespace Tizen.Xamarin.Forms.Extension
 
         void ClearItems();
 
-        void Show(View anchor);
+        void Show(View anchor, int xAnchorOffset, int yAnchorOffset);
 
         void Dismiss();