Skip to content

Commit

Permalink
Merge pull request #6 from bonsai-rx/feature-dev
Browse files Browse the repository at this point in the history
Add common button controls
  • Loading branch information
glopesdev authored Feb 5, 2024
2 parents 78d976c + 31c0d6f commit eb451f8
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Bonsai.Gui/ButtonBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.ComponentModel;
using System.Reactive.Subjects;

namespace Bonsai.Gui
{
/// <summary>
/// Represents an operator that interfaces with a button control and generates
/// a sequence of notifications whenever the button is clicked.
/// </summary>
[TypeVisualizer(typeof(ButtonVisualizer))]
[Description("Interfaces with a button control and generates a sequence of notifications whenever the button is clicked.")]
public class ButtonBuilder : ButtonBuilderBase<string>
{
internal readonly Subject<string> _Click = new();

/// <inheritdoc/>
protected override IObservable<string> Generate()
{
return _Click;
}
}
}
26 changes: 26 additions & 0 deletions src/Bonsai.Gui/ButtonBuilderBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.ComponentModel;
using System.Reactive.Subjects;

namespace Bonsai.Gui
{
/// <summary>
/// Provides an abstract base class with common button control functionality.
/// </summary>
/// <inheritdoc/>
[DefaultProperty(nameof(Text))]
public abstract class ButtonBuilderBase<TEvent> : ControlBuilderBase<TEvent>
{
internal readonly BehaviorSubject<string> _Text = new(string.Empty);

/// <summary>
/// Gets or sets the text associated with this control.
/// </summary>
[Category(nameof(CategoryAttribute.Appearance))]
[Description("The text associated with this control.")]
public string Text
{
get => _Text.Value;
set => _Text.OnNext(value);
}
}
}
26 changes: 26 additions & 0 deletions src/Bonsai.Gui/ButtonVisualizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Bonsai.Gui
{
/// <summary>
/// Provides a type visualizer representing a button control.
/// </summary>
public class ButtonVisualizer : ControlVisualizerBase<Button, ButtonBuilder>
{
/// <inheritdoc/>
protected override Button CreateControl(IServiceProvider provider, ButtonBuilder builder)
{
var button = new Button();
button.Dock = DockStyle.Fill;
button.Size = new Size(300, 150);
button.SubscribeTo(builder._Text, value => button.Text = value);
button.Click += (sender, e) =>
{
builder._Click.OnNext(button.Name);
};
return button;
}
}
}
30 changes: 30 additions & 0 deletions src/Bonsai.Gui/CheckBoxBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.ComponentModel;
using System.Reactive.Subjects;

namespace Bonsai.Gui
{
/// <summary>
/// Represents an operator that interfaces with a check box control and generates
/// a sequence of notifications whenever the checked status changes.
/// </summary>
[TypeVisualizer(typeof(CheckBoxVisualizer))]
[Description("Interfaces with a check box control and generates a sequence of notifications whenever the checked status changes.")]
public class CheckBoxBuilder : ButtonBuilderBase<bool>
{
internal readonly Subject<bool> _CheckedChanged = new();

/// <summary>
/// Gets or sets a value specifying the initial state of the check box.
/// </summary>
[Category(nameof(CategoryAttribute.Appearance))]
[Description("Specifies the initial state of the check box.")]
public bool Checked { get; set; }

/// <inheritdoc/>
protected override IObservable<bool> Generate()
{
return _CheckedChanged;
}
}
}
27 changes: 27 additions & 0 deletions src/Bonsai.Gui/CheckBoxVisualizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Bonsai.Gui
{
/// <summary>
/// Provides a type visualizer representing a check box control.
/// </summary>
public class CheckBoxVisualizer : ControlVisualizerBase<CheckBox, CheckBoxBuilder>
{
/// <inheritdoc/>
protected override CheckBox CreateControl(IServiceProvider provider, CheckBoxBuilder builder)
{
var checkBox = new CheckBox();
checkBox.Dock = DockStyle.Fill;
checkBox.Size = new Size(300, 75);
checkBox.Checked = builder.Checked;
checkBox.SubscribeTo(builder._Text, value => checkBox.Text = value);
checkBox.CheckedChanged += (sender, e) =>
{
builder._CheckedChanged.OnNext(checkBox.Checked);
};
return checkBox;
}
}
}
30 changes: 30 additions & 0 deletions src/Bonsai.Gui/RadioButtonBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.ComponentModel;
using System.Reactive.Subjects;

