@ -3,7 +3,6 @@ using System.Collections;
using System.Collections.Generic ;
using System.Collections.Generic ;
using System.Linq ;
using System.Linq ;
using System.Threading.Tasks ;
using System.Threading.Tasks ;
using Avalonia.Collections ;
using Avalonia.Controls.Metadata ;
using Avalonia.Controls.Metadata ;
using Avalonia.Controls.Primitives ;
using Avalonia.Controls.Primitives ;
using Avalonia.Threading ;
using Avalonia.Threading ;
@ -19,6 +18,8 @@ namespace Avalonia.Controls.Notifications
public class WindowNotificationManager : TemplatedControl , IManagedNotificationManager
public class WindowNotificationManager : TemplatedControl , IManagedNotificationManager
{
{
private IList ? _ items ;
private IList ? _ items ;
private readonly Dictionary < object , NotificationCard > _ notificationCards = new ( ) ;
/// <summary>
/// <summary>
/// Defines the <see cref="Position"/> property.
/// Defines the <see cref="Position"/> property.
/// </summary>
/// </summary>
@ -103,7 +104,7 @@ namespace Avalonia.Controls.Notifications
Show ( content , NotificationType . Information ) ;
Show ( content , NotificationType . Information ) ;
}
}
}
}
/// <summary>
/// <summary>
/// Shows a Notification
/// Shows a Notification
/// </summary>
/// </summary>
@ -121,7 +122,7 @@ namespace Avalonia.Controls.Notifications
string [ ] ? classes = null )
string [ ] ? classes = null )
{
{
Dispatcher . UIThread . VerifyAccess ( ) ;
Dispatcher . UIThread . VerifyAccess ( ) ;
var notificationControl = new NotificationCard
var notificationControl = new NotificationCard
{
{
Content = content ,
Content = content ,
@ -136,12 +137,13 @@ namespace Avalonia.Controls.Notifications
notificationControl . Classes . Add ( @class ) ;
notificationControl . Classes . Add ( @class ) ;
}
}
}
}
notificationControl . NotificationClosed + = ( sender , args ) = >
notificationControl . NotificationClosed + = ( sender , args ) = >
{
{
onClose ? . Invoke ( ) ;
onClose ? . Invoke ( ) ;
_ items ? . Remove ( sender ) ;
_ items ? . Remove ( sender ) ;
_ notificationCards . Remove ( content ) ;
} ;
} ;
notificationControl . PointerPressed + = ( sender , args ) = >
notificationControl . PointerPressed + = ( sender , args ) = >
@ -154,6 +156,7 @@ namespace Avalonia.Controls.Notifications
Dispatcher . UIThread . Post ( ( ) = >
Dispatcher . UIThread . Post ( ( ) = >
{
{
_ items ? . Add ( notificationControl ) ;
_ items ? . Add ( notificationControl ) ;
_ notificationCards . Add ( content , notificationControl ) ;
if ( _ items ? . OfType < NotificationCard > ( ) . Count ( i = > ! i . IsClosing ) > MaxItems )
if ( _ items ? . OfType < NotificationCard > ( ) . Count ( i = > ! i . IsClosing ) > MaxItems )
{
{
@ -169,6 +172,43 @@ namespace Avalonia.Controls.Notifications
await Task . Delay ( expiration ? ? TimeSpan . FromSeconds ( 5 ) ) ;
await Task . Delay ( expiration ? ? TimeSpan . FromSeconds ( 5 ) ) ;
notificationControl . Close ( ) ;
notificationControl . Close ( ) ;
_ notificationCards . Remove ( content ) ;
}
/// <inheritdoc/>
public void Close ( INotification notification )
{
Dispatcher . UIThread . VerifyAccess ( ) ;
if ( _ notificationCards . Remove ( notification , out var notificationCard ) )
{
notificationCard . Close ( ) ;
}
}
/// <inheritdoc/>
public void Close ( object content )
{
Dispatcher . UIThread . VerifyAccess ( ) ;
if ( _ notificationCards . Remove ( content , out var notificationCard ) )
{
notificationCard . Close ( ) ;
}
}
/// <inheritdoc/>
public void CloseAll ( )
{
Dispatcher . UIThread . VerifyAccess ( ) ;
foreach ( var kvp in _ notificationCards )
{
kvp . Value . Close ( ) ;
}
_ notificationCards . Clear ( ) ;
}
}
protected override void OnPropertyChanged ( AvaloniaPropertyChangedEventArgs change )
protected override void OnPropertyChanged ( AvaloniaPropertyChangedEventArgs change )
@ -181,6 +221,13 @@ namespace Avalonia.Controls.Notifications
}
}
}
}
protected override void OnDetachedFromVisualTree ( VisualTreeAttachmentEventArgs e )
{
base . OnDetachedFromVisualTree ( e ) ;
_ notificationCards . Clear ( ) ;
}
/// <summary>
/// <summary>
/// Installs the <see cref="WindowNotificationManager"/> within the <see cref="AdornerLayer"/>
/// Installs the <see cref="WindowNotificationManager"/> within the <see cref="AdornerLayer"/>
/// </summary>
/// </summary>