Skip to content

Commit

Permalink
Allow lazy initialization of webview panel
Browse files Browse the repository at this point in the history
  • Loading branch information
glopesdev committed Jun 27, 2023
1 parent e886870 commit 5772c02
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Bonsai.Editor/EditorForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2336,7 +2336,7 @@ private async Task OpenDocumentationAsync(string assemblyName, string uid)
var editorControl = selectionModel.SelectedView.EditorControl;
var url = await documentationProvider.GetDocumentationAsync(assemblyName, uid);
if (!ModifierKeys.HasFlag(Keys.Control) &&
editorControl.AnnotationPanel.WebViewInitialized)
editorControl.AnnotationPanel.HasWebView)
{
editorControl.AnnotationPanel.Navigate(url.AbsoluteUri);
var nameSeparator = uid.LastIndexOf(ExpressionHelper.MemberSeparator);
Expand Down
22 changes: 14 additions & 8 deletions Bonsai.Editor/GraphView/AnnotationPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class AnnotationPanel : UserControl
readonly RichTextBox textBox;
readonly WebView2 webView;
bool webViewInitialized;
Action onInitialize;

public AnnotationPanel()
{
Expand Down Expand Up @@ -46,7 +47,6 @@ public AnnotationPanel()
webView.Margin = new Padding(2);
webView.Size = new System.Drawing.Size(296, 70);
webView.ZoomFactor = 1D;
webView.EnsureCoreWebView2Async();
webView.CoreWebView2InitializationCompleted += (sender, e) =>
{
webViewInitialized = true;
Expand All @@ -64,21 +64,19 @@ public AnnotationPanel()
{
OnTextChanged(EventArgs.Empty);
}

onInitialize?.Invoke();
onInitialize = null;
};
Controls.Add(webView);
}
}

public ThemeRenderer ThemeRenderer { get; set; }

public WebView2 WebView
{
get { return webView; }
}

public bool WebViewInitialized
public bool HasWebView
{
get { return webViewInitialized; }
get { return webView != null; }
}

public event LinkClickedEventHandler LinkClicked
Expand All @@ -104,6 +102,7 @@ public void NavigateToString(string text)
var html = MarkdownConvert.ToHtml(Font, text);
webView.NavigateToString(html);
}
else onInitialize = () => NavigateToString(text);
}

public void Navigate(string uri)
Expand All @@ -112,6 +111,7 @@ public void Navigate(string uri)
{
webView.CoreWebView2.Navigate(uri);
}
else onInitialize = () => Navigate(uri);
}

internal void InitializeTheme()
Expand Down Expand Up @@ -175,5 +175,11 @@ private void CoreWebView2_WebResourceRequested(object sender, CoreWebView2WebRes
}
}
}

protected override void OnLoad(EventArgs e)
{
webView.EnsureCoreWebView2Async();
base.OnLoad(e);
}
}
}

0 comments on commit 5772c02

Please sign in to comment.