From f0ec32c8702d2de1858db9aba5048fb608b3b045 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Mon, 22 Jan 2024 18:32:24 +0800 Subject: [PATCH] feat: add overlay dialog sample. --- demo/Ursa.Demo/Dialogs/DialogWithAction.axaml | 18 ++++++++++ .../Dialogs/DialogWithAction.axaml.cs | 13 +++++++ .../Dialogs/DialogWithActionViewModel.cs | 36 +++++++++++++++++++ demo/Ursa.Demo/Dialogs/PlainDialog.axaml | 8 +++++ demo/Ursa.Demo/Dialogs/PlainDialog.axaml.cs | 13 +++++++ .../Ursa.Demo/Dialogs/PlainDialogViewModel.cs | 8 +++++ demo/Ursa.Demo/Pages/DialogDemo.axaml | 4 +++ .../ViewModels/DialogDemoViewModel.cs | 18 ++++++++-- src/Ursa/Controls/Dialog/DialogBox.cs | 4 +-- src/Ursa/Controls/Dialog/DialogControl.cs | 26 +++++++++++--- 10 files changed, 139 insertions(+), 9 deletions(-) create mode 100644 demo/Ursa.Demo/Dialogs/DialogWithAction.axaml create mode 100644 demo/Ursa.Demo/Dialogs/DialogWithAction.axaml.cs create mode 100644 demo/Ursa.Demo/Dialogs/DialogWithActionViewModel.cs create mode 100644 demo/Ursa.Demo/Dialogs/PlainDialog.axaml create mode 100644 demo/Ursa.Demo/Dialogs/PlainDialog.axaml.cs create mode 100644 demo/Ursa.Demo/Dialogs/PlainDialogViewModel.cs diff --git a/demo/Ursa.Demo/Dialogs/DialogWithAction.axaml b/demo/Ursa.Demo/Dialogs/DialogWithAction.axaml new file mode 100644 index 0000000..022fed8 --- /dev/null +++ b/demo/Ursa.Demo/Dialogs/DialogWithAction.axaml @@ -0,0 +1,18 @@ + + + + + + + + + + diff --git a/demo/Ursa.Demo/Dialogs/DialogWithAction.axaml.cs b/demo/Ursa.Demo/Dialogs/DialogWithAction.axaml.cs new file mode 100644 index 0000000..783f47b --- /dev/null +++ b/demo/Ursa.Demo/Dialogs/DialogWithAction.axaml.cs @@ -0,0 +1,13 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Markup.Xaml; + +namespace Ursa.Demo.Dialogs; + +public partial class DialogWithAction : UserControl +{ + public DialogWithAction() + { + InitializeComponent(); + } +} \ No newline at end of file diff --git a/demo/Ursa.Demo/Dialogs/DialogWithActionViewModel.cs b/demo/Ursa.Demo/Dialogs/DialogWithActionViewModel.cs new file mode 100644 index 0000000..d5a3b58 --- /dev/null +++ b/demo/Ursa.Demo/Dialogs/DialogWithActionViewModel.cs @@ -0,0 +1,36 @@ +using System; +using System.Windows.Input; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; +using Ursa.Controls; + +namespace Ursa.Demo.Dialogs; + +public partial class DialogWithActionViewModel: ObservableObject, IDialogContext +{ + [ObservableProperty] private string _title; + [ObservableProperty] private DateTime _date; + public object? DefaultCloseResult { get; set; } = true; + public event EventHandler? Closed; + + public ICommand OKCommand { get; set; } + public ICommand CancelCommand { get; set; } + + public DialogWithActionViewModel() + { + OKCommand = new RelayCommand(OK); + CancelCommand = new RelayCommand(Cancel); + Title = "Please select a date"; + Date = DateTime.Now; + } + + private void OK() + { + Closed?.Invoke(this, true); + } + + private void Cancel() + { + Closed?.Invoke(this, false); + } +} \ No newline at end of file diff --git a/demo/Ursa.Demo/Dialogs/PlainDialog.axaml b/demo/Ursa.Demo/Dialogs/PlainDialog.axaml new file mode 100644 index 0000000..7879fe4 --- /dev/null +++ b/demo/Ursa.Demo/Dialogs/PlainDialog.axaml @@ -0,0 +1,8 @@ + + + diff --git a/demo/Ursa.Demo/Dialogs/PlainDialog.axaml.cs b/demo/Ursa.Demo/Dialogs/PlainDialog.axaml.cs new file mode 100644 index 0000000..1e4be06 --- /dev/null +++ b/demo/Ursa.Demo/Dialogs/PlainDialog.axaml.cs @@ -0,0 +1,13 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Markup.Xaml; + +namespace Ursa.Demo.Dialogs; + +public partial class PlainDialog : UserControl +{ + public PlainDialog() + { + InitializeComponent(); + } +} \ No newline at end of file diff --git a/demo/Ursa.Demo/Dialogs/PlainDialogViewModel.cs b/demo/Ursa.Demo/Dialogs/PlainDialogViewModel.cs new file mode 100644 index 0000000..e7105f2 --- /dev/null +++ b/demo/Ursa.Demo/Dialogs/PlainDialogViewModel.cs @@ -0,0 +1,8 @@ +using CommunityToolkit.Mvvm.ComponentModel; + +namespace Ursa.Demo.Dialogs; + +public class PlainDialogViewModel: ObservableObject +{ + +} \ No newline at end of file diff --git a/demo/Ursa.Demo/Pages/DialogDemo.axaml b/demo/Ursa.Demo/Pages/DialogDemo.axaml index 24e36eb..ef5dc49 100644 --- a/demo/Ursa.Demo/Pages/DialogDemo.axaml +++ b/demo/Ursa.Demo/Pages/DialogDemo.axaml @@ -13,6 +13,10 @@ mc:Ignorable="d"> + + + + diff --git a/demo/Ursa.Demo/ViewModels/DialogDemoViewModel.cs b/demo/Ursa.Demo/ViewModels/DialogDemoViewModel.cs index f1ca5f6..f9144e2 100644 --- a/demo/Ursa.Demo/ViewModels/DialogDemoViewModel.cs +++ b/demo/Ursa.Demo/ViewModels/DialogDemoViewModel.cs @@ -4,6 +4,7 @@ using System.Windows.Input; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Ursa.Controls; +using Ursa.Demo.Dialogs; using Ursa.Demo.Pages; namespace Ursa.Demo.ViewModels; @@ -14,6 +15,14 @@ public class DialogDemoViewModel: ObservableObject public ICommand ShowGlobalOverlayDialogCommand { get; } public ICommand ShowGlobalDialogCommand { get; } + private object? _result; + + public object? Result + { + get => _result; + set => SetProperty(ref _result, value); + } + public DialogDemoViewModel() { ShowLocalOverlayDialogCommand = new AsyncRelayCommand(ShowLocalOverlayDialog); @@ -23,16 +32,19 @@ public class DialogDemoViewModel: ObservableObject private async Task ShowGlobalDialog() { - var result = await DialogBox.ShowAsync(new ButtonGroupDemoViewModel()); + var result = await DialogBox.ShowAsync(new DialogWithActionViewModel()); + Result = result; } private async Task ShowGlobalOverlayDialog() { - await DialogBox.ShowOverlayAsync(new ButtonGroupDemoViewModel(), "GlobalHost"); + await DialogBox.ShowOverlayAsync(new DialogWithActionViewModel(), "GlobalHost"); } private async Task ShowLocalOverlayDialog() { - await DialogBox.ShowOverlayAsync(new ButtonGroupDemoViewModel(), "LocalHost"); + var result = await DialogBox.ShowOverlayAsync( + new DialogWithActionViewModel(), "LocalHost"); + Result = result; } } \ No newline at end of file diff --git a/src/Ursa/Controls/Dialog/DialogBox.cs b/src/Ursa/Controls/Dialog/DialogBox.cs index 90e68d7..a5d4fa7 100644 --- a/src/Ursa/Controls/Dialog/DialogBox.cs +++ b/src/Ursa/Controls/Dialog/DialogBox.cs @@ -47,7 +47,7 @@ public static class DialogBox } - public static async Task ShowOverlayAsync(TViewModel vm, string hostId) + public static Task ShowOverlayAsync(TViewModel vm, string hostId) where TView : Control, new() where TViewModel: new() { @@ -59,6 +59,6 @@ public static class DialogBox t.DataContext = vm; var host = OverlayDialogManager.GetOverlayDialogHost(hostId); host?.AddDialog(t); - return null; + return t.ShowAsync(); } } \ No newline at end of file diff --git a/src/Ursa/Controls/Dialog/DialogControl.cs b/src/Ursa/Controls/Dialog/DialogControl.cs index d544674..7aeae47 100644 --- a/src/Ursa/Controls/Dialog/DialogControl.cs +++ b/src/Ursa/Controls/Dialog/DialogControl.cs @@ -27,18 +27,36 @@ public class DialogControl: ContentControl { _closeButton.Click += Close; } - } - public void Show() + if (this.DataContext is IDialogContext context) + { + context.Closed += Close; + } + } + + + public Task ShowAsync() { - + var tcs = new TaskCompletionSource(); + this.OnClose+= (sender, args) => + { + if (args is T result) + { + tcs.SetResult(result); + } + else + { + tcs.SetResult(default); + } + }; + return tcs.Task; } private void Close(object sender, object args) { if (this.DataContext is IDialogContext context) { - OnClose?.Invoke(this, context.DefaultCloseResult); + OnClose?.Invoke(this, args); } else {