Skip to content

Commit ea1e31f

Browse files
committed
[src] Add CGSize/CGPoint/CGRectDictionary
1 parent 9f493e0 commit ea1e31f

File tree

9 files changed

+431
-41
lines changed

9 files changed

+431
-41
lines changed

src/CoreGraphics/CGPoint.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,10 @@ public CGPoint (CGPoint point)
154154
this.y = point.y;
155155
}
156156

157-
/// <param name="dictionaryRepresentation">To be added.</param>
158-
/// <param name="point">To be added.</param>
159-
/// <summary>To be added.</summary>
160-
/// <returns>To be added.</returns>
161-
/// <remarks>To be added.</remarks>
157+
/// <summary>Attempts to parse the contents of an <see cref="NSDictionary" /> with a serialized <see cref="CGPoint" /> into a <see cref="CGPoint" />.</summary>
158+
/// <param name="dictionaryRepresentation">The dictionary to parse.</param>
159+
/// <param name="point">If successful, the resulting <see cref="CGPoint" /> value.</param>
160+
/// <returns><see langword="true" /> if the dictionary was serialized successfully, <see langword="false" /> otherwise.</returns>
162161
public static bool TryParse (NSDictionary? dictionaryRepresentation, out CGPoint point)
163162
{
164163
if (dictionaryRepresentation is null) {
@@ -173,13 +172,32 @@ public static bool TryParse (NSDictionary? dictionaryRepresentation, out CGPoint
173172
}
174173
}
175174

176-
/// <summary>To be added.</summary>
177-
/// <returns>To be added.</returns>
178-
/// <remarks>To be added.</remarks>
175+
/// <summary>Serializes a <see cref="CGPoint" /> into an <see cref="Foundation.NSDictionary" />.</summary>
176+
/// <returns>A <see cref="Foundation.NSDictionary" /> with the values from this <see cref="CGPoint" />.</returns>
177+
/// <remarks>
178+
/// <para>
179+
/// The returned dictionary conforms to the serialization
180+
/// standard of Cocoa and CocoaTouch and can be used to serialize
181+
/// the state into objects that can be parsed by other Apple APIs.
182+
/// </para>
183+
/// <para>
184+
/// It is possible to create a <see cref="CGPoint" /> from an <see cref="NSDictionary" /> using
185+
/// the <see cref="TryParse(NSDictionary,out CGPoint)" /> method.
186+
/// </para>
187+
/// </remarks>
188+
/// <seealso cref="ToCGPointDictionary" />
179189
public NSDictionary ToDictionary ()
180190
{
181191
return new NSDictionary (NativeDrawingMethods.CGPointCreateDictionaryRepresentation (this));
182192
}
193+
194+
/// <summary>Serializes a <see cref="CGPoint" /> into a <see cref="CGPointDictionary" />.</summary>
195+
/// <returns>A <see cref="CGPointDictionary" /> representing the values from this <see cref="CGPoint" />.</returns>
196+
/// <seealso cref="ToDictionary" />
197+
public CGPointDictionary ToCGPointDictionary ()
198+
{
199+
return new CGPointDictionary (ToDictionary ());
200+
}
183201
#endif // !COREBUILD
184202

185203
/// <param name="obj">To be added.</param>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Foundation;
5+
6+
namespace CoreGraphics;
7+
8+
/// <summary>This class represents a <see cref="CGPoint" /> stored in a dictionary; that is a dictionary with "X" and "Y" keys for the corresponding <see cref="CGPoint" /> fields.</summary>
9+
public class CGPointDictionary : DictionaryContainer {
10+
#if !COREBUILD
11+
/// <summary>Creates a new <see cref="CGPointDictionary" /> with default (empty) values.</summary>
12+
[Preserve (Conditional = true)]
13+
public CGPointDictionary () : base (new NSMutableDictionary ()) {}
14+
15+
/// <summary>Creates a new <see cref="CGPointDictionary" /> from the values that are specified in <paramref name="dictionary" />.</summary>
16+
/// <param name="dictionary">The dictionary to use to populate the properties of this type.</param>
17+
[Preserve (Conditional = true)]
18+
public CGPointDictionary (NSDictionary? dictionary) : base (dictionary) {}
19+
20+
/// <summary>The X component of the <see cref="CGPoint" />.</summary>
21+
public nfloat? X {
22+
get => GetNFloatValue ((NSString) "X");
23+
set => SetNumberValue ((NSString) "X", value);
24+
}
25+
26+
/// <summary>The Y component of the <see cref="CGPoint" />.</summary>
27+
public nfloat? Y {
28+
get => GetNFloatValue ((NSString) "Y");
29+
set => SetNumberValue ((NSString) "Y", value);
30+
}
31+
32+
/// <summary>Get the <see cref="CGPoint" /> stored in this dictionary.</summary>
33+
/// <returns>The <see cref="CGPoint" /> stored in this dictionary.</returns>
34+
public CGPoint ToPoint ()
35+
{
36+
if (CGPoint.TryParse (Dictionary, out var rv))
37+
return rv;
38+
return default;
39+
}
40+
#endif // !COREBUILD
41+
}

src/CoreGraphics/CGRect.cs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -547,17 +547,10 @@ public void Deconstruct (out CGPoint location, out CGSize size)
547547
size = Size;
548548
}
549549

