Skip to content

Commit 915fdc7

Browse files
committed
Fix infinite canvas json parsing regression.
1 parent 11daa4f commit 915fdc7

File tree

5 files changed

+168
-7
lines changed

5 files changed

+168
-7
lines changed

Microsoft.Toolkit.Uwp.UI.Controls/InfiniteCanvas/Controls/InfiniteCanvasVirtualDrawingSurface.Render.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ internal string GetSerializedList()
8181
return JsonSerializer.Serialize(exportModel, GetJsonSerializerOptions());
8282
}
8383

84-
private JsonSerializerOptions GetJsonSerializerOptions()
84+
private static JsonSerializerOptions GetJsonSerializerOptions()
8585
{
8686
var jsonSerializerOptions = new JsonSerializerOptions
8787
{
@@ -95,13 +95,8 @@ private JsonSerializerOptions GetJsonSerializerOptions()
9595
return jsonSerializerOptions;
9696
}
9797

98-
internal void RenderFromJsonAndDraw(Rect viewPort, string json, float zoom)
98+
internal static List<IDrawable> LoadJson(string json)
9999
{
100-
_visibleList.Clear();
101-
_drawableList.Clear();
102-
_undoCommands.Clear();
103-
_redoCommands.Clear();
104-
105100
var token = JsonDocument.Parse(json);
106101
List<IDrawable> newList;
107102
if (token.RootElement.ValueKind == JsonValueKind.Array)
@@ -114,6 +109,18 @@ internal void RenderFromJsonAndDraw(Rect viewPort, string json, float zoom)
114109
newList = JsonSerializer.Deserialize<InkCanvasExportModel>(json, GetJsonSerializerOptions()).DrawableList;
115110
}
116111

112+
return newList;
113+
}
114+
115+
internal void RenderFromJsonAndDraw(Rect viewPort, string json, float zoom)
116+
{
117+
_visibleList.Clear();
118+
_drawableList.Clear();
119+
_undoCommands.Clear();
120+
_redoCommands.Clear();
121+
122+
var newList = LoadJson(json);
123+
117124
foreach (var drawable in newList)
118125
{
119126
_drawableList.Add(drawable);

Microsoft.Toolkit.Uwp.UI.Controls/InfiniteCanvas/JsonConverters/Matrix3x2Converter.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ public override Matrix3x2 Read(ref Utf8JsonReader reader, Type typeToConvert, Js
5252
case "M32":
5353
matrix.M32 = reader.GetSingle();
5454
break;
55+
case "IsIdentity":
56+
// Ignore, as it is readonly, and from v1
57+
break;
58+
case "Translation":
59+
var translation = JsonSerializer.Deserialize<Vector2>(ref reader);
60+
matrix.Translation = translation;
61+
break;
5562
}
5663
}
5764
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
{
2+
"DrawableList": [
3+
{
4+
"$type": "Microsoft.Toolkit.Uwp.UI.Controls.InkDrawable, Microsoft.Toolkit.Uwp.UI.Controls",
5+
"SerializableStrokeList": [
6+
{
7+
"DrawingAttributes": {
8+
"Color": {
9+
"A": 255,
10+
"R": 0,
11+
"G": 0,
12+
"B": 0
13+
},
14+
"FitToCurve": true,
15+
"IgnorePressure": false,
16+
"IgnoreTilt": true,
17+
"Size": {
18+
"Width": 4.0,
19+
"Height": 4.0,
20+
"IsEmpty": false
21+
},
22+
"PenTip": 0,
23+
"PenTipTransform": {
24+
"M11": 1.0,
25+
"M12": 0.0,
26+
"M21": 0.0,
27+
"M22": 1.0,
28+
"M31": 0.0,
29+
"M32": 0.0,
30+
"IsIdentity": true,
31+
"Translation": {
32+
"X": 0.0,
33+
"Y": 0.0
34+
}
35+
},
36+
"DrawAsHighlighter": false
37+
},
38+
"SerializableFinalPointList": [
39+
{
40+
"Position": {
41+
"X": 103.97531127929688,
42+
"Y": 45.995269775390625
43+
},
44+
"Pressure": 0.5,
45+
"TiltX": 0.0,
46+
"TiltY": 0.0,
47+
"Timestamp": 0
48+
},
49+
{
50+
"Position": {
51+
"X": 103.80825805664063,
52+
"Y": 45.995269775390625
53+
},
54+
"Pressure": 0.5,
55+
"TiltX": 0.0,
56+
"TiltY": 0.0,
57+
"Timestamp": 0
58+
},
59+
{
60+
"Position": {
61+
"X": 103.41259002685547,
62+
"Y": 45.995269775390625
63+
},
64+
"Pressure": 0.5,
65+
"TiltX": 0.0,
66+
"TiltY": 0.0,
67+
"Timestamp": 0
68+
}
69+
],
70+
"SerializableDrawingAttributesKind": 0,
71+
"SerializableDrawingAttributesPencilProperties": null,
72+
"PointTransform": {
73+
"M11": 1.0,
74+
"M12": 0.0,
75+
"M21": 0.0,
76+
"M22": 1.0,
77+
"M31": 0.0,
78+
"M32": 0.0,
79+
"IsIdentity": true,
80+
"Translation": {
81+
"X": 0.0,
82+
"Y": 0.0
83+
}
84+
}
85+
}
86+
],
87+
"Bounds": {
88+
"X": 69.949455261230469,
89+
"Y": 28.069997787475586,
90+
"Width": 1668.2589111328125,
91+
"Height": 280.38735961914062,
92+
"Left": 69.949455261230469,
93+
"Top": 28.069997787475586,
94+
"Right": 1738.2083740234375,
95+
"Bottom": 308.45736694335938,
96+
"IsEmpty": false
97+
},
98+
"IsActive": true
99+
}
100+
],
101+
"Version": 1
102+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Threading.Tasks;
7+
using FluentAssertions;
8+
using Microsoft.Toolkit.Uwp.Helpers;
9+
using Microsoft.Toolkit.Uwp.UI.Controls;
10+
using Microsoft.VisualStudio.TestTools.UnitTesting;
11+
using Windows.ApplicationModel.Core;
12+
using Windows.UI.Core;
13+
14+
namespace UnitTests.UI.Controls
15+
{
16+
[TestClass]
17+
public class Test_InfiniteCanvas_Regression
18+
{
19+
[TestCategory("InfiniteCanvas")]
20+
[TestMethod]
21+
public async Task Test_InfiniteCanvas_LoadsV1File()
22+
{
23+
var taskSource = new TaskCompletionSource<object>();
24+
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
25+
CoreDispatcherPriority.Normal, async () =>
26+
{
27+
try
28+
{
29+
string json = await StorageFileHelper.ReadTextFromPackagedFileAsync(@"Assets\Samples\InfiniteCanvasExport.json");
30+
31+
InfiniteCanvasVirtualDrawingSurface.LoadJson(json).Should().NotBeEmpty();
32+
33+
taskSource.SetResult(null);
34+
}
35+
catch (Exception e)
36+
{
37+
taskSource.SetException(e);
38+
}
39+
});
40+
await taskSource.Task;
41+
}
42+
}
43+
}

UnitTests/UnitTests.UWP/UnitTests.UWP.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
<Compile Include="PrivateType.cs" />
181181
<Compile Include="Properties\AssemblyInfo.cs" />
182182
<Compile Include="Helpers\Test_WeakEventListener.cs" />
183+
<Compile Include="UI\Controls\Test_InfiniteCanvas_Regression.cs" />
183184
<Compile Include="UI\Controls\Test_TokenizingTextBox_General.cs" />
184185
<Compile Include="UI\Controls\Test_TokenizingTextBox_InterspersedCollection.cs" />
185186
<Compile Include="UI\Controls\Test_MasterDetailsView.cs" />
@@ -204,6 +205,7 @@
204205
<SubType>Designer</SubType>
205206
</AppxManifest>
206207
<None Include=".editorconfig" />
208+
<Content Include="Assets\Samples\InfiniteCanvasExport.json" />
207209
</ItemGroup>
208210
<ItemGroup>
209211
<Content Include="Assets\Samples\lorem.txt" />

0 commit comments

Comments
 (0)