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

Add drawing types templates #628

Merged
merged 7 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 14 additions & 0 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3659,6 +3659,20 @@ private StructDeclarationSyntax DeclareStruct(TypeDefinitionHandle typeDefHandle
// Add the additional members, taking care to not introduce redundant declarations.
members.AddRange(additionalMembers.Where(c => c is not StructDeclarationSyntax cs || !members.OfType<StructDeclarationSyntax>().Any(m => m.Identifier.ValueText == cs.Identifier.ValueText)));

switch (name.Identifier.ValueText)
{
case "POINTF":
case "POINT":
case "RECT":
case "RECTF":
elachlan marked this conversation as resolved.
Show resolved Hide resolved
case "SIZE":
case "SIZEF":
members.AddRange(this.ExtractMembersFromTemplate(name.Identifier.ValueText));
break;
default:
break;
}

StructDeclarationSyntax result = StructDeclaration(name.Identifier)
.AddMembers(members.ToArray())
.WithModifiers(TokenList(TokenWithSpace(this.Visibility), TokenWithSpace(SyntaxKind.PartialKeyword)));
Expand Down
13 changes: 13 additions & 0 deletions src/Microsoft.Windows.CsWin32/templates/POINT.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
partial struct POINT
{
internal POINT(global::System.Drawing.Point value) : this(value.X, value.Y) { }
internal POINT(int x, int y)
{
this.x = x;
this.y = y;
}

internal bool IsEmpty => x == 0 && y == 0;
public static implicit operator global::System.Drawing.Point(POINT value) => new global::System.Drawing.Point(value.x, value.y);
public static implicit operator POINT(global::System.Drawing.Point value) => new POINT(value);
}
13 changes: 13 additions & 0 deletions src/Microsoft.Windows.CsWin32/templates/POINTF.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
partial struct POINTF
elachlan marked this conversation as resolved.
Show resolved Hide resolved
{
internal POINTF(global::System.Drawing.PointF value) : this(value.X, value.Y) { }
internal POINTF(float x, float y)
{
this.x = x;
this.y = y;
}

internal bool IsEmpty => x == 0 && y == 0;
public static implicit operator global::System.Drawing.PointF(POINTF value) => new global::System.Drawing.PointF(value.x, value.y);
public static implicit operator POINTF(global::System.Drawing.PointF value) => new POINTF(value);
}
26 changes: 26 additions & 0 deletions src/Microsoft.Windows.CsWin32/templates/RECT.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
partial struct RECT
{
internal RECT(global::System.Drawing.Rectangle value) :
this(value.Left, value.Top, value.Right, value.Bottom) { }
internal RECT(global::System.Drawing.Point location, global::System.Drawing.Size size) :
this(location.X, location.Y, unchecked(location.X + size.Width), unchecked(location.Y + size.Height)) { }
internal RECT(int left, int top, int right, int bottom)
{
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}

internal static RECT FromXYWH(int x, int y, int width, int height) =>
new RECT(x, y, unchecked(x + width), unchecked(y + height));
internal readonly int Width => unchecked(this.right - this.left);
internal readonly int Height => unchecked(this.bottom - this.top);
internal readonly bool IsEmpty => this.left == 0 && this.top == 0 && this.right == 0 && this.bottom == 0;
internal readonly int X => this.left;
internal readonly int Y => this.top;
internal readonly global::System.Drawing.Size Size => new global::System.Drawing.Size(this.Width, this.Height);
public static implicit operator global::System.Drawing.Rectangle(RECT value) => new global::System.Drawing.Rectangle(value.left, value.top, value.Width, value.Height);
public static implicit operator global::System.Drawing.RectangleF(RECT value) => new global::System.Drawing.RectangleF(value.left, value.top, value.Width, value.Height);
public static implicit operator RECT(global::System.Drawing.Rectangle value) => new RECT(value);
}
15 changes: 15 additions & 0 deletions src/Microsoft.Windows.CsWin32/templates/SIZE.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
partial struct SIZE
{
internal SIZE(global::System.Drawing.Size value) : this(value.Width, value.Height) { }
internal SIZE(int width, int height)
{
this.cx = width;
this.cy = height;
}

internal readonly int Width => this.cx;
internal readonly int Height => this.cy;
internal readonly bool IsEmpty => this.cx == 0 && this.cy == 0;
public static implicit operator global::System.Drawing.Size(SIZE value) => new global::System.Drawing.Size(value.cx, value.cy);
public static implicit operator SIZE(global::System.Drawing.Size value) => new SIZE(value);
}
4 changes: 4 additions & 0 deletions test/GenerationSandbox.Tests/NativeMethods.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
BOOL
BOOLEAN
POINT
POINTF
RECT
SIZE
CHAR
CreateCursor
CreateFile
Expand Down
136 changes: 136 additions & 0 deletions test/GenerationSandbox.Tests/SystemDrawingStructTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Drawing;
using Windows.Win32.Foundation;
using Windows.Win32.System.Ole;

public class SystemDrawingStructTests
{
[Fact]
public void Point()
{
POINT p = new POINT(1, 1);
Point p2 = p;
Assert.False(p.IsEmpty);
Assert.False(p2.IsEmpty);

Assert.True(default(POINT).IsEmpty);
}

[Fact]
public void NotLossyConversionBetweenPointAndPOINT()
{
POINT nativePoint = new POINT(1, 1);
Point managedPoint = nativePoint;
POINT roundtrippedNativePoint = managedPoint;
Assert.Equal(nativePoint, roundtrippedNativePoint);
}

[Fact]
public void NotLossyConversionBetweenPointAndPOINT_Ctors()
{
POINT nativePoint = new POINT(1, 1);
Point managedPoint = nativePoint;
POINT roundtrippedNativePoint = new POINT(managedPoint);
Assert.Equal(nativePoint, roundtrippedNativePoint);
}

[Fact]
public void PointF()
{
POINTF p = new POINTF(1, 1);
PointF p2 = p;
Assert.False(p.IsEmpty);
Assert.False(p2.IsEmpty);

Assert.True(default(POINTF).IsEmpty);
}

[Fact]
public void NotLossyConversionBetweenPointFAndPOINTF()
{
POINTF nativePoint = new POINTF(1, 1);
PointF managedPoint = nativePoint;
POINTF roundtrippedNativePoint = managedPoint;
Assert.Equal(nativePoint, roundtrippedNativePoint);
}

[Fact]
public void NotLossyConversionBetweenPointFAndPOINTF_Ctors()
{
POINTF nativePoint = new POINTF(1, 1);
PointF managedPoint = nativePoint;
POINTF roundtrippedNativePoint = new POINTF(managedPoint);
Assert.Equal(nativePoint, roundtrippedNativePoint);
}

[Fact]
public void Size()
{
SIZE s = new SIZE(1, 1);
Size s2 = s;
Assert.False(s.IsEmpty);
Assert.False(s2.IsEmpty);

Assert.True(default(SIZE).IsEmpty);
}

[Fact]
public void NotLossyConversionBetweenSizeAndSIZE()
{
SIZE nativeSize = new SIZE(1, 1);
Size managedSize = nativeSize;
SIZE roundtrippedNativeSize = managedSize;
Assert.Equal(nativeSize, roundtrippedNativeSize);
}

[Fact]
public void NotLossyConversionBetweenSizeAndSIZE_Ctors()
{
SIZE nativeSize = new SIZE(1, 1);
Size managedSize = nativeSize;
SIZE roundtrippedNativeSize = new SIZE(managedSize);
Assert.Equal(nativeSize, roundtrippedNativeSize);
}

[Fact]
public void Rect()
{
RECT r = new RECT(1, 1, 2, 2);
Rectangle r2 = r;
Assert.False(r.IsEmpty);
Assert.False(r2.IsEmpty);

Assert.True(default(RECT).IsEmpty);
}

[Fact]
public void NotLossyConversionBetweenRectangleAndRECT()
{
RECT nativeSize = new RECT(1, 1, 2, 2);
Rectangle managedSize = nativeSize;
RECT roundtrippedNativeSize = managedSize;
Assert.Equal(nativeSize, roundtrippedNativeSize);
}

[Fact]
public void NotLossyConversionBetweenRectangleAndRECT_Ctors()
{
RECT nativeSize = new RECT(1, 1, 2, 2);
Rectangle managedSize = nativeSize;
RECT roundtrippedNativeSize = new RECT(managedSize);
Assert.Equal(nativeSize, roundtrippedNativeSize);
}

[Fact]
public void RectangleAndRECTFromXYWH_AreEqual()
{
RECT nativeSize = RECT.FromXYWH(1, 1, 2, 2);
Rectangle managedSize = new Rectangle(1, 1, 2, 2);
Assert.Equal(nativeSize.left, managedSize.Left);
Assert.Equal(nativeSize.right, managedSize.Right);
Assert.Equal(nativeSize.top, managedSize.Top);
Assert.Equal(nativeSize.bottom, managedSize.Bottom);
}
}