550-
/// <param name="dictionaryRepresentation">Dictionary containing
551-
/// a serialized CGRect.</param>
552-
/// <param name="rect">The rectangle value with the contents if
553-
/// the return value is true.</param>
554-
/// <summary>To be added.</summary>
555-
/// <returns>True if the NSDictionary contained a serialized
556-
/// CGRect and the initialized <paramref name="rect" /> with the
557-
/// contents on return. False on failure, and the contents of
558-
/// <paramref name="rect" /> are set to Empty in that case.</returns>
559-
/// <remarks>Used to create a CGRect from a dictionary containing
560-
/// keys for X, Y, Widht and Height.</remarks>
550+
/// <summary>Attempts to parse the contents of an <see cref="NSDictionary" /> with a serialized <see cref="CGRect" /> into a <see cref="CGRect" />.</summary>
551+
/// <param name="dictionaryRepresentation">The dictionary to parse.</param>
552+
/// <param name="rect">If successful, the resulting <see cref="CGRect" /> value.</param>
553+
/// <returns><see langword="true" /> if the dictionary was serialized successfully, <see langword="false" /> otherwise.</returns>
561554
public static bool TryParse (NSDictionary? dictionaryRepresentation, out CGRect rect)
562555
{
563556
if (dictionaryRepresentation is null) {
@@ -572,25 +565,33 @@ public static bool TryParse (NSDictionary? dictionaryRepresentation, out CGRect
572565
}
573566
}
574567

575-
/// <summary>Serializes the state of the rectangle into an NSDictionary.</summary>
576-
/// <returns>An NSDictionary representing the rectangle.</returns>
577-
/// <remarks>
578-
/// <para>
579-
/// The returned dictionary conforms to the serialization
580-
/// standard of Cocoa and CocoaTouch and can be used to serialize
581-
/// the state into objects that can be parsed by other Apple APIs.
582-
/// </para>
583-
/// <para>
584-
/// It is possible to create CGRect from a Dictionary using
585-
/// the <see cref="CoreGraphics.CGRect.TryParse(Foundation.NSDictionary,out CoreGraphics.CGRect)" />
586-
/// method.
587-
/// </para>
588-
/// </remarks>
568+
/// <summary>Serializes a <see cref="CGRect" /> into an <see cref="NSDictionary" />.</summary>
569+
/// <returns>An <see cref="NSDictionary" /> with the values from this <see cref="CGRect" />.</returns>
570+
/// <remarks>
571+
/// <para>
572+
/// The returned dictionary conforms to the serialization
573+
/// standard of Cocoa and CocoaTouch and can be used to serialize
574+
/// the state into objects that can be parsed by other Apple APIs.
575+
/// </para>
576+
/// <para>
577+
/// It is possible to create a <see cref="CGRect" /> from an <see cref="NSDictionary" /> using
578+
/// the <see cref="TryParse(NSDictionary,out CGRect)" /> method.
579+
/// </para>
580+
/// </remarks>
581+
/// <seealso cref="ToCGRectDictionary" />
589582
public NSDictionary ToDictionary ()
590583
{
591584
return new NSDictionary (NativeDrawingMethods.CGRectCreateDictionaryRepresentation (this));
592585
}
593586

587+
/// <summary>Serializes a <see cref="CGRect" /> into a <see cref="CGRectDictionary" />.</summary>
588+
/// <returns>A <see cref="CGRectDictionary" /> representing the values from this <see cref="CGRect" />.</returns>
589+
/// <seealso cref="ToDictionary" />
590+
public CGRectDictionary ToCGRectDictionary ()
591+
{
592+
return new CGRectDictionary (ToDictionary ());
593+
}
594+
594595
#if MONOMAC
595596
// <quote>When building for 64 bit systems, or building 32 bit like 64 bit, NSRect is typedef’d to CGRect.</quote>
596597
// https://developer.apple.com/documentation/foundation/nsrect?language=objc
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Foundation;
5+
6+
namespace CoreGraphics;
7+
8+
/// <summary>This class represents a <see cref="CGRect" /> stored in a dictionary; that is a dictionary with "X", "Y", Width" and "Height" keys for the corresponding <see cref="CGRect" /> fields.</summary>
9+
public class CGRectDictionary : DictionaryContainer {
10+
#if !COREBUILD
11+
/// <summary>Creates a new <see cref="CGRectDictionary" /> with default (empty) values.</summary>
12+
[Preserve (Conditional = true)]
13+
public CGRectDictionary () : base (new NSMutableDictionary ()) {}
14+
15+
/// <summary>Creates a new <see cref="CGRectDictionary" /> from the values that are specified in <paramref name="dictionary" />.</summary>
16+
/// <param name="dictionary">The dictionary to use to populate the properties of this type.</param>
17+
[Preserve (Conditional = true)]
18+
public CGRectDictionary (NSDictionary? dictionary) : base (dictionary) {}
19+
20+
/// <summary>The X component of the <see cref="CGPoint" />.</summary>
21+
public nfloat? X {
22+
get => GetNFloatValue ((NSString) "X");
23+
set => SetNumberValue ((NSString) "X", value);
24+
}
25+
26+
/// <summary>The Y component of the <see cref="CGPoint" />.</summary>
27+
public nfloat? Y {
28+
get => GetNFloatValue ((NSString) "Y");
29+
set => SetNumberValue ((NSString) "Y", value);
30+
}
31+
32+
/// <summary>The width component of the <see cref="CGSize" />.</summary>
33+
public nfloat? Width {
34+
get => GetNFloatValue ((NSString) "Width");
35+
set => SetNumberValue ((NSString) "Width", value);
36+
}
37+
38+
/// <summary>The height component of the <see cref="CGSize" />.</summary>
39+
public nfloat? Height {
40+
get => GetNFloatValue ((NSString) "Height");
41+
set => SetNumberValue ((NSString) "Height", value);
42+
}
43+
44+
/// <summary>Get the <see cref="CGRect" /> stored in this dictionary.</summary>
45+
/// <returns>The <see cref="CGRect" /> stored in this dictionary.</returns>
46+
public CGRect ToRect ()
47+
{
48+
if (CGRect.TryParse (Dictionary, out var rv))
49+
return rv;
50+
return default;
51+
}
52+
#endif // !COREBUILD
53+
}

src/CoreGraphics/CGSize.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,10 @@ public CGSize (CGSize size)
156156
this.height = size.height;
157157
}
158158

