2 using ElmSharp.Wearable;
4 using System.Collections.Generic;
6 using Xamarin.Forms.Platform.Tizen;
7 using ELayout = ElmSharp.Layout;
8 using EColor = ElmSharp.Color;
10 namespace Tizen.Wearable.CircularUI.Forms.Renderer
12 public class NavigationView : ELayout
14 readonly int _dafaultIconSize = 60;
18 public Element Source { get; set; }
19 public string Text { get; set; }
20 public string Icon { get; set; }
24 ELayout _surfaceLayout;
25 CircleSurface _surface;
26 CircleGenList _naviMenu;
28 GenItemClass _defaultClass;
29 SmartEvent _draggedUpCallback;
30 SmartEvent _draggedDownCallback;
35 List<List<Element>> _itemCache;
36 List<GenListItem> _items = new List<GenListItem>();
38 public NavigationView(EvasObject parent) : base(parent)
40 InitializeComponent();
43 public event EventHandler<SelectedItemChangedEventArgs> ItemSelected;
45 public event EventHandler<DraggedEventArgs> Dragged;
48 EColor _backgroundColor = EColor.Black;
49 public override EColor BackgroundColor
51 get => _backgroundColor;
54 _backgroundColor = value.IsDefault ? EColor.Black : value;
55 UpdateBackgroundColor();
59 EColor _foregroundColor = EColor.Default;
60 public EColor ForegroundColor
62 get => _foregroundColor;
65 _foregroundColor = value;
66 UpdateForegroundColor();
71 public void Build(List<List<Element>> items)
73 // Only update when items was changed
74 if (!IsUpdated(items))
83 _header = _naviMenu.Append(_defaultClass, new Item { Text = "" });
85 // TODO. need to improve, need to support group
86 foreach (var group in items)
88 foreach (var item in group)
94 if (item is BaseShellItem shellItem)
96 data.Text = shellItem.Title;
97 data.Icon = (shellItem.Icon as FileImageSource)?.ToAbsPath();
99 else if (item is MenuItem menuItem)
101 data.Text = menuItem.Text;
102 data.Icon = (menuItem.IconImageSource as FileImageSource)?.ToAbsPath();
104 var genitem = _naviMenu.Append(_defaultClass, data, GenListItemType.Normal);
105 genitem.SetPartColor("bg", _backgroundColor);
109 _footer = _naviMenu.Append(_defaultClass, new Item { Text = "" });
112 public void Activate()
114 (_naviMenu as IRotaryActionWidget)?.Activate();
116 public void Deactivate()
118 (_naviMenu as IRotaryActionWidget)?.Deactivate();
121 protected override IntPtr CreateHandle(EvasObject parent)
123 _outterBox = new Box(parent);
124 return _outterBox.Handle;
127 void InitializeComponent()
129 _outterBox.SetLayoutCallback(OnLayout);
131 _surfaceLayout = new ELayout(this);
132 _surfaceLayout.Show();
133 _surface = new CircleSurface(_surfaceLayout);
135 _naviMenu = new CircleGenList(this, _surface)
138 BackgroundColor = _backgroundColor
142 _draggedUpCallback = new SmartEvent(_naviMenu, "drag,start,up");
143 _draggedUpCallback.On += (s, e) =>
145 if (_footer.TrackObject.IsVisible)
147 Dragged?.Invoke(this, new DraggedEventArgs(DraggedState.EdgeBottom));
151 Dragged?.Invoke(this, new DraggedEventArgs(DraggedState.Up));
155 _draggedDownCallback = new SmartEvent(_naviMenu, "drag,start,down");
156 _draggedDownCallback.On += (s, e) =>
158 if (_header.TrackObject.IsVisible)
160 Dragged?.Invoke(this, new DraggedEventArgs(DraggedState.EdgeTop));
164 Dragged?.Invoke(this, new DraggedEventArgs(DraggedState.Down));
168 _outterBox.PackEnd(_naviMenu);
169 _outterBox.PackEnd(_surfaceLayout);
171 _surfaceLayout.StackAbove(_naviMenu);
173 _defaultClass = new GenItemClass("1icon_1text")
175 GetTextHandler = (obj, part) =>
177 if (part == "elm.text")
179 var text = (obj as Item).Text;
180 if (_foregroundColor != EColor.Default)
181 return $"<span color='{_foregroundColor.ToHex()}'>{text}</span>";
187 GetContentHandler = (obj, part) =>
189 if (part == "elm.swallow.icon" && obj is Item menuItem && !string.IsNullOrEmpty(menuItem.Icon))
191 var icon = new ElmSharp.Image(Xamarin.Forms.Forms.NativeParent)
197 MinimumWidth = _dafaultIconSize,
198 MinimumHeight = _dafaultIconSize,
201 icon.Load(menuItem.Icon);
208 _naviMenu.ItemSelected += OnItemSelected;
212 void OnItemSelected(object sender, GenListItemEventArgs e)
214 ItemSelected?.Invoke(this, new SelectedItemChangedEventArgs((e.Item.Data as Item).Source, -1));
219 _surfaceLayout.Geometry = Geometry;
220 _naviMenu.Geometry = Geometry;
223 void UpdateBackgroundColor()
225 _naviMenu.BackgroundColor = _backgroundColor;
226 foreach (var item in _items)
228 item.SetPartColor("bg", _backgroundColor);
232 void UpdateForegroundColor()
234 foreach (var item in _items)
240 bool IsUpdated(List<List<Element>> items)
242 if (_itemCache == null)
245 if (_itemCache.Count != items.Count)
248 for (int i = 0; i < items.Count; i++)
250 if (_itemCache[i].Count != items[i].Count)
253 for (int j = 0; j < items[i].Count; j++)
255 if (_itemCache[i][j] != items[i][j])
263 public enum DraggedState
271 public class DraggedEventArgs
273 public DraggedState State { get; private set; }
275 public DraggedEventArgs(DraggedState state)
281 static class FileImageSourceEX
283 public static string ToAbsPath(this FileImageSource source)
285 return ResourcePath.GetPath(source.File);
291 public static string ToHex(this EColor c)
293 return string.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", c.R, c.G, c.B, c.A);