blob: c12362c4ab470c7b06e92d1af670bc95b3764e01 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
using System;
using AppKit;
using CoreAnimation;
using CoreGraphics;
using Xamarin.PropertyEditing.Drawing;
namespace Xamarin.PropertyEditing.Mac
{
internal class CommonBrushLayer : CALayer
{
public CommonBrushLayer ()
{
this.CornerRadius = 3;
this.BorderColor = new CGColor (.5f, .5f, .5f, .5f);
this.BorderWidth = 1;
MasksToBounds = true;
}
private CALayer brushLayer;
private CALayer BrushLayer {
get => brushLayer;
set {
if (brushLayer != null)
brushLayer.RemoveFromSuperLayer ();
brushLayer = value;
if (brushLayer != null)
AddSublayer (brushLayer);
}
}
private CommonBrush brush;
public CommonBrush Brush {
get => brush;
set {
brush = value;
BrushLayer = CreateBrushLayer (brush);
Opacity = brush == null ? 0 : 1;
}
}
public static CALayer CreateBrushLayer (CommonBrush brush)
{
switch (brush) {
case CommonSolidBrush solid:
return new CALayer {
BackgroundColor = solid.Color.ToCGColor (),
Opacity = (float)solid.Opacity
};
case CommonGradientBrush gradient:
return new CommonGradientBrushLayer {
Opacity = (float)gradient.Opacity
};
default:
return new CALayer {
BackgroundColor = NSColor.Clear.CGColor
};
}
}
public override void LayoutSublayers ()
{
base.LayoutSublayers ();
BrushLayer.Frame = Bounds;
Contents = DrawingExtensions.GenerateCheckerboard (Bounds);
}
public NSImage RenderPreview ()
{
var scale = this.ContentsScale;
nint h = (nint)(this.Bounds.Height * scale);
nint w = (nint)(this.Bounds.Width * scale);
nint bytesPerRow = w * 4;
if (h <= 0 || w <= 0)
return null;
using (var colorSpace = CGColorSpace.CreateGenericRgb ())
using (var context = new CGBitmapContext (IntPtr.Zero, w, h, 8, bytesPerRow, colorSpace, CGImageAlphaInfo.PremultipliedLast)) {
this.RenderInContext (context);
using (var image = context.ToImage ()) {
return new NSImage (image, new CGSize (w, h));
}
}
}
}
}
|