A Swiss Army knife for developers.
C# blazorcross-platformcsharpdesktopdesktop-appdesktop-applicationdeveloper-toolsextensiblefluentfluent-designlinuxmacosmicasyntax-highlightingtypescriptwindowswindows-10windows-11winui
Added element finding
Etienne Baudoux806083b64b8f
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIDataGrid.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIDataGrid.csindex 844e602db..b6ef8b6e5 100644--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIDataGrid.cs+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIDataGrid.cs@@ -5,7 +5,7 @@ namespace DevToys.Api;/// <summary>/// A component that represents a grid that can display data with rows and columns./// </summary>-public interface IUIDataGrid : IUIElement+public interface IUIDataGrid : IUIElementWithChildren{/// <summary>/// Gets the title of each column in the data grid.@@ -44,7 +44,7 @@ public interface IUIDataGrid : IUIElement}[DebuggerDisplay($"Id = {{{nameof(Id)}}}, SelectedRow = {{{nameof(SelectedRow)}}}")]-internal sealed class UIDataGrid : UIElement, IUIDataGrid, IDisposable+internal sealed class UIDataGrid : UIElementWithChildren, IUIDataGrid, IDisposable{private readonly ObservableCollection<IUIDataGridRow> _rows = new();private string[] _columns = Array.Empty<string>();@@ -104,6 +104,29 @@ internal sealed class UIDataGrid : UIElement, IUIDataGrid, IDisposable_rows.CollectionChanged -= Rows_CollectionChanged;}+ protected override IEnumerable<IUIElement> GetChildren()+ {+ IUIDataGridRow[] rows = _rows.ToArray();+ for (int i = 0; i < rows.Length; i++)+ {+ IUIDataGridRow row = rows[i];+ IUIDataGridCell[] cells = row.ToArray();+ for (int j = 0; j < cells.Length; j++)+ {+ IUIDataGridCell cell = cells[j];+ if (cell.UIElement is not null)+ {+ yield return cell.UIElement;+ }+ }++ if (row.Details is not null)+ {+ yield return row.Details;+ }+ }+ }+private void Rows_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e){// Try to preserve the selection.diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIElementWithChildren.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIElementWithChildren.csnew file mode 100644index 000000000..e453b117b--- /dev/null+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIElementWithChildren.cs@@ -0,0 +1,64 @@+namespace DevToys.Api;++/// <summary>+/// A base interface for all UI elements that can have children elements.+/// </summary>+public interface IUIElementWithChildren : IUIElement+{+ /// <summary>+ /// Gets the first child element with the specified id.+ /// </summary>+ /// <param name="id">The id of a child element.</param>+ /// <returns>Returns null if the element could not be found. If many elements have the name id, this method returns the first it finds</returns>+ IUIElement? GetChildElementById(string id);+}++internal abstract class UIElementWithChildren : UIElement, IUIElementWithChildren+{+ protected UIElementWithChildren(string? id)+ : base(id)+ {+ }++ protected abstract IEnumerable<IUIElement> GetChildren();++ public IUIElement? GetChildElementById(string id)+ {+ if (string.IsNullOrEmpty(id))+ {+ return null;+ }++ return GetChildElementById(id, GetChildren());+ }++ internal static IUIElement? GetChildElementById(string id, IEnumerable<IUIElement> children)+ {+ if (string.IsNullOrEmpty(id))+ {+ return null;+ }++ if (children is not null)+ {+ foreach (IUIElement? element in children)+ {+ if (element is not null && string.Equals(element.Id, id, StringComparison.Ordinal))+ {+ return element;+ }++ if (element is IUIElementWithChildren uIElementWithChildren)+ {+ IUIElement? matchingChild = uIElementWithChildren.GetChildElementById(id);+ if (matchingChild is not null)+ {+ return matchingChild;+ }+ }+ }+ }++ return null;+ }+}diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIGrid.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIGrid.csindex 8bf6bbd30..6afed85be 100644--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIGrid.cs+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIGrid.cs@@ -6,7 +6,7 @@ namespace DevToys.Api;/// A component provides a flexible grid area that consists of columns and rows./// Child elements of the Grid are measured and arranged according to their row/column assignments and other logic./// </summary>-public interface IUIGrid : IUIElement+public interface IUIGrid : IUIElementWithChildren{/// <summary>/// Gets a value that indicates the space between rows.@@ -64,7 +64,7 @@ public interface IUIGrid : IUIElement}[DebuggerDisplay($"Id = {{{nameof(Id)}}}")]-internal sealed class UIGrid : UIElement, IUIGrid+internal sealed class UIGrid : UIElementWithChildren, IUIGrid{private UISpacing _rowSpacing = UISpacing.Small;private UISpacing _columnSpacing = UISpacing.Small;@@ -77,6 +77,20 @@ internal sealed class UIGrid : UIElement, IUIGrid{}+ protected override IEnumerable<IUIElement> GetChildren()+ {+ if (_cells is not null)+ {+ foreach (IUIGridCell cell in _cells)+ {+ if (cell.Child is not null)+ {+ yield return cell.Child;+ }+ }+ }+ }+public UISpacing RowSpacing{get => _rowSpacing;diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIList.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIList.csindex e462e92d3..c35cee05d 100644--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIList.cs+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIList.cs@@ -5,7 +5,7 @@ namespace DevToys.Api;/// <summary>/// A component that represents a list of items./// </summary>-public interface IUIList : IUIElement+public interface IUIList : IUIElementWithChildren{/// <summary>/// Gets the list of items displayed in the list.@@ -39,7 +39,7 @@ public interface IUIList : IUIElement}[DebuggerDisplay($"Id = {{{nameof(Id)}}}, SelectedItem = {{{nameof(SelectedItem)}}}")]-internal sealed class UIList : UIElement, IUIList, IDisposable+internal sealed class UIList : UIElementWithChildren, IUIList, IDisposable{private readonly ObservableCollection<IUIListItem> _items = new();private IUIListItem? _selectedItem;@@ -51,6 +51,20 @@ internal sealed class UIList : UIElement, IUIList, IDisposable_items.CollectionChanged += Items_CollectionChanged;}+ protected override IEnumerable<IUIElement> GetChildren()+ {+ if (_items is not null)+ {+ foreach (IUIListItem item in _items)+ {+ if (item.UIElement is not null)+ {+ yield return item.UIElement;+ }+ }+ }+ }+public ObservableCollection<IUIListItem> Items => _items;public bool CanSelectItemdiff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISetting.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISetting.csindex aa3051e37..2dee3d174 100644--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISetting.cs+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISetting.cs@@ -3,7 +3,7 @@/// <summary>/// A component that represents a setting, with a title, description, icon and <see cref="IUIElement"/> for the option value./// </summary>-public interface IUISetting : IUITitledElement+public interface IUISetting : IUITitledElementWithChildren{/// <summary>/// Gets the description of the setting.@@ -47,7 +47,7 @@ public interface IUISetting : IUITitledElement}[DebuggerDisplay($"Id = {{{nameof(Id)}}}, Title = {{{nameof(Title)}}}")]-internal class UISetting : UITitledElement, IUISetting+internal class UISetting : UITitledElementWithChildren, IUISetting{private string? _description;private string? _stateDescription;@@ -59,6 +59,14 @@ internal class UISetting : UITitledElement, IUISetting{}+ protected override IEnumerable<IUIElement> GetChildren()+ {+ if (InteractiveElement is not null)+ {+ yield return InteractiveElement;+ }+ }+public string? Description{get => _description;diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISettingGroup.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISettingGroup.csindex 152c49b7b..6f9f7e3d6 100644--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISettingGroup.cs+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISettingGroup.cs@@ -31,6 +31,25 @@ internal sealed class UISettingGroup : UISetting, IUISettingGroup{}+ protected override IEnumerable<IUIElement> GetChildren()+ {+ if (InteractiveElement is not null)+ {+ yield return InteractiveElement;+ }++ if (_children is not null)+ {+ foreach (IUIElement child in _children)+ {+ if (child is not null)+ {+ yield return child;+ }+ }+ }+ }+public IUIElement[]? Children{get => _children;diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISinglelineTextInput.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISinglelineTextInput.csindex 8e0c3d72d..4405b4a4b 100644--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISinglelineTextInput.cs+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISinglelineTextInput.cs@@ -3,7 +3,7 @@/// <summary>/// A component that can be used to display or edit unformatted text on a single line./// </summary>-public interface IUISingleLineTextInput : IUITitledElement+public interface IUISingleLineTextInput : IUITitledElementWithChildren{/// <summary>/// Gets whether the user can edit the text or not. Default is false.@@ -57,7 +57,7 @@ public interface IUISingleLineTextInput : IUITitledElement}[DebuggerDisplay($"Id = {{{nameof(Id)}}}, Text = {{{nameof(Text)}}}")]-internal class UISingleLineTextInput : UITitledElement, IUISingleLineTextInput+internal class UISingleLineTextInput : UITitledElementWithChildren, IUISingleLineTextInput{private string? _text;private bool _isReadOnly;@@ -70,6 +70,14 @@ internal class UISingleLineTextInput : UITitledElement, IUISingleLineTextInput{}+ protected override IEnumerable<IUIElement> GetChildren()+ {+ if (CommandBarExtraContent is not null)+ {+ yield return CommandBarExtraContent;+ }+ }+public string Text{get => _text ?? string.Empty;diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISplitGrid.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISplitGrid.csindex f1a5443b9..023833ce3 100644--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISplitGrid.cs+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISplitGrid.cs@@ -3,7 +3,7 @@/// <summary>/// A component that splits horizontally or vertically an area into two panes. Panes can be resized by the user./// </summary>-public interface IUISplitGrid : IUIElement+public interface IUISplitGrid : IUIElementWithChildren{/// <summary>/// Gets the orientation of the grid gutter.@@ -71,7 +71,7 @@ public interface IUISplitGrid : IUIElement}[DebuggerDisplay($"Id = {{{nameof(Id)}}}, Orientation = {{{nameof(Orientation)}}}")]-internal sealed class UISplitGrid : UIElement, IUISplitGrid+internal sealed class UISplitGrid : UIElementWithChildren, IUISplitGrid{private UIOrientation _orientation = UIOrientation.Vertical;private int _minimumCellLength = 50;@@ -85,6 +85,19 @@ internal sealed class UISplitGrid : UIElement, IUISplitGrid{}+ protected override IEnumerable<IUIElement> GetChildren()+ {+ if (_rightOrBottomCellContent is not null)+ {+ yield return _rightOrBottomCellContent;+ }++ if (_leftOrTopCellContent is not null)+ {+ yield return _leftOrTopCellContent;+ }+ }+public UIOrientation Orientation{get => _orientation;diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIStack.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIStack.csindex 46d50d694..982ea90a1 100644--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIStack.cs+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIStack.cs@@ -3,7 +3,7 @@/// <summary>/// A component that stacks child elements into a single line that can be oriented horizontally or vertically./// </summary>-public interface IUIStack : IUIElement+public interface IUIStack : IUIElementWithChildren{/// <summary>/// Gets a value that indicates the dimension by which child elements are stacked.@@ -39,7 +39,7 @@ public interface IUIStack : IUIElement}[DebuggerDisplay($"Id = {{{nameof(Id)}}}, Orientation = {{{nameof(Orientation)}}}")]-internal sealed class UIStack : UIElement, IUIStack+internal sealed class UIStack : UIElementWithChildren, IUIStack{private UIOrientation _orientation = UIOrientation.Horizontal;private UISpacing _spacing = UISpacing.Small;@@ -50,6 +50,20 @@ internal sealed class UIStack : UIElement, IUIStack{}+ protected override IEnumerable<IUIElement> GetChildren()+ {+ if (_children is not null)+ {+ foreach (IUIElement child in _children)+ {+ if (child is not null)+ {+ yield return child;+ }+ }+ }+ }+public UIOrientation Orientation{get => _orientation;diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUITitledElementWithChildren.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUITitledElementWithChildren.csnew file mode 100644index 000000000..3c76b473d--- /dev/null+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUITitledElementWithChildren.cs@@ -0,0 +1,28 @@+namespace DevToys.Api;++/// <summary>+/// A base interface for all UI elements that can have a title / header on top of the element along with children elements.+/// </summary>+public interface IUITitledElementWithChildren : IUITitledElement, IUIElementWithChildren+{+}++internal abstract class UITitledElementWithChildren : UITitledElement, IUIElementWithChildren+{+ protected UITitledElementWithChildren(string? id)+ : base(id)+ {+ }++ protected abstract IEnumerable<IUIElement> GetChildren();++ public IUIElement? GetChildElementById(string id)+ {+ if (string.IsNullOrEmpty(id))+ {+ return null;+ }++ return UIElementWithChildren.GetChildElementById(id, GetChildren());+ }+}diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIWrap.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIWrap.csindex 1a9a41389..e4e3ad88b 100644--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIWrap.cs+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIWrap.cs@@ -3,7 +3,7 @@/// <summary>/// A component that stacks child elements horizontally and stack them into multiple lines if there's not enough space to keep everything on a single line./// </summary>-public interface IUIWrap : IUIElement+public interface IUIWrap : IUIElementWithChildren{/// <summary>/// Gets a value that indicates the space between stacked elements.@@ -28,7 +28,7 @@ public interface IUIWrap : IUIElement}[DebuggerDisplay($"Id = {{{nameof(Id)}}}")]-internal sealed class UIWrap : UIElement, IUIWrap+internal sealed class UIWrap : UIElementWithChildren, IUIWrap{private UISpacing _spacing = UISpacing.Small;private IUIElement[]? _children;@@ -38,6 +38,20 @@ internal sealed class UIWrap : UIElement, IUIWrap{}+ protected override IEnumerable<IUIElement> GetChildren()+ {+ if (_children is not null)+ {+ foreach (IUIElement child in _children)+ {+ if (child is not null)+ {+ yield return child;+ }+ }+ }+ }+public UISpacing Spacing{get => _spacing;diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/UIToolView.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/UIToolView.csindex 07ccd4352..d672c9130 100644--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/UIToolView.cs+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/UIToolView.cs@@ -94,6 +94,21 @@ public class UIToolView : INotifyPropertyChangedpublic event PropertyChangedEventHandler? PropertyChanged;+ /// <summary>+ /// Gets the first child element with the specified id.+ /// </summary>+ /// <param name="id">The id of a child element.</param>+ /// <returns>Returns null if the element could not be found. If many elements have the name id, this method returns the first it finds</returns>+ public IUIElement? GetChildElementById(string id)+ {+ if (RootElement is IUIElementWithChildren uiElementWithChildren)+ {+ return uiElementWithChildren.GetChildElementById(id);+ }++ return null;+ }+/// <summary>/// Shows the dialog box in the UI. Only one dialog can be opened at a time./// </summary>