using Avalonia; using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls.Shapes; using Avalonia.Media; using Ursa.Common; namespace Ursa.Controls; public static class Dialog { /// /// 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(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(); } else { window.Show(owner); } } /// /// Show a Window Dialog that with all content fully customized. And the owner of the dialog is specified. /// /// View to show in Dialog Window /// ViewModel /// Owner Window /// Dialog options to configure the window. public static void ShowCustom(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(); } else { window.Show(owner); } } /// /// Show a Modal Dialog Window with default style. /// /// /// /// /// /// /// public static Task ShowModal(TViewModel vm, Window? owner = null, DialogOptions? options = null) where TView: Control, new() { 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); } /// /// Show a Modal Dialog Window with default style. /// /// /// /// /// /// 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); } /// /// Show a Modal Dialog Window with all content fully customized. /// /// /// /// /// /// /// /// 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); } /// /// Show a Modal Dialog Window with all content fully customized. /// /// /// /// /// /// /// 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); } /// /// Get the main window of the application as default owner of the dialog. /// /// private static Window? GetMainWindow() { var lifetime = Application.Current?.ApplicationLifetime; return lifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: { } w } ? w : null; } /// /// Attach options to dialog window. /// /// /// private static void AssignOptionsToDialogWindow(DialogWindow window, DialogOptions? options) { if (options is null) { 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; } } } /// /// Attach options to default dialog window. /// /// /// private static void AssignOptionsToDefaultDialogWindow(DefaultDialogWindow window, DialogOptions? options) { if (options is null) { options = new DialogOptions(); } window.WindowStartupLocation = options.StartupLocation; window.Title = options.Title; window.Buttons = options.Button; window.Mode = options.Mode; if (options.StartupLocation == WindowStartupLocation.Manual) { if (options.Position is not null) { window.Position = options.Position.Value; } else { window.WindowStartupLocation = WindowStartupLocation.CenterOwner; } } } }