feat: add cancellation token support.
This commit is contained in:
@@ -26,7 +26,7 @@ public static class Dialog
|
|||||||
Content = new TView(),
|
Content = new TView(),
|
||||||
DataContext = vm,
|
DataContext = vm,
|
||||||
};
|
};
|
||||||
AssignOptionsToDialogWindow(window, options);
|
ConfigureDialogWindow(window, options);
|
||||||
owner ??= GetMainWindow();
|
owner ??= GetMainWindow();
|
||||||
if (owner is null)
|
if (owner is null)
|
||||||
{
|
{
|
||||||
@@ -52,7 +52,7 @@ public static class Dialog
|
|||||||
Content = view,
|
Content = view,
|
||||||
DataContext = vm,
|
DataContext = vm,
|
||||||
};
|
};
|
||||||
AssignOptionsToDialogWindow(window, options);
|
ConfigureDialogWindow(window, options);
|
||||||
owner ??= GetMainWindow();
|
owner ??= GetMainWindow();
|
||||||
if (owner is null)
|
if (owner is null)
|
||||||
{
|
{
|
||||||
@@ -82,7 +82,7 @@ public static class Dialog
|
|||||||
Content = new TView(),
|
Content = new TView(),
|
||||||
DataContext = vm,
|
DataContext = vm,
|
||||||
};
|
};
|
||||||
AssignOptionsToDefaultDialogWindow(window, options);
|
ConfigureDefaultDialogWindow(window, options);
|
||||||
owner ??= GetMainWindow();
|
owner ??= GetMainWindow();
|
||||||
if (owner is null)
|
if (owner is null)
|
||||||
{
|
{
|
||||||
@@ -107,7 +107,7 @@ public static class Dialog
|
|||||||
Content = view,
|
Content = view,
|
||||||
DataContext = vm,
|
DataContext = vm,
|
||||||
};
|
};
|
||||||
AssignOptionsToDefaultDialogWindow(window, options);
|
ConfigureDefaultDialogWindow(window, options);
|
||||||
owner ??= GetMainWindow();
|
owner ??= GetMainWindow();
|
||||||
if (owner is null)
|
if (owner is null)
|
||||||
{
|
{
|
||||||
@@ -136,7 +136,7 @@ public static class Dialog
|
|||||||
Content = new TView(),
|
Content = new TView(),
|
||||||
DataContext = vm,
|
DataContext = vm,
|
||||||
};
|
};
|
||||||
AssignOptionsToDialogWindow(window, options);
|
ConfigureDialogWindow(window, options);
|
||||||
owner ??= GetMainWindow();
|
owner ??= GetMainWindow();
|
||||||
if (owner is null)
|
if (owner is null)
|
||||||
{
|
{
|
||||||
@@ -163,7 +163,7 @@ public static class Dialog
|
|||||||
Content = view,
|
Content = view,
|
||||||
DataContext = vm,
|
DataContext = vm,
|
||||||
};
|
};
|
||||||
AssignOptionsToDialogWindow(window, options);
|
ConfigureDialogWindow(window, options);
|
||||||
owner ??= GetMainWindow();
|
owner ??= GetMainWindow();
|
||||||
if (owner is null)
|
if (owner is null)
|
||||||
{
|
{
|
||||||
@@ -188,7 +188,7 @@ public static class Dialog
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="window"></param>
|
/// <param name="window"></param>
|
||||||
/// <param name="options"></param>
|
/// <param name="options"></param>
|
||||||
private static void AssignOptionsToDialogWindow(DialogWindow window, DialogOptions? options)
|
private static void ConfigureDialogWindow(DialogWindow window, DialogOptions? options)
|
||||||
{
|
{
|
||||||
if (options is null)
|
if (options is null)
|
||||||
{
|
{
|
||||||
@@ -214,7 +214,7 @@ public static class Dialog
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="window"></param>
|
/// <param name="window"></param>
|
||||||
/// <param name="options"></param>
|
/// <param name="options"></param>
|
||||||
private static void AssignOptionsToDefaultDialogWindow(DefaultDialogWindow window, DialogOptions? options)
|
private static void ConfigureDefaultDialogWindow(DefaultDialogWindow window, DialogOptions? options)
|
||||||
{
|
{
|
||||||
if (options is null)
|
if (options is null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -76,9 +76,10 @@ public class DialogControl: ContentControl
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Task<T> ShowAsync<T>()
|
public Task<T?> ShowAsync<T>(CancellationToken? token = default)
|
||||||
{
|
{
|
||||||
var tcs = new TaskCompletionSource<T>();
|
var tcs = new TaskCompletionSource<T?>();
|
||||||
|
token?.Register(Close);
|
||||||
void OnCloseHandler(object sender, object? args)
|
void OnCloseHandler(object sender, object? args)
|
||||||
{
|
{
|
||||||
if (args is T result)
|
if (args is T result)
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ public static class OverlayDialog
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Task<DialogResult> ShowModal<TView, TViewModel>(TViewModel vm, string? hostId = null,
|
public static Task<DialogResult> ShowModal<TView, TViewModel>(TViewModel vm, string? hostId = null,
|
||||||
OverlayDialogOptions? options = null)
|
OverlayDialogOptions? options = null, CancellationToken? token = default)
|
||||||
where TView: Control, new()
|
where TView: Control, new()
|
||||||
{
|
{
|
||||||
var host = OverlayDialogManager.GetHost(hostId);
|
var host = OverlayDialogManager.GetHost(hostId);
|
||||||
@@ -111,11 +111,11 @@ public static class OverlayDialog
|
|||||||
};
|
};
|
||||||
ConfigureDefaultDialogControl(t, options);
|
ConfigureDefaultDialogControl(t, options);
|
||||||
host?.AddModalDialog(t);
|
host?.AddModalDialog(t);
|
||||||
return t.ShowAsync<DialogResult>();
|
return t.ShowAsync<DialogResult>(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Task<DialogResult> ShowModal(Control control, object? vm, string? hostId = null,
|
public static Task<DialogResult> ShowModal(Control control, object? vm, string? hostId = null,
|
||||||
OverlayDialogOptions? options = null)
|
OverlayDialogOptions? options = null, CancellationToken? token = default)
|
||||||
{
|
{
|
||||||
var host = OverlayDialogManager.GetHost(hostId);
|
var host = OverlayDialogManager.GetHost(hostId);
|
||||||
if (host is null) return Task.FromResult(DialogResult.None);
|
if (host is null) return Task.FromResult(DialogResult.None);
|
||||||
@@ -126,11 +126,11 @@ public static class OverlayDialog
|
|||||||
};
|
};
|
||||||
ConfigureDefaultDialogControl(t, options);
|
ConfigureDefaultDialogControl(t, options);
|
||||||
host?.AddModalDialog(t);
|
host?.AddModalDialog(t);
|
||||||
return t.ShowAsync<DialogResult>();
|
return t.ShowAsync<DialogResult>(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Task<TResult?> ShowCustomModal<TView, TViewModel, TResult>(TViewModel vm, string? hostId = null,
|
public static Task<TResult?> ShowCustomModal<TView, TViewModel, TResult>(TViewModel vm, string? hostId = null,
|
||||||
OverlayDialogOptions? options = null)
|
OverlayDialogOptions? options = null, CancellationToken? token = default)
|
||||||
where TView: Control, new()
|
where TView: Control, new()
|
||||||
{
|
{
|
||||||
var host = OverlayDialogManager.GetHost(hostId);
|
var host = OverlayDialogManager.GetHost(hostId);
|
||||||
@@ -142,11 +142,11 @@ public static class OverlayDialog
|
|||||||
};
|
};
|
||||||
ConfigureDialogControl(t, options);
|
ConfigureDialogControl(t, options);
|
||||||
host?.AddModalDialog(t);
|
host?.AddModalDialog(t);
|
||||||
return t.ShowAsync<TResult?>();
|
return t.ShowAsync<TResult?>(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Task<TResult?> ShowCustomModal<TResult>(Control control, object? vm, string? hostId = null,
|
public static Task<TResult?> ShowCustomModal<TResult>(Control control, object? vm, string? hostId = null,
|
||||||
OverlayDialogOptions? options = null)
|
OverlayDialogOptions? options = null, CancellationToken? token = default)
|
||||||
{
|
{
|
||||||
var host = OverlayDialogManager.GetHost(hostId);
|
var host = OverlayDialogManager.GetHost(hostId);
|
||||||
if (host is null) return Task.FromResult(default(TResult));
|
if (host is null) return Task.FromResult(default(TResult));
|
||||||
@@ -157,11 +157,11 @@ public static class OverlayDialog
|
|||||||
};
|
};
|
||||||
ConfigureDialogControl(t, options);
|
ConfigureDialogControl(t, options);
|
||||||
host?.AddModalDialog(t);
|
host?.AddModalDialog(t);
|
||||||
return t.ShowAsync<TResult?>();
|
return t.ShowAsync<TResult?>(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Task<TResult?> ShowCustomModal<TResult>(object? vm, string? hostId = null,
|
public static Task<TResult?> ShowCustomModal<TResult>(object? vm, string? hostId = null,
|
||||||
OverlayDialogOptions? options = null)
|
OverlayDialogOptions? options = null, CancellationToken? token = default)
|
||||||
{
|
{
|
||||||
var host = OverlayDialogManager.GetHost(hostId);
|
var host = OverlayDialogManager.GetHost(hostId);
|
||||||
if (host is null) return Task.FromResult(default(TResult));
|
if (host is null) return Task.FromResult(default(TResult));
|
||||||
@@ -175,7 +175,7 @@ public static class OverlayDialog
|
|||||||
};
|
};
|
||||||
ConfigureDialogControl(t, options);
|
ConfigureDialogControl(t, options);
|
||||||
host?.AddModalDialog(t);
|
host?.AddModalDialog(t);
|
||||||
return t.ShowAsync<TResult?>();
|
return t.ShowAsync<TResult?>(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ConfigureDialogControl(DialogControl control, OverlayDialogOptions? options)
|
private static void ConfigureDialogControl(DialogControl control, OverlayDialogOptions? options)
|
||||||
@@ -185,7 +185,7 @@ public static class OverlayDialog
|
|||||||
control.VerticalAnchor = options.VerticalAnchor;
|
control.VerticalAnchor = options.VerticalAnchor;
|
||||||
control.InitialHorizontalOffset =
|
control.InitialHorizontalOffset =
|
||||||
control.HorizontalAnchor == HorizontalPosition.Center ? null : options.HorizontalOffset;
|
control.HorizontalAnchor == HorizontalPosition.Center ? null : options.HorizontalOffset;
|
||||||
control.InitialHorizontalOffset =
|
control.InitialVerticalOffset =
|
||||||
options.VerticalAnchor == VerticalPosition.Center ? null : options.VerticalOffset;
|
options.VerticalAnchor == VerticalPosition.Center ? null : options.VerticalOffset;
|
||||||
control.CanClickOnMaskToClose = options.CanClickOnMaskToClose;
|
control.CanClickOnMaskToClose = options.CanClickOnMaskToClose;
|
||||||
}
|
}
|
||||||
@@ -197,7 +197,7 @@ public static class OverlayDialog
|
|||||||
control.VerticalAnchor = options.VerticalAnchor;
|
control.VerticalAnchor = options.VerticalAnchor;
|
||||||
control.InitialHorizontalOffset =
|
control.InitialHorizontalOffset =
|
||||||
control.HorizontalAnchor == HorizontalPosition.Center ? null : options.HorizontalOffset;
|
control.HorizontalAnchor == HorizontalPosition.Center ? null : options.HorizontalOffset;
|
||||||
control.InitialHorizontalOffset =
|
control.InitialVerticalOffset =
|
||||||
options.VerticalAnchor == VerticalPosition.Center ? null : options.VerticalOffset;
|
options.VerticalAnchor == VerticalPosition.Center ? null : options.VerticalOffset;
|
||||||
control.CanClickOnMaskToClose = options.CanClickOnMaskToClose;
|
control.CanClickOnMaskToClose = options.CanClickOnMaskToClose;
|
||||||
control.Mode = options.Mode;
|
control.Mode = options.Mode;
|
||||||
|
|||||||
Reference in New Issue
Block a user