Skip to content

Commit

Permalink
Merge pull request #22 from MartinZikmund/dev/mazi/macos-support
Browse files Browse the repository at this point in the history
macOS support
  • Loading branch information
jeromelaban authored Jun 6, 2020
2 parents 7248979 + 405ca2a commit 4b755b1
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 3 deletions.
95 changes: 95 additions & 0 deletions source/SkiaSharp.Views/SkiaSharp.Views.Uno/SKXamlCanvas.macOS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#if __MACOS__
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using CoreGraphics;
using SkiaSharp;
using SkiaSharp.Views.Mac;
using AppKit;
using Windows.ApplicationModel;
using Windows.Graphics.Display;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace SkiaSharp.Views.UWP
{
public partial class SKXamlCanvas : FrameworkElement
{
private SKCGSurfaceFactory drawable;

public SKXamlCanvas()
{
Loaded += OnLoaded;
Unloaded += OnUnloaded;
SizeChanged += OnSizeChanged;

RegisterPropertyChangedCallback(VisibilityProperty, (s, e) => OnVisibilityChanged(s));
OnVisibilityChanged(this);
Initialize();
}

private SKSize GetCanvasSize() => drawable?.Info.Size ?? SKSize.Empty;

private static bool GetIsInitialized() => true;

private void Initialize()
{
drawable = new SKCGSurfaceFactory();
}

private void OnDpiChanged(DisplayInformation sender, object args = null)
{
Dpi = sender.LogicalDpi / 96.0f;
Invalidate();
}

private void OnLoaded(object sender, RoutedEventArgs e)
{
var display = DisplayInformation.GetForCurrentView();
display.DpiChanged += OnDpiChanged;

OnDpiChanged(display);
Invalidate();
}

private void OnUnloaded(object sender, RoutedEventArgs e)
{
var display = DisplayInformation.GetForCurrentView();
display.DpiChanged -= OnDpiChanged;
}

private void DoInvalidate()
=> NeedsDisplay = true;

public override void DrawRect(CGRect rect)
{
base.DrawRect(rect);

using (var ctx = NSGraphicsContext.CurrentContext.CGContext)
{
// create the skia context
SKImageInfo info;
using (var surface = drawable.CreateSurface(Bounds, IgnorePixelScaling ? 1 : Window.BackingScaleFactor, out info))
{
// draw on the image using SKiaSharp
OnPaintSurface(new SKPaintSurfaceEventArgs(surface, info));

// draw the surface to the context
drawable.DrawSurface(ctx, Bounds, info, surface);
}
}
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);

drawable?.Dispose();
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="MSBuild.Sdk.Extras/2.0.54" ToolsVersion="15.0">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;xamarinios10;monoandroid80;monoandroid90</TargetFrameworks>
<TargetFrameworks>netstandard2.0;xamarinios10;monoandroid80;monoandroid90;xamarinmac20</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GeneratePackageOnBuild Condition="'$(Configuration)'=='Release'">true</GeneratePackageOnBuild>
<NoWarn>$(NoWarm);NU1701</NoWarn>
Expand Down Expand Up @@ -63,15 +63,24 @@
<Compile Remove="..\SkiaSharp.Views.AppleiOS\SKGLView.cs" />
<Compile Remove="..\SkiaSharp.Views.AppleiOS\SKGLLayer.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'!='xamarinios10'">
<ItemGroup Condition="'$(TargetFramework)'=='xamarinmac20'">
<Compile Include="..\SkiaSharp.Views.Mac\**\*.cs" Link="SharedViews\%(RecursiveDir)%(Filename)%(Extension)" />
<Compile Include="..\SkiaSharp.Views.Apple\**\*.cs" Link="SharedViews\%(RecursiveDir)%(Filename)%(Extension)" />
<Compile Remove="..\SkiaSharp.Views.Mac\SKCanvasView.cs" />
<Compile Remove="..\SkiaSharp.Views.Apple\SKCanvasLayer.cs" />
<Compile Remove="..\SkiaSharp.Views.Mac\SKGLView.cs" />
<Compile Remove="..\SkiaSharp.Views.Mac\SKGLLayer.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'!='xamarinios10' and '$(TargetFramework)'!='xamarinmac20'">
<None Include="..\SkiaSharp.Views.AppleiOS\**\*.cs" Link="SharedViews\%(RecursiveDir)%(Filename)%(Extension)" />
<None Include="..\SkiaSharp.Views.Mac\**\*.cs" Link="SharedViews\%(RecursiveDir)%(Filename)%(Extension)" />
<None Include="..\SkiaSharp.Views.Apple\**\*.cs" Link="SharedViews\%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
<ProjectReference Include="..\..\..\binding\SkiaSharp.Wasm\SkiaSharp.Wasm.csproj" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='xamarinios10' or '$(TargetFramework)'=='monoandroid80' or '$(TargetFramework)'=='monoandroid90'">
<ItemGroup Condition="'$(TargetFramework)'=='xamarinios10' or '$(TargetFramework)'=='xamarinmac20' or '$(TargetFramework)'=='monoandroid80' or '$(TargetFramework)'=='monoandroid90'">
<PackageReference Include="SkiaSharp" Version="1.68.0" />
</ItemGroup>

Expand Down

0 comments on commit 4b755b1

Please sign in to comment.