Skip to content

A Swiss Army knife for developers.

Added DataGrid

Etienne Baudoux5966e49cf931
diff --git a/src/app/dev/DevToys.Api/IDataGridRow.cs b/src/app/dev/DevToys.Api/IDataGridRow.cs
new file mode 100644
index 000000000..c9903b200
--- /dev/null
+++ b/src/app/dev/DevToys.Api/IDataGridRow.cs
@@ -0,0 +1,9 @@
+namespace DevToys.Api;
+
+public interface IDataGridRow<T> where T : class
+{
+ /// <summary>
+ /// Gets the element to display as detail information in the row.
+ /// </summary>
+ T? Details { get; }
+}
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIDataGrid.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIDataGrid.cs
new file mode 100644
index 000000000..7167637b3
--- /dev/null
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIDataGrid.cs
@@ -0,0 +1,227 @@
+using System.Collections.ObjectModel;
+
+namespace DevToys.Api;
+
+/// <summary>
+/// A component that represents a grid that can display data with rows and columns.
+/// </summary>
+public interface IUIDataGrid : IUIElement
+{
+ /// <summary>
+ /// Gets the title of each column in the data grid.
+ /// </summary>
+ string[] Columns { get; }
+
+ /// <summary>
+ /// Gets the list of rows displayed in the data grid.
+ /// </summary>
+ ObservableCollection<IUIDataGridRow> Rows { get; }
+
+ /// <summary>
+ /// Gets whether rows are selectable in the data grid. Default is true.
+ /// </summary>
+ bool CanSelectRow { get; }
+
+ /// <summary>
+ /// Gets the selected row in the data grid.
+ /// </summary>
+ IUIDataGridRow? SelectedRow { get; }
+
+ /// <summary>
+ /// Gets the action to run when the user selects an row in the data grid.
+ /// </summary>
+ Func<IUIDataGridRow?, ValueTask>? OnRowSelectedAction { get; }
+
+ /// <summary>
+ /// Raised when <see cref="CanSelectRow"/> is changed.
+ /// </summary>
+ event EventHandler? CanSelectRowChanged;
+
+ /// <summary>
+ /// Raised when <see cref="SelectedRow"/> is changed.
+ /// </summary>
+ event EventHandler? SelectedRowChanged;
+}
+
+[DebuggerDisplay($"Id = {{{nameof(Id)}}}, SelectedRow = {{{nameof(SelectedRow)}}}")]
+internal sealed class UIDataGrid : UIElement, IUIDataGrid, IDisposable
+{
+ private readonly ObservableCollection<IUIDataGridRow> _rows = new();
+ private string[] _columns = Array.Empty<string>();
+ private IUIDataGridRow? _selectedRow;
+ private bool _canSelectRow = true;
+
+ internal UIDataGrid(string? id)
+ : base(id)
+ {
+ _rows.CollectionChanged += Rows_CollectionChanged;
+ }
+
+ public string[] Columns
+ {
+ get => _columns;
+ internal set => SetPropertyValue(ref _columns, value, ColumnsChanged);
+ }
+
+ public ObservableCollection<IUIDataGridRow> Rows => _rows;
+
+ public bool CanSelectRow
+ {
+ get => _canSelectRow;
+ internal set
+ {
+ SetPropertyValue(ref _canSelectRow, value, CanSelectRowChanged);
+ if (!value)
+ {
+ SelectedRow = null;
+ }
+ }
+ }
+
+ public IUIDataGridRow? SelectedRow
+ {
+ get => _selectedRow;
+ internal set
+ {
+ if (_selectedRow != value)
+ {
+ _selectedRow = value;
+ OnRowSelectedAction?.Invoke(_selectedRow);
+ SelectedRowChanged?.Invoke(this, EventArgs.Empty);
+ OnPropertyChanged();
+ }
+ }
+ }
+
+ public Func<IUIDataGridRow?, ValueTask>? OnRowSelectedAction { get; internal set; }
+
+ public event EventHandler? CanSelectRowChanged;
+ public event EventHandler? SelectedRowChanged;
+ public event EventHandler? ColumnsChanged;
+
+ public void Dispose()
+ {
+ _rows.CollectionChanged -= Rows_CollectionChanged;
+ }
+
+ private void Rows_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
+ {
+ // Try to preserve the selection.
+ this.Select(SelectedRow);
+ }
+}
+
+public static partial class GUI
+{
+ /// <summary>
+ /// A component that represents a grid that can display data in with rows and columns.
+ /// </summary>
+ public static IUIDataGrid DataGrid()
+ {
+ return DataGrid(null);
+ }
+
+ /// <summary>
+ /// A component that represents a grid that can display data in with rows and columns.
+ /// </summary>
+ /// <param name="id">An optional unique identifier for this UI element.</param>
+ public static IUIDataGrid DataGrid(string? id)
+ {
+ return new UIDataGrid(id);
+ }
+
+ /// <summary>
+ /// Sets the title of each column in the data grid.
+ /// </summary>
+ public static IUIDataGrid WithColumns(this IUIDataGrid element, params string[] columns)
+ {
+ var dataGrid = (UIDataGrid)element;
+ dataGrid.Columns = columns ?? Array.Empty<string>();
+
+ return element;
+ }
+
+ /// <summary>
+ /// Sets the <see cref="IUIDataGrid.Rows"/> of the data grid.
+ /// </summary>
+ public static IUIDataGrid WithRows(this IUIDataGrid element, params IUIDataGridRow[] rows)
+ {
+ var dataGrid = (UIDataGrid)element;
+ dataGrid.Rows.Clear();
+ dataGrid.Rows.AddRange(rows);
+
+ return element;
+ }
+
+ /// <summary>
+ /// Sets the action to run when selecting an item in the data grid.
+ /// </summary>
+ public static IUIDataGrid OnRowSelected(this IUIDataGrid element, Func<IUIDataGridRow?, ValueTask>? onRowSelectedAction)
+ {
+ ((UIDataGrid)element).OnRowSelectedAction = onRowSelectedAction;
+ return element;
+ }
+
+ /// <summary>
+ /// Sets the <see cref="IUIDataGridRow"/> that should be selected in the data grid.
+ /// If <paramref name="row"/> is null or does not exist in the data grid, no row will be selected.
+ /// </summary>
+ public static IUIDataGrid Select(this IUIDataGrid element, IUIDataGridRow? row)
+ {
+ var dataGrid = (UIDataGrid)element;
+ if ((row is not null
+ && dataGrid.Rows is not null
+ && !dataGrid.Rows.Contains(row))
+ || !dataGrid.CanSelectRow)
+ {
+ dataGrid.SelectedRow = null;
+ }
+ else
+ {
+ dataGrid.SelectedRow = row;
+ }
+
+ return element;
+ }
+
+ /// <summary>
+ /// Sets the <see cref="IUIDataGridRow"/> that should be selected in the data grid, using its index in the data grid.
+ /// If <paramref name="index"/> smaller or greater than the amount of rows in the data grid, no row will be selected.
+ /// </summary>
+ public static IUIDataGrid Select(this IUIDataGrid element, int index)
+ {
+ var dataGrid = (UIDataGrid)element;
+
+ if (dataGrid.Rows is null
+ || index < 0
+ || index > dataGrid.Rows.Count - 1
+ || !dataGrid.CanSelectRow)
+ {
+ dataGrid.SelectedRow = null;
+ }
+ else
+ {
+ dataGrid.SelectedRow = dataGrid.Rows[index];
+ }
+
+ return element;
+ }
+
+ /// <summary>
+ /// Allows the user to select a row in the data grid.
+ /// </summary>
+ public static IUIDataGrid AllowSelectItem(this IUIDataGrid element)
+ {
+ ((UIDataGrid)element).CanSelectRow = true;
+ return element;
+ }
+
+ /// <summary>
+ /// Prevents the user from selecting a row in the data grid.
+ /// </summary>
+ public static IUIDataGrid ForbidSelectItem(this IUIDataGrid element)
+ {
+ ((UIDataGrid)element).CanSelectRow = false;
+ return element;
+ }
+}
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIDataGridCell.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIDataGridCell.cs
new file mode 100644
index 000000000..c8c98d37e
--- /dev/null
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIDataGridCell.cs
@@ -0,0 +1,48 @@
+namespace DevToys.Api;
+
+/// <summary>
+/// A component that represents a cell in a <see cref="IUIDataGridRow"/>.
+/// </summary>
+public interface IUIDataGridCell
+{
+ /// <summary>
+ /// Gets the <see cref="IUIElement"/> to display in the cell.
+ /// </summary>
+ IUIElement? UIElement { get; }
+}
+
+[DebuggerDisplay($"UIElement = {{{nameof(UIElement)}}}")]
+internal sealed class UIDataGridCell : IUIDataGridCell
+{
+ internal UIDataGridCell(IUIElement uiElement)
+ {
+ UIElement = uiElement;
+ }
+
+ public IUIElement? UIElement { get; }
+}
+
+public static partial class GUI
+{
+ /// <summary>
+ /// Create component that represents a cell in a <see cref="IUIDataGridRow"/>.
+ /// </summary>
+ /// <param name="text">The text to display in the cell.</param>
+ public static IUIDataGridCell Cell(string? text)
+ {
+ return
+ new UIDataGridCell(
+ Label()
+ .NeverWrap()
+ .Text(text));
+ }
+
+ /// <summary>
+ /// Create component that represents a cell in a <see cref="IUIDataGridRow"/>.
+ /// </summary>
+ /// <param name="uiElement">The element to display in the cell.</param>
+ public static IUIDataGridCell Cell(IUIElement uiElement)
+ {
+ return new UIDataGridCell(uiElement);
+ }
+}
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIDataGridRow.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIDataGridRow.cs
new file mode 100644
index 000000000..8051eb477
--- /dev/null
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIDataGridRow.cs
@@ -0,0 +1,90 @@
+using System.Collections.ObjectModel;
+
+namespace DevToys.Api;
+
+/// <summary>
+/// A component that represents a row in a <see cref="IUIDataGrid"/>.
+/// </summary>
+public interface IUIDataGridRow : ICollection<IUIDataGridCell>, IDataGridRow<IUIElement>
+{
+ /// <summary>
+ /// Gets the value associated to the row.
+ /// </summary>
+ object? Value { get; }
+}
+
+[DebuggerDisplay($"Value = {{{nameof(Value)}}}")]
+internal sealed class UIDataGridRow : ObservableCollection<IUIDataGridCell>, IUIDataGridRow
+{
+ internal UIDataGridRow(object? value)
+ {
+ Value = value;
+ }
+
+ public object? Value { get; }
+
+ public IUIElement? Details { get; internal set; }
+}
+
+public static partial class GUI
+{
+ /// <summary>
+ /// Create component that represents an item in a list.
+ /// </summary>
+ /// <param name="value">The value associated to the item.</param>
+ /// <param name="cells">The cells to display in the row.</param>
+ public static IUIDataGridRow Row(object? value, params IUIDataGridCell[] cells)
+ {
+ var row = new UIDataGridRow(value);
+ row.AddRange(cells);
+ return row;
+ }
+
+ /// <summary>
+ /// Create component that represents an item in a list.
+ /// </summary>
+ /// <param name="value">The value associated to the item.</param>
+ /// <param name="cells">The cells to display in the row.</param>
+ public static IUIDataGridRow Row(object? value, params string[] cells)
+ {
+ var row = new UIDataGridRow(value);
+ if (cells is not null)
+ {
+ var gridCells = new IUIDataGridCell[cells.Length];
+ for (int i = 0; i < cells.Length; i++)
+ {
+ gridCells[i] = Cell(cells[i]);
+ }
+ row.AddRange(gridCells);
+ }
+
+ return row;
+ }
+
+ /// <summary>
+ /// Create component that represents an item in a list.
+ /// </summary>
+ /// <param name="value">The value associated to the item.</param>
+ /// <param name="details">The element to display as detail information in the row.</param>
+ /// <param name="cells">The cells to display in the row.</param>
+ public static IUIDataGridRow Row(object? value, IUIElement details, params IUIDataGridCell[] cells)
+ {
+ var row = (UIDataGridRow)Row(value, cells);
+ row.Details = details;
+ return row;
+ }
+
+ /// <summary>
+ /// Create component that represents an item in a list.
+ /// </summary>
+ /// <param name="value">The value associated to the item.</param>
+ /// <param name="details">The element to display as detail information in the row.</param>
+ /// <param name="cells">The cells to display in the row.</param>
+ public static IUIDataGridRow Row(object? value, IUIElement details, params string[] cells)
+ {
+ var row = (UIDataGridRow)Row(value, cells);
+ row.Details = details;
+ return row;
+ }
+}
+
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUILabel.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUILabel.cs
index 1d607cd8e..c0c79101b 100644
--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUILabel.cs
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUILabel.cs
@@ -16,6 +16,12 @@ public interface IUILabel : IUIElement
/// </summary>
UILabelStyle Style { get; }
+ /// <summary>
+ /// Gets whether the text can wrap if it can't fit within the available horizontal space.
+ /// Default is true.
+ /// </summary>
+ bool AutoWrap { get; }
+
/// <summary>
/// Raised when <see cref="Text"/> is changed.
/// </summary>
@@ -25,6 +31,11 @@ public interface IUILabel : IUIElement
/// Raised when <see cref="Style"/> is changed.
/// </summary>
event EventHandler? StyleChanged;
+
+ /// <summary>
+ /// Raised when <see cref="AutoWrap"/> is changed.
+ /// </summary>
+ event EventHandler? AutoWrapChanged;
}
[DebuggerDisplay($"Id = {{{nameof(Id)}}}, Text = {{{nameof(Text)}}}")]
@@ -32,6 +43,7 @@ internal sealed class UILabel : UIElement, IUILabel
{
private UILabelStyle _style = UILabelStyle.Body;
private string? _text;
+ private bool _autoWrap = true;
internal UILabel(string? id)
: base(id)
@@ -50,9 +62,17 @@ internal sealed class UILabel : UIElement, IUILabel
internal set => SetPropertyValue(ref _style, value, StyleChanged);
}
+ public bool AutoWrap
+ {
+ get => _autoWrap;
+ internal set => SetPropertyValue(ref _autoWrap, value, AutoWrapChanged);
+ }
+
public event EventHandler? TextChanged;
public event EventHandler? StyleChanged;
+
+ public event EventHandler? AutoWrapChanged;
}
public static partial class GUI
@@ -101,4 +121,22 @@ public static partial class GUI
((UILabel)element).Style = style;
return element;
}
+
+ /// <summary>
+ /// Sets the <see cref="IUILabel.AutoWrap"/> to true.
+ /// </summary>
+ public static IUILabel WrapIfNeeded(this IUILabel element)
+ {
+ ((UILabel)element).AutoWrap = true;
+ return element;
+ }
+
+ /// <summary>
+ /// Sets the <see cref="IUILabel.AutoWrap"/> to false.
+ /// </summary>
+ public static IUILabel NeverWrap(this IUILabel element)
+ {
+ ((UILabel)element).AutoWrap = false;
+ return element;
+ }
}
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/UIOrientation.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/UIOrientation.cs
index 12d2f5eb5..d88028dc5 100644
--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/UIOrientation.cs
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/UIOrientation.cs
@@ -3,15 +3,16 @@
/// <summary>
/// Defines the different orientations that an element or layout can have.
/// </summary>
+[Flags]
public enum UIOrientation
{
/// <summary>
/// Element or layout should be horizontally oriented.
/// </summary>
- Horizontal = 0,
+ Horizontal = 1,
/// <summary>
/// Element or layout should be vertically oriented.
/// </summary>
- Vertical = 1
+ Vertical = 2
}
diff --git a/src/app/dev/DevToys.Blazor/Assets/javascript/dom.ts b/src/app/dev/DevToys.Blazor/Assets/javascript/dom.ts
index 1fb3de75a..d5baccca0 100644
--- a/src/app/dev/DevToys.Blazor/Assets/javascript/dom.ts
+++ b/src/app/dev/DevToys.Blazor/Assets/javascript/dom.ts
@@ -38,6 +38,37 @@ class DOM {
css.innerHTML = fontDefinition;
document.head.appendChild(css);
}
+
+ public static registerDocumentEventService(dotNetObjRef: DotNet.DotNetObject): void {
+ (<any>document).documentEventService = dotNetObjRef;
+ }
+
+ public static subscribeDocumentEvent(eventName: string): void {
+ document.addEventListener(eventName, DOM.documentEventListener);
+ }
+
+ public static unsubscribeDocumentEvent(eventName: string): void {
+ document.removeEventListener(eventName, DOM.documentEventListener);
+ }
+
+ private static documentEventListener(e?: Event): void {
+ const eventJson = DOM.stringifyEvent(e);
+ const dotNetObjRef = (<DotNet.DotNetObject>(<any>document).documentEventService);
+ const eventName = e.type;
+ dotNetObjRef.invokeMethodAsync("EventCallback", eventName, eventJson);
+ }
+
+ private static stringifyEvent(e): string {
+ const obj = {};
+ for (const k in e) {
+ obj[k] = e[k];
+ }
+ return JSON.stringify(obj, (k, v) => {
+ if (v instanceof Node) return "Node";
+ if (v instanceof Window) return "Window";
+ return v;
+ }, " ");
+ }
}
export default DOM;
\ No newline at end of file
diff --git a/src/app/dev/DevToys.Blazor/Assets/sass/theme/macos/dark.scss b/src/app/dev/DevToys.Blazor/Assets/sass/theme/macos/dark.scss
index b39e2303c..613e75443 100644
--- a/src/app/dev/DevToys.Blazor/Assets/sass/theme/macos/dark.scss
+++ b/src/app/dev/DevToys.Blazor/Assets/sass/theme/macos/dark.scss
@@ -281,9 +281,9 @@
--list-box-item-background-pointer-over: var(--subtle-fill-secondary);
--list-box-item-background-selected: var(--subtle-fill-secondary);
--list-box-item-background-selected-disabled: var(--subtle-fill-secondary);
- --list-box-item-background-active: var(--subtle-fill-tertiary);
+ --list-box-item-background-pressed: var(--subtle-fill-tertiary);
--list-box-item-foreground: var(--text-fill-color-primary);
- --list-box-item-foreground-active: var(--text-fill-color-secondary);
+ --list-box-item-foreground-pressed: var(--text-fill-color-secondary);
--list-box-item-foreground-disabled: var(--text-fill-color-disabled);
/* Context Menu */
@@ -296,9 +296,9 @@
--context-menu-item-background: var(--subtle-fill-transparent);
--context-menu-item-background-disabled: var(--subtle-fill-transparent);
--context-menu-item-background-pointer-over: var(--subtle-fill-secondary);
- --context-menu-item-background-active: var(--subtle-fill-tertiary);
+ --context-menu-item-background-pressed: var(--subtle-fill-tertiary);
--context-menu-item-foreground: var(--text-fill-color-primary);
- --context-menu-item-foreground-active: var(--text-fill-color-secondary);
+ --context-menu-item-foreground-pressed: var(--text-fill-color-secondary);
--context-menu-item-foreground-disabled: var(--text-fill-color-disabled);
--context-menu-item-key-accelerator-foreground: var(--text-fill-color-secondary);
--context-menu-item-key-accelerator-foreground-disabled: var(--text-fill-color-disabled);
@@ -353,6 +353,20 @@
--info-bar-border-thickness: 1px;
--info-bar-border-corner-radius: var(--control-corner-radius);
+ /* DataGrid */
+ --data-grid-column-header-foreground: var(--text-fill-color-tertiary);
+ --data-grid-column-header-background: var(--control-fill-color-default);
+ --data-grid-column-header-background-pointer-over: rgba(255, 255, 255, 0.15);
+ --data-grid-column-header-background-pressed: var(--subtle-fill-tertiary);
+ --data-grid-row-foreground: var(--text-fill-color-primary);
+ --data-grid-row-background: var(--control-fill-color-default);
+ --data-grid-row-background-pointer-over: rgba(255, 255, 255, 0.15);
+ --data-grid-row-odd-background: rgba(255, 255, 255, 0.095);
+ --data-grid-row-odd-background-pointer-over: rgba(255, 255, 255, 0.18);
+ --data-grid-row-selected-background: rgba(var(--accent-base), 0.9);
+ --data-grid-row-selected-background-pointer-over: rgba(var(--accent-base), 0.8);
+ --data-grid-border: var(--control-stroke-color-default);
+
/* UISettingGroup */
--ui-setting-group-presenter-inner-ui-setting-background-color: transparent;
diff --git a/src/app/dev/DevToys.Blazor/Assets/sass/theme/windows/dark.scss b/src/app/dev/DevToys.Blazor/Assets/sass/theme/windows/dark.scss
index caa28560b..283142101 100644
--- a/src/app/dev/DevToys.Blazor/Assets/sass/theme/windows/dark.scss
+++ b/src/app/dev/DevToys.Blazor/Assets/sass/theme/windows/dark.scss
@@ -281,9 +281,9 @@
--list-box-item-background-pointer-over: var(--subtle-fill-secondary);
--list-box-item-background-selected: var(--subtle-fill-secondary);
--list-box-item-background-selected-disabled: var(--subtle-fill-secondary);
- --list-box-item-background-active: var(--subtle-fill-tertiary);
+ --list-box-item-background-pressed: var(--subtle-fill-tertiary);
--list-box-item-foreground: var(--text-fill-color-primary);
- --list-box-item-foreground-active: var(--text-fill-color-secondary);
+ --list-box-item-foreground-pressed: var(--text-fill-color-secondary);
--list-box-item-foreground-disabled: var(--text-fill-color-disabled);
/* Context Menu */
@@ -296,9 +296,9 @@
--context-menu-item-background: var(--subtle-fill-transparent);
--context-menu-item-background-disabled: var(--subtle-fill-transparent);
--context-menu-item-background-pointer-over: var(--subtle-fill-secondary);
- --context-menu-item-background-active: var(--subtle-fill-tertiary);
+ --context-menu-item-background-pressed: var(--subtle-fill-tertiary);
--context-menu-item-foreground: var(--text-fill-color-primary);
- --context-menu-item-foreground-active: var(--text-fill-color-secondary);
+ --context-menu-item-foreground-pressed: var(--text-fill-color-secondary);
--context-menu-item-foreground-disabled: var(--text-fill-color-disabled);
--context-menu-item-key-accelerator-foreground: var(--text-fill-color-secondary);
--context-menu-item-key-accelerator-foreground-disabled: var(--text-fill-color-disabled);
@@ -353,6 +353,20 @@
--info-bar-border-thickness: 1px;
--info-bar-border-corner-radius: var(--control-corner-radius);
+ /* DataGrid */
+ --data-grid-column-header-foreground: var(--text-fill-color-tertiary);
+ --data-grid-column-header-background: var(--control-fill-color-default);
+ --data-grid-column-header-background-pointer-over: rgba(255, 255, 255, 0.15);
+ --data-grid-column-header-background-pressed: var(--subtle-fill-tertiary);
+ --data-grid-row-foreground: var(--text-fill-color-primary);
+ --data-grid-row-background: var(--control-fill-color-default);
+ --data-grid-row-background-pointer-over: rgba(255, 255, 255, 0.15);
+ --data-grid-row-odd-background: rgba(255, 255, 255, 0.095);
+ --data-grid-row-odd-background-pointer-over: rgba(255, 255, 255, 0.18);
+ --data-grid-row-selected-background: rgba(var(--accent-base), 0.9);
+ --data-grid-row-selected-background-pointer-over: rgba(var(--accent-base), 0.8);
+ --data-grid-border: var(--control-stroke-color-default);
+
/* UISettingGroup */
--ui-setting-group-presenter-inner-ui-setting-background-color: transparent;
diff --git a/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGrid.razor b/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGrid.razor
new file mode 100644
index 000000000..01a72ecc1
--- /dev/null
+++ b/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGrid.razor
@@ -0,0 +1,110 @@
+@namespace DevToys.Blazor.Components
+@typeparam TRow
+@typeparam TCell
+@typeparam TRowDetail
+@inherits JSStyledComponentBase
+
+<CascadingValue Value="this" IsFixed="true">
+ <CascadingValue Name="ParentIsEnabled" Value="@IsActuallyEnabled">
+ <Container HorizontalAlignment="@HorizontalAlignment"
+ VerticalAlignment="@VerticalAlignment"
+ Width="@Width"
+ Height="@Height"
+ MarginLeft="@MarginLeft"
+ MarginRight="@MarginRight"
+ MarginTop="@MarginTop"
+ MarginBottom="@MarginBottom"
+ PaddingLeft="@PaddingLeft"
+ PaddingRight="@PaddingRight"
+ PaddingTop="@PaddingTop"
+ PaddingBottom="@PaddingBottom"
+ IsEnabled="@IsActuallyEnabled"
+ IsVisible="@IsVisible">
+ <div class="data-grid-container">
+ <ScrollViewer Orientation="@(UIOrientation.Vertical | UIOrientation.Horizontal)"
+ IsEnabled="@IsActuallyEnabled"
+ Class="@FinalCssClasses"
+ Style="@Style"
+ @attributes="@AdditionalAttributes">
+ <table @ref=Element class="data-grid @((IsResizing ? "resizing" : string.Empty))">
+ <thead>
+ @if (Columns is not null)
+ {
+ foreach (string column in Columns)
+ {
+ <DataGridColumnHeader TCell="TCell"
+ TRow="TRow"
+ TRowDetail="TRowDetail"
+ Title="@column"/>
+ }
+ }
+ </thead>
+ <tbody>
+ @if (Virtualize)
+ {
+ <Virtualize Items="@Rows" Context="row">
+ @RenderRow(row)
+ </Virtualize>
+ }
+ else if (Rows is not null)
+ {
+ foreach (var row in Rows)
+ {
+ RenderRow(row);
+ }
+ }
+ </tbody>
+ </table>
+ </ScrollViewer>
+ <div class="data-grid-underline" />
+ </div>
+ </Container>
+ </CascadingValue>
+</CascadingValue>
+
+@code {
+ private RenderFragment<TRow> RenderRow { get; set; } = default!;
+
+ protected override Task OnInitializedAsync()
+ {
+ RenderRow = (row) => __builder =>
+ {
+ Guard.IsNotNull(Rows);
+ bool isOdd = Rows.IndexOf(row) % 2 == 1;
+ <tr class="@($"{(isOdd ? "odd" : string.Empty)} {(object.ReferenceEquals(row, SelectedRow) ? "selected" : string.Empty)}")"
+ @onclick="@(() => OnRowClick(row))">
+ @if (row is not null)
+ {
+ var columnCount = 0;
+ foreach (var cell in row)
+ {
+ if (Columns is null || columnCount == Columns.Length)
+ {
+ // Make sure we don't write more cell than columns.
+ break;
+ }
+
+ <td>
+ @CellTemplate(cell)
+ </td>
+
+ columnCount++;
+ }
+ }
+ </tr>
+
+ @if ((object.ReferenceEquals(row, SelectedRow) || !CanSelectRow)
+ && row is not null
+ && row.Details is not null)
+ {
+ <tr class="@($"{(isOdd ? "odd" : string.Empty)} {(CanSelectRow ? "selected" : string.Empty)}")">
+ <td colspan="1000">
+ @RowDetailTemplate(row.Details)
+ </td>
+ </tr>
+ }
+ };
+
+ return base.OnInitializedAsync();
+ }
+}
\ No newline at end of file
diff --git a/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGrid.razor.cs b/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGrid.razor.cs
new file mode 100644
index 000000000..9f4577e7f
--- /dev/null
+++ b/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGrid.razor.cs
@@ -0,0 +1,302 @@
+using System.Collections.Specialized;
+using DevToys.Blazor.Core.Services;
+using Microsoft.AspNetCore.Components.Web;
+
+namespace DevToys.Blazor.Components;
+
+public partial class DataGrid<TRow, TCell, TRowDetail> : JSStyledComponentBase, IFocusable
+ where TRow : class, ICollection<TCell>, IDataGridRow<TRowDetail>
+ where TCell : class
+ where TRowDetail : class
+{
+ private readonly Lazy<DataGridColumnResizeService<TRow, TCell, TRowDetail>> _dataGridColumnResizeService;
+ private readonly List<DataGridColumnHeader<TRow, TCell, TRowDetail>> _columns = new();
+
+ private int _oldSelectedIndex = int.MinValue;
+ private TRow? _oldSelectedItem = default;
+
+ protected override string? JavaScriptFile => "./_content/DevToys.Blazor/Components/Collections/DataGrid/DataGrid.razor.js";
+
+ [Inject]
+ internal DocumentEventService DocumentEventService { get; set; } = default!;
+
+ [Parameter]
+ public string[]? Columns { get; set; }
+
+ [Parameter]
+ public IList<TRow>? Rows { get; set; }
+
+ [Parameter]
+ public RenderFragment<TCell> CellTemplate { get; set; } = default!;
+
+ [Parameter]
+ public RenderFragment<TRowDetail> RowDetailTemplate { get; set; } = default!;
+
+ [Parameter]
+ public TRow? SelectedRow { get; set; }
+
+ [Parameter]
+ public int SelectedIndex { get; set; } = -1;
+
+ [Parameter]
+ public EventCallback<int> SelectedIndexChanged { get; set; }
+
+ [Parameter]
+ public EventCallback<TRow> SelectedRowChanged { get; set; }
+
+ [Parameter]
+ public bool CanSelectRow { get; set; } = true;
+
+ [Parameter]
+ public bool Virtualize { get; set; } = true;
+
+ [Parameter]
+ public virtual bool RaiseSelectionEventOnKeyboardNavigation { get; set; } = true;
+
+ internal bool IsResizing { get; set; }
+
+ public DataGrid()
+ {
+ _dataGridColumnResizeService = new(() =>
+ {
+ Guard.IsNotNull(DocumentEventService);
+ return new DataGridColumnResizeService<TRow, TCell, TRowDetail>(this, DocumentEventService);
+ });
+ }
+
+ public ValueTask<bool> FocusAsync()
+ {
+ return JSRuntime.InvokeVoidWithErrorHandlingAsync("devtoys.DOM.setFocus", Element);
+ }
+
+ internal void AddColumn(DataGridColumnHeader<TRow, TCell, TRowDetail> dataGridColumnHeader)
+ {
+ _columns.Add(dataGridColumnHeader);
+ }
+
+ internal void RemoveColumn(DataGridColumnHeader<TRow, TCell, TRowDetail> dataGridColumnHeader)
+ {
+ _columns.Remove(dataGridColumnHeader);
+ }
+
+ internal async Task<double> GetActualHeightAsync()
+ {
+ using (await Semaphore.WaitAsync(CancellationToken.None))
+ {
+ Guard.IsNotNull(Element);
+ return await (await JSModule).InvokeAsync<double>("getActualHeight", Element);
+ }
+ }
+
+ internal Task<bool> StartResizeColumn(DataGridColumnHeader<TRow, TCell, TRowDetail> dataGridColumnHeader, double clientX)
+ {
+ return _dataGridColumnResizeService.Value.StartResizeColumn(dataGridColumnHeader, clientX, _columns, isContainerResizeMode: false);
+ }
+
+ internal void SelectNextRow()
+ {
+ if (Rows is not null)
+ {
+ if (SelectedIndex == -1 || SelectedIndex == Rows.Count - 1)
+ {
+ SetSelectedIndex(0, raiseEvent: RaiseSelectionEventOnKeyboardNavigation);
+ }
+ else
+ {
+ SetSelectedIndex(SelectedIndex + 1, raiseEvent: RaiseSelectionEventOnKeyboardNavigation);
+ }
+ }
+ }
+
+ internal void SelectPreviousRow()
+ {
+ if (Rows is not null)
+ {
+ if (SelectedIndex == -1 || SelectedIndex == 0)
+ {
+ SetSelectedIndex(Rows.Count - 1, raiseEvent: RaiseSelectionEventOnKeyboardNavigation);
+ }
+ else
+ {
+ SetSelectedIndex(SelectedIndex - 1, raiseEvent: RaiseSelectionEventOnKeyboardNavigation);
+ }
+ }
+ }
+
+ internal void SetSelectedIndex(int index)
+ {
+ SetSelectedIndex(index, raiseEvent: false);
+ }
+
+ internal void SetSelectedRow(TRow? row)
+ {
+ SetSelectedRow(row, raiseEvent: false);
+ }
+
+ public override ValueTask DisposeAsync()
+ {
+ if (Rows is INotifyCollectionChanged notifyCollection)
+ {
+ notifyCollection.CollectionChanged -= OnItemsChanged;
+ }
+ return base.DisposeAsync();
+ }
+
+ protected override void OnParametersSet()
+ {
+ base.OnParametersSet();
+
+ if (_oldSelectedIndex != SelectedIndex)
+ {
+ SetSelectedIndex(SelectedIndex, raiseEvent: false);
+ }
+ else if (_oldSelectedItem != SelectedRow)
+ {
+ SetSelectedRow(SelectedRow, raiseEvent: false);
+ }
+ }
+
+ protected override void OnAfterRender(bool firstRender)
+ {
+ base.OnAfterRender(firstRender);
+
+ if (firstRender)
+ {
+ if (Rows is INotifyCollectionChanged notifyCollection)
+ {
+ notifyCollection.CollectionChanged += OnItemsChanged;
+ }
+ }
+ }
+
+ private void SetSelectedIndex(int index, bool raiseEvent)
+ {
+ if (!CanSelectRow)
+ {
+ return;
+ }
+
+ if (index >= Rows?.Count)
+ {
+ ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index));
+ }
+
+ SelectedIndex = index;
+ _oldSelectedIndex = index;
+ if (index > -1)
+ {
+ Guard.IsNotNull(Rows);
+ SelectedRow = Rows.ElementAt(index);
+ }
+ else
+ {
+ SelectedRow = default;
+ }
+ _oldSelectedItem = SelectedRow;
+
+ if (raiseEvent)
+ {
+ RaiseOnSelectionChanged();
+ }
+
+ StateHasChanged();
+ }
+
+ private void SetSelectedRow(TRow? row, bool raiseEvent)
+ {
+ Guard.IsNotNull(Rows);
+
+ if (!CanSelectRow)
+ {
+ return;
+ }
+ else if (row == SelectedRow && !raiseEvent)
+ {
+ return;
+ }
+ else if (row is null)
+ {
+ SetSelectedIndex(-1, raiseEvent);
+ }
+ else if (Rows is IList<TRow> rowsList)
+ {
+ int itemIndex = rowsList.IndexOf(row);
+ SetSelectedIndex(itemIndex, raiseEvent);
+ }
+ else
+ {
+ int i = 0;
+ foreach (TRow childRow in Rows)
+ {
+ if (object.ReferenceEquals(childRow, row))
+ {
+ SetSelectedIndex(i, raiseEvent);
+ return;
+ }
+ i++;
+ }
+
+ SetSelectedIndex(-1, raiseEvent);
+ }
+ }
+
+ private void RaiseOnSelectionChanged()
+ {
+ if (CanSelectRow)
+ {
+ OnRowSelected();
+
+ if (SelectedIndexChanged.HasDelegate)
+ {
+ SelectedIndexChanged.InvokeAsync(SelectedIndex);
+ }
+
+ if (SelectedRowChanged.HasDelegate)
+ {
+ SelectedRowChanged.InvokeAsync(SelectedRow);
+ }
+ }
+ }
+
+ private void OnItemsChanged(object? sender, NotifyCollectionChangedEventArgs e)
+ {
+ SetSelectedRow(SelectedRow, raiseEvent: false);
+ }
+
+ protected void OnRowClick(object? selectedRow)
+ {
+ SetSelectedRow(selectedRow as TRow, raiseEvent: true);
+ }
+
+ protected void OnKeyDown(KeyboardEventArgs ev)
+ {
+ if (Rows is not null && Rows.Count > 0)
+ {
+ if (string.Equals(ev.Code, "Enter", StringComparison.OrdinalIgnoreCase)
+ || string.Equals(ev.Code, "Space", StringComparison.OrdinalIgnoreCase))
+ {
+ RaiseOnSelectionChanged();
+ }
+ else if (string.Equals(ev.Code, "ArrowDown", StringComparison.OrdinalIgnoreCase))
+ {
+ SelectNextRow();
+ }
+ else if (string.Equals(ev.Code, "ArrowUp", StringComparison.OrdinalIgnoreCase))
+ {
+ SelectPreviousRow();
+ }
+ else if (string.Equals(ev.Code, "Home", StringComparison.OrdinalIgnoreCase))
+ {
+ SetSelectedIndex(Math.Min(0, Rows.Count - 1));
+ }
+ else if (string.Equals(ev.Code, "End", StringComparison.OrdinalIgnoreCase))
+ {
+ SetSelectedIndex(Rows.Count - 1);
+ }
+ }
+ }
+
+ protected virtual void OnRowSelected()
+ {
+ }
+}
diff --git a/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGrid.razor.js b/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGrid.razor.js
new file mode 100644
index 000000000..b39528e69
--- /dev/null
+++ b/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGrid.razor.js
@@ -0,0 +1,4 @@
+export function getActualHeight(element) {
+ return element.getBoundingClientRect().height;
+}
+//# sourceMappingURL=DataGrid.razor.js.map
\ No newline at end of file
diff --git a/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGrid.razor.scss b/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGrid.razor.scss
new file mode 100644
index 000000000..5e46234c5
--- /dev/null
+++ b/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGrid.razor.scss
@@ -0,0 +1,95 @@
+.data-grid {
+ position: relative;
+ width: 100%;
+ height: 100%;
+ flex: 1;
+ border-collapse: collapse;
+ border-spacing: 0px;
+
+ &-container {
+ position: relative;
+ width: 100%;
+ height: 100%;
+ flex: 1;
+ border-radius: var(--control-corner-radius);
+ background-clip: padding-box;
+ border: 1px solid var(--data-grid-border);
+ }
+
+ &-underline {
+ position: absolute;
+ inset-inline-start: -1px;
+ inset-block-start: -1px;
+ inline-size: calc(100% + 2px);
+ block-size: calc(100% + 2px);
+ pointer-events: none;
+ border-radius: var(--control-corner-radius);
+ overflow: hidden;
+
+ &::after {
+ content: "";
+ box-sizing: border-box;
+ position: absolute;
+ inset-block-end: 0;
+ inset-inline-start: 0;
+ inline-size: 100%;
+ block-size: 100%;
+ border-bottom: 1px solid var(--control-strong-stroke-default);
+ }
+ }
+
+ &-resizer {
+ position: absolute;
+ top: 0;
+ right: 0;
+ width: 8px;
+ cursor: col-resize;
+ user-select: none;
+ }
+
+ thead {
+ th {
+ display: table-cell;
+ position: relative;
+ color: var(--data-grid-column-header-foreground);
+ background-color: var(--data-grid-column-header-background);
+ border-right: 1px solid var(--control-stroke-color-default);
+ border-bottom: 1px solid var(--control-stroke-color-default);
+
+ &:hover {
+ background-color: var(--data-grid-column-header-background-pointer-over);
+ }
+
+ &:active {
+ background-color: var(--data-grid-column-header-background-pressed);
+ }
+ }
+ }
+
+ tbody {
+ tr {
+ color: var(--data-grid-row-foreground);
+ background-color: var(--data-grid-row-background);
+
+ &:hover {
+ background-color: var(--data-grid-row-background-pointer-over);
+ }
+
+ &.odd {
+ background-color: var(--data-grid-row-odd-background);
+
+ &:hover {
+ background-color: var(--data-grid-row-odd-background-pointer-over);
+ }
+ }
+
+ &.selected {
+ background-color: var(--data-grid-row-selected-background) !important;
+
+ &:hover {
+ background-color: var(--data-grid-row-selected-background-pointer-over) !important;
+ }
+ }
+ }
+ }
+}
diff --git a/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGrid.razor.ts b/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGrid.razor.ts
new file mode 100644
index 000000000..54a43817c
--- /dev/null
+++ b/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGrid.razor.ts
@@ -0,0 +1,3 @@
+export function getActualHeight(element: HTMLElement): number {
+ return element.getBoundingClientRect().height;
+}
\ No newline at end of file
diff --git a/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGridColumnHeader.razor b/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGridColumnHeader.razor
new file mode 100644
index 000000000..f86648610
--- /dev/null
+++ b/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGridColumnHeader.razor
@@ -0,0 +1,17 @@
+@namespace DevToys.Blazor.Components
+@typeparam TRow
+@typeparam TCell
+@typeparam TRowDetail
+@inherits JSStyledComponentBase
+
+<th @ref=Element scope="col" colspan="1" style="@DataGridColumnHeaderStyle">
+ <TextBlock NoWrap="true"
+ CanTrim="true"
+ HorizontalAlignment="UIHorizontalAlignment.Left"
+ Text="@Title" />
+ <div class="@($"data-grid-resizer {(_isResizing ? "data-grid-resizing" : string.Empty)} {FinalCssClasses}")"
+ style="@ResizerStyle"
+ @onmousedown=OnResizerMouseDown
+ @onmouseover=OnResizerMouseOver
+ @onmouseleave=OnResizerMouseLeave />
+</th>
\ No newline at end of file
diff --git a/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGridColumnHeader.razor.cs b/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGridColumnHeader.razor.cs
new file mode 100644
index 000000000..293f0643d
--- /dev/null
+++ b/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGridColumnHeader.razor.cs
@@ -0,0 +1,100 @@
+using Microsoft.AspNetCore.Components.Web;
+
+namespace DevToys.Blazor.Components;
+
+public partial class DataGridColumnHeader<TRow, TCell, TRowDetail> : JSStyledComponentBase
+ where TRow : class, ICollection<TCell>, IDataGridRow<TRowDetail>
+ where TCell : class
+ where TRowDetail : class
+{
+ private double? _resizerHeight;
+ private bool _isResizing;
+
+ protected override string? JavaScriptFile => "./_content/DevToys.Blazor/Components/Collections/DataGrid/DataGridColumnHeader.razor.js";
+
+ protected string? DataGridColumnHeaderStyle => new StyleBuilder()
+ .AddStyle("width", Width!.ToPx(), when: Width.HasValue)
+ .AddStyle(Style)
+ .Build();
+
+ protected string? ResizerStyle => new StyleBuilder()
+ .AddStyle("height", _resizerHeight?.ToPx() ?? "100%")
+ .Build();
+
+ [Parameter]
+ public string? Title { get; set; }
+
+ [CascadingParameter]
+ protected DataGrid<TRow, TCell, TRowDetail>? DataGrid { get; set; }
+
+ protected override void OnInitialized()
+ {
+ Guard.IsNotNull(DataGrid);
+ DataGrid.AddColumn(this);
+
+ base.OnInitialized();
+ }
+
+ public override ValueTask DisposeAsync()
+ {
+ Guard.IsNotNull(DataGrid);
+ DataGrid.RemoveColumn(this);
+ return base.DisposeAsync();
+ }
+
+ internal async Task<double> UpdateColumnWidthAsync(double targetWidth, double gridHeight, bool finishResize)
+ {
+ if (targetWidth > 0)
+ {
+ _resizerHeight = gridHeight;
+ Width = (int)targetWidth;
+ await InvokeAsync(StateHasChanged);
+ }
+
+ if (finishResize)
+ {
+ _isResizing = false;
+ await InvokeAsync(StateHasChanged);
+ }
+
+ return await GetCurrentCellWidthAsync();
+ }
+
+ internal async Task<double> GetCurrentCellWidthAsync()
+ {
+ using (await Semaphore.WaitAsync(CancellationToken.None))
+ {
+ Guard.IsNotNull(Element);
+ return await (await JSModule).InvokeAsync<double>("getActualWidth", Element);
+ }
+ }
+
+ private async Task OnResizerMouseDown(MouseEventArgs args)
+ {
+ if (args.Detail > 1) // Double click clears the width, hence setting it to minimum size.
+ {
+ Width = null;
+ return;
+ }
+
+ Guard.IsNotNull(DataGrid);
+ _isResizing = await DataGrid.StartResizeColumn(this, args.ClientX);
+ }
+
+ private async Task OnResizerMouseOver()
+ {
+ if (!_isResizing)
+ {
+ Guard.IsNotNull(DataGrid);
+ _resizerHeight = await DataGrid.GetActualHeightAsync();
+ }
+ }
+
+ private void OnResizerMouseLeave()
+ {
+ if (!_isResizing)
+ {
+ _resizerHeight = null;
+ }
+ }
+}
diff --git a/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGridColumnHeader.razor.js b/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGridColumnHeader.razor.js
new file mode 100644
index 000000000..5e29dfc20
--- /dev/null
+++ b/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGridColumnHeader.razor.js
@@ -0,0 +1,4 @@
+export function getActualWidth(element) {
+ return element.getBoundingClientRect().width;
+}
+//# sourceMappingURL=DataGridColumnHeader.razor.js.map
\ No newline at end of file
diff --git a/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGridColumnHeader.razor.ts b/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGridColumnHeader.razor.ts
new file mode 100644
index 000000000..fcbe63f86
--- /dev/null
+++ b/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGridColumnHeader.razor.ts
@@ -0,0 +1,3 @@
+export function getActualWidth(element: HTMLElement): number {
+ return element.getBoundingClientRect().width;
+}
\ No newline at end of file
diff --git a/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGridColumnResizeService.cs b/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGridColumnResizeService.cs
new file mode 100644
index 000000000..b0a6b3519
--- /dev/null
+++ b/src/app/dev/DevToys.Blazor/Components/Collections/DataGrid/DataGridColumnResizeService.cs
@@ -0,0 +1,188 @@
+using System.Text.Json;
+using DevToys.Blazor.Core.Services;
+using Microsoft.AspNetCore.Components.Web;
+
+namespace DevToys.Blazor.Components;
+
+internal sealed class DataGridColumnResizeService<TRow, TCell, TRowDetail> : IAsyncDisposable
+ where TRow : class, ICollection<TCell>, IDataGridRow<TRowDetail>
+ where TCell : class
+ where TRowDetail : class
+{
+ private static readonly JsonSerializerOptions JsonOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
+ private const string EventMouseMove = "mousemove";
+ private const string EventMouseUp = "mouseup";
+
+ private readonly DocumentEventService _documentEventService;
+ private readonly DataGrid<TRow, TCell, TRowDetail> _dataGrid;
+ private bool _isContainerResizeMode;
+
+ private double _currentX;
+ private double _startWidth;
+ private double _nextStartWidth;
+ private DataGridColumnHeader<TRow, TCell, TRowDetail>? _startColumn;
+ private DataGridColumnHeader<TRow, TCell, TRowDetail>? _nextColumn;
+ private IAsyncDisposable? _mouseMoveSubscription;
+ private IAsyncDisposable? _mouseUpSubscription;
+
+ internal DataGridColumnResizeService(DataGrid<TRow, TCell, TRowDetail> dataGrid, DocumentEventService documentEventService)
+ {
+ _documentEventService = documentEventService;
+ _dataGrid = dataGrid;
+ }
+
+ public async ValueTask DisposeAsync()
+ {
+ await UnsubscribeApplicationEvents();
+ }
+
+ internal async Task<bool> StartResizeColumn(
+ DataGridColumnHeader<TRow, TCell, TRowDetail> headerCell,
+ double clientX,
+ IList<DataGridColumnHeader<TRow, TCell, TRowDetail>> columns,
+ bool isContainerResizeMode)
+ {
+ if (_mouseMoveSubscription != default || _mouseUpSubscription != default)
+ {
+ return false;
+ }
+
+ _isContainerResizeMode = isContainerResizeMode;
+ _currentX = clientX;
+
+ _startWidth = await headerCell.GetCurrentCellWidthAsync();
+ _startColumn = headerCell;
+
+ if (!_isContainerResizeMode)
+ {
+ // In case resize mode is column, we have to find any column right of the current one that can also be resized and is not hidden.
+ if (headerCell is not null)
+ {
+ DataGridColumnHeader<TRow, TCell, TRowDetail>? nextResizableColumn
+ = columns.Skip(columns.IndexOf(headerCell) + 1)
+ .FirstOrDefault(c => c.IsVisible);
+
+ if (nextResizableColumn == null)
+ {
+ return false;
+ }
+
+ _nextStartWidth = await nextResizableColumn.GetCurrentCellWidthAsync();
+ _nextColumn = nextResizableColumn;
+ }
+ }
+
+ _mouseMoveSubscription = await _documentEventService.SubscribeAsync(EventMouseMove, OnApplicationMouseMove);
+ _mouseUpSubscription = await _documentEventService.SubscribeAsync(EventMouseUp, OnApplicationMouseUp);
+
+ _dataGrid.IsResizing = true;
+ _dataGrid.StateHasChanged();
+ return true;
+ }
+
+ private async ValueTask OnApplicationMouseMove(string eventJson)
+ {
+ await ResizeColumn(JsonSerializer.Deserialize<MouseEventArgs>(eventJson, JsonOptions)!, false);
+ }
+
+ private async ValueTask OnApplicationMouseUp(string eventJson)
+ {
+ bool requiresUpdate = _mouseMoveSubscription != default || _mouseUpSubscription != default;
+
+ _dataGrid.IsResizing = false;
+ _dataGrid.StateHasChanged();
+ await UnsubscribeApplicationEvents();
+
+ if (requiresUpdate)
+ {
+ await ResizeColumn(JsonSerializer.Deserialize<MouseEventArgs>(eventJson, JsonOptions)!, true);
+ }
+ }
+
+ private async Task UnsubscribeApplicationEvents()
+ {
+ if (_mouseMoveSubscription != null)
+ {
+ await _mouseMoveSubscription.DisposeAsync();
+ _mouseMoveSubscription = null;
+ }
+
+ if (_mouseUpSubscription != null)
+ {
+ await _mouseUpSubscription.DisposeAsync();
+ _mouseUpSubscription = null;
+ }
+ }
+
+ private async Task ResizeColumn(MouseEventArgs mouseEventArgs, bool finish)
+ {
+ // Need to update height, because resizing of columns can lead to height changes in grid (due to line-breaks)
+ double gridHeight = await _dataGrid.GetActualHeightAsync();
+
+ double deltaX = mouseEventArgs.ClientX - _currentX;
+ double targetWidth = _startWidth + deltaX;
+
+ // Easy case: ResizeMode is container, we simply update the width of the resized column
+ if (_isContainerResizeMode)
+ {
+ if (_startColumn is not null)
+ {
+ await _startColumn.UpdateColumnWidthAsync(targetWidth, gridHeight, finish);
+ }
+
+ return;
+ }
+
+ // In case of column resize mode, we have to find another column that can be resized to
+ // enlarge/shrink this other column by the same amount, the current column shall be shrinked/enlarged.
+ double nextTargetWidth = _nextStartWidth - deltaX;
+
+ // In case we shrink the current column, make sure to not shrink further after min width has been reached:
+ if (deltaX < 0)
+ {
+ if (_startColumn is not null && _nextColumn is not null)
+ {
+ await ResizeColumns(
+ _startColumn,
+ _nextColumn,
+ targetWidth,
+ nextTargetWidth,
+ gridHeight,
+ finish);
+ }
+ }
+ // In case we enlarge, we first shrink the following column and ensure it is not shrinked beyond min width:
+ else
+ {
+ if (_nextColumn is not null && _startColumn is not null)
+ {
+ await ResizeColumns(
+ _nextColumn,
+ _startColumn,
+ nextTargetWidth,
+ targetWidth,
+ gridHeight,
+ finish);
+ }
+ }
+ }
+
+ private static async Task ResizeColumns(
+ DataGridColumnHeader<TRow, TCell, TRowDetail> columnToShrink,
+ DataGridColumnHeader<TRow, TCell, TRowDetail> columnToEnlarge,
+ double shrinkedWidth,
+ double enlargedWidth,
+ double gridHeight,
+ bool finish)
+ {
+ double actualWidth = await columnToShrink.UpdateColumnWidthAsync(shrinkedWidth, gridHeight, finish);
+
+ // Use actualWidth to see if the column could be made smaller or if it reached its min size.
+ if (actualWidth >= shrinkedWidth)
+ {
+ enlargedWidth -= (actualWidth - shrinkedWidth);
+ }
+
+ await columnToEnlarge.UpdateColumnWidthAsync(enlargedWidth, gridHeight, finish);
+ }
+}
diff --git a/src/app/dev/DevToys.Blazor/Components/Collections/ListBox/ListBox.razor b/src/app/dev/DevToys.Blazor/Components/Collections/ListBox/ListBox.razor
index 13e759d65..6e81993ad 100644
--- a/src/app/dev/DevToys.Blazor/Components/Collections/ListBox/ListBox.razor
+++ b/src/app/dev/DevToys.Blazor/Components/Collections/ListBox/ListBox.razor
@@ -39,7 +39,7 @@
{
foreach (var item in Items)
{
- @RenderItem(item)
+ RenderItem(item);
}
}
diff --git a/src/app/dev/DevToys.Blazor/Components/Collections/ListBox/ListBoxItem.razor.scss b/src/app/dev/DevToys.Blazor/Components/Collections/ListBox/ListBoxItem.razor.scss
index 25aeb6891..3e2ed8160 100644
--- a/src/app/dev/DevToys.Blazor/Components/Collections/ListBox/ListBoxItem.razor.scss
+++ b/src/app/dev/DevToys.Blazor/Components/Collections/ListBox/ListBoxItem.razor.scss
@@ -49,8 +49,8 @@
}
&:active {
- background-color: var(--list-box-item-background-active);
- color: var(--list-box-item-foreground-active);
+ background-color: var(--list-box-item-background-pressed);
+ color: var(--list-box-item-foreground-pressed);
&::before {
transform: scaleY(0.625);
diff --git a/src/app/dev/DevToys.Blazor/Components/Layout/ScrollViewer/ScrollViewer.razor.cs b/src/app/dev/DevToys.Blazor/Components/Layout/ScrollViewer/ScrollViewer.razor.cs
index 63b998cbb..1d2cb7f6b 100644
--- a/src/app/dev/DevToys.Blazor/Components/Layout/ScrollViewer/ScrollViewer.razor.cs
+++ b/src/app/dev/DevToys.Blazor/Components/Layout/ScrollViewer/ScrollViewer.razor.cs
@@ -64,8 +64,11 @@ public partial class ScrollViewer : JSStyledComponentBase
}
var scrollViewerCssClasses = new CssBuilder();
- if (((Orientation & UIOrientation.Vertical) != 0 && (Orientation & UIOrientation.Horizontal) != 0)
- || !IsScrollable)
+ if (((Orientation & UIOrientation.Vertical) != 0 && (Orientation & UIOrientation.Horizontal) != 0))
+ {
+ // Nothing. Scrollable in both axis.
+ }
+ else if (!IsScrollable)
{
scrollViewerCssClasses.AddClass("not-scrollable");
}
diff --git a/src/app/dev/DevToys.Blazor/Components/Menu/ContextMenu/ContextMenu.razor.scss b/src/app/dev/DevToys.Blazor/Components/Menu/ContextMenu/ContextMenu.razor.scss
index ef602aebc..9bfd678c3 100644
--- a/src/app/dev/DevToys.Blazor/Components/Menu/ContextMenu/ContextMenu.razor.scss
+++ b/src/app/dev/DevToys.Blazor/Components/Menu/ContextMenu/ContextMenu.razor.scss
@@ -41,8 +41,8 @@
}
&:active {
- background-color: var(--context-menu-item-background-active);
- color: var(--context-menu-item-foreground-active);
+ background-color: var(--context-menu-item-background-pressed);
+ color: var(--context-menu-item-foreground-pressed);
}
&.disabled {
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 24ed2dd14..98c2cbf45 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
@@ -1,5 +1,4 @@
using System.Collections.ObjectModel;
-using DevToys.Blazor.Components.Monaco.Editor;
using DevToys.Core.Settings;
using Microsoft.AspNetCore.Components.Forms;
diff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UIDataGridPresenter.razor b/src/app/dev/DevToys.Blazor/Components/UIElements/UIDataGridPresenter.razor
new file mode 100644
index 000000000..be78aa238
--- /dev/null
+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UIDataGridPresenter.razor
@@ -0,0 +1,26 @@
+@namespace DevToys.Blazor.Components.UIElements
+@using DevToys.Api;
+@using System.Collections.ObjectModel;
+@inherits ComponentBase
+
+<DataGrid id="@UIDataGrid.Id"
+ TRow="IUIDataGridRow"
+ TCell="IUIDataGridCell"
+ TRowDetail="IUIElement"
+ IsVisible="@UIDataGrid.IsVisible"
+ IsEnabled="@UIDataGrid.IsEnabled"
+ HorizontalAlignment="@UIDataGrid.HorizontalAlignment"
+ VerticalAlignment="@UIDataGrid.VerticalAlignment"
+ @bind-SelectedIndex="@SelectedIndex"
+ Columns="@UIDataGrid.Columns"
+ Rows="@UIDataGrid.Rows"
+ Context="element"
+ CanSelectRow="@UIDataGrid.CanSelectRow"
+ Virtualize="true">
+ <CellTemplate>
+ <UIElementPresenter UIElement="@element.UIElement" />
+ </CellTemplate>
+ <RowDetailTemplate>
+ <UIElementPresenter UIElement="@element" />
+ </RowDetailTemplate>
+</DataGrid>
\ No newline at end of file
diff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UIDataGridPresenter.razor.cs b/src/app/dev/DevToys.Blazor/Components/UIElements/UIDataGridPresenter.razor.cs
new file mode 100644
index 000000000..8eba51c7c
--- /dev/null
+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UIDataGridPresenter.razor.cs
@@ -0,0 +1,56 @@
+namespace DevToys.Blazor.Components.UIElements;
+
+public partial class UIDataGridPresenter : ComponentBase, IDisposable
+{
+ private int _selectedIndex = -1;
+
+ [Parameter]
+ public IUIDataGrid UIDataGrid { get; set; } = default!;
+
+ [Parameter]
+ public int SelectedIndex { get; set; }
+
+ protected override void OnInitialized()
+ {
+ base.OnInitialized();
+ UIDataGrid.SelectedRowChanged += UIDataGrid_SelectedRowChanged;
+ UIDataGrid_SelectedRowChanged(this, EventArgs.Empty);
+ }
+
+ protected override void OnParametersSet()
+ {
+ base.OnParametersSet();
+ if (_selectedIndex != SelectedIndex)
+ {
+ if (UIDataGrid.CanSelectRow)
+ {
+ _selectedIndex = SelectedIndex;
+ UIDataGrid.Select(SelectedIndex);
+ }
+ else
+ {
+ SelectedIndex = -1;
+ UIDataGrid.Select(-1);
+ }
+ }
+ }
+
+ private void UIDataGrid_SelectedRowChanged(object? sender, EventArgs e)
+ {
+ if (UIDataGrid.Rows is not null && UIDataGrid.SelectedRow is not null && UIDataGrid.CanSelectRow)
+ {
+ SelectedIndex = UIDataGrid.Rows.IndexOf(UIDataGrid.SelectedRow);
+ }
+ else
+ {
+ SelectedIndex = -1;
+ UIDataGrid.Select(-1);
+ }
+ }
+
+ public void Dispose()
+ {
+ UIDataGrid.SelectedRowChanged -= UIDataGrid_SelectedRowChanged;
+ GC.SuppressFinalize(this);
+ }
+}
diff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UIElementPresenter.razor b/src/app/dev/DevToys.Blazor/Components/UIElements/UIElementPresenter.razor
index d0e17e2e2..7937dbca4 100644
--- a/src/app/dev/DevToys.Blazor/Components/UIElements/UIElementPresenter.razor
+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UIElementPresenter.razor
@@ -86,6 +86,10 @@
<UIListPresenter UIList="@list" />
break;
+ case IUIDataGrid dataGrid:
+ <UIDataGridPresenter UIDataGrid="@dataGrid" />
+ 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
index 1a7c53446..e90038648 100644
--- a/src/app/dev/DevToys.Blazor/Components/UIElements/UILabelPresenter.razor
+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UILabelPresenter.razor
@@ -8,4 +8,6 @@
HorizontalAlignment="@UILabel.HorizontalAlignment"
VerticalAlignment="@UILabel.VerticalAlignment"
Text="@UILabel.Text"
+ NoWrap="@(!UILabel.AutoWrap)"
+ CanTrim="@(!UILabel.AutoWrap)"
Appearance="@Appearance" />
\ No newline at end of file
diff --git a/src/app/dev/DevToys.Blazor/Core/Services/DocumentEventService.cs b/src/app/dev/DevToys.Blazor/Core/Services/DocumentEventService.cs
new file mode 100644
index 000000000..bb40c9ad4
--- /dev/null
+++ b/src/app/dev/DevToys.Blazor/Core/Services/DocumentEventService.cs
@@ -0,0 +1,127 @@
+namespace DevToys.Blazor.Core.Services;
+
+public sealed class DocumentEventService : IDisposable
+{
+ private readonly IJSRuntime _jsRuntime;
+ private readonly DotNetObjectReference<DocumentEventService> _reference;
+ private readonly Dictionary<string, List<EventSubscription>> _callbackResolver = new();
+ private readonly DisposableSemaphore _semaphore = new();
+
+ private bool _isInitialized;
+
+ public DocumentEventService(IJSRuntime jsRuntime)
+ {
+ Guard.IsNotNull(jsRuntime);
+ _jsRuntime = jsRuntime;
+ _reference = DotNetObjectReference.Create(this);
+ }
+
+ public async Task<IAsyncDisposable> SubscribeAsync(string eventName, Func<string, ValueTask> callback)
+ {
+ using (await _semaphore.WaitAsync(CancellationToken.None))
+ {
+ if (!_isInitialized)
+ {
+ await _jsRuntime.InvokeVoidWithErrorHandlingAsync("devtoys.DOM.registerDocumentEventService", _reference);
+ _isInitialized = true;
+ }
+ }
+
+ (EventSubscription eventSubscription, bool firstTimeRegistered) = await RegisterCallBackAsync(eventName, callback);
+
+ if (firstTimeRegistered)
+ {
+ await _jsRuntime.InvokeVoidWithErrorHandlingAsync("devtoys.DOM.subscribeDocumentEvent", eventName);
+ }
+
+ return eventSubscription;
+ }
+
+ [JSInvokable]
+ public void EventCallback(string eventName, string eventJson)
+ {
+ if (_callbackResolver.TryGetValue(eventName, out List<EventSubscription>? listeners))
+ {
+ for (int i = 0; i < listeners.Count; i++)
+ {
+ listeners[i].Invoke(eventJson);
+ }
+ }
+ }
+
+ private async ValueTask UnsubscribeAsync(EventSubscription listener)
+ {
+ using (await _semaphore.WaitAsync(CancellationToken.None))
+ {
+ if (_callbackResolver.TryGetValue(listener.EventName, out List<EventSubscription>? listeners))
+ {
+ listeners.Remove(listener);
+
+ if (listeners.Count == 0)
+ {
+ _callbackResolver.Remove(listener.EventName);
+ try
+ {
+ await _jsRuntime.InvokeVoidWithErrorHandlingAsync("devtoys.DOM.unsubscribeDocumentEvent", listener.EventName);
+ }
+ catch (Exception)
+ {
+ }
+ }
+ }
+ }
+ }
+
+ private async Task<(EventSubscription, bool firstTimeRegistered)> RegisterCallBackAsync(string eventName, Func<string, ValueTask> callback)
+ {
+ using (await _semaphore.WaitAsync(CancellationToken.None))
+ {
+ bool firstTimeRegistered = false;
+ var eventSubscription = new EventSubscription(eventName, callback, this);
+
+ if (!_callbackResolver.TryGetValue(eventName, out List<EventSubscription>? listeners))
+ {
+ listeners = new List<EventSubscription>();
+ _callbackResolver[eventName] = listeners;
+ firstTimeRegistered = true;
+ }
+
+ listeners.Add(eventSubscription);
+
+ return new(eventSubscription, firstTimeRegistered);
+ }
+ }
+
+ public void Dispose()
+ {
+ _semaphore.Dispose();
+ }
+
+ private class EventSubscription : IAsyncDisposable
+ {
+ private readonly Func<string, ValueTask> _callback;
+ private readonly DocumentEventService _documentEventService;
+
+ internal EventSubscription(string eventName, Func<string, ValueTask> callback, DocumentEventService documentEventService)
+ {
+ Guard.IsNotNull(eventName);
+ Guard.IsNotNull(callback);
+ Guard.IsNotNull(documentEventService);
+ EventName = eventName;
+ _callback = callback;
+ _documentEventService = documentEventService;
+ }
+
+ internal string EventName { get; }
+
+ internal void Invoke(string eventJson)
+ {
+ _callback.Invoke(eventJson);
+ }
+
+ public async ValueTask DisposeAsync()
+ {
+ await _documentEventService.UnsubscribeAsync(this);
+ }
+ }
+}
diff --git a/src/app/dev/DevToys.Blazor/Pages/Index.razor.cs b/src/app/dev/DevToys.Blazor/Pages/Index.razor.cs
index 780a01be0..ea1212d8d 100644
--- a/src/app/dev/DevToys.Blazor/Pages/Index.razor.cs
+++ b/src/app/dev/DevToys.Blazor/Pages/Index.razor.cs
@@ -1,5 +1,4 @@
using DevToys.Blazor.Components;
-using DevToys.Blazor.Components.Monaco;
using DevToys.Blazor.Core.Services;
using DevToys.Business.ViewModels;
using DevToys.Core;
diff --git a/src/app/dev/DevToys.Blazor/Pages/SubPages/ToolGroup.razor.cs b/src/app/dev/DevToys.Blazor/Pages/SubPages/ToolGroup.razor.cs
index 5a09fdd1e..19accda8f 100644
--- a/src/app/dev/DevToys.Blazor/Pages/SubPages/ToolGroup.razor.cs
+++ b/src/app/dev/DevToys.Blazor/Pages/SubPages/ToolGroup.razor.cs
@@ -2,7 +2,6 @@
using DevToys.Core.Tools;
using DevToys.Core.Tools.ViewItems;
using DevToys.Blazor.Components;
-using System.Runtime.InteropServices;
namespace DevToys.Blazor.Pages.SubPages;
diff --git a/src/app/dev/DevToys.Tools/Tools/Sample/SampleToolFactory.cs b/src/app/dev/DevToys.Tools/Tools/Sample/SampleToolFactory.cs
index 89b0f886b..84be3b3be 100644
--- a/src/app/dev/DevToys.Tools/Tools/Sample/SampleToolFactory.cs
+++ b/src/app/dev/DevToys.Tools/Tools/Sample/SampleToolFactory.cs
@@ -1,5 +1,4 @@
using System.Text.Json;
-using System.Text.Json.Nodes;
using Microsoft.Extensions.Logging;
namespace DevToys.Tools.Tools.Sample;
diff --git a/src/app/dev/DevToys.Tools/Tools/Test/TestGuiTool.cs b/src/app/dev/DevToys.Tools/Tools/Test/TestGuiTool.cs
index b3e226fdf..badf5bbb8 100644
--- a/src/app/dev/DevToys.Tools/Tools/Test/TestGuiTool.cs
+++ b/src/app/dev/DevToys.Tools/Tools/Test/TestGuiTool.cs
@@ -1,6 +1,5 @@
#if DEBUG
using System.Text.Json;
-using System.Text.Json.Nodes;
namespace DevToys.Tools.Tools.Test;
@@ -191,11 +190,18 @@ internal sealed class TestGuiTool : IGuiTool
.Title("Input"))
.WithRightPaneChild(
- MultilineTextInput()
- .Language("json")
- .Title("Output")
- .ReadOnly()
- .Extendable()))));
+ DataGrid()
+ .WithColumns("Column 1", "Column 2", "Column 3")
+ .WithRows(
+ Row(null, "Cell 1", "This is the Cell number 2", "Cell3"),
+ Row(null, Cell("Cell 1"), Cell("Cell 2"), Cell(Button().Text("Cell 3"))),
+ Row(null, Label().Text("Duis accusam sed eos veniam molestie invidunt vel invidunt magna kasd diam invidunt erat option diam. Ullamcorper euismod et justo ipsum lorem dolores nulla. Hendrerit odio ea dolor eos amet et ea vero. Ut consetetur et elitr nonumy."), "Cell 1", "This is the Cell number 2", "Cell3"),
+ Row(null, "Cell 1", "This is the Cell number 2", "Cell3"),
+ Row(null, "Cell 1", "This is the Cell number 2", "Cell3"),
+ Row(null, "Cell 1", "This is the Cell number 2", "Cell3"),
+ Row(null, "Cell 1", "This is the Cell number 2", "Cell3"),
+ Row(null, "Cell 1", "This is the Cell number 2", "Cell3"))
+ ))));
// Smart detection handler.
public void OnDataReceived(string dataTypeName, object? parsedData)
diff --git a/src/app/dev/platforms/desktop/DevToys.MacOS/MauiProgram.cs b/src/app/dev/platforms/desktop/DevToys.MacOS/MauiProgram.cs
index 80ecc2855..1b5f183e6 100644
--- a/src/app/dev/platforms/desktop/DevToys.MacOS/MauiProgram.cs
+++ b/src/app/dev/platforms/desktop/DevToys.MacOS/MauiProgram.cs
@@ -105,6 +105,7 @@ public partial class MauiProgram
serviceCollection.AddSingleton(provider => MefComposer!.Provider);
serviceCollection.AddSingleton<IWindowService>(provider => _windowService);
+ serviceCollection.AddScoped<DocumentEventService, DocumentEventService>();
serviceCollection.AddScoped<PopoverService, PopoverService>();
serviceCollection.AddScoped<ContextMenuService, ContextMenuService>();
serviceCollection.AddScoped<FontService, FontService>();
diff --git a/src/app/dev/platforms/desktop/DevToys.Windows/MainWindow.xaml.cs b/src/app/dev/platforms/desktop/DevToys.Windows/MainWindow.xaml.cs
index c0a17f84a..3aa52ba5a 100644
--- a/src/app/dev/platforms/desktop/DevToys.Windows/MainWindow.xaml.cs
+++ b/src/app/dev/platforms/desktop/DevToys.Windows/MainWindow.xaml.cs
@@ -1,5 +1,4 @@
-using System.Transactions;
-using System.Windows.Interop;
+using System.Windows.Interop;
using DevToys.Api;
using DevToys.Blazor.BuiltInTools;
using DevToys.Blazor.Core.Languages;
@@ -153,6 +152,7 @@ public partial class MainWindow : MicaWindowWithOverlay
serviceCollection.AddSingleton(provider => _mefComposer.Provider);
serviceCollection.AddSingleton<IWindowService, WindowService>();
+ serviceCollection.AddScoped<DocumentEventService, DocumentEventService>();
serviceCollection.AddScoped<PopoverService, PopoverService>();
serviceCollection.AddScoped<ContextMenuService, ContextMenuService>();
serviceCollection.AddScoped<FontService, FontService>();
diff --git a/src/app/dev/shared/GlobalUsings.cs b/src/app/dev/shared/GlobalUsings.cs
index fca6a1eb8..657476eba 100644
--- a/src/app/dev/shared/GlobalUsings.cs
+++ b/src/app/dev/shared/GlobalUsings.cs
@@ -4,7 +4,6 @@ global using System.Collections.Generic;
global using System.ComponentModel;
global using System.ComponentModel.Composition;
global using System.Diagnostics;
-global using System.Diagnostics.CodeAnalysis;
global using System.Linq;
global using System.Threading;
global using System.Threading.Tasks;