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

Add support for adding rich annotations to the workflow #1311

Merged
merged 19 commits into from
Mar 31, 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.Core/Bonsai.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<PackageTags>Bonsai Rx Reactive Extensions</PackageTags>
<TargetFrameworks>net462;netstandard2.0;net6.0</TargetFrameworks>
<RootNamespace>Bonsai</RootNamespace>
<VersionPrefix>2.7.3</VersionPrefix>
<VersionPrefix>2.8.0</VersionPrefix>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net462'">
<PackageReference Include="Rx-Linq" Version="2.2.5" />
Expand Down
66 changes: 66 additions & 0 deletions Bonsai.Core/Expressions/AnnotationBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Xml;
using System.Xml.Serialization;
using Bonsai.Dag;

namespace Bonsai.Expressions
{
/// <summary>
/// Represents a written explanation or critical comment added to the workflow.
/// </summary>
[DefaultProperty(nameof(Name))]
[WorkflowElementCategory(ElementCategory.Property)]
[XmlType("Annotation", Namespace = Constants.XmlNamespace)]
[Editor("Bonsai.Design.AnnotationBuilderEditor, Bonsai.Design", typeof(ComponentEditor))]
[Description("Represents a written explanation or critical comment added to the workflow.")]
public class AnnotationBuilder : ExpressionBuilder, INamedElement, IArgumentBuilder
{
static readonly XmlDocument cdataFactory = new XmlDocument();
static readonly Range<int> argumentRange = Range.Create(lowerBound: 0, upperBound: 0);

/// <summary>
/// Gets or sets the name of the annotation node in the workflow.
/// </summary>
[Externalizable(false)]
[Category(nameof(CategoryAttribute.Design))]
[Description("The name of the annotation node in the workflow.")]
public string Name { get; set; }

/// <summary>
/// Gets or sets the text associated with this annotation.
/// </summary>
[XmlIgnore]
[Externalizable(false)]
[Category(nameof(CategoryAttribute.Design))]
[Description("The text associated with this annotation.")]
public string Text { get; set; }

/// <summary>
/// Gets or sets a CDATA section representing the annotation for serialization.
/// </summary>
[Browsable(false)]
[XmlElement(nameof(Text))]
public XmlCDataSection TextCData
{
get { return cdataFactory.CreateCDataSection(Text); }
set { Text = value?.Data; }
}

/// <inheritdoc/>
public override Range<int> ArgumentRange => argumentRange;

/// <inheritdoc/>
public override Expression Build(IEnumerable<Expression> arguments)
{
return EmptyExpression.Instance;
}

bool IArgumentBuilder.BuildArgument(Expression source, Edge<ExpressionBuilder, ExpressionBuilderArgument> successor, out Expression argument)
{
argument = source;
return false;
}
}
}
35 changes: 35 additions & 0 deletions Bonsai.Design/AnnotationBuilderEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.ComponentModel;
using System.Windows.Forms;
using Bonsai.Expressions;

namespace Bonsai.Design
{
/// <summary>
/// Provides a user interface editor that displays a dialog box for editing
/// a workflow annotation.
/// </summary>
public class AnnotationBuilderEditor : WorkflowComponentEditor
{
/// <inheritdoc/>
public override bool EditComponent(ITypeDescriptorContext context, object component, IServiceProvider provider, IWin32Window owner)
{
if (provider != null)
{
var editorState = (IWorkflowEditorState)provider.GetService(typeof(IWorkflowEditorState));
if (editorState != null && !editorState.WorkflowRunning && component is AnnotationBuilder annotationBuilder)
{
using var editorDialog = new AnnotationBuilderEditorDialog();
editorDialog.Annotation = annotationBuilder.Text;
if (editorDialog.ShowDialog(owner) == DialogResult.OK)
{
annotationBuilder.Text = editorDialog.Annotation;
}
return true;
}
}

return false;
}
}
}
96 changes: 96 additions & 0 deletions Bonsai.Design/AnnotationBuilderEditorDialog.Designer.cs

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

