diff --git a/samples/Avalonia.Labs.Catalog/Avalonia.Labs.Catalog.csproj b/samples/Avalonia.Labs.Catalog/Avalonia.Labs.Catalog.csproj index 0e4c0c0..14a2011 100644 --- a/samples/Avalonia.Labs.Catalog/Avalonia.Labs.Catalog.csproj +++ b/samples/Avalonia.Labs.Catalog/Avalonia.Labs.Catalog.csproj @@ -38,4 +38,11 @@ + + + NotificationView.axaml + Code + + + diff --git a/samples/Avalonia.Labs.Catalog/ViewModels/NotificationsViewModel.cs b/samples/Avalonia.Labs.Catalog/ViewModels/NotificationsViewModel.cs new file mode 100644 index 0000000..8465ac4 --- /dev/null +++ b/samples/Avalonia.Labs.Catalog/ViewModels/NotificationsViewModel.cs @@ -0,0 +1,117 @@ +using System.Threading.Tasks; +using System; +using System.Reactive; +using Avalonia.Controls.Notifications; +using Avalonia.Labs.Catalog.Views; +using Avalonia.Labs.Controls; +using ReactiveUI; + +namespace Avalonia.Labs.Catalog.ViewModels; + +internal class NotificationsViewModel : ViewModelBase +{ + static NotificationsViewModel() + { + ViewLocator.Register(typeof(NotificationsViewModel), () => new NotificationsView()); + } + + public NotificationsViewModel() + { + Title = "Notification"; + ShowNotificationCommand = ReactiveCommand.CreateFromTask(ShowNotificationAsync); + } + + private string? _notificationText = null; + public string? NotificationText + { + get => _notificationText; + set => this.RaiseAndSetIfChanged(ref _notificationText, value); + } + private string? _actionButtonText = "action"; + public string? ActionButtonText + { + get => _actionButtonText; + set => this.RaiseAndSetIfChanged(ref _actionButtonText, value); + } + + private int _notificationDurationSeconds = 3; + public int NotificationDurationSeconds + { + get => _notificationDurationSeconds; + set => this.RaiseAndSetIfChanged(ref _notificationDurationSeconds, value); + } + private NotificationType _notificationType = Avalonia.Controls.Notifications.NotificationType.Information; + public NotificationType NotificationType + { + get => _notificationType; + set => this.RaiseAndSetIfChanged(ref _notificationType, value); + } + private NotificationPosition _notificationPosition = NotificationPosition.TopLeft; + public NotificationPosition NotificationPosition + { + get => _notificationPosition; + set => this.RaiseAndSetIfChanged(ref _notificationPosition, value); + } + + public ReactiveCommand ShowNotificationCommand { get; private set; } + + private Task ShowNotificationAsync() + { + if (NotificationPosition == NotificationPosition.TopCenter) + { + NotificationManager.Default.ShowNotification(new NotificationOptions() + { + Type=NotificationType.Error, + Content="NotificationPosition.BottomCenter is not yet supported by Avalonia.Labs.Controls since it targets avalonia 11.0.0" + }); + return Task.CompletedTask; + } + + if (NotificationPosition == NotificationPosition.BottomCenter) + { + NotificationManager.Default.ShowNotification(new NotificationOptions() + { + Type=NotificationType.Error, + Content="NotificationPosition.BottomCenter is not yet supported by Avalonia.Labs.Controls since it targets avalonia 11.0.0" + }); + return Task.CompletedTask; + } + + var o = new NotificationOptions() + { + Content = NotificationText ?? $"Hello {_notificationCount:00}", + Type = NotificationType, + Duration = NotificationDurationSeconds == 0 ? + TimeSpan.FromDays(30) // "infinitely open" + : + TimeSpan.FromSeconds(NotificationDurationSeconds), + Position = NotificationPosition, + ClickActionText = ActionButtonText ?? string.Empty, + DismissAction = () => + { + NotificationManager.Default.ShowNotification(new NotificationOptions + { + Content = $"Notification dismissed: '{NotificationText}'", Type = NotificationType.Success, + }); + return Task.CompletedTask; + } + }; + if (!string.IsNullOrEmpty(o.ClickActionText)) + o.ClickAction = () => + { + NotificationManager.Default.ShowNotification(new NotificationOptions + { + Content = $"Action '{ActionButtonText}' clicked", + }); + return Task.CompletedTask; + }; + + NotificationManager.Default.ShowNotification(o); + + _notificationCount++; + + return Task.CompletedTask; + } + + private int _notificationCount = 1; +} diff --git a/samples/Avalonia.Labs.Catalog/Views/NotificationsView.axaml b/samples/Avalonia.Labs.Catalog/Views/NotificationsView.axaml new file mode 100644 index 0000000..221d890 --- /dev/null +++ b/samples/Avalonia.Labs.Catalog/Views/NotificationsView.axaml @@ -0,0 +1,110 @@ + + + M 23.935547,4.0019531 C 23.275047,4.0354318 22.748047,4.5831669 22.748047,5.2519531 V 7.7519531 C 22.748047,8.4423031 23.307647,9.0019531 23.998047,9.0019531 24.688347,9.0019531 25.248047,8.4423031 25.248047,7.7519531 V 5.2519531 C 25.248047,4.5615931 24.688347,4.0019531 23.998047,4.0019531 23.976472,4.0019531 23.956853,4.0008732 23.935547,4.0019531 Z M 10.25,9 C 9.9300987,9 9.6093094,9.1231125 9.3652344,9.3671875 8.8770744,9.8553475 8.8770744,10.646666 9.3652344,11.134766 L 11.865234,13.634766 C 12.353334,14.122966 13.146666,14.122966 13.634766,13.634766 14.122966,13.146666 14.122966,12.355287 13.634766,11.867188 L 11.134766,9.3671875 C 10.890716,9.1231125 10.569901,9 10.25,9 Z M 37.75,9 C 37.430087,9 37.111288,9.1231125 36.867188,9.3671875 L 34.367188,11.867188 C 33.879088,12.355287 33.879087,13.146666 34.367188,13.634766 34.855388,14.122966 35.646666,14.122966 36.134766,13.634766 L 38.634766,11.134766 C 39.122966,10.646666 39.122966,9.8553475 38.634766,9.3671875 38.390716,9.1231125 38.069913,9 37.75,9 Z M 24,14 C 18.47715,14 14,18.47715 14,24 14,29.5228 18.47715,34 24,34 29.5228,34 34,29.5228 34,24 34,18.47715 29.5228,14 24,14 Z M 24,15.5 C 28.6944,15.5 32.5,19.30558 32.5,24 32.5,28.6944 28.6944,32.5 24,32.5 Z M 5.25,22.75 C 4.55965,22.75 4,23.3097 4,24 4,24.6904 4.55965,25.25 5.25,25.25 H 7.75 C 8.44035,25.25 9,24.6904 9,24 9,23.3097 8.44035,22.75 7.75,22.75 Z M 40.25,22.75 C 39.5596,22.75 39,23.3097 39,24 39,24.6904 39.5596,25.25 40.25,25.25 H 42.75 C 43.4403,25.25 44,24.6904 44,24 44,23.3097 43.4403,22.75 42.75,22.75 Z M 12.75,34 C 12.4301,34 12.109284,34.123087 11.865234,34.367188 L 9.3652344,36.867188 C 8.8770744,37.355287 8.8770744,38.146666 9.3652344,38.634766 9.8533844,39.122966 10.646666,39.122966 11.134766,38.634766 L 13.634766,36.134766 C 14.122966,35.646666 14.122966,34.855288 13.634766,34.367188 13.390716,34.123087 13.0699,34 12.75,34 Z M 35.25,34 C 34.930087,34 34.611288,34.123087 34.367188,34.367188 33.879088,34.855288 33.879087,35.646666 34.367188,36.134766 L 36.867188,38.634766 C 37.355388,39.122966 38.146666,39.122966 38.634766,38.634766 39.122966,38.146666 39.122966,37.355287 38.634766,36.867188 L 36.134766,34.367188 C 35.890716,34.123087 35.569913,34 35.25,34 Z M 23.998047,39 C 23.307647,39 22.748047,39.5597 22.748047,40.25 V 42.75 C 22.748047,43.4404 23.307647,44 23.998047,44 24.688347,44 25.248047,43.4404 25.248047,42.75 V 40.25 C 25.248047,39.5597 24.688347,39 23.998047,39 Z + M3,6H21V8H3V6M3,11H21V13H3V11M3,16H21V18H3V16Z + + + + + + + + + + + + + + + + + + + + Information + Success + Warning + Error + + + + + TopLeft + TopCenter + TopRight + BottomLeft + BottomCenter + BottomRight + + + + + + + + + + + + + + + + + + +