feat: dialog: refactor to support non-generic call, add options and remove parameters.
This commit is contained in:
@@ -90,11 +90,13 @@ public class DialogDemoViewModel: ObservableObject
|
|||||||
var vm = new PlainDialogViewModel();
|
var vm = new PlainDialogViewModel();
|
||||||
if (IsWindow)
|
if (IsWindow)
|
||||||
{
|
{
|
||||||
DefaultResult = await Dialog.ShowModalAsync<PlainDialog, PlainDialogViewModel>(
|
DefaultResult = await Dialog.ShowModal<PlainDialog, PlainDialogViewModel>(
|
||||||
vm,
|
vm, options: new DialogOptions()
|
||||||
"Please select a date",
|
{
|
||||||
SelectedMode,
|
Title = "Please select a date",
|
||||||
SelectedButton);
|
Mode = SelectedMode,
|
||||||
|
Button = SelectedButton
|
||||||
|
});
|
||||||
Date = vm.Date;
|
Date = vm.Date;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -131,7 +133,7 @@ public class DialogDemoViewModel: ObservableObject
|
|||||||
|
|
||||||
if (IsModal)
|
if (IsModal)
|
||||||
{
|
{
|
||||||
Result = await Dialog.ShowCustomModalAsync<DialogWithAction, DialogWithActionViewModel, bool>(
|
Result = await Dialog.ShowCustomModal<DialogWithAction, DialogWithActionViewModel, bool>(
|
||||||
vm);
|
vm);
|
||||||
Date = vm.Date;
|
Date = vm.Date;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,81 +9,25 @@ namespace Ursa.Controls;
|
|||||||
|
|
||||||
public static class Dialog
|
public static class Dialog
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Show a Window Modal Dialog that with all content fully customized.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="vm"></param>
|
|
||||||
/// <typeparam name="TView"></typeparam>
|
|
||||||
/// <typeparam name="TViewModel"></typeparam>
|
|
||||||
/// <typeparam name="TResult"></typeparam>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static async Task<TResult?> ShowCustomModalAsync<TView, TViewModel, TResult>(TViewModel vm)
|
|
||||||
where TView : Control, new()
|
|
||||||
{
|
|
||||||
var mainWindow = GetMainWindow();
|
|
||||||
return await ShowCustomModalAsync<TView, TViewModel, TResult>(mainWindow, vm);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Show a Window Dialog that with all content fully customized.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="vm"></param>
|
|
||||||
/// <typeparam name="TView"></typeparam>
|
|
||||||
/// <typeparam name="TViewModel"></typeparam>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static void ShowCustom<TView, TViewModel>(TViewModel vm)
|
|
||||||
where TView : Control, new()
|
|
||||||
{
|
|
||||||
var mainWindow = GetMainWindow();
|
|
||||||
ShowCustom<TView, TViewModel>(mainWindow, vm);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Show a Window Modal Dialog that with all content fully customized. And the owner of the dialog is specified.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="owner"></param>
|
|
||||||
/// <param name="vm"></param>
|
|
||||||
/// <typeparam name="TView"></typeparam>
|
|
||||||
/// <typeparam name="TViewModel"></typeparam>
|
|
||||||
/// <typeparam name="TResult"></typeparam>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static async Task<TResult> ShowCustomModalAsync<TView, TViewModel, TResult>(Window? owner, TViewModel? vm)
|
|
||||||
where TView: Control, new()
|
|
||||||
{
|
|
||||||
var window = new DialogWindow
|
|
||||||
{
|
|
||||||
Content = new TView { DataContext = vm },
|
|
||||||
DataContext = vm,
|
|
||||||
};
|
|
||||||
if (owner is null)
|
|
||||||
{
|
|
||||||
window.Show();
|
|
||||||
return default;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var result = await window.ShowDialog<TResult>(owner);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Show a Window Dialog that with all content fully customized. And the owner of the dialog is specified.
|
/// Show a Window Dialog that with all content fully customized. And the owner of the dialog is specified.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="vm">Dialog ViewModel instance</param>
|
||||||
/// <param name="owner"></param>
|
/// <param name="owner"></param>
|
||||||
/// <param name="vm"></param>
|
/// <param name="options"></param>
|
||||||
/// <typeparam name="TView"></typeparam>
|
/// <typeparam name="TView"></typeparam>
|
||||||
/// <typeparam name="TViewModel"></typeparam>
|
/// <typeparam name="TViewModel"></typeparam>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static void ShowCustom<TView, TViewModel>(Window? owner, TViewModel? vm)
|
public static void ShowCustom<TView, TViewModel>(TViewModel? vm, Window? owner = null, DialogOptions? options = null)
|
||||||
where TView: Control, new()
|
where TView: Control, new()
|
||||||
{
|
{
|
||||||
var window = new DialogWindow
|
var window = new DialogWindow
|
||||||
{
|
{
|
||||||
Content = new TView { DataContext = vm },
|
Content = new TView(),
|
||||||
DataContext = vm,
|
DataContext = vm,
|
||||||
};
|
};
|
||||||
|
AssignOptionsToDialogWindow(window, options);
|
||||||
|
owner ??= GetMainWindow();
|
||||||
if (owner is null)
|
if (owner is null)
|
||||||
{
|
{
|
||||||
window.Show();
|
window.Show();
|
||||||
@@ -94,43 +38,96 @@ public static class Dialog
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<DialogResult> ShowModalAsync<TView, TViewModel>(
|
public static void ShowCustom(Control view, object? vm, Window? owner = null, DialogOptions? options = null)
|
||||||
Window? owner,
|
|
||||||
TViewModel vm,
|
|
||||||
string? title = null,
|
|
||||||
DialogMode mode = DialogMode.None,
|
|
||||||
DialogButton buttons = DialogButton.OKCancel)
|
|
||||||
where TView : Control, new()
|
|
||||||
{
|
{
|
||||||
var window = new DefaultDialogWindow()
|
var window = new DialogWindow
|
||||||
{
|
{
|
||||||
Content = new TView() { DataContext = vm },
|
Content = view,
|
||||||
DataContext = vm,
|
DataContext = vm,
|
||||||
Buttons = buttons,
|
|
||||||
Title = title,
|
|
||||||
Mode = mode,
|
|
||||||
};
|
};
|
||||||
|
AssignOptionsToDialogWindow(window, options);
|
||||||
|
owner ??= GetMainWindow();
|
||||||
if (owner is null)
|
if (owner is null)
|
||||||
{
|
{
|
||||||
window.Show();
|
window.Show();
|
||||||
return DialogResult.None;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var result = await window.ShowDialog<DialogResult>(owner);
|
window.Show(owner);
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<DialogResult> ShowModalAsync<TView, TViewModel>(
|
public static Task<DialogResult> ShowModal<TView, TViewModel>(TViewModel vm, Window? owner = null,
|
||||||
TViewModel vm,
|
DialogOptions? options = null)
|
||||||
string? title = null,
|
|
||||||
DialogMode mode = DialogMode.None,
|
|
||||||
DialogButton buttons = DialogButton.OKCancel)
|
|
||||||
where TView: Control, new()
|
where TView: Control, new()
|
||||||
{
|
{
|
||||||
var mainWindow = GetMainWindow();
|
var window = new DefaultDialogWindow
|
||||||
return await ShowModalAsync<TView, TViewModel>(mainWindow, vm, title, mode, buttons);
|
{
|
||||||
|
Content = new TView(),
|
||||||
|
DataContext = vm,
|
||||||
|
};
|
||||||
|
AssignOptionsToDefaultDialogWindow(window, options);
|
||||||
|
owner ??= GetMainWindow();
|
||||||
|
if (owner is null)
|
||||||
|
{
|
||||||
|
window.Show();
|
||||||
|
return Task.FromResult(DialogResult.None);
|
||||||
|
}
|
||||||
|
return window.ShowDialog<DialogResult>(owner);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Task<DialogResult> ShowModal(Control view, object? vm, Window? owner = null, DialogOptions? options = null)
|
||||||
|
{
|
||||||
|
var window = new DefaultDialogWindow
|
||||||
|
{
|
||||||
|
Content = view,
|
||||||
|
DataContext = vm,
|
||||||
|
};
|
||||||
|
AssignOptionsToDefaultDialogWindow(window, options);
|
||||||
|
owner ??= GetMainWindow();
|
||||||
|
if (owner is null)
|
||||||
|
{
|
||||||
|
window.Show();
|
||||||
|
return Task.FromResult(DialogResult.None);
|
||||||
|
}
|
||||||
|
return window.ShowDialog<DialogResult>(owner);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Task<TResult?> ShowCustomModal<TView, TViewModel, TResult>(TViewModel vm, Window? owner = null,
|
||||||
|
DialogOptions? options = null)
|
||||||
|
where TView: Control, new()
|
||||||
|
{
|
||||||
|
var window = new DialogWindow
|
||||||
|
{
|
||||||
|
Content = new TView(),
|
||||||
|
DataContext = vm,
|
||||||
|
};
|
||||||
|
AssignOptionsToDialogWindow(window, options);
|
||||||
|
owner ??= GetMainWindow();
|
||||||
|
if (owner is null)
|
||||||
|
{
|
||||||
|
window.Show();
|
||||||
|
return Task.FromResult(default(TResult));
|
||||||
|
}
|
||||||
|
return window.ShowDialog<TResult?>(owner);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Task<TResult?> ShowCustomModal<TResult>(Control view, object? vm, Window? owner = null,
|
||||||
|
DialogOptions? options = null)
|
||||||
|
{
|
||||||
|
var window = new DialogWindow
|
||||||
|
{
|
||||||
|
Content = view,
|
||||||
|
DataContext = vm,
|
||||||
|
};
|
||||||
|
AssignOptionsToDialogWindow(window, options);
|
||||||
|
owner ??= GetMainWindow();
|
||||||
|
if (owner is null)
|
||||||
|
{
|
||||||
|
window.Show();
|
||||||
|
return Task.FromResult(default(TResult));
|
||||||
|
}
|
||||||
|
return window.ShowDialog<TResult?>(owner);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Window? GetMainWindow()
|
private static Window? GetMainWindow()
|
||||||
@@ -138,76 +135,49 @@ public static class Dialog
|
|||||||
var lifetime = Application.Current?.ApplicationLifetime;
|
var lifetime = Application.Current?.ApplicationLifetime;
|
||||||
return lifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: { } w } ? w : null;
|
return lifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: { } w } ? w : null;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static class OverlayDialog
|
private static void AssignOptionsToDialogWindow(DialogWindow window, DialogOptions? options)
|
||||||
{
|
|
||||||
public static Task<DialogResult> ShowModalAsync<TView, TViewModel>(
|
|
||||||
TViewModel vm,
|
|
||||||
string? hostId = null,
|
|
||||||
string? title = null,
|
|
||||||
DialogMode mode = DialogMode.None,
|
|
||||||
DialogButton buttons = DialogButton.OKCancel)
|
|
||||||
where TView : Control, new()
|
|
||||||
{
|
{
|
||||||
var t = new DefaultDialogControl()
|
if (options is null)
|
||||||
{
|
{
|
||||||
Content = new TView(){ DataContext = vm },
|
options = new DialogOptions();
|
||||||
DataContext = vm,
|
}
|
||||||
Buttons = buttons,
|
window.WindowStartupLocation = options.StartupLocation;
|
||||||
Title = title,
|
window.Title = options.Title;
|
||||||
Mode = mode,
|
if (options.StartupLocation == WindowStartupLocation.Manual)
|
||||||
};
|
{
|
||||||
var host = OverlayDialogManager.GetHost(hostId);
|
if (options.Position is not null)
|
||||||
host?.AddModalDialog(t);
|
{
|
||||||
return t.ShowAsync<DialogResult>();
|
window.Position = options.Position.Value;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Task<TResult> ShowCustomModalAsync<TView, TViewModel, TResult>(
|
private static void AssignOptionsToDefaultDialogWindow(DefaultDialogWindow window, DialogOptions? options)
|
||||||
TViewModel vm,
|
|
||||||
string? hostId = null)
|
|
||||||
where TView: Control, new()
|
|
||||||
{
|
{
|
||||||
var t = new DialogControl()
|
if (options is null)
|
||||||
{
|
{
|
||||||
Content = new TView() { DataContext = vm },
|
options = new DialogOptions();
|
||||||
DataContext = vm,
|
}
|
||||||
};
|
window.WindowStartupLocation = options.StartupLocation;
|
||||||
var host = OverlayDialogManager.GetHost(hostId);
|
window.Title = options.Title;
|
||||||
host?.AddModalDialog(t);
|
window.Buttons = options.Button;
|
||||||
return t.ShowAsync<TResult>();
|
window.Mode = options.Mode;
|
||||||
}
|
if (options.StartupLocation == WindowStartupLocation.Manual)
|
||||||
|
|
||||||
public static void Show<TView, TViewModel>(
|
|
||||||
TViewModel vm,
|
|
||||||
string? hostId = null,
|
|
||||||
string? title = null,
|
|
||||||
DialogMode mode = DialogMode.None,
|
|
||||||
DialogButton buttons = DialogButton.OKCancel)
|
|
||||||
where TView: Control, new()
|
|
||||||
{
|
|
||||||
var t = new DefaultDialogControl()
|
|
||||||
{
|
{
|
||||||
Content = new TView() { DataContext = vm },
|
if (options.Position is not null)
|
||||||
DataContext = vm,
|
{
|
||||||
Buttons = buttons,
|
window.Position = options.Position.Value;
|
||||||
Title = title,
|
}
|
||||||
Mode = mode,
|
else
|
||||||
};
|
{
|
||||||
var host = OverlayDialogManager.GetHost(hostId);
|
window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
|
||||||
host?.AddDialog(t);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ShowCustom<TView, TViewModel>(TViewModel vm, string? hostId = null)
|
|
||||||
where TView: Control, new()
|
|
||||||
{
|
|
||||||
var t = new DialogControl()
|
|
||||||
{
|
|
||||||
Content = new TView() { DataContext = vm },
|
|
||||||
DataContext = vm,
|
|
||||||
};
|
|
||||||
var host = OverlayDialogManager.GetHost(hostId);
|
|
||||||
host?.AddDialog(t);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
32
src/Ursa/Controls/Dialog/DialogOptions.cs
Normal file
32
src/Ursa/Controls/Dialog/DialogOptions.cs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
using Avalonia;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Ursa.Common;
|
||||||
|
|
||||||
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
|
public class DialogOptions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The Startup Location of DialogWindow. Default is <see cref="WindowStartupLocation.CenterOwner"/>
|
||||||
|
/// </summary>
|
||||||
|
public WindowStartupLocation StartupLocation { get; set; } = WindowStartupLocation.CenterOwner;
|
||||||
|
/// <summary>
|
||||||
|
/// The Position of DialogWindow startup location if <see cref="StartupLocation"/> is <see cref="WindowStartupLocation.Manual"/>
|
||||||
|
/// </summary>
|
||||||
|
public PixelPoint? Position { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Title of DialogWindow, Default is null
|
||||||
|
/// </summary>
|
||||||
|
public string? Title { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// DialogWindow's Mode, Default is <see cref="DialogMode.None"/>
|
||||||
|
/// </summary>
|
||||||
|
public DialogMode Mode { get; set; } = DialogMode.None;
|
||||||
|
|
||||||
|
public DialogButton Button { get; set; } = DialogButton.OKCancel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class OverlayDialogOptions
|
||||||
|
{
|
||||||
|
public bool ClickOnMaskToClose { get; set; } = false;
|
||||||
|
}
|
||||||
77
src/Ursa/Controls/Dialog/OverlayDialog.cs
Normal file
77
src/Ursa/Controls/Dialog/OverlayDialog.cs
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
using Avalonia.Controls;
|
||||||
|
using Ursa.Common;
|
||||||
|
|
||||||
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
|
public static class OverlayDialog
|
||||||
|
{
|
||||||
|
public static Task<DialogResult> ShowModalAsync<TView, TViewModel>(
|
||||||
|
TViewModel vm,
|
||||||
|
string? hostId = null,
|
||||||
|
string? title = null,
|
||||||
|
DialogMode mode = DialogMode.None,
|
||||||
|
DialogButton buttons = DialogButton.OKCancel)
|
||||||
|
where TView : Control, new()
|
||||||
|
{
|
||||||
|
var t = new DefaultDialogControl()
|
||||||
|
{
|
||||||
|
Content = new TView(){ DataContext = vm },
|
||||||
|
DataContext = vm,
|
||||||
|
Buttons = buttons,
|
||||||
|
Title = title,
|
||||||
|
Mode = mode,
|
||||||
|
};
|
||||||
|
var host = OverlayDialogManager.GetHost(hostId);
|
||||||
|
host?.AddModalDialog(t);
|
||||||
|
return t.ShowAsync<DialogResult>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Task<TResult> ShowCustomModalAsync<TView, TViewModel, TResult>(
|
||||||
|
TViewModel vm,
|
||||||
|
string? hostId = null)
|
||||||
|
where TView: Control, new()
|
||||||
|
{
|
||||||
|
var t = new DialogControl()
|
||||||
|
{
|
||||||
|
Content = new TView() { DataContext = vm },
|
||||||
|
DataContext = vm,
|
||||||
|
};
|
||||||
|
var host = OverlayDialogManager.GetHost(hostId);
|
||||||
|
host?.AddModalDialog(t);
|
||||||
|
return t.ShowAsync<TResult>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Show<TView, TViewModel>(
|
||||||
|
TViewModel vm,
|
||||||
|
string? hostId = null,
|
||||||
|
string? title = null,
|
||||||
|
DialogMode mode = DialogMode.None,
|
||||||
|
DialogButton buttons = DialogButton.OKCancel)
|
||||||
|
where TView: Control, new()
|
||||||
|
{
|
||||||
|
var t = new DefaultDialogControl()
|
||||||
|
{
|
||||||
|
Content = new TView() { DataContext = vm },
|
||||||
|
DataContext = vm,
|
||||||
|
Buttons = buttons,
|
||||||
|
Title = title,
|
||||||
|
Mode = mode,
|
||||||
|
};
|
||||||
|
var host = OverlayDialogManager.GetHost(hostId);
|
||||||
|
host?.AddDialog(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void ShowCustom<TView, TViewModel>(TViewModel vm, string? hostId = null)
|
||||||
|
where TView: Control, new()
|
||||||
|
{
|
||||||
|
var t = new DialogControl()
|
||||||
|
{
|
||||||
|
Content = new TView() { DataContext = vm },
|
||||||
|
DataContext = vm,
|
||||||
|
};
|
||||||
|
var host = OverlayDialogManager.GetHost(hostId);
|
||||||
|
host?.AddDialog(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user