Skip to content

Commit

Permalink
Editor IO exception handling (#1491)
Browse files Browse the repository at this point in the history
* Close input stream
Show MessageBox with exception message
Remove unused arguments
Guard all of the Editor input/output code parts and show MessageBox
with appropriate message and issue reporting link

* Extract showing messageBox into method
  • Loading branch information
stefansjfw authored Mar 9, 2020
1 parent 52e08a2 commit 5581e25
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected void OnSaveApplyTemplate(object sender, RoutedEventArgs e)
EditorOverlay mainEditor = EditorOverlay.Current;
if (mainEditor.DataContext is LayoutModel model)
{
model.Persist(mainEditor.GetZoneRects());
model.Persist();
}

_choosing = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ private void Apply_Click(object sender, RoutedEventArgs e)
{
if (model is GridLayoutModel)
{
model.Apply(mainEditor.GetZoneRects());
model.Apply();
}
else
{
model.Apply((model as CanvasLayoutModel).Zones.ToArray());
model.Apply();
}

Close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,46 +117,53 @@ public override LayoutModel Clone()
// Implements the LayoutModel.PersistData abstract method
protected override void PersistData()
{
FileStream outputStream = File.Open(Settings.AppliedZoneSetTmpFile, FileMode.Create);
JsonWriterOptions writerOptions = new JsonWriterOptions
try
{
SkipValidation = true,
};
using (var writer = new Utf8JsonWriter(outputStream, writerOptions))
{
writer.WriteStartObject();
writer.WriteString("uuid", "{" + Guid.ToString().ToUpper() + "}");
writer.WriteString("name", Name);
FileStream outputStream = File.Open(Settings.AppliedZoneSetTmpFile, FileMode.Create);
JsonWriterOptions writerOptions = new JsonWriterOptions
{
SkipValidation = true,
};
using (var writer = new Utf8JsonWriter(outputStream, writerOptions))
{
writer.WriteStartObject();
writer.WriteString("uuid", "{" + Guid.ToString().ToUpper() + "}");
writer.WriteString("name", Name);

writer.WriteString("type", "canvas");
writer.WriteString("type", "canvas");

writer.WriteStartObject("info");
writer.WriteStartObject("info");

writer.WriteNumber("ref-width", _referenceWidth);
writer.WriteNumber("ref-height", _referenceHeight);
writer.WriteNumber("ref-width", _referenceWidth);
writer.WriteNumber("ref-height", _referenceHeight);

writer.WriteStartArray("zones");
foreach (Int32Rect rect in Zones)
{
writer.WriteStartObject();
writer.WriteNumber("X", rect.X);
writer.WriteNumber("Y", rect.Y);
writer.WriteNumber("width", rect.Width);
writer.WriteNumber("height", rect.Height);
writer.WriteEndObject();
}
writer.WriteStartArray("zones");
foreach (Int32Rect rect in Zones)
{
writer.WriteStartObject();
writer.WriteNumber("X", rect.X);
writer.WriteNumber("Y", rect.Y);
writer.WriteNumber("width", rect.Width);
writer.WriteNumber("height", rect.Height);
writer.WriteEndObject();
}

writer.WriteEndArray();
writer.WriteEndArray();

// end info object
writer.WriteEndObject();
// end info object
writer.WriteEndObject();

// end root object
writer.WriteEndObject();
writer.Flush();
}
// end root object
writer.WriteEndObject();
writer.Flush();
}

outputStream.Close();
outputStream.Close();
}
catch (Exception ex)
{
ShowExceptionMessageBox("Error persisting canvas layout", ex);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Windows;

namespace FancyZonesEditor.Models
{
Expand Down Expand Up @@ -170,59 +171,66 @@ public override LayoutModel Clone()
// Implements the LayoutModel.PersistData abstract method
protected override void PersistData()
{
FileStream outputStream = File.Open(Settings.AppliedZoneSetTmpFile, FileMode.Create);
using (var writer = new Utf8JsonWriter(outputStream, options: default))
try
{
writer.WriteStartObject();
writer.WriteString("uuid", "{" + Guid.ToString().ToUpper() + "}");
writer.WriteString("name", Name);
FileStream outputStream = File.Open(Settings.AppliedZoneSetTmpFile, FileMode.Create);
using (var writer = new Utf8JsonWriter(outputStream, options: default))
{
writer.WriteStartObject();
writer.WriteString("uuid", "{" + Guid.ToString().ToUpper() + "}");
writer.WriteString("name", Name);

writer.WriteString("type", "grid");
writer.WriteString("type", "grid");

writer.WriteStartObject("info");
writer.WriteStartObject("info");

writer.WriteNumber("rows", Rows);
writer.WriteNumber("columns", Columns);
writer.WriteNumber("rows", Rows);
writer.WriteNumber("columns", Columns);

writer.WriteStartArray("rows-percentage");
for (int row = 0; row < Rows; row++)
{
writer.WriteNumberValue(RowPercents[row]);
}
writer.WriteStartArray("rows-percentage");
for (int row = 0; row < Rows; row++)
{
writer.WriteNumberValue(RowPercents[row]);
}

writer.WriteEndArray();
writer.WriteEndArray();

writer.WriteStartArray("columns-percentage");
for (int col = 0; col < Columns; col++)
{
writer.WriteNumberValue(ColumnPercents[col]);
}
writer.WriteStartArray("columns-percentage");
for (int col = 0; col < Columns; col++)
{
writer.WriteNumberValue(ColumnPercents[col]);
}

writer.WriteEndArray();
writer.WriteEndArray();

writer.WriteStartArray("cell-child-map");
for (int row = 0; row < Rows; row++)
{
writer.WriteStartArray();
for (int col = 0; col < Columns; col++)
writer.WriteStartArray("cell-child-map");
for (int row = 0; row < Rows; row++)
{
writer.WriteNumberValue(CellChildMap[row, col]);
writer.WriteStartArray();
for (int col = 0; col < Columns; col++)
{
writer.WriteNumberValue(CellChildMap[row, col]);
}

writer.WriteEndArray();
}

writer.WriteEndArray();
}

writer.WriteEndArray();
// end info object
writer.WriteEndObject();

// end info object
writer.WriteEndObject();
// end root object
writer.WriteEndObject();
writer.Flush();
}

// end root object
writer.WriteEndObject();
writer.Flush();
outputStream.Close();
}
catch (Exception ex)
{
ShowExceptionMessageBox("Error persisting grid layout", ex);
}

outputStream.Close();
}
}
}
Loading

0 comments on commit 5581e25

Please sign in to comment.