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

Editor IO exception handling #1491

Merged
merged 2 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
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