159-
/// <param name="dictionaryRepresentation">To be added.</param>
160-
/// <param name="size">To be added.</param>
161-
/// <summary>Attempts to parse the contents of an NSDictionary with a serialized CGSize into a CGSize.</summary>
162-
/// <returns>To be added.</returns>
163-
/// <remarks>To be added.</remarks>
159+
/// <summary>Attempts to parse the contents of an <see cref="NSDictionary" /> with a serialized <see cref="CGSize" /> into a <see cref="CGSize" />.</summary>
160+
/// <param name="dictionaryRepresentation">The dictionary to parse.</param>
161+
/// <param name="size">If successful, the resulting <see cref="CGSize" /> value.</param>
162+
/// <returns><see langword="true" /> if the dictionary was serialized successfully, <see langword="false" /> otherwise.</returns>
164163
public static bool TryParse (NSDictionary? dictionaryRepresentation, out CGSize size)
165164
{
166165
if (dictionaryRepresentation is null) {
@@ -175,14 +174,33 @@ public static bool TryParse (NSDictionary? dictionaryRepresentation, out CGSize
175174
}
176175
}
177176

178-
/// <summary>Serializes a CGSize into an <see cref="Foundation.NSDictionary" />.</summary>
179-
/// <returns>To be added.</returns>
180-
/// <remarks>To be added.</remarks>
177+
/// <summary>Serializes a <see cref="CGSize" /> into an <see cref="Foundation.NSDictionary" />.</summary>
178+
/// <returns>A <see cref="Foundation.NSDictionary" /> with the values from this <see cref="CGSize" />.</returns>
179+
/// <remarks>
180+
/// <para>
181+
/// The returned dictionary conforms to the serialization
182+
/// standard of Cocoa and CocoaTouch and can be used to serialize
183+
/// the state into objects that can be parsed by other Apple APIs.
184+
/// </para>
185+
/// <para>
186+
/// It is possible to create a <see cref="CGSize" /> from an <see cref="NSDictionary" /> using
187+
/// the <see cref="TryParse(NSDictionary,out CGSize)" /> method.
188+
/// </para>
189+
/// </remarks>
190+
/// <seealso cref="ToCGSizeDictionary" />
181191
public NSDictionary ToDictionary ()
182192
{
183193
return new NSDictionary (NativeDrawingMethods.CGSizeCreateDictionaryRepresentation (this));
184194
}
185195

196+
/// <summary>Serializes a <see cref="CGSize" /> into a <see cref="CGSizeDictionary" />.</summary>
197+
/// <returns>A <see cref="CGSizeDictionary" /> representing the values from this <see cref="CGSize" />.</returns>
198+
/// <seealso cref="ToDictionary" />
199+
public CGSizeDictionary ToCGSizeDictionary ()
200+
{
201+
return new CGSizeDictionary (ToDictionary ());
202+
}
203+
186204
/// <param name="point">To be added.</param>
187205
/// <summary>Initializes a CGSize object from a CGPoint.</summary>
188206
/// <remarks>To be added.</remarks>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Foundation;
5+
6+
namespace CoreGraphics;
7+
8+
/// <summary>This class represents a <see cref="CGSize" /> stored in a dictionary; that is a dictionary with "Width" and "Height" keys for the corresponding <see cref="CGSize" /> fields.</summary>
9+
public class CGSizeDictionary : DictionaryContainer {
10+
#if !COREBUILD
11+
/// <summary>Creates a new <see cref="CGSizeDictionary" /> with default (empty) values.</summary>
12+
[Preserve (Conditional = true)]
13+
public CGSizeDictionary () : base (new NSMutableDictionary ()) {}
14+
15+
/// <summary>Creates a new <see cref="CGSizeDictionary" /> from the values that are specified in <paramref name="dictionary" />.</summary>
16+
/// <param name="dictionary">The dictionary to use to populate the properties of this type.</param>
17+
[Preserve (Conditional = true)]
18+
public CGSizeDictionary (NSDictionary? dictionary) : base (dictionary) {}
19+
20+
/// <summary>The width component of the <see cref="CGSize" />.</summary>
21+
public nfloat? Width {
22+
get => GetNFloatValue ((NSString) "Width");
23+
set => SetNumberValue ((NSString) "Width", value);
24+
}
25+
26+
/// <summary>The height component of the <see cref="CGSize" />.</summary>
27+
public nfloat? Height {
28+
get => GetNFloatValue ((NSString) "Height");
29+
set => SetNumberValue ((NSString) "Height", value);
30+
}
31+
32+
/// <summary>Get the <see cref="CGSize" /> stored in this dictionary.</summary>
33+
/// <returns>The <see cref="CGSize" /> stored in this dictionary.</returns>
34+
public CGSize ToSize ()
35+
{
36+
if (CGSize.TryParse (Dictionary, out var rv))
37+
return rv;
38+
return default;
39+
}
40+
#endif // !COREBUILD
41+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
6+
using Foundation;
7+
using CoreGraphics;
8+
using ObjCRuntime;
9+
10+
using NUnit.Framework;
11+
12+
namespace MonoTouchFixtures.CoreGraphics;
13+
14+
[TestFixture]
15+
[Preserve (AllMembers = true)]
16+
public class CGPointDictionaryTests {
17+
18+
[Test]
19+
public void PropertiesTest ()
20+
{
21+
Assert.Multiple (() => {
22+
var point = new CGPoint ((nfloat) 1, (nfloat) 2);
23+
using var dict = point.ToDictionary ();
24+
var strongDict = new CGPointDictionary (dict);
25+
Assert.AreEqual (point.X, strongDict.X, "X");
26+
Assert.AreEqual (point.Y, strongDict.Y, "Y");
27+
28+
var point2 = strongDict.ToPoint ();
29+
Assert.AreEqual (point, point2, "Point");
30+
31+
strongDict = new CGPointDictionary ();
32+
strongDict.X = 3;
33+
Assert.AreEqual ((nfloat) 3, strongDict.X, "X 2");
34+
strongDict.Y = 4;
35+
Assert.AreEqual ((nfloat) 4, strongDict.Y, "Y 2");
36+
point2 = strongDict.ToPoint ();
37+
Assert.AreEqual (new CGPoint (3, 4), point2, "Point 2");
38+
});
39+
}
40+
41+
[Test]
42+
public void Default ()
43+
{
44+
var strongDict = new CGPointDictionary ();
45+
Assert.IsNull (strongDict.X, "X");
46+
Assert.IsNull (strongDict.Y, "Y");
47+
var point = strongDict.ToPoint ();
48+
Assert.AreEqual (default (CGPoint), point, "Point");
49+
}
50+
51+
[Test]
52+
public void ToStringTest1 ()
53+
{
54+
var strongDict = new CGPointDictionary ();
55+
Assert.AreEqual ("CoreGraphics.CGPointDictionary", strongDict.ToString (), "A");
56+
Assert.AreEqual ("{\n}", strongDict.Dictionary.ToString (), "B");
57+
}
58+
59+
[Test]
60+
public void ToStringTest2 ()
61+
{
62+
var strongDict = new CGPointDictionary ();
63+
strongDict.X = 3;
64+
strongDict.Y = 4;
65+
Assert.AreEqual ("CoreGraphics.CGPointDictionary", strongDict.ToString (), "A");
66+
Assert.AreEqual ("{\n X = 3;\n Y = 4;\n}", strongDict.Dictionary.ToString (), "B");
67+
}
68+
}

0 commit comments

Comments
 (0)