Browse Source

Add Close and ClearAll api for INotificationManager and IManagedNotificationManager (#15628)

* Add Close and ClearAll api for INotificationManager

* Remove notification card item on close

* Make _notificationCards readonly

* Clear _notificationCards when detached from visual tree

* Update Avalonia.nupkg.xml

* Use cancellationToken = default instead of null

* Rename ClearAll to CloseAll for consistency with Close method naming

* Remove cancellationToken param from Show method for better compatibility

* Update Avalonia.nupkg.xml

---------

Co-authored-by: Jumar Macato <16554748+jmacato@users.noreply.github.com>
pull/15650/head
Wiesław Šoltés 2 years ago
committed by GitHub
parent
commit
f7feaba5a0
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 18
      api/Avalonia.nupkg.xml
  2. 8
      src/Avalonia.Controls/Notifications/IManagedNotificationManager.cs
  3. 11
      src/Avalonia.Controls/Notifications/INotificationManager.cs
  4. 55
      src/Avalonia.Controls/Notifications/WindowNotificationManager.cs

18
api/Avalonia.nupkg.xml

@ -1105,4 +1105,22 @@
<Left>baseline/netstandard2.0/Avalonia.Base.dll</Left> <Left>baseline/netstandard2.0/Avalonia.Base.dll</Left>
<Right>target/netstandard2.0/Avalonia.Base.dll</Right> <Right>target/netstandard2.0/Avalonia.Base.dll</Right>
</Suppression> </Suppression>
<Suppression>
<DiagnosticId>CP0006</DiagnosticId>
<Target>M:Avalonia.Controls.Notifications.IManagedNotificationManager.Close(System.Object)</Target>
<Left>baseline/netstandard2.0/Avalonia.Controls.dll</Left>
<Right>target/netstandard2.0/Avalonia.Controls.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0006</DiagnosticId>
<Target>M:Avalonia.Controls.Notifications.INotificationManager.Close(Avalonia.Controls.Notifications.INotification)</Target>
<Left>baseline/netstandard2.0/Avalonia.Controls.dll</Left>
<Right>target/netstandard2.0/Avalonia.Controls.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0006</DiagnosticId>
<Target>M:Avalonia.Controls.Notifications.INotificationManager.CloseAll</Target>
<Left>baseline/netstandard2.0/Avalonia.Controls.dll</Left>
<Right>target/netstandard2.0/Avalonia.Controls.dll</Right>
</Suppression>
</Suppressions> </Suppressions>

8
src/Avalonia.Controls/Notifications/IManagedNotificationManager.cs

@ -1,4 +1,4 @@
using System; using System.Threading;
using Avalonia.Metadata; using Avalonia.Metadata;
namespace Avalonia.Controls.Notifications namespace Avalonia.Controls.Notifications
@ -20,5 +20,11 @@ namespace Avalonia.Controls.Notifications
/// </summary> /// </summary>
/// <param name="content">The content to be displayed.</param> /// <param name="content">The content to be displayed.</param>
void Show(object content); void Show(object content);
/// <summary>
/// Closes a notification.
/// </summary>
/// <param name="content">The content to be closed.</param>
void Close(object content);
} }
} }

11
src/Avalonia.Controls/Notifications/INotificationManager.cs

@ -14,5 +14,16 @@ namespace Avalonia.Controls.Notifications
/// </summary> /// </summary>
/// <param name="notification">The notification to be displayed.</param> /// <param name="notification">The notification to be displayed.</param>
void Show(INotification notification); void Show(INotification notification);
/// <summary>
/// Closes a notification.
/// </summary>
/// <param name="notification">The notification to be closed.</param>
void Close(INotification notification);
/// <summary>
/// Closes all notifications.
/// </summary>
void CloseAll();
} }
} }

55
src/Avalonia.Controls/Notifications/WindowNotificationManager.cs

@ -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>

Loading…
Cancel
Save