namespace Bonsai.Gui
{
/// <summary>
/// Represents an operator that interfaces with a radio button control and generates
/// a sequence of notifications whenever the checked status changes.
/// </summary>
[TypeVisualizer(typeof(RadioButtonVisualizer))]
[Description("Interfaces with a radio button control and generates a sequence of notifications whenever the checked status changes.")]
public class RadioButtonBuilder : ButtonBuilderBase<bool>
{
internal readonly Subject<bool> _CheckedChanged = new();

/// <summary>
/// Gets or sets a value specifying the initial state of the radio button.
/// </summary>
[Category(nameof(CategoryAttribute.Appearance))]
[Description("Specifies the initial state of the radio button.")]
public bool Checked { get; set; }

/// <inheritdoc/>
protected override IObservable<bool> Generate()
{
return _CheckedChanged;
}
}
}
27 changes: 27 additions & 0 deletions src/Bonsai.Gui/RadioButtonVisualizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Bonsai.Gui
{
/// <summary>
/// Provides a type visualizer representing a radio button control.
/// </summary>
public class RadioButtonVisualizer : ControlVisualizerBase<RadioButton, RadioButtonBuilder>
{
/// <inheritdoc/>
protected override RadioButton CreateControl(IServiceProvider provider, RadioButtonBuilder builder)
{
var radioButton = new RadioButton();
radioButton.Dock = DockStyle.Fill;
radioButton.Size = new Size(300, 75);
radioButton.Checked = builder.Checked;
radioButton.SubscribeTo(builder._Text, value => radioButton.Text = value);
radioButton.CheckedChanged += (sender, e) =>
{
builder._CheckedChanged.OnNext(radioButton.Checked);
};
return radioButton;
}
}
}
23 changes: 23 additions & 0 deletions src/Bonsai.Gui/ToggleButtonBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.ComponentModel;
using System.Reactive.Subjects;

namespace Bonsai.Gui
{
/// <summary>
/// Represents an operator that interfaces with a toggle button control and generates
/// a sequence of notifications whenever the toggle status changes.
/// </summary>
[TypeVisualizer(typeof(ToggleButtonVisualizer))]
[Description("Interfaces with a toggle button control and generates a sequence of notifications whenever the toggle status changes.")]
public class ToggleButtonBuilder : ButtonBuilderBase<bool>
{
internal readonly Subject<bool> _CheckedChanged = new();

/// <inheritdoc/>
protected override IObservable<bool> Generate()
{
return _CheckedChanged;
}
}
}
28 changes: 28 additions & 0 deletions src/Bonsai.Gui/ToggleButtonVisualizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Bonsai.Gui
{
/// <summary>
/// Provides a type visualizer representing a toggle button control.
/// </summary>
public class ToggleButtonVisualizer : ControlVisualizerBase<CheckBox, ToggleButtonBuilder>
{
/// <inheritdoc/>
protected override CheckBox CreateControl(IServiceProvider provider, ToggleButtonBuilder builder)
{
var checkBox = new CheckBox();
checkBox.Dock = DockStyle.Fill;
checkBox.Size = new Size(300, 150);
checkBox.Appearance = Appearance.Button;
checkBox.TextAlign = ContentAlignment.MiddleCenter;
checkBox.SubscribeTo(builder._Text, value => checkBox.Text = value);
checkBox.CheckedChanged += (sender, e) =>
{
builder._CheckedChanged.OnNext(checkBox.Checked);
};
return checkBox;
}
}
}

0 comments on commit eb451f8

Please sign in to comment.