Skip to content

A Swiss Army knife for developers.

Added IUILabel

Etienne Baudouxd0dee6d7091b
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUILabel.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUILabel.cs
new file mode 100644
index 000000000..1d607cd8e
--- /dev/null
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUILabel.cs
@@ -0,0 +1,104 @@
+namespace DevToys.Api;
+
+/// <summary>
+/// A component that displays a text.
+/// </summary>
+public interface IUILabel : IUIElement
+{
+ /// <summary>
+ /// Gets the text to display.
+ /// </summary>
+ string? Text { get; }
+
+ /// <summary>
+ /// Gets the style of the text.
+ /// Default value is <see cref="UILabelStyle.Body"/>.
+ /// </summary>
+ UILabelStyle Style { get; }
+
+ /// <summary>
+ /// Raised when <see cref="Text"/> is changed.
+ /// </summary>
+ event EventHandler? TextChanged;
+
+ /// <summary>
+ /// Raised when <see cref="Style"/> is changed.
+ /// </summary>
+ event EventHandler? StyleChanged;
+}
+
+[DebuggerDisplay($"Id = {{{nameof(Id)}}}, Text = {{{nameof(Text)}}}")]
+internal sealed class UILabel : UIElement, IUILabel
+{
+ private UILabelStyle _style = UILabelStyle.Body;
+ private string? _text;
+
+ internal UILabel(string? id)
+ : base(id)
+ {
+ }
+
+ public string? Text
+ {
+ get => _text;
+ internal set => SetPropertyValue(ref _text, value, TextChanged);
+ }
+
+ public UILabelStyle Style
+ {
+ get => _style;
+ internal set => SetPropertyValue(ref _style, value, StyleChanged);
+ }
+
+ public event EventHandler? TextChanged;
+
+ public event EventHandler? StyleChanged;
+}
+
+public static partial class GUI
+{
+ /// <summary>
+ /// A component that displays a text.
+ /// </summary>
+ public static IUILabel Label()
+ {
+ return Label(null);
+ }
+
+ /// <summary>
+ /// A component that displays a text.
+ /// </summary>
+ /// <param name="id">An optional unique identifier for this UI element.</param>
+ public static IUILabel Label(string? id)
+ {
+ return new UILabel(id);
+ }
+
+ /// <summary>
+ /// A component that displays a text.
+ /// </summary>
+ /// <param name="id">An optional unique identifier for this UI element.</param>
+ /// <param name="text">The text to display.</param>
+ public static IUILabel Label(string? id, string text)
+ {
+ return new UILabel(id).Text(text);
+ }
+
+ /// <summary>
+ /// Sets the <see cref="IUILabel.Text"/>.
+ /// </summary>
+ public static IUILabel Text(this IUILabel element, string? text)
+ {
+ ((UILabel)element).Text = text;
+ return element;
+ }
+
+ /// <summary>
+ /// Sets the text style.
+ /// </summary>
+ public static IUILabel Style(this IUILabel element, UILabelStyle style)
+ {
+ ((UILabel)element).Style = style;
+ return element;
+ }
+}
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/UILabelStyle.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/UILabelStyle.cs
new file mode 100644
index 000000000..bb3350364
--- /dev/null
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/UILabelStyle.cs
@@ -0,0 +1,13 @@
+namespace DevToys.Api;
+
+public enum UILabelStyle
+{
+ Caption,
+ Body,
+ BodyStrong,
+ BodyLarge,
+ Subtitle,
+ Title,
+ TitleLarge,
+ Display
+}
diff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UIElementPresenter.razor b/src/app/dev/DevToys.Blazor/Components/UIElements/UIElementPresenter.razor
index ddb752f1f..638ac374f 100644
--- a/src/app/dev/DevToys.Blazor/Components/UIElements/UIElementPresenter.razor
+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UIElementPresenter.razor
@@ -70,6 +70,10 @@
<UIInfoBarPresenter UIInfoBar="@infoBar" />
break;
+ case IUILabel label:
+ <UILabelPresenter UILabel="@label" />
+ break;
+
default:
throw new NotSupportedException($"Gui Tool component of type '{UIElement.GetType().FullName}' isn't supported.");
}
diff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UILabelPresenter.razor b/src/app/dev/DevToys.Blazor/Components/UIElements/UILabelPresenter.razor
new file mode 100644
index 000000000..1a7c53446
--- /dev/null
+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UILabelPresenter.razor
@@ -0,0 +1,11 @@
+@namespace DevToys.Blazor.Components.UIElements
+@using DevToys.Api;
+@inherits ComponentBase
+
+<TextBlock id="@UILabel.Id"
+ IsVisible="@UILabel.IsVisible"
+ IsEnabled="@UILabel.IsEnabled"
+ HorizontalAlignment="@UILabel.HorizontalAlignment"
+ VerticalAlignment="@UILabel.VerticalAlignment"
+ Text="@UILabel.Text"
+ Appearance="@Appearance" />
\ No newline at end of file
diff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UILabelPresenter.razor.cs b/src/app/dev/DevToys.Blazor/Components/UIElements/UILabelPresenter.razor.cs
new file mode 100644
index 000000000..daf2eaeee
--- /dev/null
+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UILabelPresenter.razor.cs
@@ -0,0 +1,70 @@
+namespace DevToys.Blazor.Components.UIElements;
+
+public partial class UILabelPresenter : ComponentBase, IDisposable
+{
+ [Parameter]
+ public IUILabel UILabel { get; set; } = default!;
+
+ [Parameter]
+ public TextBlockAppearance Appearance { get; set; } = default!;
+
+ protected override void OnInitialized()
+ {
+ base.OnInitialized();
+
+ ApplyStyle();
+ UILabel.StyleChanged += UILabel_StyleChanged;
+ }
+
+ public void Dispose()
+ {
+ UILabel.StyleChanged -= UILabel_StyleChanged;
+ }
+
+ private void UILabel_StyleChanged(object? sender, EventArgs e)
+ {
+ ApplyStyle();
+ }
+
+ private void ApplyStyle()
+ {
+ switch (UILabel.Style)
+ {
+ case UILabelStyle.Caption:
+ Appearance = TextBlockAppearance.Caption;
+ break;
+
+ case UILabelStyle.Body:
+ Appearance = TextBlockAppearance.Body;
+ break;
+
+ case UILabelStyle.BodyStrong:
+ Appearance = TextBlockAppearance.BodyStrong;
+ break;
+
+ case UILabelStyle.BodyLarge:
+ Appearance = TextBlockAppearance.BodyLarge;
+ break;
+
+ case UILabelStyle.Subtitle:
+ Appearance = TextBlockAppearance.Subtitle;
+ break;
+
+ case UILabelStyle.Title:
+ Appearance = TextBlockAppearance.Title;
+ break;
+
+ case UILabelStyle.TitleLarge:
+ Appearance = TextBlockAppearance.TitleLarge;
+ break;
+
+ case UILabelStyle.Display:
+ Appearance = TextBlockAppearance.Display;
+ break;
+
+ default:
+ ThrowHelper.ThrowNotSupportedException($"{nameof(UILabelStyle)}.{UILabel.Style} is not supported by {nameof(UILabelPresenter)}.");
+ break;
+ }
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Tools/Test/TestGuiTool.cs b/src/app/dev/DevToys.Tools/Tools/Test/TestGuiTool.cs
index 4da820213..12a1b2b7c 100644
--- a/src/app/dev/DevToys.Tools/Tools/Test/TestGuiTool.cs
+++ b/src/app/dev/DevToys.Tools/Tools/Test/TestGuiTool.cs
@@ -1,4 +1,5 @@
#if DEBUG
+using DevToys.Api;
using Newtonsoft.Json.Linq;
namespace DevToys.Tools.Tools.Test;
@@ -112,6 +113,10 @@ internal sealed class TestGuiTool : IGuiTool
.Vertical()
.WithChildren(
+ Label()
+ .Style(UILabelStyle.Display)
+ .Text("A large text"),
+
SettingGroup()
.Icon("FluentSystemIcons", '\uF6A9')
.Title("Dummy list setting")