Shell hamburger (#5961)
authorShane Neuville <shane94@hotmail.com>
Thu, 25 Apr 2019 00:36:19 +0000 (18:36 -0600)
committerGitHub <noreply@github.com>
Thu, 25 Apr 2019 00:36:19 +0000 (18:36 -0600)
* Draw the hamburger if the user hasn't supplied an icon

* move variable

* remove excess code

* remove public

Xamarin.Forms.Platform.Android/PickerManager.cs
Xamarin.Forms.Platform.iOS/Renderers/ShellPageRendererTracker.cs
Xamarin.Forms.Platform.iOS/Resources/3bar.png [deleted file]
Xamarin.Forms.Platform.iOS/Resources/3bar@2x.png [deleted file]
Xamarin.Forms.Platform.iOS/Xamarin.Forms.Platform.iOS.csproj

index 52fcd99..d200513 100644 (file)
@@ -79,8 +79,6 @@ namespace Xamarin.Forms.Platform.Android
                                                renderer1.OnClick();
                                        else if (picker?.Parent?.Parent?.Parent is IPickerRenderer renderer2)
                                                renderer2.OnClick();
-                                       else
-                                               throw new System.Exception("Renderer not found temp check for Shane things");
                                }
                        }
                }
index e5589d6..71befeb 100644 (file)
@@ -1,4 +1,5 @@
 using CoreGraphics;
+using Foundation;
 using System;
 using System.Collections.Generic;
 using System.Collections.Specialized;
@@ -54,6 +55,7 @@ namespace Xamarin.Forms.Platform.iOS
                UISearchController _searchController;
                SearchHandler _searchHandler;
                Page _page;
+               NSCache _nSCache;
 
                BackButtonBehavior BackButtonBehavior { get; set; }
                UINavigationItem NavigationItem { get; set; }
@@ -61,6 +63,7 @@ namespace Xamarin.Forms.Platform.iOS
                public ShellPageRendererTracker(IShellContext context)
                {
                        _context = context;
+                       _nSCache = new NSCache();
                }
 
                public async void OnFlyoutBehaviorChanged(FlyoutBehavior behavior)
@@ -247,14 +250,55 @@ namespace Xamarin.Forms.Platform.iOS
                                }
                                item = item?.Parent;
                        }
-                       if (image == null)
-                               image = "3bar.png";
-                       var icon = await image.GetNativeImageAsync();
+
+                       UIImage icon = null;
+
+                       if (image != null)
+                               icon = await image.GetNativeImageAsync();
+                       else
+                               icon = DrawHamburger();
+                       
+
                        var barButtonItem = new UIBarButtonItem(icon, UIBarButtonItemStyle.Plain, OnMenuButtonPressed);
+
                        barButtonItem.AccessibilityIdentifier = "OK";
                        NavigationItem.LeftBarButtonItem = barButtonItem;
                }
 
+               UIImage DrawHamburger()
+               {
+                       const string hamburgerKey = "Hamburger";
+                       UIImage img = (UIImage)_nSCache.ObjectForKey((NSString)hamburgerKey);
+
+                       if (img != null)
+                               return img;
+
+                       var rect = new CGRect(0, 0, 23f, 23f);
+
+                       UIGraphics.BeginImageContextWithOptions(rect.Size, false, 0);
+                       var ctx = UIGraphics.GetCurrentContext();
+                       ctx.SaveState();
+                       ctx.SetStrokeColor(UIColor.Blue.CGColor);
+
+                       float size = 3f;
+                       float start = 4f;
+                       ctx.SetLineWidth(size);
+
+                       for(int i = 0; i< 3; i++)
+                       {
+                               ctx.MoveTo(1f, start + i * (size * 2));
+                               ctx.AddLineToPoint(22f, start + i * (size * 2));
+                               ctx.StrokePath();
+                       }
+
+                       ctx.RestoreState();
+                       img = UIGraphics.GetImageFromCurrentImageContext();
+                       UIGraphics.EndImageContext();
+
+                       _nSCache.SetObjectforKey(img, (NSString)hamburgerKey);
+                       return img;
+               }
+
                void OnMenuButtonPressed(object sender, EventArgs e)
                {
                        _context.Shell.SetValueFromRenderer(Shell.FlyoutIsPresentedProperty, true);
diff --git a/Xamarin.Forms.Platform.iOS/Resources/3bar.png b/Xamarin.Forms.Platform.iOS/Resources/3bar.png
deleted file mode 100644 (file)
index 50d6771..0000000
Binary files a/Xamarin.Forms.Platform.iOS/Resources/3bar.png and /dev/null differ
diff --git a/Xamarin.Forms.Platform.iOS/Resources/3bar@2x.png b/Xamarin.Forms.Platform.iOS/Resources/3bar@2x.png
deleted file mode 100644 (file)
index 7693e4f..0000000
Binary files a/Xamarin.Forms.Platform.iOS/Resources/3bar@2x.png and /dev/null differ
index 49ccbaf..2c2e945 100644 (file)
@@ -41,9 +41,6 @@
     <Reference Include="System.Net.Http" />
   </ItemGroup>
   <ItemGroup>
-    <BundleResource Include="Resources\3bar%402x.png" />
-  </ItemGroup>
-  <ItemGroup>
     <Compile Include="$(MSBuildThisFileDirectory)ContextActionCell.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)ContextScrollViewDelegate.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)GlobalCloseContextGestureRecognizer.cs" />
       <Link>Properties\GlobalAssemblyInfo.cs</Link>
     </Compile>
   </ItemGroup>
-  <ItemGroup>
-    <BundleResource Include="Resources\3bar.png" />
-  </ItemGroup>
   <ItemGroup />
 </Project>
\ No newline at end of file