3 using System.Collections.Generic;
6 using Xamarin.Forms.Platform.Tizen;
7 using XForms = Xamarin.Forms.Forms;
8 using XShell = Xamarin.Forms.Shell;
9 using Tizen.Wearable.CircularUI.Forms;
11 [assembly: ExportRenderer(typeof(CircularShell), typeof(Tizen.Wearable.CircularUI.Forms.Renderer.ShellRenderer))]
12 namespace Tizen.Wearable.CircularUI.Forms.Renderer
14 public class ShellRenderer : VisualElementRenderer<XShell>
16 NavigationDrawer _drawer;
17 NavigationView _navigationView;
19 Dictionary<BaseShellItem, IShellItemRenderer> _rendererCache = new Dictionary<BaseShellItem, IShellItemRenderer>();
21 public ShellRenderer()
23 RegisterPropertyHandler(XShell.CurrentItemProperty, UpdateCurrentItem);
24 RegisterPropertyHandler(XShell.FlyoutIsPresentedProperty, UpdateFlyoutIsPresented);
25 RegisterPropertyHandler(XShell.FlyoutBehaviorProperty, UpdateFlyoutBehavior);
26 RegisterPropertyHandler(XShell.FlyoutIconProperty, UpdateFlyoutIcon);
27 RegisterPropertyHandler(XShell.FlyoutBackgroundColorProperty, UpdateFlyoutBackgroundColor);
28 RegisterPropertyHandler(CircularShell.FlyoutIconBackgroundColorProperty, UpdateFlyoutIconBackgroundColor);
29 RegisterPropertyHandler(CircularShell.FlyoutForegroundColorProperty, UpdateFlyoutForegroundColor);
32 protected override void OnElementChanged(ElementChangedEventArgs<XShell> e)
34 InitializeComponent();
35 base.OnElementChanged(e);
38 protected override void OnElementReady()
40 base.OnElementReady();
42 (Element as IShellController).StructureChanged += OnNavigationStructureChanged;
45 protected override void Dispose(bool disposing)
49 foreach (var renderer in _rendererCache.Values)
53 (Element as IShellController).StructureChanged -= OnNavigationStructureChanged;
55 base.Dispose(disposing);
58 void InitializeComponent()
62 _drawer = new NavigationDrawer(XForms.NativeParent);
63 _drawer.IsOpen = Element.FlyoutIsPresented;
64 _drawer.Toggled += OnNavigationDrawerToggled;
65 SetNativeView(_drawer);
69 void OnNavigationStructureChanged(object sender, EventArgs e)
74 void UpdateFlyoutMenu()
76 if (Element.FlyoutBehavior == FlyoutBehavior.Disabled)
79 var flyoutItems = (Element as IShellController).GenerateFlyoutGrouping();
81 foreach (var item in flyoutItems)
83 itemCount += item.Count;
88 InitializeNavigationDrawer();
89 _navigationView.Build(flyoutItems);
93 DeinitializeNavigationView();
97 void InitializeNavigationDrawer()
99 if (_navigationView != null)
104 _navigationView = new NavigationView(XForms.NativeParent)
111 _navigationView.Show();
112 _navigationView.ItemSelected += OnMenuItemSelected;
114 _drawer.SetDrawerContent(_navigationView);
117 void OnNavigationDrawerToggled(object sender, EventArgs e)
121 _navigationView.Activate();
125 _navigationView.Deactivate();
127 var stack = (Element.CurrentItem.CurrentItem as ShellSection)?.Stack;
128 var currentPage = stack?.LastOrDefault<Page>();
130 if (currentPage == null)
132 currentPage = (Element.CurrentItem.CurrentItem.CurrentItem as IShellContentController)?.Page;
135 if (currentPage != null)
137 var renderer = Platform.GetOrCreateRenderer(currentPage);
138 (renderer as CirclePageRenderer)?.UpdateRotaryFocusObject();
142 Element.SetValueFromRenderer(XShell.FlyoutIsPresentedProperty, _drawer.IsOpen);
145 void DeinitializeNavigationView()
147 if (_navigationView == null)
149 _drawer.SetDrawerContent(null);
150 _navigationView.Unrealize();
151 _navigationView = null;
154 void OnMenuItemSelected(object sender, SelectedItemChangedEventArgs e)
156 ((IShellController)Element).OnFlyoutItemSelected(e.SelectedItem as Element);
159 void UpdateCurrentItem()
162 if (Element.CurrentItem != null)
164 if (!_rendererCache.TryGetValue(Element.CurrentItem, out IShellItemRenderer renderer))
166 renderer = ShellRendererFactory.Default.CreateItemRenderer(Element.CurrentItem);
167 _rendererCache[Element.CurrentItem] = renderer;
169 SetCurrentItem(renderer.NativeView);
173 void UpdateFlyoutBehavior(bool init)
178 if (Element.FlyoutBehavior == FlyoutBehavior.Disabled)
180 DeinitializeNavigationView();
182 else if (Element.FlyoutBehavior == FlyoutBehavior.Flyout)
186 else if (Element.FlyoutBehavior == FlyoutBehavior.Locked)
188 // Locked behavior is not supported on circularshell
192 void UpdateFlyoutIcon(bool init)
194 if (init && Element.FlyoutIcon == null)
197 _drawer.UpdateDrawerIcon(Element.FlyoutIcon);
200 void UpdateFlyoutBackgroundColor(bool init)
202 if (init && Element.FlyoutBackgroundColor.IsDefault)
205 if (_navigationView != null)
207 _navigationView.BackgroundColor = Element.FlyoutBackgroundColor.ToNative();
211 void UpdateFlyoutForegroundColor(bool init)
213 if (init && CircularShell.GetFlyoutForegroundColor(Element).IsDefault)
216 if (_navigationView != null)
218 _navigationView.ForegroundColor = CircularShell.GetFlyoutForegroundColor(Element).ToNative();
222 void UpdateFlyoutIconBackgroundColor()
224 _drawer.HandlerBackgroundColor = CircularShell.GetFlyoutIconBackgroundColor(Element).ToNative();
227 void UpdateFlyoutIsPresented()
229 _drawer.IsOpen = Element.FlyoutIsPresented;
232 void SetCurrentItem(EvasObject item)
234 _drawer.SetMainContent(item);
237 void ResetCurrentItem()
239 _drawer.SetMainContent(null);