diff --git a/demo/Ursa.Demo/ViewModels/DialogDemoViewModel.cs b/demo/Ursa.Demo/ViewModels/DialogDemoViewModel.cs index cc8c5e5..901f183 100644 --- a/demo/Ursa.Demo/ViewModels/DialogDemoViewModel.cs +++ b/demo/Ursa.Demo/ViewModels/DialogDemoViewModel.cs @@ -90,11 +90,13 @@ public class DialogDemoViewModel: ObservableObject var vm = new PlainDialogViewModel(); if (IsWindow) { - DefaultResult = await Dialog.ShowModalAsync( - vm, - "Please select a date", - SelectedMode, - SelectedButton); + DefaultResult = await Dialog.ShowModal( + vm, options: new DialogOptions() + { + Title = "Please select a date", + Mode = SelectedMode, + Button = SelectedButton + }); Date = vm.Date; } else @@ -131,7 +133,7 @@ public class DialogDemoViewModel: ObservableObject if (IsModal) { - Result = await Dialog.ShowCustomModalAsync( + Result = await Dialog.ShowCustomModal( vm); Date = vm.Date; } diff --git a/src/Ursa/Controls/Dialog/Dialog.cs b/src/Ursa/Controls/Dialog/Dialog.cs index bdccbcd..ec987ad 100644 --- a/src/Ursa/Controls/Dialog/Dialog.cs +++ b/src/Ursa/Controls/Dialog/Dialog.cs @@ -9,81 +9,25 @@ namespace Ursa.Controls; public static class Dialog { - /// - /// Show a Window Modal Dialog that with all content fully customized. - /// - /// - /// - /// - /// - /// - public static async Task ShowCustomModalAsync(TViewModel vm) - where TView : Control, new() - { - var mainWindow = GetMainWindow(); - return await ShowCustomModalAsync(mainWindow, vm); - } - - /// - /// Show a Window Dialog that with all content fully customized. - /// - /// - /// - /// - /// - public static void ShowCustom(TViewModel vm) - where TView : Control, new() - { - var mainWindow = GetMainWindow(); - ShowCustom(mainWindow, vm); - } - - /// - /// Show a Window Modal Dialog that with all content fully customized. And the owner of the dialog is specified. - /// - /// - /// - /// - /// - /// - /// - public static async Task ShowCustomModalAsync(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(owner); - return result; - } - } - - /// /// Show a Window Dialog that with all content fully customized. And the owner of the dialog is specified. /// + /// Dialog ViewModel instance /// - /// + /// /// /// /// - public static void ShowCustom(Window? owner, TViewModel? vm) + public static void ShowCustom(TViewModel? vm, Window? owner = null, DialogOptions? options = null) where TView: Control, new() { var window = new DialogWindow { - Content = new TView { DataContext = vm }, + Content = new TView(), DataContext = vm, }; + AssignOptionsToDialogWindow(window, options); + owner ??= GetMainWindow(); if (owner is null) { window.Show(); @@ -94,43 +38,96 @@ public static class Dialog } } - public static async Task ShowModalAsync( - Window? owner, - TViewModel vm, - string? title = null, - DialogMode mode = DialogMode.None, - DialogButton buttons = DialogButton.OKCancel) - where TView : Control, new() - { - var window = new DefaultDialogWindow() + public static void ShowCustom(Control view, object? vm, Window? owner = null, DialogOptions? options = null) + { + var window = new DialogWindow { - Content = new TView() { DataContext = vm }, + Content = view, DataContext = vm, - Buttons = buttons, - Title = title, - Mode = mode, }; + AssignOptionsToDialogWindow(window, options); + owner ??= GetMainWindow(); if (owner is null) { window.Show(); - return DialogResult.None; } else { - var result = await window.ShowDialog(owner); - return result; + window.Show(owner); } } - - public static async Task ShowModalAsync( - TViewModel vm, - string? title = null, - DialogMode mode = DialogMode.None, - DialogButton buttons = DialogButton.OKCancel) + + public static Task ShowModal(TViewModel vm, Window? owner = null, + DialogOptions? options = null) where TView: Control, new() { - var mainWindow = GetMainWindow(); - return await ShowModalAsync(mainWindow, vm, title, mode, buttons); + var window = new DefaultDialogWindow + { + Content = new TView(), + DataContext = vm, + }; + AssignOptionsToDefaultDialogWindow(window, options); + owner ??= GetMainWindow(); + if (owner is null) + { + window.Show(); + return Task.FromResult(DialogResult.None); + } + return window.ShowDialog(owner); + } + + public static Task 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(owner); + } + + public static Task ShowCustomModal(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(owner); + } + + public static Task ShowCustomModal(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(owner); } private static Window? GetMainWindow() @@ -138,76 +135,49 @@ public static class Dialog var lifetime = Application.Current?.ApplicationLifetime; return lifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: { } w } ? w : null; } -} -public static class OverlayDialog -{ - public static Task ShowModalAsync( - TViewModel vm, - string? hostId = null, - string? title = null, - DialogMode mode = DialogMode.None, - DialogButton buttons = DialogButton.OKCancel) - where TView : Control, new() + private static void AssignOptionsToDialogWindow(DialogWindow window, DialogOptions? options) { - var t = new DefaultDialogControl() + if (options is null) { - Content = new TView(){ DataContext = vm }, - DataContext = vm, - Buttons = buttons, - Title = title, - Mode = mode, - }; - var host = OverlayDialogManager.GetHost(hostId); - host?.AddModalDialog(t); - return t.ShowAsync(); + options = new DialogOptions(); + } + window.WindowStartupLocation = options.StartupLocation; + window.Title = options.Title; + if (options.StartupLocation == WindowStartupLocation.Manual) + { + if (options.Position is not null) + { + window.Position = options.Position.Value; + } + else + { + window.WindowStartupLocation = WindowStartupLocation.CenterOwner; + } + } } - - public static Task ShowCustomModalAsync( - TViewModel vm, - string? hostId = null) - where TView: Control, new() + + private static void AssignOptionsToDefaultDialogWindow(DefaultDialogWindow window, DialogOptions? options) { - var t = new DialogControl() + if (options is null) { - Content = new TView() { DataContext = vm }, - DataContext = vm, - }; - var host = OverlayDialogManager.GetHost(hostId); - host?.AddModalDialog(t); - return t.ShowAsync(); - } - - public static void Show( - TViewModel vm, - string? hostId = null, - string? title = null, - DialogMode mode = DialogMode.None, - DialogButton buttons = DialogButton.OKCancel) - where TView: Control, new() - { - var t = new DefaultDialogControl() + options = new DialogOptions(); + } + window.WindowStartupLocation = options.StartupLocation; + window.Title = options.Title; + window.Buttons = options.Button; + window.Mode = options.Mode; + if (options.StartupLocation == WindowStartupLocation.Manual) { - 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(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); + if (options.Position is not null) + { + window.Position = options.Position.Value; + } + else + { + window.WindowStartupLocation = WindowStartupLocation.CenterOwner; + } + } } diff --git a/src/Ursa/Controls/Dialog/DialogOptions.cs b/src/Ursa/Controls/Dialog/DialogOptions.cs new file mode 100644 index 0000000..4d50db7 --- /dev/null +++ b/src/Ursa/Controls/Dialog/DialogOptions.cs @@ -0,0 +1,32 @@ +using Avalonia; +using Avalonia.Controls; +using Ursa.Common; + +namespace Ursa.Controls; + +public class DialogOptions +{ + /// + /// The Startup Location of DialogWindow. Default is + /// + public WindowStartupLocation StartupLocation { get; set; } = WindowStartupLocation.CenterOwner; + /// + /// The Position of DialogWindow startup location if is + /// + public PixelPoint? Position { get; set; } + /// + /// Title of DialogWindow, Default is null + /// + public string? Title { get; set; } + /// + /// DialogWindow's Mode, Default is + /// + public DialogMode Mode { get; set; } = DialogMode.None; + + public DialogButton Button { get; set; } = DialogButton.OKCancel; +} + +public class OverlayDialogOptions +{ + public bool ClickOnMaskToClose { get; set; } = false; +} \ No newline at end of file diff --git a/src/Ursa/Controls/Dialog/OverlayDialog.cs b/src/Ursa/Controls/Dialog/OverlayDialog.cs new file mode 100644 index 0000000..3e74325 --- /dev/null +++ b/src/Ursa/Controls/Dialog/OverlayDialog.cs @@ -0,0 +1,77 @@ +using Avalonia.Controls; +using Ursa.Common; + +namespace Ursa.Controls; + +public static class OverlayDialog +{ + public static Task ShowModalAsync( + 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(); + } + + public static Task ShowCustomModalAsync( + 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(); + } + + public static void Show( + 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(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); + } + + +} \ No newline at end of file