Skip to content

A Swiss Army knife for developers.

Fixed inconcistency and bug in IUIMultilineTextInput (#1102)

Etienne Baudoux18912b8a338a
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIMultiLineTextInput.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIMultiLineTextInput.cs
index af901b647..1a3f36813 100644
--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIMultiLineTextInput.cs
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIMultiLineTextInput.cs
@@ -155,9 +155,9 @@ public static partial class GUI
/// <remarks>
/// This component is powered by Monaco Editor.
/// </remarks>
- public static IUIMultiLineTextInput MultilineTextInput()
+ public static IUIMultiLineTextInput MultiLineTextInput()
{
- return MultilineTextInput(null);
+ return MultiLineTextInput(null);
}
/// <summary>
@@ -167,7 +167,7 @@ public static partial class GUI
/// This component is powered by Monaco Editor.
/// </remarks>
/// <param name="id">An optional unique identifier for this UI element.</param>
- public static IUIMultiLineTextInput MultilineTextInput(string? id)
+ public static IUIMultiLineTextInput MultiLineTextInput(string? id)
{
return new UIMultilineTextInput(id);
}
@@ -180,7 +180,7 @@ public static partial class GUI
/// </remarks>
/// <param name="id">An optional unique identifier for this UI element.</param>
/// <param name="programmingLanguageName">the programming language name to use to colorize the text in the control.</param>
- public static IUIMultiLineTextInput MultilineTextInput(string? id, string programmingLanguageName)
+ public static IUIMultiLineTextInput MultiLineTextInput(string? id, string programmingLanguageName)
{
return new UIMultilineTextInput(id).Language(programmingLanguageName);
}
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/UIHighlightedTextSpan.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/UIHighlightedTextSpan.cs
index b04110112..079ff9cd6 100644
--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/UIHighlightedTextSpan.cs
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/UIHighlightedTextSpan.cs
@@ -20,10 +20,10 @@ public record UIHighlightedTextSpan : TextSpan
/// <summary>
/// Initializes a new instance of the <see cref="UIHighlightedTextSpan"/> class based on the original <see cref="TextSpan"/>.
/// </summary>
- /// <param name="original">The original <see cref="TextSpan"/>.</param>
+ /// <param name="span">The location where the text document should be highlighted <see cref="TextSpan"/>.</param>
/// <param name="color">The color of the highlighted text span.</param>
- protected UIHighlightedTextSpan(TextSpan original, UIHighlightedTextSpanColor color = UIHighlightedTextSpanColor.Default)
- : base(original)
+ public UIHighlightedTextSpan(TextSpan span, UIHighlightedTextSpanColor color = UIHighlightedTextSpanColor.Default)
+ : base(span)
{
Color = color;
}
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/UIHoverTooltip.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/UIHoverTooltip.cs
index c0030d512..f6e75ffed 100644
--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/UIHoverTooltip.cs
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/UIHoverTooltip.cs
@@ -3,24 +3,33 @@
/// <summary>
/// Represents the Tooltip to display on hover
/// </summary>
-public record UIHoverTooltip
+public record UIHoverTooltip : TextSpan
{
/// <summary>
- /// Contain the position of the span to search
+ /// Initializes a new instance of the <see cref="UIHoverTooltip"/> class.
/// </summary>
- public TextSpan Span { get; }
+ /// <param name="startPosition">The start position of the highlighted text span.</param>
+ /// <param name="length">The length of the highlighted text span.</param>
+ /// <param name="tooltip">The information to display on hover.</param>
+ public UIHoverTooltip(int startPosition, int length, string tooltip)
+ : base(startPosition, length)
+ {
+ Tooltip = tooltip;
+ }
/// <summary>
- /// Contain the information we want to display on hover
+ /// Initializes a new instance of the <see cref="UIHoverTooltip"/> class.
/// </summary>
- public string Description { get; }
+ /// <param name="span">The location in the text document where the tooltip must appear.</param>
+ /// <param name="tooltip">The information to display on hover.</param>
+ public UIHoverTooltip(TextSpan span, string tooltip)
+ : base(span)
+ {
+ Tooltip = tooltip;
+ }
/// <summary>
- /// Create a new isntance of the <see cref="UIHoverTooltip"/> class.
+ /// Contain the information to display on hover
/// </summary>
- public UIHoverTooltip(TextSpan span, string description)
- {
- Span = span;
- Description = description;
- }
+ public string Tooltip { get; }
}
diff --git a/src/app/dev/DevToys.Blazor/BuiltInTools/Settings/SettingsGuiTool.cs b/src/app/dev/DevToys.Blazor/BuiltInTools/Settings/SettingsGuiTool.cs
index 63441acd4..61137c9eb 100644
--- a/src/app/dev/DevToys.Blazor/BuiltInTools/Settings/SettingsGuiTool.cs
+++ b/src/app/dev/DevToys.Blazor/BuiltInTools/Settings/SettingsGuiTool.cs
@@ -198,7 +198,7 @@ internal sealed class SettingsGuiTool : IGuiTool
stateDescriptionWhenOn: Settings.PasteClearsTextStateDescriptionWhenOn,
stateDescriptionWhenOff: null),
- MultilineTextInput("text-editor-render-preview")
+ MultiLineTextInput("text-editor-render-preview")
.Title(Settings.TextEditorPreview)
.AlignVertically(UIVerticalAlignment.Top)
.Language("json")
diff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UIMultiLineTextInputPresenter.razor.cs b/src/app/dev/DevToys.Blazor/Components/UIElements/UIMultiLineTextInputPresenter.razor.cs
index d4b3a02f2..ace3df401 100644
--- a/src/app/dev/DevToys.Blazor/Components/UIElements/UIMultiLineTextInputPresenter.razor.cs
+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UIMultiLineTextInputPresenter.razor.cs
@@ -1,6 +1,5 @@
using DevToys.Blazor.Components.Monaco;
using DevToys.Blazor.Components.Monaco.Editor;
-using Microsoft.VisualBasic;
using Range = DevToys.Blazor.Components.Monaco.Range;
namespace DevToys.Blazor.Components.UIElements;
@@ -97,90 +96,14 @@ public partial class UIMultiLineTextInputPresenter : JSStyledComponentBase
}
}
- private async void UIMultiLineTextInput_HighlightedSpansChanged(object? sender, EventArgs e)
+ private void UIMultiLineTextInput_HighlightedSpansChanged(object? sender, EventArgs e)
{
- await _monacoInitializationAwaiter.Task;
-
- using (await _semaphore.WaitAsync(CancellationToken.None))
- {
- TextModel model = await _monacoEditor.GetModelAsync();
-
- if (model is not null)
- {
- var decorations = new ModelDeltaDecoration[UIMultiLineTextInput.HighlightedSpans.Count];
-
- for (int i = 0; i < decorations.Length; i++)
- {
- UIHighlightedTextSpan highlightedSpan = UIMultiLineTextInput.HighlightedSpans[i];
- Position selectionStartPosition = await model.GetPositionAtAsync(JSRuntime, highlightedSpan.StartPosition);
- Position selectionEndPosition = await model.GetPositionAtAsync(JSRuntime, highlightedSpan.EndPosition);
-
- decorations[i]
- = new ModelDeltaDecoration
- {
- Range = new Monaco.Range
- {
- StartLineNumber = selectionStartPosition.LineNumber,
- StartColumn = selectionStartPosition.Column,
- EndLineNumber = selectionEndPosition.LineNumber,
- EndColumn = selectionEndPosition.Column
- },
- Options = new ModelDecorationOptions
- {
- InlineClassName = GetClassNameForHighlightedTextSpan(highlightedSpan)
- }
- };
- }
-
- await _monacoEditor.ReplaceAllDecorationsByAsync(decorations);
- }
- }
+ ApplyAllDecorationsAsync().Forget();
}
- private async void UIMultiLineTextInput_HoverTooltipChanged(object? sender, EventArgs e)
+ private void UIMultiLineTextInput_HoverTooltipChanged(object? sender, EventArgs e)
{
- await _monacoInitializationAwaiter.Task;
-
- using (await _semaphore.WaitAsync(CancellationToken.None))
- {
- TextModel model = await _monacoEditor.GetModelAsync();
-
- if (model is not null)
- {
- var decorations = new ModelDeltaDecoration[UIMultiLineTextInput.HoverTooltips.Count];
-
- for (int i = 0; i < decorations.Length; i++)
- {
- UIHoverTooltip hoverTooltip = UIMultiLineTextInput.HoverTooltips[i];
- Position selectionStartPosition = await model.GetPositionAtAsync(JSRuntime, hoverTooltip.Span.StartPosition);
- Position selectionEndPosition = await model.GetPositionAtAsync(JSRuntime, hoverTooltip.Span.EndPosition);
-
- decorations[i]
- = new ModelDeltaDecoration
- {
- Range = new Monaco.Range
- {
- StartLineNumber = selectionStartPosition.LineNumber,
- StartColumn = selectionStartPosition.Column,
- EndLineNumber = selectionEndPosition.LineNumber,
- EndColumn = selectionEndPosition.Column
- },
- Options = new ModelDecorationOptions
- {
- HoverMessage = new[]
- {
- new MarkdownString()
- {
- Value = hoverTooltip.Description
- }
- }
- }
- };
- }
-
- await _monacoEditor.ReplaceAllDecorationsByAsync(decorations);
- }
- }
+ ApplyAllDecorationsAsync().Forget();
}
private async Task OnMonacoEditorTextChangedAsync(ModelContentChangedEvent ev)
@@ -218,14 +141,11 @@ public partial class UIMultiLineTextInputPresenter : JSStyledComponentBase
await textModel.SetValueAsync(JSRuntime, UIMultiLineTextInput.Text);
}
- private Task OnMonacoEditorInitializedAsync()
+ private async Task OnMonacoEditorInitializedAsync()
{
_monacoInitializationAwaiter.TrySetResult();
- UIMultiLineTextInput_HighlightedSpansChanged(UIMultiLineTextInput, EventArgs.Empty);
- UIMultiLineTextInput_SelectionChanged(UIMultiLineTextInput, EventArgs.Empty);
-
- return Task.CompletedTask;
+ await ApplyAllDecorationsAsync();
}
private async Task OnMonacoEditorSelectionChanged(CursorSelectionChangedEvent newSelection)
@@ -274,6 +194,74 @@ public partial class UIMultiLineTextInputPresenter : JSStyledComponentBase
};
}
+ private async Task ApplyAllDecorationsAsync()
+ {
+ await _monacoInitializationAwaiter.Task;
+
+ using (await _semaphore.WaitAsync(CancellationToken.None))
+ {
+ TextModel model = await _monacoEditor.GetModelAsync();
+
+ if (model is not null)
+ {
+ UIHoverTooltip[] hoverTooltips = UIMultiLineTextInput.HoverTooltips.ToArray(); // Copy to make it thread safe
+ UIHighlightedTextSpan[] highlightedSpans = UIMultiLineTextInput.HighlightedSpans.ToArray();
+ var monacoDecorations = new List<ModelDeltaDecoration>();
+
+ for (int i = 0; i < hoverTooltips.Length; i++)
+ {
+ UIHoverTooltip hoverTooltip = hoverTooltips[i];
+ ModelDeltaDecoration decoration = await CreateModelDeltaDecorationAsync(model, hoverTooltip);
+
+ decoration.Options = new ModelDecorationOptions
+ {
+ HoverMessage =
+ [
+ new MarkdownString()
+ {
+ Value = hoverTooltip.Tooltip
+ }
+ ]
+ };
+
+ monacoDecorations.Add(decoration);
+ }
+
+ for (int i = 0; i < highlightedSpans.Length; i++)
+ {
+ UIHighlightedTextSpan highlightedSpan = highlightedSpans[i];
+ ModelDeltaDecoration decoration = await CreateModelDeltaDecorationAsync(model, highlightedSpan);
+
+ decoration.Options = new ModelDecorationOptions
+ {
+ InlineClassName = GetClassNameForHighlightedTextSpan(highlightedSpan)
+ };
+
+ monacoDecorations.Add(decoration);
+ }
+
+ await _monacoEditor.ReplaceAllDecorationsByAsync(monacoDecorations.ToArray());
+ }
+ }
+ }
+
+ private async Task<ModelDeltaDecoration> CreateModelDeltaDecorationAsync(TextModel model, TextSpan textSpan)
+ {
+ Position selectionStartPosition = await model.GetPositionAtAsync(JSRuntime, textSpan.StartPosition);
+ Position selectionEndPosition = await model.GetPositionAtAsync(JSRuntime, textSpan.EndPosition);
+
+ return new ModelDeltaDecoration
+ {
+ Range = new Range
+ {
+ StartLineNumber = selectionStartPosition.LineNumber,
+ StartColumn = selectionStartPosition.Column,
+ EndLineNumber = selectionEndPosition.LineNumber,
+ EndColumn = selectionEndPosition.Column
+ },
+ };
+ }
+
private static string GetClassNameForHighlightedTextSpan(UIHighlightedTextSpan highlightedTextSpan)
{
return highlightedTextSpan.Color switch