Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Add the MaterialShellRenderer for Tizen
Browse files Browse the repository at this point in the history
  • Loading branch information
yourina committed May 15, 2019
1 parent e071494 commit 0fe5144
Show file tree
Hide file tree
Showing 31 changed files with 1,770 additions and 2 deletions.
Expand Up @@ -9,7 +9,7 @@
<PackageReference Include="Tizen.NET" Version="4.0.0">
<ExcludeAssets>Runtime</ExcludeAssets>
</PackageReference>
<PackageReference Include="Tizen.NET.MaterialComponents" Version="0.9.5-preview" />
<PackageReference Include="Tizen.NET.MaterialComponents" Version="0.9.6-preview" />
<PackageReference Include="Tizen.NET.Sdk" Version="1.0.1" />
</ItemGroup>

Expand Down
Binary file added Xamarin.Forms.ControlGallery.Tizen/res/bell.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Xamarin.Forms.ControlGallery.Tizen/res/books.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Xamarin.Forms.ControlGallery.Tizen/res/card.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Xamarin.Forms.ControlGallery.Tizen/res/film.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Xamarin.Forms.ControlGallery.Tizen/res/games.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Xamarin.Forms.ControlGallery.Tizen/res/grid.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Xamarin.Forms.ControlGallery.Tizen/res/jet.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Xamarin.Forms.ControlGallery.Tizen/res/loop.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Xamarin.Forms.ControlGallery.Tizen/res/newspaper.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Xamarin.Forms.ControlGallery.Tizen/res/person.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Xamarin.Forms.ControlGallery.Tizen/res/star.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Xamarin.Forms.Material.Tizen/Resource/arrow_left.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Xamarin.Forms.Material.Tizen/Resource/menu.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions Xamarin.Forms.Material.Tizen/Shell/IFlyoutController.cs
@@ -0,0 +1,7 @@
namespace Xamarin.Forms.Material.Tizen
{
public interface IFlyoutController
{
void Open();
}
}
191 changes: 191 additions & 0 deletions Xamarin.Forms.Material.Tizen/Shell/MaterialShellRenderer.cs
@@ -0,0 +1,191 @@
using System;
using System.Collections.Generic;
using ElmSharp;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Tizen;
using Tizen.NET.MaterialComponents;
using Xamarin.Forms.Material.Tizen;
using TForms = Xamarin.Forms.Platform.Tizen.Forms;
using TNative = Xamarin.Forms.Material.Tizen.Native;
using TPlatform = Xamarin.Forms.Platform.Tizen.Platform;

[assembly: ExportRenderer(typeof(Shell), typeof(MaterialShellRenderer), new[] { typeof(VisualMarker.MaterialVisual) })]
namespace Xamarin.Forms.Material.Tizen
{
public class MaterialShellRenderer : VisualElementRenderer<Shell>, IFlyoutController
{
MNavigationDrawer _native;
ShellItemRenderer _shellItem;

IDictionary<int, Element> _flyoutMenu = new Dictionary<int, Element>();

public static readonly Color DefaultBackgroundColor = Color.FromRgb(33, 150, 243);
public static readonly Color DefaultForegroundColor = Color.White;
public static readonly Color DefaultTitleColor = Color.White;

public MaterialShellRenderer()
{
RegisterPropertyHandler(Shell.CurrentItemProperty, UpdateCurrentItem);
RegisterPropertyHandler(Shell.FlyoutBackgroundColorProperty, UpdateFlyoutBackgroundColor);
RegisterPropertyHandler(Shell.FlyoutIsPresentedProperty, UpdateFlyoutIsPresented);
}

public override Rect GetNativeContentGeometry()
{
var rect = base.GetNativeContentGeometry();
rect.X = 0;
rect.Y = 0;
return rect;
}

protected override void OnElementChanged(ElementChangedEventArgs<Shell> e)
{
if (_native == null)
{
_native = new MNavigationDrawer(TForms.NativeParent)
{
NavigationView = new MNavigationView(TForms.NativeParent)
};
SetNativeView(_native);

_native.Toggled += OnFlyoutIsPresentedChanged;

InitializeFlyout();
}

base.OnElementChanged(e);
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
((IShellController)Element).StructureChanged -= OnShellStructureChanged;
if (_native != null)
{
_native.Toggled -= OnFlyoutIsPresentedChanged;
_native.NavigationView.MenuItemSelected -= OnItemSelected;
}
}
base.Dispose(disposing);
}

void InitializeFlyout()
{
((IShellController)Element).StructureChanged += OnShellStructureChanged;

View flyoutHeader = ((IShellController)Element).FlyoutHeader;
if (flyoutHeader != null)
{
var headerView = TPlatform.GetOrCreateRenderer(flyoutHeader);
(headerView as LayoutRenderer)?.RegisterOnLayoutUpdated();

Size request = flyoutHeader.Measure(TForms.ConvertToScaledDP(_native.NavigationView.MinimumWidth), TForms.ConvertToScaledDP(_native.NavigationView.MinimumHeight)).Request;
headerView.NativeView.MinimumHeight = TForms.ConvertToScaledPixel(request.Height);

_native.NavigationView.Header = headerView.NativeView;
}

BuildMenu();
_native.NavigationView.MenuItemSelected += OnItemSelected;
}

void UpdateCurrentItem()
{
_shellItem?.Dispose();
if (Element.CurrentItem != null)
{
_shellItem = new ShellItemRenderer(this, Element.CurrentItem);
_shellItem.Control.SetAlignment(-1, -1);
_shellItem.Control.SetWeight(1, 1);
_native.Main = _shellItem.Control;
}
else
{
_native.Main = null;
}
}

void UpdateFlyoutBackgroundColor()
{
_native.NavigationView.BackgroundColor = Element.FlyoutBackgroundColor.ToNative();
}

void UpdateFlyoutIsPresented()
{
_native.IsOpen = Element.FlyoutIsPresented;
}

void OnFlyoutIsPresentedChanged(object sender, EventArgs e)
{
Element.SetValueFromRenderer(Shell.FlyoutIsPresentedProperty, _native.IsOpen);
}

void OnItemSelected(object sender, GenListItemEventArgs e)
{
_flyoutMenu.TryGetValue(e.Item.Index - 1, out Element element);

if (element != null)
{
((IShellController)Element).OnFlyoutItemSelected(element);
}
}

void OnShellStructureChanged(object sender, EventArgs e)
{
BuildMenu();
}

void BuildMenu()
{
var groups = new List<MGroup>();
var flyoutGroups = ((IShellController)Element).GenerateFlyoutGrouping();

_flyoutMenu.Clear();

int index = 0;
for (int i = 0; i < flyoutGroups.Count; i++)
{
var flyoutGroup = flyoutGroups[i];
var items = new List<MItem>();
for (int j = 0; j < flyoutGroup.Count; j++)
{
string title = null;
string icon = null;
if (flyoutGroup[j] is BaseShellItem shellItem)
{
title = shellItem.Title;

if (shellItem.FlyoutIcon is FileImageSource flyoutIcon)
{
icon = flyoutIcon.File;
}
}
else if (flyoutGroup[j] is MenuItem menuItem)
{
title = menuItem.Text;
if (menuItem.IconImageSource is FileImageSource source)
{
icon = source.File;
}
}

items.Add(new MItem(title, icon));

_flyoutMenu.Add(index, flyoutGroup[j]);
index++;
}

var group = new MGroup(items);
groups.Add(group);
}

_native.NavigationView.GroupMenu = groups;
}

void IFlyoutController.Open()
{
_native.IsOpen = true;
}
}
}

0 comments on commit 0fe5144

Please sign in to comment.