Skip to content

Commit

Permalink
Add descriptive title to docs and annotation panel
Browse files Browse the repository at this point in the history
  • Loading branch information
glopesdev committed May 15, 2023
1 parent 669b599 commit 896f255
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 60 deletions.
66 changes: 12 additions & 54 deletions Bonsai.Editor/EditorForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1531,55 +1531,6 @@ private void toolboxTreeView_ItemDrag(object sender, ItemDragEventArgs e)
}
}

static string GetElementName(object component)
{
var name = ExpressionBuilder.GetElementDisplayName(component);
if (component is ExternalizedProperty workflowProperty &&
!string.IsNullOrWhiteSpace(workflowProperty.Name) &&
workflowProperty.Name != workflowProperty.MemberName)
{
return name + " (" + workflowProperty.MemberName + ")";
}

var componentType = component.GetType();
if (component is BinaryOperatorBuilder binaryOperator && binaryOperator.Operand != null)
{
var operandType = binaryOperator.Operand.GetType();
if (operandType.IsGenericType) operandType = operandType.GetGenericArguments()[0];
return name + " (" + ExpressionBuilder.GetElementDisplayName(operandType) + ")";
}
else if (component is SubscribeSubject subscribeSubject && componentType.IsGenericType)
{
componentType = componentType.GetGenericArguments()[0];
if (string.IsNullOrWhiteSpace(subscribeSubject.Name))
{
name = name.Substring(0, name.IndexOf("`"));
}
return name + " (" + ExpressionBuilder.GetElementDisplayName(componentType) + ")";
}
else
{
if (component is INamedElement namedExpressionBuilder && !string.IsNullOrWhiteSpace(namedExpressionBuilder.Name))
{
name += " (" + ExpressionBuilder.GetElementDisplayName(componentType) + ")";
}

return name;
}
}

static string GetElementDescription(object component)
{
if (component is WorkflowExpressionBuilder workflowExpressionBuilder)
{
var description = workflowExpressionBuilder.Description;
if (!string.IsNullOrEmpty(description)) return description;
}

var descriptionAttribute = (DescriptionAttribute)TypeDescriptor.GetAttributes(component)[typeof(DescriptionAttribute)];
return descriptionAttribute.Description;
}

void editorControl_Enter(object sender, EventArgs e)
{
var selectedView = selectionModel.SelectedView;
Expand All @@ -1602,9 +1553,9 @@ private void selectionModel_SelectionChanged(object sender, EventArgs e)

private void GetSelectionDescription(object[] selectedObjects, out string displayName, out string description)
{
var displayNames = selectedObjects.Select(GetElementName).Distinct().Reverse().ToArray();
var displayNames = selectedObjects.Select(ElementHelper.GetElementName).Distinct().Reverse().ToArray();
displayName = string.Join(CultureInfo.CurrentCulture.TextInfo.ListSeparator + " ", displayNames);
var objectDescriptions = selectedObjects.Select(GetElementDescription).Distinct().Reverse().ToArray();
var objectDescriptions = selectedObjects.Select(ElementHelper.GetElementDescription).Distinct().Reverse().ToArray();
description = objectDescriptions.Length == 1 ? objectDescriptions[0] : string.Empty;
}

Expand Down Expand Up @@ -1645,8 +1596,8 @@ private void UpdatePropertyGrid()
var launcher = selectedView.Launcher;
if (launcher != null)
{
displayName = GetElementName(launcher.Builder);
description = GetElementDescription(launcher.Builder);
displayName = ElementHelper.GetElementName(launcher.Builder);
description = ElementHelper.GetElementDescription(launcher.Builder);
}
else
{
Expand Down Expand Up @@ -2365,7 +2316,14 @@ private async Task OpenDocumentationAsync(string assemblyName, string uid)
if (!ModifierKeys.HasFlag(Keys.Control) && editorControl.WebViewInitialized)
{
editorControl.WebView.CoreWebView2.Navigate(url.AbsoluteUri);
editorControl.ExpandWebView();
var nameSeparator = uid.LastIndexOf(ExpressionHelper.MemberSeparator);
if (nameSeparator >= 0)
{
var name = uid.Substring(nameSeparator + 1);
var categoryName = GetPackageDisplayName(uid.Substring(0, nameSeparator));
editorControl.ExpandWebView(label: $"{name} ({categoryName})");
}
else editorControl.ExpandWebView(label: uid);
}
else EditorDialog.OpenUrl(url);
}
Expand Down
57 changes: 57 additions & 0 deletions Bonsai.Editor/GraphModel/ElementHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.ComponentModel;
using Bonsai.Expressions;

namespace Bonsai.Editor.GraphModel
{
static class ElementHelper
{
public static string GetElementName(object component)
{
var name = ExpressionBuilder.GetElementDisplayName(component);
if (component is ExternalizedProperty workflowProperty &&
!string.IsNullOrWhiteSpace(workflowProperty.Name) &&
workflowProperty.Name != workflowProperty.MemberName)
{
return name + " (" + workflowProperty.MemberName + ")";
}

var componentType = component.GetType();
if (component is BinaryOperatorBuilder binaryOperator && binaryOperator.Operand != null)
{
var operandType = binaryOperator.Operand.GetType();
if (operandType.IsGenericType) operandType = operandType.GetGenericArguments()[0];
return name + " (" + ExpressionBuilder.GetElementDisplayName(operandType) + ")";
}
else if (component is SubscribeSubject subscribeSubject && componentType.IsGenericType)
{
componentType = componentType.GetGenericArguments()[0];
if (string.IsNullOrWhiteSpace(subscribeSubject.Name))
{
name = name.Substring(0, name.IndexOf("`"));
}
return name + " (" + ExpressionBuilder.GetElementDisplayName(componentType) + ")";
}
else
{
if (component is INamedElement namedExpressionBuilder && !string.IsNullOrWhiteSpace(namedExpressionBuilder.Name))
{
name += " (" + ExpressionBuilder.GetElementDisplayName(componentType) + ")";
}

return name;
}
}

public static string GetElementDescription(object component)
{
if (component is WorkflowExpressionBuilder workflowExpressionBuilder)
{
var description = workflowExpressionBuilder.Description;
if (!string.IsNullOrEmpty(description)) return description;
}

var descriptionAttribute = (DescriptionAttribute)TypeDescriptor.GetAttributes(component)[typeof(DescriptionAttribute)];
return descriptionAttribute.Description;
}
}
}
13 changes: 7 additions & 6 deletions Bonsai.Editor/GraphView/WorkflowEditorControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Bonsai.Editor.Themes;
using Microsoft.Web.WebView2.WinForms;
using Microsoft.Web.WebView2.Core;
using Bonsai.Editor.GraphModel;

namespace Bonsai.Editor.GraphView
{
Expand Down Expand Up @@ -76,16 +77,16 @@ public ExpressionBuilderGraph Workflow
set { WorkflowGraphView.Workflow = value; }
}

public void ExpandWebView()
public void ExpandWebView(ExpressionBuilder builder)
{
splitContainer.Panel1Collapsed = false;
webView.Tag = builder;
ExpandWebView(ElementHelper.GetElementName(builder));
}

public void ExpandWebView(ExpressionBuilder builder)
public void ExpandWebView(string label)
{
webView.Tag = builder;
browserLabel.Text = $"Browser ({ExpressionBuilder.GetElementDisplayName(builder)})";
ExpandWebView();
browserLabel.Text = label;
splitContainer.Panel1Collapsed = false;
}

public void CollapseWebView()
Expand Down

0 comments on commit 896f255

Please sign in to comment.