88 changes: 88 additions & 0 deletions Bonsai.Design/AnnotationBuilderEditorDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using ScintillaNET;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Bonsai.Design
{
internal partial class AnnotationBuilderEditorDialog : Form
{
public AnnotationBuilderEditorDialog()
{
InitializeComponent();
scintilla.StyleResetDefault();
scintilla.Styles[Style.Default].Font = "Consolas";
scintilla.Styles[Style.Default].Size = 10;
scintilla.StyleClearAll();

scintilla.CaretLineBackColor = ColorTranslator.FromHtml("#feefff");
scintilla.Styles[Style.Markdown.Default].ForeColor = Color.Black;
scintilla.Styles[Style.Markdown.Link].Underline = true;
scintilla.Styles[Style.Markdown.Em1].Italic = true;
scintilla.Styles[Style.Markdown.Em2].Italic = true;
scintilla.Styles[Style.Markdown.Strong1].Bold = true;
scintilla.Styles[Style.Markdown.Strong2].Bold = true;
scintilla.Styles[Style.Markdown.Header1].Bold = true;
scintilla.Styles[Style.Markdown.Header2].Bold = true;
scintilla.Styles[Style.Markdown.Header3].Bold = true;
scintilla.Styles[Style.Markdown.Header4].Bold = true;
scintilla.Styles[Style.Markdown.Header5].Bold = true;
scintilla.Styles[Style.Markdown.Header6].Bold = true;
scintilla.Styles[Style.Markdown.Strong1].ForeColor = ColorTranslator.FromHtml("#2b91af");
scintilla.Styles[Style.Markdown.Strong2].ForeColor = ColorTranslator.FromHtml("#2b91af");
scintilla.Styles[Style.Markdown.Header1].ForeColor = ColorTranslator.FromHtml("#2b91af");
scintilla.Styles[Style.Markdown.Header2].ForeColor = ColorTranslator.FromHtml("#2b91af");
scintilla.Styles[Style.Markdown.Header3].ForeColor = ColorTranslator.FromHtml("#2b91af");
scintilla.Styles[Style.Markdown.Header4].ForeColor = ColorTranslator.FromHtml("#2b91af");
scintilla.Styles[Style.Markdown.Header5].ForeColor = ColorTranslator.FromHtml("#2b91af");
scintilla.Styles[Style.Markdown.Header6].ForeColor = ColorTranslator.FromHtml("#2b91af");
scintilla.Styles[Style.Markdown.UListItem].ForeColor = ColorTranslator.FromHtml("#2b91af");
scintilla.Styles[Style.Markdown.OListItem].ForeColor = ColorTranslator.FromHtml("#2b91af");
scintilla.Styles[Style.Markdown.HRule].ForeColor = ColorTranslator.FromHtml("#2b91af");
scintilla.Styles[Style.Markdown.Code].ForeColor = ColorTranslator.FromHtml("#a31515");
scintilla.Styles[Style.Markdown.Code2].ForeColor = ColorTranslator.FromHtml("#a31515");
scintilla.Styles[Style.Markdown.CodeBk].ForeColor = ColorTranslator.FromHtml("#a31515");
scintilla.Styles[Style.Markdown.BlockQuote].ForeColor = ColorTranslator.FromHtml("#a31515");
scintilla.Lexer = Lexer.Markdown;
}

public string Annotation { get; set; }

protected override void OnLoad(EventArgs e)
{
scintilla.Text = Annotation;
scintilla.EmptyUndoBuffer();
if (Owner != null)
{
Icon = Owner.Icon;
ShowIcon = true;
}

base.OnLoad(e);
}

protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape && !e.Handled)
{
Close();
e.Handled = true;
}

base.OnKeyDown(e);
}

private void scintilla_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.Control)
{
okButton.PerformClick();
}
}

private void scintilla_TextChanged(object sender, EventArgs e)
{
Annotation = scintilla.Text;
}
}
}
Loading