Skip to content

Commit

Permalink
Add slider control
Browse files Browse the repository at this point in the history
  • Loading branch information
glopesdev committed Mar 11, 2024
1 parent feec7a2 commit 55ea48f
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/Bonsai.Gui/SliderBuilder.cs
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;
}
}
}
30 changes: 30 additions & 0 deletions src/Bonsai.Gui/SliderVisualizer.cs
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;
}
}
}

0 comments on commit 55ea48f

Please sign in to comment.