From 517d51f1f1ad4ac9f0b7240ff51a736665765cc3 Mon Sep 17 00:00:00 2001 From: glopesdev Date: Fri, 16 Feb 2024 00:18:52 +0000 Subject: [PATCH 1/2] Add label and text box controls --- src/Bonsai.Gui/LabelBuilder.cs | 13 ++++++++++ src/Bonsai.Gui/LabelVisualizer.cs | 21 ++++++++++++++++ src/Bonsai.Gui/TextBoxBuilder.cs | 39 +++++++++++++++++++++++++++++ src/Bonsai.Gui/TextBoxVisualizer.cs | 31 +++++++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 src/Bonsai.Gui/LabelBuilder.cs create mode 100644 src/Bonsai.Gui/LabelVisualizer.cs create mode 100644 src/Bonsai.Gui/TextBoxBuilder.cs create mode 100644 src/Bonsai.Gui/TextBoxVisualizer.cs diff --git a/src/Bonsai.Gui/LabelBuilder.cs b/src/Bonsai.Gui/LabelBuilder.cs new file mode 100644 index 0000000..af64802 --- /dev/null +++ b/src/Bonsai.Gui/LabelBuilder.cs @@ -0,0 +1,13 @@ +using System.ComponentModel; + +namespace Bonsai.Gui +{ + /// + /// Represents an operator that interfaces with a label control. + /// + [TypeVisualizer(typeof(LabelVisualizer))] + [Description("Interfaces with a label control.")] + public class LabelBuilder : TextControlBuilderBase + { + } +} diff --git a/src/Bonsai.Gui/LabelVisualizer.cs b/src/Bonsai.Gui/LabelVisualizer.cs new file mode 100644 index 0000000..95a6fb1 --- /dev/null +++ b/src/Bonsai.Gui/LabelVisualizer.cs @@ -0,0 +1,21 @@ +using System; +using System.Drawing; +using System.Windows.Forms; + +namespace Bonsai.Gui +{ + /// + /// Provides a type visualizer representing a label control. + /// + public class LabelVisualizer : ControlVisualizerBase + { + /// + 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; + } + } +} diff --git a/src/Bonsai.Gui/TextBoxBuilder.cs b/src/Bonsai.Gui/TextBoxBuilder.cs new file mode 100644 index 0000000..a0d89d6 --- /dev/null +++ b/src/Bonsai.Gui/TextBoxBuilder.cs @@ -0,0 +1,39 @@ +using System; +using System.ComponentModel; +using System.Reactive.Subjects; + +namespace Bonsai.Gui +{ + /// + /// Represents an operator that interfaces with a text box control and generates + /// a sequence of notifications whenever the text changes. + /// + [TypeVisualizer(typeof(TextBoxVisualizer))] + public class TextBoxBuilder : TextControlBuilderBase + { + internal readonly BehaviorSubject _Multiline = new(true); + + /// + /// Gets or sets a value specifying whether the text box is multiline. + /// + [Description("Specifies whether the text box is multiline.")] + public bool Multiline + { + get => _Multiline.Value; + set => _Multiline.OnNext(value); + } + + /// + /// Generates an observable sequence of values containing the contents + /// of the text box whenever the text changes. + /// + /// + /// A sequence of values representing the contents + /// of the text box. + /// + protected override IObservable Generate() + { + return _Text; + } + } +} diff --git a/src/Bonsai.Gui/TextBoxVisualizer.cs b/src/Bonsai.Gui/TextBoxVisualizer.cs new file mode 100644 index 0000000..05d29cd --- /dev/null +++ b/src/Bonsai.Gui/TextBoxVisualizer.cs @@ -0,0 +1,31 @@ +using System; +using System.Drawing; +using System.Windows.Forms; + +namespace Bonsai.Gui +{ + /// + /// Provides a type visualizer representing a text box control. + /// + public class TextBoxVisualizer : ControlVisualizerBase + { + /// + 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.SubscribeTo(builder._Multiline, value => textBox.Multiline = value); + textBox.TextChanged += (sender, e) => + { + builder._Text.OnNext(textBox.Text); + }; + return textBox; + } + } +} From ac0ff9de84c424d976edd4611860f1a265af5b42 Mon Sep 17 00:00:00 2001 From: glopesdev Date: Fri, 16 Feb 2024 00:19:36 +0000 Subject: [PATCH 2/2] Ensure control text is initialized before event --- src/Bonsai.Gui/TextBoxVisualizer.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Bonsai.Gui/TextBoxVisualizer.cs b/src/Bonsai.Gui/TextBoxVisualizer.cs index 05d29cd..1155a24 100644 --- a/src/Bonsai.Gui/TextBoxVisualizer.cs +++ b/src/Bonsai.Gui/TextBoxVisualizer.cs @@ -20,6 +20,7 @@ protected override TextBox CreateControl(IServiceProvider provider, TextBoxBuild textBox.Size = new Size(320, 240); } + textBox.Text = builder._Text.Value; textBox.SubscribeTo(builder._Multiline, value => textBox.Multiline = value); textBox.TextChanged += (sender, e) => {