Adapt Raw hierarchy density and menu placement to touch layouts
This commit is contained in:
@@ -22,14 +22,20 @@
|
||||
@using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Raw
|
||||
@using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers
|
||||
@inject IRawTreeService Svc
|
||||
@inject IJSRuntime TreeLayoutJS
|
||||
@using Microsoft.JSInterop
|
||||
@implements IAsyncDisposable
|
||||
|
||||
@* One flat, virtualized row list (R04 P5): only the rows in view render, and each expanded level
|
||||
shows RawTreeRows.DefaultPageSize children before a "Show more" row. Flatten runs per render; it is
|
||||
O(visible rows) with no allocation per node beyond one row struct, so every mutation path below
|
||||
(reload, toggle, load) is picked up without separate invalidation. *@
|
||||
<Virtualize Items="@RawTreeRows.Flatten(Roots)" ItemSize="30" OverscanCount="20" Context="row">
|
||||
@RenderRow(row)
|
||||
</Virtualize>
|
||||
<div class="raw-tree-rows" role="region" aria-label="Raw hierarchy" tabindex="0"
|
||||
data-density="@(_rowHeight == 44 ? "touch" : "compact")" style="@($"--raw-row-height:{_rowHeight}px")">
|
||||
<Virtualize Items="@RawTreeRows.Flatten(Roots)" ItemSize="@_rowHeight" OverscanCount="20" Context="row">
|
||||
@RenderRow(row)
|
||||
</Virtualize>
|
||||
</div>
|
||||
|
||||
@* --- Real modals + dialogs (one instance each for the whole recursive tree) --- *@
|
||||
<DriverConfigModal @bind-Visible="_driverCfgVisible" DriverInstanceId="@_driverCfgId" OnSaved="DriverCfgSaved" />
|
||||
@@ -63,6 +69,73 @@
|
||||
NodeName="@_msgNodeName" OnClose="CloseMessage" />
|
||||
|
||||
@code {
|
||||
private readonly string _densityId = "raw-density-" + Guid.NewGuid().ToString("N");
|
||||
private readonly SemaphoreSlim _densityGate = new(1);
|
||||
private DotNetObjectReference<RawTree>? _densityReference;
|
||||
private IJSObjectReference? _treeLayoutModule;
|
||||
private bool _densityDisposed;
|
||||
private Task? _densityDisposal;
|
||||
private int _rowHeight = 30;
|
||||
private long _densityRevision = -1;
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
if (!firstRender) return;
|
||||
await _densityGate.WaitAsync();
|
||||
try
|
||||
{
|
||||
if (_densityDisposed) return;
|
||||
_treeLayoutModule = await TreeLayoutJS.InvokeAsync<IJSObjectReference>("import", "./_content/ZB.MOM.WW.OtOpcUa.AdminUI/js/tree-layout.js");
|
||||
if (_densityDisposed) return;
|
||||
_densityReference = DotNetObjectReference.Create(this);
|
||||
var coarse = await _treeLayoutModule.InvokeAsync<bool>("observeDensity", _densityId, _densityReference);
|
||||
if (!_densityDisposed && _densityRevision < 0) await SetPointerDensity(0, coarse);
|
||||
}
|
||||
catch (JSDisconnectedException) { }
|
||||
catch (JSException) { /* Keep the compact, internally consistent fallback. */ }
|
||||
catch (InvalidOperationException) { /* prerender */ }
|
||||
finally { _densityGate.Release(); }
|
||||
}
|
||||
|
||||
/// <summary>Applies the current native pointer density to both rows and their virtualization estimate.</summary>
|
||||
/// <param name="revision">Monotonic identity of the media-query observation.</param>
|
||||
/// <param name="coarse">Whether the current pointer needs larger targets.</param>
|
||||
/// <returns>The serialized render update.</returns>
|
||||
[JSInvokable]
|
||||
public Task SetPointerDensity(long revision, bool coarse) => InvokeAsync(() =>
|
||||
{
|
||||
if (_densityDisposed || revision <= _densityRevision) return;
|
||||
_densityRevision = revision;
|
||||
var height = coarse ? 44 : 30;
|
||||
if (height == _rowHeight) return;
|
||||
_rowHeight = height;
|
||||
StateHasChanged();
|
||||
});
|
||||
|
||||
/// <summary>Releases only the local density observer and its callback handle.</summary>
|
||||
/// <returns>The shared teardown operation.</returns>
|
||||
public ValueTask DisposeAsync() => new(_densityDisposal ??= DisposeDensityAsync());
|
||||
|
||||
private async Task DisposeDensityAsync()
|
||||
{
|
||||
_densityDisposed = true;
|
||||
await _densityGate.WaitAsync();
|
||||
try
|
||||
{
|
||||
if (_treeLayoutModule is not null)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _treeLayoutModule.InvokeVoidAsync("stopDensity", _densityId);
|
||||
await _treeLayoutModule.DisposeAsync();
|
||||
}
|
||||
catch (JSDisconnectedException) { }
|
||||
catch (TaskCanceledException) { }
|
||||
}
|
||||
}
|
||||
finally { _densityReference?.Dispose(); _densityGate.Release(); }
|
||||
}
|
||||
|
||||
private readonly string _labelScope = "raw-control-" + Guid.NewGuid().ToString("N") + "-";
|
||||
/// <summary>The top-level Raw nodes to render (the enterprise roots from <c>LoadRootsAsync</c>).</summary>
|
||||
[Parameter, EditorRequired] public IReadOnlyList<RawNode> Roots { get; set; } = default!;
|
||||
@@ -179,17 +252,17 @@
|
||||
@RenderNode(row.Node, row.Depth)
|
||||
break;
|
||||
case RawTreeRowKind.Loading:
|
||||
<div @key="row.Key" class="small text-muted py-1" style="@($"padding-left:{row.Depth * 18}px")">
|
||||
<div @key="row.Key" class="raw-tree-row small text-muted" style="@($"padding-left:{row.Depth * 18}px")">
|
||||
<span class="spinner-border spinner-border-sm me-1"></span>Loading…
|
||||
</div>
|
||||
break;
|
||||
case RawTreeRowKind.Error:
|
||||
<div @key="row.Key" class="small text-danger py-1" style="@($"padding-left:{row.Depth * 18}px")">
|
||||
<div @key="row.Key" class="raw-tree-row small text-danger" style="@($"padding-left:{row.Depth * 18}px")">
|
||||
@row.Node.Error
|
||||
</div>
|
||||
break;
|
||||
default:
|
||||
<div @key="row.Key" class="small py-1" style="@($"padding-left:{row.Depth * 18}px")">
|
||||
<div @key="row.Key" class="raw-tree-row small" style="@($"padding-left:{row.Depth * 18}px")">
|
||||
<button type="button" class="btn btn-sm btn-link p-0" aria-label="@($"Show {Math.Min(row.Remaining, RawTreeRows.DefaultPageSize)} more ({row.Remaining} not shown) under {row.Node.DisplayName}")" @onclick="@(() => ShowMore(row.Node))">
|
||||
Show @Math.Min(row.Remaining, RawTreeRows.DefaultPageSize) more
|
||||
(@row.Remaining not shown)
|
||||
@@ -208,7 +281,7 @@
|
||||
var muted = node.Enabled == false && node.Kind is RawNodeKind.Driver or RawNodeKind.Device;
|
||||
var items = _menus.GetOrBuild(node, PickerMode, MenuFor);
|
||||
|
||||
<div @key="node.Key" class="d-flex align-items-center gap-1 py-1" style="@indent">
|
||||
<div @key="node.Key" class="raw-tree-row d-flex align-items-center gap-1" style="@indent">
|
||||
@if (hasExpander)
|
||||
{
|
||||
<button type="button" class="btn btn-sm btn-link p-0 tree-twist"
|
||||
@@ -238,12 +311,14 @@
|
||||
// The icon + label + badge shown for a node — the ContextMenu right-click surface.
|
||||
private RenderFragment RenderRowBody(RawNode node, bool muted) => __builder =>
|
||||
{
|
||||
<span class="d-inline-flex align-items-center gap-1">
|
||||
<span class="raw-node-body d-inline-flex align-items-center gap-1">
|
||||
@if (PickerMode && node.Kind == RawNodeKind.Tag)
|
||||
{
|
||||
<label class="raw-select-target">
|
||||
<input type="checkbox" class="form-check-input me-1" aria-labelledby="@(_labelScope + Convert.ToHexString(System.Text.Encoding.UTF8.GetBytes(node.Key)))"
|
||||
title="@($"{node.Kind} {node.Key}")" checked="@IsTagSelected(node)"
|
||||
@onchange="@(e => ToggleTagSelection(node, e.Value is true))" />
|
||||
</label>
|
||||
}
|
||||
<span aria-hidden="true">@KindIcon(node.Kind)</span>
|
||||
<span id="@(_labelScope + Convert.ToHexString(System.Text.Encoding.UTF8.GetBytes(node.Key)))" class="mono small @(muted ? "text-muted" : "")">@node.DisplayName</span>
|
||||
|
||||
Reference in New Issue
Block a user