Skip to content

Commit

Permalink
[File Explorer] OneDrive SVG fix (#8926)
Browse files Browse the repository at this point in the history
* Move SVG file reading out of the UI thread

* Also move Blocked-check out of the UI thread
  • Loading branch information
ivan100sic committed Jan 5, 2021
1 parent 93bdb81 commit d4a4203
Showing 1 changed file with 39 additions and 14 deletions.
53 changes: 39 additions & 14 deletions src/modules/previewpane/SvgPreviewHandler/SvgPreviewControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,37 @@ public class SvgPreviewControl : FormHandlerControl
/// <param name="dataSource">Stream reference to access source file.</param>
public override void DoPreview<T>(T dataSource)
{
string svgData = null;
bool blocked = false;

try
{
using (var stream = new ReadonlyStream(dataSource as IStream))
{
using (var reader = new StreamReader(stream))
{
svgData = reader.ReadToEnd();
}
}

blocked = SvgPreviewHandlerHelper.CheckBlockedElements(svgData);
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
{
PreviewError(ex, dataSource);
return;
}

InvokeOnControlThread(() =>
{
try
{
_infoBarAdded = false;
string svgData = null;
using (var stream = new ReadonlyStream(dataSource as IStream))
{
using (var reader = new StreamReader(stream))
{
svgData = reader.ReadToEnd();
}
}
// Add a info bar on top of the Preview if any blocked element is present.
if (SvgPreviewHandlerHelper.CheckBlockedElements(svgData))
if (blocked)
{
_infoBarAdded = true;
AddTextBoxControl(Resource.BlockedElementInfoText);
Expand All @@ -72,11 +87,7 @@ public override void DoPreview<T>(T dataSource)
catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
{
PowerToysTelemetry.Log.WriteEvent(new SvgFilePreviewError { Message = ex.Message });
Controls.Clear();
_infoBarAdded = true;
AddTextBoxControl(Resource.SvgNotPreviewedError);
base.DoPreview(dataSource);
PreviewError(ex, dataSource);
}
});
}
Expand Down Expand Up @@ -138,5 +149,19 @@ private void AddTextBoxControl(string message)
_textBox.BorderStyle = BorderStyle.None;
Controls.Add(_textBox);
}

/// <summary>
/// Called when an error occurs during preview.
/// </summary>
/// <param name="exception">The exception which occurred.</param>
/// <param name="dataSource">Stream reference to access source file.</param>
private void PreviewError<T>(Exception exception, T dataSource)
{
PowerToysTelemetry.Log.WriteEvent(new SvgFilePreviewError { Message = exception.Message });
Controls.Clear();
_infoBarAdded = true;
AddTextBoxControl(Resource.SvgNotPreviewedError);
base.DoPreview(dataSource);
}
}
}

0 comments on commit d4a4203

Please sign in to comment.