using System;
using System.Collections.Specialized;
using System.ComponentModel;
+using System.Threading.Tasks;
using ActionBarDrawerToggle = Android.Support.V7.App.ActionBarDrawerToggle;
using AView = Android.Views.View;
using LP = Android.Views.ViewGroup.LayoutParams;
{
UpdateTitleView(_shellContext.AndroidContext, _toolbar, null);
- _drawerToggle.Dispose();
+ _drawerToggle?.Dispose();
if (_searchView != null)
{
_searchView.View.RemoveFromParent();
var backButtonHandler = Shell.GetBackButtonBehavior(page);
toolbar.SetNavigationOnClickListener(this);
+ var activity = (FormsAppCompatActivity)context;
+
if (backButtonHandler != null)
{
- using (var icon = await context.GetFormsDrawable(backButtonHandler.IconOverride))
- using (var mutatedIcon = icon.GetConstantState().NewDrawable().Mutate())
+ await UpdateDrawerArrowFromBackButtonBehavior(context, toolbar, drawerLayout, backButtonHandler, activity);
+ }
+ else
+ {
+ await UpdateDrawerArrow(context, toolbar, drawerLayout, activity);
+ }
+ }
+
+ protected virtual async Task UpdateDrawerArrow(Context context, Toolbar toolbar, DrawerLayout drawerLayout, FormsAppCompatActivity activity)
+ {
+ if (_drawerToggle == null)
+ {
+ _drawerToggle = new ActionBarDrawerToggle((Activity)context, drawerLayout, toolbar,
+ R.String.Ok, R.String.Ok)
{
- mutatedIcon.SetColorFilter(TintColor.ToAndroid(Color.White), PorterDuff.Mode.SrcAtop);
- toolbar.NavigationIcon = mutatedIcon;
+ ToolbarNavigationClickListener = this,
+ };
+
+ await UpdateDrawerArrowFromFlyoutIcon(context, _drawerToggle);
+
+ _drawerToggle.DrawerSlideAnimationEnabled = false;
+ drawerLayout.AddDrawerListener(_drawerToggle);
+ }
+
+ if (CanNavigateBack)
+ {
+ _drawerToggle.DrawerIndicatorEnabled = false;
+ using (var icon = new DrawerArrowDrawable(activity.SupportActionBar.ThemedContext))
+ {
+ icon.SetColorFilter(TintColor.ToAndroid(Color.White), PorterDuff.Mode.SrcAtop);
+ icon.Progress = 1;
+ toolbar.NavigationIcon = icon;
}
}
+ else if (_flyoutBehavior == FlyoutBehavior.Flyout)
+ {
+ toolbar.NavigationIcon = null;
+ var drawable = _drawerToggle.DrawerArrowDrawable;
+ drawable.SetColorFilter(TintColor.ToAndroid(Color.White), PorterDuff.Mode.SrcAtop);
+ _drawerToggle.DrawerIndicatorEnabled = true;
+ }
else
{
- var activity = (FormsAppCompatActivity)context;
- if (_drawerToggle == null)
+ _drawerToggle.DrawerIndicatorEnabled = false;
+ }
+ _drawerToggle.SyncState();
+ }
+
+ protected virtual async Task UpdateDrawerArrowFromBackButtonBehavior(Context context, Toolbar toolbar, DrawerLayout drawerLayout, BackButtonBehavior backButtonHandler, FormsAppCompatActivity activity)
+ {
+ var behavior = backButtonHandler;
+ var command = behavior.Command;
+ var commandParameter = behavior.CommandParameter;
+ var image = behavior.IconOverride;
+ var text = behavior.TextOverride;
+ var enabled = behavior.IsEnabled;
+
+ Drawable icon = null;
+
+ if (image != null)
+ icon = await context.GetFormsDrawable(image);
+
+ if (text != null)
+ icon = new FlyoutIconDrawerDrawable(context, TintColor, icon, text);
+
+ if (CanNavigateBack && icon == null)
+ {
+ icon = new DrawerArrowDrawable(activity.SupportActionBar.ThemedContext);
+ (icon as DrawerArrowDrawable).Progress = 1;
+ }
+
+ var iconState = icon.GetConstantState();
+ if (iconState != null)
+ {
+ using (var mutatedIcon = iconState.NewDrawable().Mutate())
{
- _drawerToggle = new ActionBarDrawerToggle((Activity)context, drawerLayout, toolbar,
- R.String.Ok, R.String.Ok)
- {
- ToolbarNavigationClickListener = this,
- };
- _drawerToggle.DrawerSlideAnimationEnabled = false;
- drawerLayout.AddDrawerListener(_drawerToggle);
+ mutatedIcon.SetColorFilter(TintColor.ToAndroid(Color.White), PorterDuff.Mode.SrcAtop);
+ toolbar.NavigationIcon = mutatedIcon;
}
+ }
+ else
+ {
+ toolbar.NavigationIcon = icon;
+ }
+ }
- if (CanNavigateBack)
+ protected virtual async Task UpdateDrawerArrowFromFlyoutIcon(Context context, ActionBarDrawerToggle actionBarDrawerToggle)
+ {
+ Element item = Page;
+ ImageSource icon = null;
+ while (!Application.IsApplicationOrNull(item))
+ {
+ if (item is IShellController shell)
{
- _drawerToggle.DrawerIndicatorEnabled = false;
- using (var icon = new DrawerArrowDrawable(activity.SupportActionBar.ThemedContext))
+ icon = shell.FlyoutIcon;
+ if (icon != null)
{
- icon.SetColorFilter(TintColor.ToAndroid(Color.White), PorterDuff.Mode.SrcAtop);
- icon.Progress = 1;
- toolbar.NavigationIcon = icon;
+ var drawable = await context.GetFormsDrawable(icon);
+ actionBarDrawerToggle.DrawerArrowDrawable = new FlyoutIconDrawerDrawable(context, TintColor, drawable, null);
}
+ return;
}
- else if (_flyoutBehavior == FlyoutBehavior.Flyout)
- {
- toolbar.NavigationIcon = null;
- using (var drawable = _drawerToggle.DrawerArrowDrawable)
- drawable.SetColorFilter(TintColor.ToAndroid(Color.White), PorterDuff.Mode.SrcAtop);
- _drawerToggle.DrawerIndicatorEnabled = true;
- }
- else
- {
- _drawerToggle.DrawerIndicatorEnabled = false;
- }
- _drawerToggle.SyncState();
+ item = item?.Parent;
}
}
{
UpdateToolbarItems(_toolbar, Page);
}
+
+ class FlyoutIconDrawerDrawable : DrawerArrowDrawable
+ {
+ Drawable _iconBitmap;
+ string _text;
+ Color _defaultColor;
+ float _defaultSize;
+
+ Color _pressedBackgroundColor => _defaultColor.AddLuminosity(-.12);//<item name="highlight_alpha_material_light" format="float" type="dimen">0.12</item>
+
+ protected override void Dispose(bool disposing)
+ {
+ base.Dispose(disposing);
+ if (disposing && _iconBitmap != null)
+ {
+ _iconBitmap.Dispose();
+ }
+ }
+
+ public FlyoutIconDrawerDrawable(Context context, Color defaultColor, Drawable icon, string text) : base(context)
+ {
+ _defaultColor = defaultColor;
+ _defaultSize = Forms.GetFontSizeNormal(context);
+ _iconBitmap = icon;
+ _text = text;
+ }
+
+ public override void Draw(Canvas canvas)
+ {
+ bool pressed = false;
+ if (_iconBitmap != null)
+ {
+ _iconBitmap.SetBounds(Bounds.Left, Bounds.Top, Bounds.Right, Bounds.Bottom);
+ _iconBitmap.Draw(canvas);
+ }
+ else if (!string.IsNullOrEmpty(_text))
+ {
+ var paint = new Paint { AntiAlias = true };
+ paint.TextSize = _defaultSize;
+ paint.Color = pressed ? _pressedBackgroundColor.ToAndroid() : _defaultColor.ToAndroid();
+ paint.SetStyle(Paint.Style.Fill);
+ var y = (Bounds.Height() + paint.TextSize) / 2;
+ canvas.DrawText(_text, 0, y, paint);
+ }
+ }
+ }
}
-}
\ No newline at end of file
+}
using System.Collections.Specialized;
using System.ComponentModel;
using System.Threading.Tasks;
+using System.Windows.Input;
using UIKit;
namespace Xamarin.Forms.Platform.iOS
{
_context = context;
}
-
+
public async void OnFlyoutBehaviorChanged(FlyoutBehavior behavior)
{
_flyoutBehavior = behavior;
var commandParameter = behavior.CommandParameter;
var image = behavior.IconOverride;
var enabled = behavior.IsEnabled;
+
if (image == null)
{
var text = BackButtonBehavior.TextOverride;
NavigationItem.LeftBarButtonItem =
- new UIBarButtonItem(text, UIBarButtonItemStyle.Plain, (s, e) => command?.Execute(commandParameter)) { Enabled = enabled };
+ new UIBarButtonItem(text, UIBarButtonItemStyle.Plain, (s, e) => LeftBarButtonItemHandler(ViewController, command, commandParameter, IsRootPage)) { Enabled = enabled };
}
else
{
var icon = await image.GetNativeImageAsync();
NavigationItem.LeftBarButtonItem =
- new UIBarButtonItem(icon, UIBarButtonItemStyle.Plain, (s, e) => command?.Execute(commandParameter)) { Enabled = enabled };
+ new UIBarButtonItem(icon, UIBarButtonItemStyle.Plain, (s, e) => LeftBarButtonItemHandler(ViewController, command, commandParameter, IsRootPage)) { Enabled = enabled };
}
}
else if (IsRootPage && _flyoutBehavior == FlyoutBehavior.Flyout)
{
- ImageSource image = "3bar.png";
- var icon = await image.GetNativeImageAsync();
- NavigationItem.LeftBarButtonItem = new UIBarButtonItem(icon, UIBarButtonItemStyle.Plain, OnMenuButtonPressed);
+
+ await SetDrawerArrowDrawableFromFlyoutIcon();
}
else
{
}
}
+ static void LeftBarButtonItemHandler(UIViewController controller, ICommand command, object commandParameter, bool isRootPage)
+ {
+ if (command == null && !isRootPage && controller?.ParentViewController is UINavigationController navigationController)
+ {
+ navigationController.PopViewController(true);
+ return;
+ }
+ command?.Execute(commandParameter);
+ }
+
+ async Task SetDrawerArrowDrawableFromFlyoutIcon()
+ {
+ Element item = Page;
+ ImageSource image = null;
+ while (!Application.IsApplicationOrNull(item))
+ {
+ if (item is IShellController shell)
+ {
+ image = shell.FlyoutIcon;
+ item = null;
+ }
+ item = item?.Parent;
+ }
+ if (image == null)
+ image = "3bar.png";
+ var icon = await image.GetNativeImageAsync();
+ NavigationItem.LeftBarButtonItem = new UIBarButtonItem(icon, UIBarButtonItemStyle.Plain, OnMenuButtonPressed);
+ }
+
void OnMenuButtonPressed(object sender, EventArgs e)
{
_context.Shell.SetValueFromRenderer(Shell.FlyoutIsPresentedProperty, true);