Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,20 @@ This section provides an overview of all available classes and their purpose in
- **SetClipboardTextAction**
*Places text onto the clipboard.*

### Notifications
- **NotificationManagerBehavior**
*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.*
Expand Down
3 changes: 3 additions & 0 deletions samples/BehaviorsTestApplication/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@
<TabItem Header="Reactive Navigation">
<pages:ReactiveNavigationView />
</TabItem>
<TabItem Header="Notifications">
<pages:NotificationsView />
</TabItem>
</SingleSelectionTabControl>
</DockPanel>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<UserControl x:Class="BehaviorsTestApplication.Views.Pages.NotificationsView"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:BehaviorsTestApplication.ViewModels"
x:DataType="vm:MainWindowViewModel"
mc:Ignorable="d" d:DesignWidth="600" d:DesignHeight="400">
<Design.DataContext>
<vm:MainWindowViewModel />
</Design.DataContext>
<StackPanel Margin="5" Spacing="5">
<Interaction.Behaviors>
<NotificationManagerBehavior x:Name="Manager" />
</Interaction.Behaviors>
<Button Content="Info Notification">
<Interaction.Behaviors>
<EventTriggerBehavior EventName="Click">
<ShowInformationNotificationAction NotificationManager="{Binding #Manager.NotificationManager}"
Title="Info"
Message="Information message" />
</EventTriggerBehavior>
</Interaction.Behaviors>
</Button>
<Button Content="Success Notification">
<Interaction.Behaviors>
<EventTriggerBehavior EventName="Click">
<ShowSuccessNotificationAction NotificationManager="{Binding #Manager.NotificationManager}"
Title="Success"
Message="Operation succeeded" />
</EventTriggerBehavior>
</Interaction.Behaviors>
</Button>
<Button Content="Warning Notification">
<Interaction.Behaviors>
<EventTriggerBehavior EventName="Click">
<ShowWarningNotificationAction NotificationManager="{Binding #Manager.NotificationManager}"
Title="Warning"
Message="Something to watch" />
</EventTriggerBehavior>
</Interaction.Behaviors>
</Button>
<Button Content="Error Notification">
<Interaction.Behaviors>
<EventTriggerBehavior EventName="Click">
<ShowErrorNotificationAction NotificationManager="{Binding #Manager.NotificationManager}"
Title="Error"
Message="Operation failed" />
</EventTriggerBehavior>
</Interaction.Behaviors>
</Button>
</StackPanel>
</UserControl>
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using Avalonia.Controls;
using Avalonia.Controls.Notifications;
using Avalonia.VisualTree;

namespace Avalonia.Xaml.Interactions.Custom;

