Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macOS support #22

Merged
merged 2 commits into from
Jun 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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