Skip to content

A Swiss Army knife for developers.

Added IUINumberInput

Etienne Baudoux4d5ece89275c
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUINumberInput.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUINumberInput.cs
new file mode 100644
index 000000000..87ee52aa3
--- /dev/null
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUINumberInput.cs
@@ -0,0 +1,127 @@
+namespace DevToys.Api;
+
+/// <summary>
+/// A component that can be used to display or edit numbers on a single line.
+/// </summary>
+public interface IUINumberInput : IUISingleLineTextInput
+{
+ /// <summary>
+ /// Gets the minimum value possible. Default is <see cref="int.MinValue"/>.
+ /// </summary>
+ double Min { get; }
+
+ /// <summary>
+ /// Gets the maximum value possible. Default is <see cref="int.MaxValue"/>.
+ /// </summary>
+ double Max { get; }
+
+ /// <summary>
+ /// Gets the interval between legal numbers. Default is 1.
+ /// </summary>
+ double Step { get; }
+
+ /// <summary>
+ /// Raised when <see cref="Min"/> is changed.
+ /// </summary>
+ event EventHandler? MinChanged;
+
+ /// <summary>
+ /// Raised when <see cref="Max"/> is changed.
+ /// </summary>
+ event EventHandler? MaxChanged;
+
+ /// <summary>
+ /// Raised when <see cref="Step"/> is changed.
+ /// </summary>
+ event EventHandler? StepChanged;
+}
+
+[DebuggerDisplay($"Id = {{{nameof(Id)}}}, Text = {{{nameof(Text)}}}, Min = {{{nameof(Min)}}}, Max = {{{nameof(Max)}}}, Step = {{{nameof(Step)}}}")]
+internal class UINumberInput : UISingleLineTextInput, IUINumberInput
+{
+ private double _min = int.MinValue;
+ private double _max = int.MaxValue;
+ private double _step = 1;
+
+ internal UINumberInput(string? id)
+ : base(id)
+ {
+ }
+
+ public double Min
+ {
+ get => _min;
+ internal set => SetPropertyValue(ref _min, value, MinChanged);
+ }
+
+ public double Max
+ {
+ get => _max;
+ internal set => SetPropertyValue(ref _max, value, MaxChanged);
+ }
+
+ public double Step
+ {
+ get => _step;
+ internal set => SetPropertyValue(ref _step, value, StepChanged);
+ }
+
+ public event EventHandler? MinChanged;
+ public event EventHandler? MaxChanged;
+ public event EventHandler? StepChanged;
+}
+
+public static partial class GUI
+{
+ /// <summary>
+ /// A component that can be used to display or edit numbers on a single line.
+ /// </summary>
+ public static IUINumberInput NumberInput()
+ {
+ return NumberInput(null);
+ }
+
+ /// <summary>
+ /// A component that can be used to display or edit numbers on a single line.
+ /// </summary>
+ /// <param name="id">An optional unique identifier for this UI element.</param>
+ /// <param name="min">The minimum value possible.</param>
+ /// <param name="max">The maximum value possible.</param>
+ /// <param name="step">The interval between legal numbers.</param>
+ public static IUINumberInput NumberInput(string? id, double min = int.MinValue, double max = int.MaxValue, double step = 1)
+ {
+ return new UINumberInput(id)
+ {
+ Min = min,
+ Max = max,
+ Step = step
+ };
+ }
+
+ /// <summary>
+ /// Sets the minimum value possible.
+ /// </summary>
+ public static IUINumberInput Minimum(this IUINumberInput element, double minimum)
+ {
+ ((UINumberInput)element).Min = minimum;
+ return element;
+ }
+
+ /// <summary>
+ /// Sets the maximum value possible.
+ /// </summary>
+ public static IUINumberInput Maximum(this IUINumberInput element, double maximum)
+ {
+ ((UINumberInput)element).Max = maximum;
+ return element;
+ }
+
+ /// <summary>
+ /// Sets the interval between legal numbers.
+ /// </summary>
+ public static IUINumberInput Step(this IUINumberInput element, double step)
+ {
+ ((UINumberInput)element).Step = step;
+ return element;
+ }
+}
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISinglelineTextInput.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISinglelineTextInput.cs
index 30bfd311d..76f4979cd 100644
--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISinglelineTextInput.cs
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISinglelineTextInput.cs
@@ -25,6 +25,11 @@ public interface IUISingleLineTextInput : IUITitledElement
/// </summary>
TextSpan Selection { get; }
+ /// <summary>
+ /// Gets an extra interactive content to display in the command bar of the text input.
+ /// </summary>
+ IUIElement? CommandBarExtraContent { get; }
+
/// <summary>
/// Raised when <see cref="IsReadOnly"/> is changed.
/// </summary>
@@ -44,6 +49,11 @@ public interface IUISingleLineTextInput : IUITitledElement
/// Raised when <see cref="Selection"/> is changed.
/// </summary>
event EventHandler? SelectionChanged;
+
+ /// <summary>
+ /// Raised when <see cref="CommandBarExtraContent"/> is changed.
+ /// </summary>
+ event EventHandler? CommandBarExtraContentChanged;
}
[DebuggerDisplay($"Id = {{{nameof(Id)}}}, Text = {{{nameof(Text)}}}")]
@@ -53,6 +63,7 @@ internal class UISingleLineTextInput : UITitledElement, IUISingleLineTextInput
private bool _isReadOnly;
private bool _canCopyWhenEditable;
private TextSpan? _selection;
+ private IUIElement? _commandBarExtraContent;
internal UISingleLineTextInput(string? id)
: base(id)
@@ -103,10 +114,17 @@ internal class UISingleLineTextInput : UITitledElement, IUISingleLineTextInput
}
}
+ public IUIElement? CommandBarExtraContent
+ {
+ get => _commandBarExtraContent;
+ internal set => SetPropertyValue(ref _commandBarExtraContent, value, CommandBarExtraContentChanged);
+ }
+
public event EventHandler? TextChanged;
public event EventHandler? IsReadOnlyChanged;
public event EventHandler? CanCopyWhenEditableChanged;
public event EventHandler? SelectionChanged;
+ public event EventHandler? CommandBarExtraContentChanged;
}
public static partial class GUI
@@ -211,4 +229,16 @@ public static partial class GUI
}
return element;
}
+
+ /// <summary>
+ /// Defines an additional element to display in the command bar.
+ /// </summary>
+ public static T CommandBarExtraContent<T>(this T element, IUIElement? extraElement) where T : IUISingleLineTextInput
+ {
+ if (element is UISingleLineTextInput strongElement)
+ {
+ strongElement.CommandBarExtraContent = extraElement;
+ }
+ return element;
+ }
}
diff --git a/src/app/dev/DevToys.Blazor/Components/Text/TextBox/TextBox.razor.cs b/src/app/dev/DevToys.Blazor/Components/Text/TextBox/TextBox.razor.cs
index a9d7a7a52..4967f4895 100644
--- a/src/app/dev/DevToys.Blazor/Components/Text/TextBox/TextBox.razor.cs
+++ b/src/app/dev/DevToys.Blazor/Components/Text/TextBox/TextBox.razor.cs
@@ -63,19 +63,19 @@ public partial class TextBox : MefComponentBase, IFocusable
/// Gets or sets the minimum value possible when <see cref="Type"/> is <see cref="TextBoxTypes.Number"/>.
/// </summary>
[Parameter]
- public int Min { get; set; } = int.MinValue;
+ public double Min { get; set; } = int.MinValue;
/// <summary>
/// Gets or sets the maximum value possible when <see cref="Type"/> is <see cref="TextBoxTypes.Number"/>.
/// </summary>
[Parameter]
- public int Max { get; set; } = int.MaxValue;
+ public double Max { get; set; } = int.MaxValue;
/// <summary>
/// Gets or sets the interval between legal numbers when <see cref="Type"/> is <see cref="TextBoxTypes.Number"/>.
/// </summary>
[Parameter]
- public int Step { get; set; } = 1;
+ public double Step { get; set; } = 1;
[Parameter]
public RenderFragment? Buttons { get; set; }
@@ -130,7 +130,7 @@ public partial class TextBox : MefComponentBase, IFocusable
{
Guard.IsNotNull(_input);
Guard.IsNotNull(_input.Element);
- await SetTextAsync((await (await JSModule).InvokeAsync<int>("decreaseValue", _input.Element)).ToString());
+ await SetTextAsync((await (await JSModule).InvokeAsync<double>("decreaseValue", _input.Element)).ToString());
await FocusAsync();
}
}
@@ -141,7 +141,7 @@ public partial class TextBox : MefComponentBase, IFocusable
{
Guard.IsNotNull(_input);
Guard.IsNotNull(_input.Element);
- await SetTextAsync((await (await JSModule).InvokeAsync<int>("increaseValue", _input.Element)).ToString());
+ await SetTextAsync((await (await JSModule).InvokeAsync<double>("increaseValue", _input.Element)).ToString());
await FocusAsync();
}
}
diff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UIElementPresenter.razor b/src/app/dev/DevToys.Blazor/Components/UIElements/UIElementPresenter.razor
index e6ae67ea0..eba3c3d5a 100644
--- a/src/app/dev/DevToys.Blazor/Components/UIElements/UIElementPresenter.razor
+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UIElementPresenter.razor
@@ -42,6 +42,10 @@
<UIMultiLineTextInputPresenter UIMultiLineTextInput="@multiLineTextInput" />
break;
+ case IUINumberInput numberInput:
+ <UINumberInputPresenter UINumberInput="@numberInput" />
+ break;
+
case IUISingleLineTextInput singleLineTextInput:
<UISingleLineTextInputPresenter UISingleLineTextInput="@singleLineTextInput" />
break;
diff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UINumberInputPresenter.razor b/src/app/dev/DevToys.Blazor/Components/UIElements/UINumberInputPresenter.razor
new file mode 100644
index 000000000..88bd3a15e
--- /dev/null
+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UINumberInputPresenter.razor
@@ -0,0 +1,13 @@
+@namespace DevToys.Blazor.Components.UIElements
+@using DevToys.Api;
+@inherits StyledComponentBase
+
+<UITextInputWrapper @ref="_textInputWrapper" UITextInput="@UINumberInput">
+ <TextBox IsVisible="@UINumberInput.IsVisible"
+ IsEnabled="@UINumberInput.IsEnabled"
+ Text="@UINumberInput.Text"
+ IsReadOnly="@UINumberInput.IsReadOnly"
+ TextChanged="@OnTextChanged"
+ Type="TextBoxTypes.Number"
+ ParentComponent="_textInputWrapper" />
+</UITextInputWrapper>
\ No newline at end of file
diff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UINumberInputPresenter.razor.cs b/src/app/dev/DevToys.Blazor/Components/UIElements/UINumberInputPresenter.razor.cs
new file mode 100644
index 000000000..cac89a07c
--- /dev/null
+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UINumberInputPresenter.razor.cs
@@ -0,0 +1,14 @@
+namespace DevToys.Blazor.Components.UIElements;
+
+public partial class UINumberInputPresenter : StyledComponentBase
+{
+ private UITextInputWrapper _textInputWrapper = default!;
+
+ [Parameter]
+ public IUINumberInput UINumberInput { get; set; } = default!;
+
+ private void OnTextChanged(string newText)
+ {
+ UINumberInput.Text(newText);
+ }
+}
diff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UITextInputWrapper.razor b/src/app/dev/DevToys.Blazor/Components/UIElements/UITextInputWrapper.razor
index b77de5425..24e1264a7 100644
--- a/src/app/dev/DevToys.Blazor/Components/UIElements/UITextInputWrapper.razor
+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UITextInputWrapper.razor
@@ -39,6 +39,12 @@
<GridCell Row="0"
Column="1">
<StackPanel IsEnabled="@(!IsLoadingFile)" Wrap="false">
+ @if (UITextInput.CommandBarExtraContent is not null)
+ {
+ <UIElementPresenter UIElement="@UITextInput.CommandBarExtraContent" />
+ <div class="ui-text-input-wrapper-separator"></div>
+ }
+
@if (!_hideNonEssentialToolBar)
{
@if (!UITextInput.IsReadOnly)
@@ -100,16 +106,19 @@
</Button>
}
- <div class="ui-text-input-wrapper-separator"></div>
- <DropDownButton ToolTip="Transfer to another tool"
- Items="@SmartDetectionDropDownItems"
- AnchorOrigin="Origin.BottomRight"
- TransformOrigin="Origin.TopRight">
- <img src="@(ToolsDetectedBySmartDetection ? "_content/DevToys.Blazor/img/idea.svg" : "_content/DevToys.Blazor/img/idea-disabled.svg")"
- height="22"
- width="22"
- style="flex-basis: auto; align-self:center;" />
- </DropDownButton>
+ @if (UITextInput.IsReadOnly)
+ {
+ <div class="ui-text-input-wrapper-separator"></div>
+ <DropDownButton ToolTip="Transfer to another tool"
+ Items="@SmartDetectionDropDownItems"
+ AnchorOrigin="Origin.BottomRight"
+ TransformOrigin="Origin.TopRight">
+ <img src="@(ToolsDetectedBySmartDetection ? "_content/DevToys.Blazor/img/idea.svg" : "_content/DevToys.Blazor/img/idea-disabled.svg")"
+ height="22"
+ width="22"
+ style="flex-basis: auto; align-self:center;" />
+ </DropDownButton>
+ }
</StackPanel>
</GridCell>
<GridCell Row="1" Column="0" ColumnSpan="2">
diff --git a/src/app/dev/DevToys.Tools/Tools/Settings/SettingsGuiTool.cs b/src/app/dev/DevToys.Tools/Tools/Settings/SettingsGuiTool.cs
index fc3ed29d7..7bf02ec8c 100644
--- a/src/app/dev/DevToys.Tools/Tools/Settings/SettingsGuiTool.cs
+++ b/src/app/dev/DevToys.Tools/Tools/Settings/SettingsGuiTool.cs
@@ -89,12 +89,14 @@ internal sealed class SettingsGuiTool : IGuiTool
Button().Text("Bottom Left button"),
Button().Text("Bottom Center button").OnClick(OnBottomCenterButtonClickAsync),
Button().Text("Bottom Right button")),
+ NumberInput(),
SingleLineTextInput().Title("Read-write text input with copy").ReadOnly(),
MultilineTextInput()
.Title("Monaco editor")
.Extendable()
.Text("{\"hello\": \"there\"}")
- .Language("json"),
+ .Language("json")
+ .CommandBarExtraContent(Button().Text("None")),
DiffTextInput()
.Title("Difference")
.OriginalText("hello")