Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions src/ShadUI/Controls/Dialog/DialogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,6 @@ internal void Show(Control control, DialogOptions options)

internal void CloseDialog(Control control)
{
var context = control.DataContext;
if (context is not null)
{
var contextType = context.GetType();
OnSuccessCallbacks.Remove(contextType);
OnSuccessWithContextCallbacks.Remove(contextType);
OnSuccessAsyncCallbacks.Remove(contextType);
OnSuccessWithContextAsyncCallbacks.Remove(contextType);
OnCancelCallbacks.Remove(contextType);
OnCancelAsyncCallbacks.Remove(contextType);
}

Dialogs.Remove(control);

OnDialogClosed?.Invoke(this, new DialogClosedEventArgs
Expand Down Expand Up @@ -96,7 +84,7 @@ internal void RemoveLast()

var context = lastDialog.Key.DataContext;
var contextType = context?.GetType();
if (contextType is not null) InvokeCallBacks(context, contextType, false);
if (contextType is not null) InvokeCallBacks(context, contextType, false, true);
}

internal readonly Dictionary<Type, Type> CustomDialogs = [];
Expand All @@ -120,7 +108,7 @@ public DialogManager Register<TView, TContext>() where TView : Control
internal readonly Dictionary<Type, Func<Task>> OnSuccessAsyncCallbacks = [];
internal readonly Dictionary<Type, Func<object, Task>> OnSuccessWithContextAsyncCallbacks = [];

private void InvokeCallBacks(object? context, Type type, bool success)
private void InvokeCallBacks(object? context, Type type, bool success, bool cleanup)
{
if (OnSuccessCallbacks.Remove(type, out var successCallback) && success)
{
Expand Down Expand Up @@ -151,6 +139,16 @@ private void InvokeCallBacks(object? context, Type type, bool success)
{
cancelAsyncCallback?.Invoke();
}

if (cleanup)
{
OnSuccessCallbacks.Remove(type);
OnSuccessWithContextCallbacks.Remove(type);
OnSuccessAsyncCallbacks.Remove(type);
OnSuccessWithContextAsyncCallbacks.Remove(type);
OnCancelCallbacks.Remove(type);
OnCancelAsyncCallbacks.Remove(type);
}
}

/// <summary>
Expand All @@ -169,7 +167,7 @@ public void Close<TContext>(TContext context, CloseDialogOptions? options = null
foreach (var dialog in dialogs) CloseDialog(dialog.Key);

var success = options?.Success ?? false;
InvokeCallBacks(context, typeof(TContext), success);
InvokeCallBacks(context, typeof(TContext), success, true);

if (!clearAll) OpenLast();
}
Expand Down