-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from bonsai-rx/feature-dev
Add slider control
- Loading branch information
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Reactive.Subjects; | ||
|
||
namespace Bonsai.Gui | ||
{ | ||
/// <summary> | ||
/// Represents an operator that interfaces with a slider control and generates | ||
/// a sequence of notifications whenever the slider value changes. | ||
/// </summary> | ||
[DefaultProperty(nameof(Value))] | ||
[TypeVisualizer(typeof(SliderVisualizer))] | ||
[Description("Interfaces with a slider control and generates a sequence of notifications whenever the slider value changes.")] | ||
public class SliderBuilder : ControlBuilderBase<double> | ||
{ | ||
internal readonly BehaviorSubject<double> _Minimum = new(0); | ||
internal readonly BehaviorSubject<double> _Maximum = new(100); | ||
internal readonly BehaviorSubject<int?> _DecimalPlaces = new(null); | ||
internal readonly BehaviorSubject<double> _Value = new(0); | ||
|
||
/// <summary> | ||
/// Gets or sets the lower limit of values in the slider. | ||
/// </summary> | ||
[Description("The lower limit of values in the slider.")] | ||
public double Minimum | ||
{ | ||
get => _Minimum.Value; | ||
set => _Minimum.OnNext(value); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the upper limit of values in the slider. | ||
/// </summary> | ||
[Description("The upper limit of values in the slider.")] | ||
public double Maximum | ||
{ | ||
get => _Maximum.Value; | ||
set => _Maximum.OnNext(value); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the optional maximum number of decimal places | ||
/// used when formatting the numeric display of the slider. | ||
/// </summary> | ||
[Category(nameof(CategoryAttribute.Appearance))] | ||
[Description("The optional maximum number of decimal places used when formatting the numeric display of the slider.")] | ||
public int? DecimalPlaces | ||
{ | ||
get => _DecimalPlaces.Value; | ||
set => _DecimalPlaces.OnNext(value); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets a numeric value which represents the position of the slider. | ||
/// </summary> | ||
[Description("The numeric value which represents the position of the slider.")] | ||
public double Value | ||
{ | ||
get { return _Value.Value; } | ||
set { _Value.OnNext(value); } | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override IObservable<double> Generate() | ||
{ | ||
return _Value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Drawing; | ||
using System.Windows.Forms; | ||
using Bonsai.Design; | ||
|
||
namespace Bonsai.Gui | ||
{ | ||
/// <summary> | ||
/// Provides a type visualizer representing a slider control. | ||
/// </summary> | ||
public class SliderVisualizer : ControlVisualizerBase<Slider, SliderBuilder> | ||
{ | ||
/// <inheritdoc/> | ||
protected override Slider CreateControl(IServiceProvider provider, SliderBuilder builder) | ||
{ | ||
var slider = new Slider(); | ||
slider.Dock = DockStyle.Fill; | ||
slider.Size = new Size(300, slider.Height); | ||
slider.SubscribeTo(builder._Minimum, value => slider.Minimum = value); | ||
slider.SubscribeTo(builder._Maximum, value => slider.Maximum = value); | ||
slider.SubscribeTo(builder._DecimalPlaces, value => slider.DecimalPlaces = value); | ||
slider.SubscribeTo(builder._Value, value => slider.Value = builder.Value); | ||
slider.ValueChanged += (sender, e) => | ||
{ | ||
builder._Value.OnNext(slider.Value); | ||
}; | ||
return slider; | ||
} | ||
} | ||
} |