From 37603f1a0302f3151df8dc2df54cf8f78dfd84a8 Mon Sep 17 00:00:00 2001 From: Bill Long Date: Tue, 25 Oct 2022 20:44:20 -0500 Subject: [PATCH] Implement column resizing --- .../Components/EventTable.razor | 15 ++++--- .../Components/EventTable.razor.cs | 44 +++++++++++++++++++ .../Components/EventTable.razor.css | 5 +++ .../Components/EventTable.razor.js | 13 ++++++ 4 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 src/EventLogExpert/Components/EventTable.razor.js diff --git a/src/EventLogExpert/Components/EventTable.razor b/src/EventLogExpert/Components/EventTable.razor index 34aa1d98..df81d6e8 100644 --- a/src/EventLogExpert/Components/EventTable.razor +++ b/src/EventLogExpert/Components/EventTable.razor @@ -1,25 +1,26 @@ @inherits FluxorComponent @inject IState EventLogState +@inject IJSRuntime JS
- + - + - + - + - + - + - + diff --git a/src/EventLogExpert/Components/EventTable.razor.cs b/src/EventLogExpert/Components/EventTable.razor.cs index a2510b78..43aae001 100644 --- a/src/EventLogExpert/Components/EventTable.razor.cs +++ b/src/EventLogExpert/Components/EventTable.razor.cs @@ -1,6 +1,10 @@ // // Copyright (c) Microsoft Corporation. // // Licensed under the MIT License. +using Microsoft.AspNetCore.Components.Web; +using Microsoft.JSInterop; +using System.Diagnostics; + namespace EventLogExpert.Components; public partial class EventTable @@ -20,6 +24,20 @@ public partial class EventTable private const int ScrollBarWidth = 18; + private string _columnResizingName = ""; + + private double _columnMouseXLast = -1; + + private IJSObjectReference? _jsModule; + + private DotNetObjectReference? _thisObj; + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + _jsModule = await JS.InvokeAsync("import", "./Components/EventTable.razor.js"); + _thisObj = DotNetObjectReference.Create(this); + } + private string GetDescriptionStyle() { var total = _colWidths.Values.Sum() + (TableDividerWidth * _colWidths.Count) + ScrollBarWidth; @@ -30,4 +48,30 @@ private string GetInlineStyle(string colName) { return $"min-width: {_colWidths[colName]}px; max-width: {_colWidths[colName]}px;"; } + + private void OnMouseDownDivider(MouseEventArgs e, string columnName) + { + _columnResizingName = columnName; + _columnMouseXLast = e.ClientX; + _jsModule?.InvokeVoidAsync("startColumnResize", new[] {_thisObj}); + } + + [JSInvokable] + public void MouseMoveCallback(MouseEventArgs e) + { + int difference = (int)(_columnMouseXLast - e.ClientX); + var newColumnWidth = _colWidths[_columnResizingName] - difference; + if (newColumnWidth < 10) newColumnWidth = 10; + + _colWidths[_columnResizingName] = newColumnWidth; + _columnMouseXLast = e.ClientX; + StateHasChanged(); + } + + [JSInvokable] + public void MouseUpCallback() + { + _columnResizingName = ""; + _columnMouseXLast = -1; + } } diff --git a/src/EventLogExpert/Components/EventTable.razor.css b/src/EventLogExpert/Components/EventTable.razor.css index 6552682d..9c54ae85 100644 --- a/src/EventLogExpert/Components/EventTable.razor.css +++ b/src/EventLogExpert/Components/EventTable.razor.css @@ -19,6 +19,11 @@ th { position: sticky; top: 0; background-color: var(--background-darkgray); + cursor: default; +} + +th.table-divider { + cursor: col-resize; } td { diff --git a/src/EventLogExpert/Components/EventTable.razor.js b/src/EventLogExpert/Components/EventTable.razor.js new file mode 100644 index 00000000..45df30e3 --- /dev/null +++ b/src/EventLogExpert/Components/EventTable.razor.js @@ -0,0 +1,13 @@ +export function startColumnResize(dotNetObj) { + document.onmousemove = e => { + // For some reason we have to manually pass this as a new object. Simply passing the event + // to .NET does not work. We're only including the one value we care about. + dotNetObj.invokeMethodAsync('MouseMoveCallback', { ClientX: e.clientX }); + }; + + document.onmouseup = e => { + document.onmousemove = null; + document.onmouseup = null; + dotNetObj.invokeMethodAsync('MouseUpCallback'); + }; +}
Rec Time Id Machine Level Source Task Description