2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Runtime.CompilerServices;
5 using Xamarin.Forms.Platform.Tizen;
\r
6 using EContextPopup = ElmSharp.ContextPopup;
7 using EContextPopupDirection = ElmSharp.ContextPopupDirection;
8 using EContextPopupItem = ElmSharp.ContextPopupItem;
9 using EIcon = ElmSharp.Icon;
10 using TForms = Xamarin.Forms.Platform.Tizen.Forms;
11 using XForms = Xamarin.Forms;
12 using XFPlatformTizen = Xamarin.Forms.Platform.Tizen;
14 [assembly: XForms.Dependency(typeof(Tizen.Xamarin.Forms.Extension.Renderer.ContextPopupImplementation))]
15 namespace Tizen.Xamarin.Forms.Extension.Renderer
17 internal class ContextPopupImplementation : IContextPopup, INotifyPropertyChanged, IDisposable
20 IDictionary<ContextPopupItem, EContextPopupItem> _items;
21 bool _isAutoHidingEnabled = true;
23 ContextPopupDirectionPriorities _priorities =
24 new ContextPopupDirectionPriorities(ContextPopupDirection.Up, ContextPopupDirection.Left, ContextPopupDirection.Right, ContextPopupDirection.Down);
26 ContextPopupItem _selectedItem = null;
30 public ContextPopupImplementation()
32 _popup = new EContextPopup((TForms.Context as FormsApplication).MainWindow);
34 _popup.BackButtonPressed += (s, e) =>
39 _popup.Dismissed += (s, e) =>
41 Dismissed?.Invoke(this, EventArgs.Empty);
44 _items = new Dictionary<ContextPopupItem, EContextPopupItem>();
47 ~ContextPopupImplementation()
52 public event PropertyChangedEventHandler PropertyChanged;
54 public event EventHandler ItemSelected;
56 public event EventHandler Dismissed;
58 public bool IsAutoHidingEnabled
62 return _isAutoHidingEnabled;
67 _isAutoHidingEnabled = value;
68 UpdateIsAutoHidingEnabled();
73 public ContextPopupDirectionPriorities DirectionPriorities
83 UpdateDirectionPriorities();
88 public ContextPopupItem SelectedItem
97 _selectedItem = value;
102 public void Dismiss()
107 public void AddItems(IEnumerable<ContextPopupItem> items)
109 foreach (var item in items)
111 item.PropertyChanged += ContextPopupItemPropertyChanged;
116 public void RemoveItems(IEnumerable<ContextPopupItem> items)
118 foreach (var item in items)
120 item.PropertyChanged -= ContextPopupItemPropertyChanged;
121 if (_items.ContainsKey(item))
123 var nativeItem = _items[item];
130 public void ClearItems()
132 foreach (var item in _items.Keys)
133 item.PropertyChanged -= ContextPopupItemPropertyChanged;
139 public void Show(XForms.View anchor, int xAnchorOffset, int yAnchorOffset)
141 var geometry = XFPlatformTizen.Platform.GetRenderer(anchor).NativeView.Geometry;
142 _popup.Move(geometry.X + xAnchorOffset, geometry.Y + yAnchorOffset);
146 public bool TryGetContextPopupDirection(out ContextPopupDirection direction)
148 var nativeDirection = _popup.Direction;
149 if (nativeDirection != EContextPopupDirection.Unknown)
151 direction = (ContextPopupDirection)nativeDirection;
156 direction = default(ContextPopupDirection);
161 public void UpdateContextPopupItemLabel(ContextPopupItem item)
163 EContextPopupItem nativeItem = _items[item];
164 nativeItem.SetPartText("default", item.Label);
167 public void UpdateContextPopupItemIcon(ContextPopupItem item)
169 if(string.IsNullOrEmpty(item.Icon))
170 _items[item]?.SetPartContent("icon", null);
172 AppendOrModifyItemWithIcon(item);
175 public void Dispose()
178 GC.SuppressFinalize(this);
181 protected virtual void Dispose(bool isDisposing)
198 protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
200 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
203 void ContextPopupItemPropertyChanged(object sender, PropertyChangedEventArgs e)
205 var item = sender as ContextPopupItem;
207 if (e.PropertyName == nameof(ContextPopupItem.Label))
209 // If the native item already has a label
210 UpdateContextPopupItemLabel(item);
212 else if (e.PropertyName == nameof(ContextPopupItem.Icon))
214 // If the native item already has an icon
215 UpdateContextPopupItemIcon(item);
219 void UpdateDirectionPriorities()
221 _popup.SetDirectionPriorty(
222 (EContextPopupDirection)_priorities.First,
223 (EContextPopupDirection)_priorities.Second,
224 (EContextPopupDirection)_priorities.Third,
225 (EContextPopupDirection)_priorities.Fourth);
228 void UpdateIsAutoHidingEnabled()
230 _popup.AutoHide = IsAutoHidingEnabled;
233 void AddItem(ContextPopupItem item)
235 if (_items.ContainsKey(item))
238 EContextPopupItem nativeItem;
239 if (string.IsNullOrEmpty(item.Icon))
241 nativeItem = _popup.Append(item.Label);
245 nativeItem = AppendOrModifyItemWithIcon(item);
248 _items.Add(item, nativeItem);
250 nativeItem.Selected += (s, e) =>
252 SelectedItem = item; // This will invoke SelectedIndexChanged if the index has changed
253 ItemSelected?.Invoke(this, EventArgs.Empty);
257 EContextPopupItem AppendOrModifyItemWithIcon(ContextPopupItem item)
259 EContextPopupItem nativeItem = null;
260 EIcon icon = new EIcon(_popup);
261 icon.StandardIconName = item.Icon;
262 if (!string.IsNullOrEmpty(icon.StandardIconName))
264 if (!_items.ContainsKey(item))
265 nativeItem = _popup.Append(item.Label, icon);
267 _items[item].SetPartContent("icon", icon);
271 //Not a standard icon
272 XFPlatformTizen.Native.Image iconImage = new XFPlatformTizen.Native.Image(_popup);
273 var task = iconImage.LoadFromImageSourceAsync(item.Icon);
274 if (!_items.ContainsKey(item))
275 nativeItem = _popup.Append(item.Label, iconImage);
277 _items[item].SetPartContent("icon", iconImage);