Guard dialog completion by presentation identity
This commit is contained in:
@@ -18,12 +18,12 @@
|
||||
aria-modal="true"
|
||||
aria-labelledby="@TitleId"
|
||||
aria-describedby="@(state.Kind == DialogKind.Confirm ? BodyId : null)"
|
||||
@onkeydown="OnKeyDown">
|
||||
@onkeydown="e => OnKeyDown(state, e)">
|
||||
<div class="modal-dialog modal-dialog-centered@(state.Size is null ? "" : $" {state.Size}")" role="document">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 id="@TitleId" class="modal-title">@state.Title</h5>
|
||||
<button type="button" class="btn-close" aria-label="Close" @onclick="Cancel"></button>
|
||||
<button type="button" class="btn-close" aria-label="Close" @onclick="() => Cancel(state)"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
@if (state.Kind == DialogKind.Confirm)
|
||||
@@ -41,7 +41,7 @@
|
||||
class="form-control form-control-sm"
|
||||
placeholder="@state.Placeholder"
|
||||
value="@_promptValue"
|
||||
@oninput="OnPromptInput" />
|
||||
@oninput="e => OnPromptInput(state, e)" />
|
||||
}
|
||||
</div>
|
||||
@* Custom dialogs supply their own action buttons inside the body,
|
||||
@@ -50,10 +50,10 @@
|
||||
{
|
||||
<div class="modal-footer">
|
||||
<div class="btn-group btn-group-sm" role="group">
|
||||
<button type="button" class="btn btn-outline-secondary" @onclick="Cancel">Cancel</button>
|
||||
<button type="button" class="btn btn-outline-secondary" @onclick="() => Cancel(state)">Cancel</button>
|
||||
<button type="button"
|
||||
class="btn @ConfirmButtonClass(state)"
|
||||
@onclick="Confirm">
|
||||
@onclick="() => Confirm(state)">
|
||||
@ConfirmLabel(state)
|
||||
</button>
|
||||
</div>
|
||||
@@ -86,7 +86,7 @@
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (Service is DialogService svc) svc.OnChange -= OnServiceChanged;
|
||||
Cancel(); // A disposed host must not leave its local dialog result pending.
|
||||
if ((Service as DialogService)?.Current is { } current) Cancel(current); // Complete the disposed presentation.
|
||||
try { await JS.InvokeVoidAsync("sbDialog.deactivate", _overlayId); }
|
||||
catch (JSDisconnectedException) { }
|
||||
catch (InvalidOperationException) { /* prerender */ }
|
||||
@@ -114,7 +114,7 @@
|
||||
{
|
||||
var activated = await JS.InvokeAsync<bool?>("sbDialog.activate", _overlayId,
|
||||
current.Kind == DialogKind.Prompt ? "#" + PromptId : null);
|
||||
if (activated == false) Cancel(); // Single-active-overlay contract.
|
||||
if (activated == false) Cancel(current); // Single-active-overlay contract.
|
||||
}
|
||||
catch (JSDisconnectedException) { }
|
||||
catch (InvalidOperationException) { /* prerender */ }
|
||||
@@ -128,21 +128,22 @@
|
||||
}
|
||||
}
|
||||
|
||||
private void OnKeyDown(KeyboardEventArgs e)
|
||||
private void OnKeyDown(DialogState state, KeyboardEventArgs e)
|
||||
{
|
||||
// Native DOM keydown owns synchronous Tab containment; Escape retains
|
||||
// the service's existing typed cancellation path.
|
||||
if (e.Key == "Escape") Cancel();
|
||||
if (e.Key == "Escape") Cancel(state);
|
||||
}
|
||||
|
||||
private void OnPromptInput(ChangeEventArgs e)
|
||||
private void OnPromptInput(DialogState state, ChangeEventArgs e)
|
||||
{
|
||||
if (!ReferenceEquals((Service as DialogService)?.Current, state)) return;
|
||||
_promptValue = e.Value?.ToString() ?? string.Empty;
|
||||
}
|
||||
|
||||
private void Cancel()
|
||||
private void Cancel(DialogState state)
|
||||
{
|
||||
if (Service is not DialogService svc || svc.Current is null) return;
|
||||
if (Service is not DialogService svc || !ReferenceEquals(svc.Current, state)) return;
|
||||
var resolveValue = svc.Current.Kind == DialogKind.Confirm
|
||||
? (object)false
|
||||
: (object?)null;
|
||||
@@ -150,9 +151,9 @@
|
||||
svc.Resolve(resolveValue);
|
||||
}
|
||||
|
||||
private void Confirm()
|
||||
private void Confirm(DialogState state)
|
||||
{
|
||||
if (Service is not DialogService svc || svc.Current is null) return;
|
||||
if (Service is not DialogService svc || !ReferenceEquals(svc.Current, state)) return;
|
||||
var resolveValue = svc.Current.Kind == DialogKind.Confirm
|
||||
? (object)true
|
||||
: (object?)_promptValue;
|
||||
|
||||
@@ -78,11 +78,12 @@ public class DialogService : IDialogService
|
||||
var tcs = new TaskCompletionSource<object?>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
_tcs = tcs;
|
||||
|
||||
// The context routes Close/Cancel back through the same boxed Resolve()
|
||||
// path the host uses for confirm/prompt — Close boxes the typed value,
|
||||
// Cancel passes null. Capturing Resolve (not a service reference) keeps
|
||||
// the body's surface narrow and the resolution path uniform.
|
||||
var context = new DialogContext<TResult>(Resolve);
|
||||
// A custom body can finish after its presentation has closed. Only its
|
||||
// captured completion source may resolve the active dialog.
|
||||
var context = new DialogContext<TResult>(result =>
|
||||
{
|
||||
if (ReferenceEquals(_tcs, tcs)) Resolve(result);
|
||||
});
|
||||
|
||||
// Materialise the caller's RenderFragment<DialogContext<TResult>> into a
|
||||
// plain RenderFragment bound to this context, so DialogState stays
|
||||
|
||||
Reference in New Issue
Block a user