Skip to content

Commit

Permalink
Merge pull request #9 from bonsai-rx/feature-dev
Browse files Browse the repository at this point in the history
Add label and text box controls
  • Loading branch information
glopesdev authored Feb 16, 2024
2 parents 0fb8df2 + ac0ff9d commit 5070e6e
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Bonsai.Gui/LabelBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.ComponentModel;

namespace Bonsai.Gui
{
/// <summary>
/// Represents an operator that interfaces with a label control.
/// </summary>
[TypeVisualizer(typeof(LabelVisualizer))]
[Description("Interfaces with a label control.")]
public class LabelBuilder : TextControlBuilderBase
{
}
}
21 changes: 21 additions & 0 deletions src/Bonsai.Gui/LabelVisualizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Bonsai.Gui
{
/// <summary>
/// Provides a type visualizer representing a label control.
/// </summary>
public class LabelVisualizer : ControlVisualizerBase<Label, LabelBuilder>
{
/// <inheritdoc/>
protected override Label CreateControl(IServiceProvider provider, LabelBuilder builder)
{
var label = new Label();
label.Dock = DockStyle.Fill;
label.Size = new Size(300, label.Height);
return label;
}
}
}
39 changes: 39 additions & 0 deletions src/Bonsai.Gui/TextBoxBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.ComponentModel;
using System.Reactive.Subjects;

namespace Bonsai.Gui
{
/// <summary>
/// Represents an operator that interfaces with a text box control and generates
/// a sequence of notifications whenever the text changes.
/// </summary>
[TypeVisualizer(typeof(TextBoxVisualizer))]
public class TextBoxBuilder : TextControlBuilderBase<string>
{
internal readonly BehaviorSubject<bool> _Multiline = new(true);

/// <summary>
/// Gets or sets a value specifying whether the text box is multiline.
/// </summary>
[Description("Specifies whether the text box is multiline.")]
public bool Multiline
{
get => _Multiline.Value;
set => _Multiline.OnNext(value);
}

/// <summary>
/// Generates an observable sequence of values containing the contents
/// of the text box whenever the text changes.
/// </summary>
/// <returns>
/// A sequence of <see cref="string"/> values representing the contents
/// of the text box.
/// </returns>
protected override IObservable<string> Generate()
{
return _Text;
}
}
}
32 changes: 32 additions & 0 deletions src/Bonsai.Gui/TextBoxVisualizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Bonsai.Gui
{
/// <summary>
/// Provides a type visualizer representing a text box control.
/// </summary>
public class TextBoxVisualizer : ControlVisualizerBase<TextBox, TextBoxBuilder>
{
/// <inheritdoc/>
protected override TextBox CreateControl(IServiceProvider provider, TextBoxBuilder builder)
{
var textBox = new TextBox();
textBox.Dock = DockStyle.Fill;
textBox.Multiline = builder._Multiline.Value;
if (textBox.Multiline)
{
textBox.Size = new Size(320, 240);
}

textBox.Text = builder._Text.Value;
textBox.SubscribeTo(builder._Multiline, value => textBox.Multiline = value);
textBox.TextChanged += (sender, e) =>
{
builder._Text.OnNext(textBox.Text);
};
return textBox;
}
}
}

0 comments on commit 5070e6e

Please sign in to comment.