Skip to content

Commit

Permalink
Alert Notes WebView2 error fix
Browse files Browse the repository at this point in the history
Fix System.IO.FileNotFoundException exception that occurs when editing alert notes if WebView2 is not installed.  WebView2 is used for rendering the html generated from the markdown.  There is an existing wrapper control used for job timeline that will prompt the user to install the runtime.
#1183
  • Loading branch information
DavidWiseman committed Jan 21, 2025
1 parent 989c2a3 commit 858af5d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
6 changes: 4 additions & 2 deletions DBADashGUI/AgentJobs/WebView2Wrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ private void Setup_Completed(object sender, EventArgs e)
SetupCompleted?.Invoke();
}

public async Task NavigateToLargeString(string html)
public async Task<bool> NavigateToLargeString(string html)
{
try
{
await EnsureCoreWebView2WrapperAsync();
}
catch (Exception)
{
return;
return false;
}
try
{
Expand All @@ -91,6 +91,8 @@ public async Task NavigateToLargeString(string html)
{
LoadHTMLFromDisk(ex, html); // NavigateToString might fail if size exceeds 2MB. Try loading from disk instead.
}

return true;
}

// <summary>
Expand Down
40 changes: 22 additions & 18 deletions DBADashGUI/SchemaCompare/CodeEditorForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions DBADashGUI/SchemaCompare/CodeEditorForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ public CodeEditor.CodeEditorModes Syntax
codeEditor1.Mode = value;
splitContainer1.Panel2Collapsed = value != CodeEditor.CodeEditorModes.Markdown;
tsMarkdownView.Visible = value == CodeEditor.CodeEditorModes.Markdown;
ShowMarkdownPreview();
_ = ShowMarkdownPreview();
}
}

private void ShowMarkdownPreview()
private async Task ShowMarkdownPreview()
{
if (codeEditor1.Mode != CodeEditor.CodeEditorModes.Markdown) return;
if (!isWebViewInit) return;
lblError.Visible = false;
try
{
var pipeline = new MarkdownPipelineBuilder()
Expand All @@ -56,12 +57,16 @@ private void ShowMarkdownPreview()
.Build();

var html = Markdig.Markdown.ToHtml(codeEditor1.txtCode.Text, pipeline);
webView.NavigateToString(html);
ToggleMarkdown(EditEnabled, true);

var success = await webView2Wrapper.NavigateToLargeString(html);
ToggleMarkdown(EditEnabled | !success, true);
}
catch (Exception ex)
{
webView.NavigateToString(ex.Message);
ToggleMarkdown(true, false);
lblError.Text = "Error loading markdown preview";
lblError.ToolTipText = ex.Message;
lblError.Visible = true;
}
}

Expand Down Expand Up @@ -90,9 +95,8 @@ public CodeEditorForm()

private async void InitializeAsync()
{
await webView.EnsureCoreWebView2Async(null);
isWebViewInit = true;
ShowMarkdownPreview();
await ShowMarkdownPreview();
}

private void TsLineNumbers_Click(object sender, EventArgs e)
Expand Down

0 comments on commit 858af5d

Please sign in to comment.