From edd6b402a3aa33c58933f1d397ea906b12fe634e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wies=C5=82aw=20=C5=A0olt=C3=A9s?= Date: Thu, 22 May 2025 14:07:00 +0200 Subject: [PATCH 1/3] Add notification type actions and sample --- README.md | 14 ++++ .../Views/MainView.axaml | 3 + .../Views/Pages/NotificationsView.axaml | 53 +++++++++++++ .../Views/Pages/NotificationsView.axaml.cs | 17 +++++ .../Actions/ShowErrorNotificationAction.cs | 75 +++++++++++++++++++ .../ShowInformationNotificationAction.cs | 75 +++++++++++++++++++ .../Actions/ShowNotificationAction.cs | 61 +++++++++++++++ .../Actions/ShowSuccessNotificationAction.cs | 75 +++++++++++++++++++ .../Actions/ShowWarningNotificationAction.cs | 75 +++++++++++++++++++ .../WindowNotificationManagerBehavior.cs | 33 ++++++++ 10 files changed, 481 insertions(+) create mode 100644 samples/BehaviorsTestApplication/Views/Pages/NotificationsView.axaml create mode 100644 samples/BehaviorsTestApplication/Views/Pages/NotificationsView.axaml.cs create mode 100644 src/Avalonia.Xaml.Interactions.Custom/Actions/ShowErrorNotificationAction.cs create mode 100644 src/Avalonia.Xaml.Interactions.Custom/Actions/ShowInformationNotificationAction.cs create mode 100644 src/Avalonia.Xaml.Interactions.Custom/Actions/ShowNotificationAction.cs create mode 100644 src/Avalonia.Xaml.Interactions.Custom/Actions/ShowSuccessNotificationAction.cs create mode 100644 src/Avalonia.Xaml.Interactions.Custom/Actions/ShowWarningNotificationAction.cs create mode 100644 src/Avalonia.Xaml.Interactions.Custom/Notifications/WindowNotificationManagerBehavior.cs diff --git a/README.md b/README.md index 2fb7cac85..d7e922596 100644 --- a/README.md +++ b/README.md @@ -155,6 +155,20 @@ This section provides an overview of all available classes and their purpose in - **SetClipboardTextAction** *Places text onto the clipboard.* +### Notifications +- **WindowNotificationManagerBehavior** + *Creates a notification manager for a window when attached.* +- **ShowNotificationAction** + *Shows a notification using an attached notification manager.* +- **ShowInformationNotificationAction** + *Displays an information notification via a manager.* +- **ShowSuccessNotificationAction** + *Displays a success notification via a manager.* +- **ShowWarningNotificationAction** + *Displays a warning notification via a manager.* +- **ShowErrorNotificationAction** + *Displays an error notification via a manager.* + ### Composition - **SelectingItemsControlBehavior** *Animates selection transitions in items controls such as ListBox or TabControl.* diff --git a/samples/BehaviorsTestApplication/Views/MainView.axaml b/samples/BehaviorsTestApplication/Views/MainView.axaml index b222f3eec..c2c9697f1 100644 --- a/samples/BehaviorsTestApplication/Views/MainView.axaml +++ b/samples/BehaviorsTestApplication/Views/MainView.axaml @@ -156,6 +156,9 @@ + + + diff --git a/samples/BehaviorsTestApplication/Views/Pages/NotificationsView.axaml b/samples/BehaviorsTestApplication/Views/Pages/NotificationsView.axaml new file mode 100644 index 000000000..7d280a40c --- /dev/null +++ b/samples/BehaviorsTestApplication/Views/Pages/NotificationsView.axaml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + diff --git a/samples/BehaviorsTestApplication/Views/Pages/NotificationsView.axaml.cs b/samples/BehaviorsTestApplication/Views/Pages/NotificationsView.axaml.cs new file mode 100644 index 000000000..dc3e5be1b --- /dev/null +++ b/samples/BehaviorsTestApplication/Views/Pages/NotificationsView.axaml.cs @@ -0,0 +1,17 @@ +using Avalonia.Controls; +using Avalonia.Markup.Xaml; + +namespace BehaviorsTestApplication.Views.Pages; + +public partial class NotificationsView : UserControl +{ + public NotificationsView() + { + InitializeComponent(); + } + + private void InitializeComponent() + { + AvaloniaXamlLoader.Load(this); + } +} diff --git a/src/Avalonia.Xaml.Interactions.Custom/Actions/ShowErrorNotificationAction.cs b/src/Avalonia.Xaml.Interactions.Custom/Actions/ShowErrorNotificationAction.cs new file mode 100644 index 000000000..696ad2574 --- /dev/null +++ b/src/Avalonia.Xaml.Interactions.Custom/Actions/ShowErrorNotificationAction.cs @@ -0,0 +1,75 @@ +using Avalonia.Controls.Notifications; +using Avalonia.Xaml.Interactivity; + +namespace Avalonia.Xaml.Interactions.Custom; + +/// +/// Shows an error notification using an . +/// +public class ShowErrorNotificationAction : StyledElementAction +{ + /// + /// Identifies the avalonia property. + /// + public static readonly StyledProperty NotificationManagerProperty = + AvaloniaProperty.Register(nameof(NotificationManager)); + + /// + /// Identifies the avalonia property. + /// + public static readonly StyledProperty TitleProperty = + AvaloniaProperty.Register(nameof(Title)); + + /// + /// Identifies the avalonia property. + /// + public static readonly StyledProperty MessageProperty = + AvaloniaProperty.Register(nameof(Message)); + + /// + /// Gets or sets the used to display the notification. This is an avalonia property. + /// + [ResolveByName] + public INotificationManager? NotificationManager + { + get => GetValue(NotificationManagerProperty); + set => SetValue(NotificationManagerProperty, value); + } + + /// + /// Gets or sets the notification title. This is an avalonia property. + /// + public string? Title + { + get => GetValue(TitleProperty); + set => SetValue(TitleProperty, value); + } + + /// + /// Gets or sets the notification message. This is an avalonia property. + /// + public string? Message + { + get => GetValue(MessageProperty); + set => SetValue(MessageProperty, value); + } + + /// + public override object? Execute(object? sender, object? parameter) + { + if (!IsEnabled) + { + return false; + } + + var manager = NotificationManager; + if (manager is null) + { + return false; + } + + var notification = new Notification(Title ?? string.Empty, Message ?? string.Empty, NotificationType.Error); + manager.Show(notification); + return true; + } +} diff --git a/src/Avalonia.Xaml.Interactions.Custom/Actions/ShowInformationNotificationAction.cs b/src/Avalonia.Xaml.Interactions.Custom/Actions/ShowInformationNotificationAction.cs new file mode 100644 index 000000000..787c52d66 --- /dev/null +++ b/src/Avalonia.Xaml.Interactions.Custom/Actions/ShowInformationNotificationAction.cs @@ -0,0 +1,75 @@ +using Avalonia.Controls.Notifications; +using Avalonia.Xaml.Interactivity; + +namespace Avalonia.Xaml.Interactions.Custom; + +/// +/// Shows an information notification using an . +/// +public class ShowInformationNotificationAction : StyledElementAction +{ + /// + /// Identifies the avalonia property. + /// + public static readonly StyledProperty NotificationManagerProperty = + AvaloniaProperty.Register(nameof(NotificationManager)); + + /// + /// Identifies the avalonia property. + /// + public static readonly StyledProperty TitleProperty = + AvaloniaProperty.Register(nameof(Title)); + + /// + /// Identifies the avalonia property. + /// + public static readonly StyledProperty MessageProperty = + AvaloniaProperty.Register(nameof(Message)); + + /// + /// Gets or sets the used to display the notification. This is an avalonia property. + /// + [ResolveByName] + public INotificationManager? NotificationManager + { + get => GetValue(NotificationManagerProperty); + set => SetValue(NotificationManagerProperty, value); + } + + /// + /// Gets or sets the notification title. This is an avalonia property. + /// + public string? Title + { + get => GetValue(TitleProperty); + set => SetValue(TitleProperty, value); + } + + /// + /// Gets or sets the notification message. This is an avalonia property. + /// + public string? Message + { + get => GetValue(MessageProperty); + set => SetValue(MessageProperty, value); + } + + /// + public override object? Execute(object? sender, object? parameter) + { + if (!IsEnabled) + { + return false; + } + + var manager = NotificationManager; + if (manager is null) + { + return false; + } + + var notification = new Notification(Title ?? string.Empty, Message ?? string.Empty, NotificationType.Information); + manager.Show(notification); + return true; + } +} diff --git a/src/Avalonia.Xaml.Interactions.Custom/Actions/ShowNotificationAction.cs b/src/Avalonia.Xaml.Interactions.Custom/Actions/ShowNotificationAction.cs new file mode 100644 index 000000000..65ff8e413 --- /dev/null +++ b/src/Avalonia.Xaml.Interactions.Custom/Actions/ShowNotificationAction.cs @@ -0,0 +1,61 @@ +using Avalonia.Controls.Notifications; +using Avalonia.Xaml.Interactivity; + +namespace Avalonia.Xaml.Interactions.Custom; + +/// +/// Shows a notification using an . +/// +public class ShowNotificationAction : StyledElementAction +{ + /// + /// Identifies the avalonia property. + /// + public static readonly StyledProperty NotificationManagerProperty = + AvaloniaProperty.Register(nameof(NotificationManager)); + + /// + /// Identifies the avalonia property. + /// + public static readonly StyledProperty NotificationProperty = + AvaloniaProperty.Register(nameof(Notification)); + + /// + /// Gets or sets the used to display the notification. This is an avalonia property. + /// + [ResolveByName] + public INotificationManager? NotificationManager + { + get => GetValue(NotificationManagerProperty); + set => SetValue(NotificationManagerProperty, value); + } + + /// + /// Gets or sets the instance to show. This is an avalonia property. + /// + public INotification? Notification + { + get => GetValue(NotificationProperty); + set => SetValue(NotificationProperty, value); + } + + /// + public override object? Execute(object? sender, object? parameter) + { + if (!IsEnabled) + { + return false; + } + + var manager = NotificationManager; + var notification = Notification; + + if (manager is null || notification is null) + { + return false; + } + + manager.Show(notification); + return true; + } +} diff --git a/src/Avalonia.Xaml.Interactions.Custom/Actions/ShowSuccessNotificationAction.cs b/src/Avalonia.Xaml.Interactions.Custom/Actions/ShowSuccessNotificationAction.cs new file mode 100644 index 000000000..33c0cb3fd --- /dev/null +++ b/src/Avalonia.Xaml.Interactions.Custom/Actions/ShowSuccessNotificationAction.cs @@ -0,0 +1,75 @@ +using Avalonia.Controls.Notifications; +using Avalonia.Xaml.Interactivity; + +namespace Avalonia.Xaml.Interactions.Custom; + +/// +/// Shows a success notification using an . +/// +public class ShowSuccessNotificationAction : StyledElementAction +{ + /// + /// Identifies the avalonia property. + /// + public static readonly StyledProperty NotificationManagerProperty = + AvaloniaProperty.Register(nameof(NotificationManager)); + + /// + /// Identifies the avalonia property. + /// + public static readonly StyledProperty TitleProperty = + AvaloniaProperty.Register(nameof(Title)); + + /// + /// Identifies the avalonia property. + /// + public static readonly StyledProperty MessageProperty = + AvaloniaProperty.Register(nameof(Message)); + + /// + /// Gets or sets the used to display the notification. This is an avalonia property. + /// + [ResolveByName] + public INotificationManager? NotificationManager + { + get => GetValue(NotificationManagerProperty); + set => SetValue(NotificationManagerProperty, value); + } + + /// + /// Gets or sets the notification title. This is an avalonia property. + /// + public string? Title + { + get => GetValue(TitleProperty); + set => SetValue(TitleProperty, value); + } + + /// + /// Gets or sets the notification message. This is an avalonia property. + /// + public string? Message + { + get => GetValue(MessageProperty); + set => SetValue(MessageProperty, value); + } + + /// + public override object? Execute(object? sender, object? parameter) + { + if (!IsEnabled) + { + return false; + } + + var manager = NotificationManager; + if (manager is null) + { + return false; + } + + var notification = new Notification(Title ?? string.Empty, Message ?? string.Empty, NotificationType.Success); + manager.Show(notification); + return true; + } +} diff --git a/src/Avalonia.Xaml.Interactions.Custom/Actions/ShowWarningNotificationAction.cs b/src/Avalonia.Xaml.Interactions.Custom/Actions/ShowWarningNotificationAction.cs new file mode 100644 index 000000000..18cb3f9dc --- /dev/null +++ b/src/Avalonia.Xaml.Interactions.Custom/Actions/ShowWarningNotificationAction.cs @@ -0,0 +1,75 @@ +using Avalonia.Controls.Notifications; +using Avalonia.Xaml.Interactivity; + +namespace Avalonia.Xaml.Interactions.Custom; + +/// +/// Shows a warning notification using an . +/// +public class ShowWarningNotificationAction : StyledElementAction +{ + /// + /// Identifies the avalonia property. + /// + public static readonly StyledProperty NotificationManagerProperty = + AvaloniaProperty.Register(nameof(NotificationManager)); + + /// + /// Identifies the avalonia property. + /// + public static readonly StyledProperty TitleProperty = + AvaloniaProperty.Register(nameof(Title)); + + /// + /// Identifies the avalonia property. + /// + public static readonly StyledProperty MessageProperty = + AvaloniaProperty.Register(nameof(Message)); + + /// + /// Gets or sets the used to display the notification. This is an avalonia property. + /// + [ResolveByName] + public INotificationManager? NotificationManager + { + get => GetValue(NotificationManagerProperty); + set => SetValue(NotificationManagerProperty, value); + } + + /// + /// Gets or sets the notification title. This is an avalonia property. + /// + public string? Title + { + get => GetValue(TitleProperty); + set => SetValue(TitleProperty, value); + } + + /// + /// Gets or sets the notification message. This is an avalonia property. + /// + public string? Message + { + get => GetValue(MessageProperty); + set => SetValue(MessageProperty, value); + } + + /// + public override object? Execute(object? sender, object? parameter) + { + if (!IsEnabled) + { + return false; + } + + var manager = NotificationManager; + if (manager is null) + { + return false; + } + + var notification = new Notification(Title ?? string.Empty, Message ?? string.Empty, NotificationType.Warning); + manager.Show(notification); + return true; + } +} diff --git a/src/Avalonia.Xaml.Interactions.Custom/Notifications/WindowNotificationManagerBehavior.cs b/src/Avalonia.Xaml.Interactions.Custom/Notifications/WindowNotificationManagerBehavior.cs new file mode 100644 index 000000000..240b739ce --- /dev/null +++ b/src/Avalonia.Xaml.Interactions.Custom/Notifications/WindowNotificationManagerBehavior.cs @@ -0,0 +1,33 @@ +using System; +using Avalonia.Controls; +using Avalonia.Controls.Notifications; + +namespace Avalonia.Xaml.Interactions.Custom; + +/// +/// Provides a for the associated . +/// +public class WindowNotificationManagerBehavior : AttachedToVisualTreeBehavior +{ + /// + /// Identifies the avalonia property. + /// + public static readonly StyledProperty NotificationManagerProperty = + AvaloniaProperty.Register(nameof(NotificationManager)); + + /// + /// Gets the instance. This is an avalonia property. + /// + public INotificationManager? NotificationManager + { + get => GetValue(NotificationManagerProperty); + private set => SetValue(NotificationManagerProperty, value); + } + + /// + protected override IDisposable OnAttachedToVisualTreeOverride() + { + NotificationManager = new WindowNotificationManager(AssociatedObject); + return DisposableAction.Create(() => NotificationManager = null); + } +} From 473571647be68598998575f0aebfbd7e4b5ebf77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wies=C5=82aw=20S=CC=8Colte=CC=81s?= Date: Thu, 22 May 2025 14:16:37 +0200 Subject: [PATCH 2/3] Fix --- README.md | 2 +- .../Views/Pages/NotificationsView.axaml | 10 +++++----- .../Actions/ShowErrorNotificationAction.cs | 3 ++- .../ShowInformationNotificationAction.cs | 3 ++- .../Actions/ShowNotificationAction.cs | 3 ++- .../Actions/ShowSuccessNotificationAction.cs | 3 ++- .../Actions/ShowWarningNotificationAction.cs | 3 ++- ...vior.cs => NotificationManagerBehavior.cs} | 20 +++++++++++++++---- 8 files changed, 32 insertions(+), 15 deletions(-) rename src/Avalonia.Xaml.Interactions.Custom/Notifications/{WindowNotificationManagerBehavior.cs => NotificationManagerBehavior.cs} (59%) diff --git a/README.md b/README.md index d7e922596..9bf3b5299 100644 --- a/README.md +++ b/README.md @@ -156,7 +156,7 @@ This section provides an overview of all available classes and their purpose in *Places text onto the clipboard.* ### Notifications -- **WindowNotificationManagerBehavior** +- **NotificationManagerBehavior** *Creates a notification manager for a window when attached.* - **ShowNotificationAction** *Shows a notification using an attached notification manager.* diff --git a/samples/BehaviorsTestApplication/Views/Pages/NotificationsView.axaml b/samples/BehaviorsTestApplication/Views/Pages/NotificationsView.axaml index 7d280a40c..cf32af4e2 100644 --- a/samples/BehaviorsTestApplication/Views/Pages/NotificationsView.axaml +++ b/samples/BehaviorsTestApplication/Views/Pages/NotificationsView.axaml @@ -11,12 +11,12 @@ - +