From dc4ef532860e082d62fee7fe6d71258e221e386b Mon Sep 17 00:00:00 2001 From: Bill Holmes Date: Tue, 24 Nov 2015 13:33:27 -0500 Subject: [PATCH] Some refactoring for the Skia Demos Now to add a new demo method you only need to add it to the SkiaSharp.Demos file and update the sampleList member at the bottom of the file. --- .../SkiaSharp.Demos.cs} | 37 ++++++++++++++++++- samples/Skia.Forms.Demo/Skia.Forms.Demo.cs | 19 +--------- .../Skia.Forms.Demo/Skia.Forms.Demo.csproj | 4 +- samples/Skia.OSX.Demo/Main.storyboard | 4 +- samples/Skia.OSX.Demo/SkaiView.cs | 4 +- samples/Skia.OSX.Demo/Skia.OSX.Demo.csproj | 6 +-- samples/Skia.OSX.Demo/ViewController.cs | 15 ++------ 7 files changed, 51 insertions(+), 38 deletions(-) rename samples/{Skia.Forms.Demo/DrawHelpers.cs => SharedDemo/SkiaSharp.Demos.cs} (86%) diff --git a/samples/Skia.Forms.Demo/DrawHelpers.cs b/samples/SharedDemo/SkiaSharp.Demos.cs similarity index 86% rename from samples/Skia.Forms.Demo/DrawHelpers.cs rename to samples/SharedDemo/SkiaSharp.Demos.cs index 1cd3c882..8ae98f56 100644 --- a/samples/Skia.Forms.Demo/DrawHelpers.cs +++ b/samples/SharedDemo/SkiaSharp.Demos.cs @@ -1,9 +1,10 @@ using System; using SkiaSharp; +using System.Linq; -namespace Skia.Forms.Demo +namespace SkiaSharp { - public class DrawHelpers + public class Demos { #pragma warning disable 414 static readonly SKColor XamLtBlue = new SKColor (0x34, 0x98, 0xdb); @@ -171,6 +172,38 @@ namespace Skia.Forms.Demo } } } + + [Flags] + public enum Platform + { + iOS = 1, + Android = 2, + OSX = 4, + All = 0xFFFF, + } + + class Sample + { + public string Title { get; set; } + public Action Method { get; set; } + public Platform Platform { get; set; } + } + + public static string [] SamplesForPlatform (Platform platform) + { + return sampleList.Where (s => 0 != (s.Platform & platform)).Select (s => s.Title).ToArray (); + } + + public static Action MethodForSample (string title) + { + return sampleList.Where (s => s.Title == title).First ().Method; + } + + static Sample [] sampleList = new Sample[] { + new Sample {Title="Xamagon", Method = DrawXamagon, Platform = Platform.All}, + new Sample {Title="Text Sample", Method = TextSample, Platform = Platform.All}, + new Sample {Title="Gradient Sample", Method = DrawGradient, Platform = Platform.All}, + }; } } diff --git a/samples/Skia.Forms.Demo/Skia.Forms.Demo.cs b/samples/Skia.Forms.Demo/Skia.Forms.Demo.cs index fa98c7e3..959cdecd 100644 --- a/samples/Skia.Forms.Demo/Skia.Forms.Demo.cs +++ b/samples/Skia.Forms.Demo/Skia.Forms.Demo.cs @@ -21,33 +21,18 @@ namespace Skia.Forms.Demo BarTextColor = Xamarin.Forms.Color.White, }; - listView.ItemsSource = new [] {"Xamagon", "Text Sample", "Gradient Sample"}; + listView.ItemsSource = Demos.SamplesForPlatform (Demos.Platform.iOS | Demos.Platform.Android); listView.ItemSelected += (sender, e) => { if (e.SelectedItem == null) return; listView.SelectedItem = null; navPage.PushAsync (new ContentPage { - Content = new SkiaView (GetDrawHandler (e.SelectedItem.ToString ())), + Content = new SkiaView (SkiaSharp.Demos.MethodForSample (e.SelectedItem.ToString ())), }); }; } - Action GetDrawHandler (string selectedItem) - { - switch (selectedItem) { - - case "Xamagon": - return DrawHelpers.DrawXamagon; - case "Text Sample": - return DrawHelpers.TextSample; - case "Gradient Sample": - return DrawHelpers.DrawGradient; - } - - throw new NotImplementedException (); - } - protected override void OnStart () { // Handle when your app starts diff --git a/samples/Skia.Forms.Demo/Skia.Forms.Demo.csproj b/samples/Skia.Forms.Demo/Skia.Forms.Demo.csproj index 861b1914..c86728eb 100644 --- a/samples/Skia.Forms.Demo/Skia.Forms.Demo.csproj +++ b/samples/Skia.Forms.Demo/Skia.Forms.Demo.csproj @@ -32,9 +32,11 @@ - + + SkiaSharp.Demos.cs + diff --git a/samples/Skia.OSX.Demo/Main.storyboard b/samples/Skia.OSX.Demo/Main.storyboard index 5d25a886..b4ea62fd 100644 --- a/samples/Skia.OSX.Demo/Main.storyboard +++ b/samples/Skia.OSX.Demo/Main.storyboard @@ -85,9 +85,9 @@ - + - + diff --git a/samples/Skia.OSX.Demo/SkaiView.cs b/samples/Skia.OSX.Demo/SkaiView.cs index 43cf92eb..22bf9dce 100644 --- a/samples/Skia.OSX.Demo/SkaiView.cs +++ b/samples/Skia.OSX.Demo/SkaiView.cs @@ -14,13 +14,15 @@ namespace Skia.OSX.Demo public SkaiView (IntPtr handle) : base (handle) { - onDrawCallback = Skia.Forms.Demo.DrawHelpers.DrawXamagon; } public override void DrawRect (CoreGraphics.CGRect dirtyRect) { base.DrawRect (dirtyRect); + if (onDrawCallback == null) + return; + // Just not that sharp using the scale pixel scale only // Going going 2x Pixel Scale var screenScale = (int)NSScreen.MainScreen.BackingScaleFactor * 2; diff --git a/samples/Skia.OSX.Demo/Skia.OSX.Demo.csproj b/samples/Skia.OSX.Demo/Skia.OSX.Demo.csproj index 366b4b1d..05b1d815 100644 --- a/samples/Skia.OSX.Demo/Skia.OSX.Demo.csproj +++ b/samples/Skia.OSX.Demo/Skia.OSX.Demo.csproj @@ -80,12 +80,12 @@ SkaiView.cs - - DrawHelpers.cs - AppDelegate.cs + + SkiaSharp.Demos.cs + diff --git a/samples/Skia.OSX.Demo/ViewController.cs b/samples/Skia.OSX.Demo/ViewController.cs index bd9fc62e..19bd951d 100644 --- a/samples/Skia.OSX.Demo/ViewController.cs +++ b/samples/Skia.OSX.Demo/ViewController.cs @@ -14,23 +14,14 @@ namespace Skia.OSX.Demo public override void ViewDidLoad () { base.ViewDidLoad (); - PopUpButton.AddItems (new [] {"Xamagon", "Text", "Gradient"}); + PopUpButton.AddItems (SkiaSharp.Demos.SamplesForPlatform (SkiaSharp.Demos.Platform.OSX)); PopUpButton.SelectItem (0); + SkiaView.OnDrawCallback = SkiaSharp.Demos.MethodForSample (PopUpButton.SelectedItem.Title); } partial void PopUpButtonAction (NSObject sender) { - switch (PopUpButton.SelectedItem.Title) { - case "Xamagon": - SkiaView.OnDrawCallback = Skia.Forms.Demo.DrawHelpers.DrawXamagon; - break; - case "Text": - SkiaView.OnDrawCallback = Skia.Forms.Demo.DrawHelpers.TextSample; - break; - case "Gradient": - SkiaView.OnDrawCallback = Skia.Forms.Demo.DrawHelpers.DrawGradient; - break; - } + SkiaView.OnDrawCallback = SkiaSharp.Demos.MethodForSample (PopUpButton.SelectedItem.Title); } public override NSObject RepresentedObject {