/// <summary>
/// Provides a <see cref="INotificationManager"/> for the associated <see cref="Control"/>.
/// </summary>
public class NotificationManagerBehavior : AttachedToVisualTreeBehavior<Control>
{
/// <summary>
/// Identifies the <seealso cref="NotificationManager"/> avalonia property.
/// </summary>
public static readonly StyledProperty<INotificationManager?> NotificationManagerProperty =
AvaloniaProperty.Register<NotificationManagerBehavior, INotificationManager?>(nameof(NotificationManager));

/// <summary>
/// Gets the <see cref="INotificationManager"/> instance. This is an avalonia property.
/// </summary>
public INotificationManager? NotificationManager
{
get => GetValue(NotificationManagerProperty);
private set => SetValue(NotificationManagerProperty, value);
}

/// <inheritdoc />
protected override IDisposable OnAttachedToVisualTreeOverride()
{
if (AssociatedObject is TopLevel topLevel)
{
NotificationManager = new WindowNotificationManager(topLevel);
}
else
{
if (AssociatedObject?.GetVisualRoot() is TopLevel visualRootTopLevel)
{
NotificationManager = new WindowNotificationManager(visualRootTopLevel);
}
}

return DisposableAction.Create(() => NotificationManager = null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Avalonia.Controls;
using Avalonia.Controls.Notifications;
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Custom;

/// <summary>
/// Shows an error notification using an <see cref="INotificationManager"/>.
/// </summary>
public class ShowErrorNotificationAction : StyledElementAction
{
/// <summary>
/// Identifies the <seealso cref="NotificationManager"/> avalonia property.
/// </summary>
public static readonly StyledProperty<INotificationManager?> NotificationManagerProperty =
AvaloniaProperty.Register<ShowErrorNotificationAction, INotificationManager?>(nameof(NotificationManager));

/// <summary>
/// Identifies the <seealso cref="Title"/> avalonia property.
/// </summary>
public static readonly StyledProperty<string?> TitleProperty =
AvaloniaProperty.Register<ShowErrorNotificationAction, string?>(nameof(Title));

/// <summary>
/// Identifies the <seealso cref="Message"/> avalonia property.
/// </summary>
public static readonly StyledProperty<string?> MessageProperty =
AvaloniaProperty.Register<ShowErrorNotificationAction, string?>(nameof(Message));

/// <summary>
/// Gets or sets the <see cref="INotificationManager"/> used to display the notification. This is an avalonia property.
/// </summary>
[ResolveByName]
public INotificationManager? NotificationManager
{
get => GetValue(NotificationManagerProperty);
set => SetValue(NotificationManagerProperty, value);
}

/// <summary>
/// Gets or sets the notification title. This is an avalonia property.
/// </summary>
public string? Title
{
get => GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}

/// <summary>
/// Gets or sets the notification message. This is an avalonia property.
/// </summary>
public string? Message
{
get => GetValue(MessageProperty);
set => SetValue(MessageProperty, value);
}

/// <inheritdoc />
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Avalonia.Controls;
using Avalonia.Controls.Notifications;
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Custom;

/// <summary>
/// Shows an information notification using an <see cref="INotificationManager"/>.
/// </summary>
public class ShowInformationNotificationAction : StyledElementAction
{
/// <summary>
/// Identifies the <seealso cref="NotificationManager"/> avalonia property.
/// </summary>
public static readonly StyledProperty<INotificationManager?> NotificationManagerProperty =
AvaloniaProperty.Register<ShowInformationNotificationAction, INotificationManager?>(nameof(NotificationManager));

/// <summary>
/// Identifies the <seealso cref="Title"/> avalonia property.
/// </summary>
public static readonly StyledProperty<string?> TitleProperty =
AvaloniaProperty.Register<ShowInformationNotificationAction, string?>(nameof(Title));

/// <summary>
/// Identifies the <seealso cref="Message"/> avalonia property.
/// </summary>
public static readonly StyledProperty<string?> MessageProperty =
AvaloniaProperty.Register<ShowInformationNotificationAction, string?>(nameof(Message));

/// <summary>
/// Gets or sets the <see cref="INotificationManager"/> used to display the notification. This is an avalonia property.
/// </summary>
[ResolveByName]
public INotificationManager? NotificationManager
{
get => GetValue(NotificationManagerProperty);
set => SetValue(NotificationManagerProperty, value);
}

/// <summary>
/// Gets or sets the notification title. This is an avalonia property.
/// </summary>
public string? Title
{
get => GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}

/// <summary>
/// Gets or sets the notification message. This is an avalonia property.
/// </summary>
public string? Message
{
get => GetValue(MessageProperty);
set => SetValue(MessageProperty, value);
}

/// <inheritdoc />
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Avalonia.Controls;
using Avalonia.Controls.Notifications;
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Custom;

/// <summary>
/// Shows a notification using an <see cref="INotificationManager"/>.
/// </summary>
public class ShowNotificationAction : StyledElementAction
{
/// <summary>
/// Identifies the <seealso cref="NotificationManager"/> avalonia property.
/// </summary>
public static readonly StyledProperty<INotificationManager?> NotificationManagerProperty =
AvaloniaProperty.Register<ShowNotificationAction, INotificationManager?>(nameof(NotificationManager));

/// <summary>
/// Identifies the <seealso cref="Notification"/> avalonia property.
/// </summary>
public static readonly StyledProperty<INotification?> NotificationProperty =
AvaloniaProperty.Register<ShowNotificationAction, INotification?>(nameof(Notification));

/// <summary>
/// Gets or sets the <see cref="INotificationManager"/> used to display the notification. This is an avalonia property.
/// </summary>
[ResolveByName]
public INotificationManager? NotificationManager
{
get => GetValue(NotificationManagerProperty);
set => SetValue(NotificationManagerProperty, value);
}

/// <summary>
/// Gets or sets the <see cref="INotification"/> instance to show. This is an avalonia property.
/// </summary>
public INotification? Notification
{
get => GetValue(NotificationProperty);
set => SetValue(NotificationProperty, value);
}

/// <inheritdoc />
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;
}
}
Loading
Loading