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

Allow lazy initialization of webview panel #1432

Merged
merged 1 commit into from
Jun 27, 2023
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
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);
}
}
}