|
| 1 | +#if __MACOS__ |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using CoreGraphics; |
| 8 | +using SkiaSharp; |
| 9 | +using SkiaSharp.Views.Mac; |
| 10 | +using AppKit; |
| 11 | +using Windows.ApplicationModel; |
| 12 | +using Windows.Graphics.Display; |
| 13 | +using Windows.UI.Core; |
| 14 | +using Windows.UI.Xaml; |
| 15 | +using Windows.UI.Xaml.Controls; |
| 16 | +using Windows.UI.Xaml.Media; |
| 17 | + |
| 18 | +namespace SkiaSharp.Views.UWP |
| 19 | +{ |
| 20 | + public partial class SKXamlCanvas : FrameworkElement |
| 21 | + { |
| 22 | + private SKCGSurfaceFactory drawable; |
| 23 | + |
| 24 | + public SKXamlCanvas() |
| 25 | + { |
| 26 | + Loaded += OnLoaded; |
| 27 | + Unloaded += OnUnloaded; |
| 28 | + SizeChanged += OnSizeChanged; |
| 29 | + |
| 30 | + RegisterPropertyChangedCallback(VisibilityProperty, (s, e) => OnVisibilityChanged(s)); |
| 31 | + OnVisibilityChanged(this); |
| 32 | + Initialize(); |
| 33 | + } |
| 34 | + |
| 35 | + private SKSize GetCanvasSize() => drawable?.Info.Size ?? SKSize.Empty; |
| 36 | + |
| 37 | + private static bool GetIsInitialized() => true; |
| 38 | + |
| 39 | + private void Initialize() |
| 40 | + { |
| 41 | + drawable = new SKCGSurfaceFactory(); |
| 42 | + } |
| 43 | + |
| 44 | + private void OnDpiChanged(DisplayInformation sender, object args = null) |
| 45 | + { |
| 46 | + Dpi = sender.LogicalDpi / 96.0f; |
| 47 | + Invalidate(); |
| 48 | + } |
| 49 | + |
| 50 | + private void OnLoaded(object sender, RoutedEventArgs e) |
| 51 | + { |
| 52 | + var display = DisplayInformation.GetForCurrentView(); |
| 53 | + display.DpiChanged += OnDpiChanged; |
| 54 | + |
| 55 | + OnDpiChanged(display); |
| 56 | + Invalidate(); |
| 57 | + } |
| 58 | + |
| 59 | + private void OnUnloaded(object sender, RoutedEventArgs e) |
| 60 | + { |
| 61 | + var display = DisplayInformation.GetForCurrentView(); |
| 62 | + display.DpiChanged -= OnDpiChanged; |
| 63 | + } |
| 64 | + |
| 65 | + private void DoInvalidate() |
| 66 | + => NeedsDisplay = true; |
| 67 | + |
| 68 | + public override void DrawRect(CGRect rect) |
| 69 | + { |
| 70 | + base.DrawRect(rect); |
| 71 | + |
| 72 | + using (var ctx = NSGraphicsContext.CurrentContext.CGContext) |
| 73 | + { |
| 74 | + // create the skia context |
| 75 | + SKImageInfo info; |
| 76 | + using (var surface = drawable.CreateSurface(Bounds, IgnorePixelScaling ? 1 : Window.BackingScaleFactor, out info)) |
| 77 | + { |
| 78 | + // draw on the image using SKiaSharp |
| 79 | + OnPaintSurface(new SKPaintSurfaceEventArgs(surface, info)); |
| 80 | + |
| 81 | + // draw the surface to the context |
| 82 | + drawable.DrawSurface(ctx, Bounds, info, surface); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + protected override void Dispose(bool disposing) |
| 88 | + { |
| 89 | + base.Dispose(disposing); |
| 90 | + |
| 91 | + drawable?.Dispose(); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | +#endif |
0 commit comments