feat: separate some Notification shared files.
This commit is contained in:
@@ -6,10 +6,10 @@
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<u:ToastCard ShowClose="False" />
|
||||
<u:ToastCard>
|
||||
Hello, Semi.Avalonia!
|
||||
Hello, Ursa!
|
||||
</u:ToastCard>
|
||||
<u:ToastCard NotificationType="Success" ShowClose="False">
|
||||
<u:Toast Content="Hello, Semi.Avalonia!" />
|
||||
<u:Toast Content="Hello, Ursa!" />
|
||||
</u:ToastCard>
|
||||
</StackPanel>
|
||||
</ThemeVariantScope>
|
||||
@@ -87,7 +87,7 @@
|
||||
Grid.Column="2"
|
||||
Theme="{StaticResource ToastCloseButton}"
|
||||
IsVisible="{TemplateBinding ShowClose}"
|
||||
u:ToastCard.CloseOnClick="True" />
|
||||
u:MessageCard.CloseOnClick="True" />
|
||||
</Grid>
|
||||
</Border>
|
||||
</Border>
|
||||
|
||||
35
src/Ursa/Controls/NotificationShared/IMessage.cs
Normal file
35
src/Ursa/Controls/NotificationShared/IMessage.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using Avalonia.Controls.Notifications;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a message that can be shown in a window or by the host operating system.
|
||||
/// </summary>
|
||||
public interface IMessage
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the <see cref="NotificationType"/> of the message.
|
||||
/// </summary>
|
||||
NotificationType Type { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the message should show a close button.
|
||||
/// </summary>
|
||||
bool ShowClose { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the expiration time of the message after which it will automatically close.
|
||||
/// If the value is <see cref="TimeSpan.Zero"/> then the message will remain open until the user closes it.
|
||||
/// </summary>
|
||||
TimeSpan Expiration { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets an Action to be run when the message is clicked.
|
||||
/// </summary>
|
||||
Action? OnClick { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets an Action to be run when the message is closed.
|
||||
/// </summary>
|
||||
Action? OnClose { get; }
|
||||
}
|
||||
207
src/Ursa/Controls/NotificationShared/MessageCard.cs
Normal file
207
src/Ursa/Controls/NotificationShared/MessageCard.cs
Normal file
@@ -0,0 +1,207 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Metadata;
|
||||
using Avalonia.Controls.Notifications;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.LogicalTree;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Control that represents and displays a message.
|
||||
/// </summary>
|
||||
[PseudoClasses(PC_Information, PC_Success, PC_Warning, PC_Error)]
|
||||
public abstract class MessageCard : ContentControl
|
||||
{
|
||||
public const string PC_Information = ":information";
|
||||
public const string PC_Success = ":success";
|
||||
public const string PC_Warning = ":warning";
|
||||
public const string PC_Error = ":error";
|
||||
|
||||
private bool _isClosing;
|
||||
|
||||
static MessageCard()
|
||||
{
|
||||
CloseOnClickProperty.Changed.AddClassHandler<Button>(OnCloseOnClickPropertyChanged);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MessageCard"/> class.
|
||||
/// </summary>
|
||||
public MessageCard()
|
||||
{
|
||||
UpdateNotificationType();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the message is already closing.
|
||||
/// </summary>
|
||||
public bool IsClosing
|
||||
{
|
||||
get => _isClosing;
|
||||
private set => SetAndRaise(IsClosingProperty, ref _isClosing, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="IsClosing"/> property.
|
||||
/// </summary>
|
||||
public static readonly DirectProperty<MessageCard, bool> IsClosingProperty =
|
||||
AvaloniaProperty.RegisterDirect<MessageCard, bool>(nameof(IsClosing), o => o.IsClosing);
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the message is closed.
|
||||
/// </summary>
|
||||
public bool IsClosed
|
||||
{
|
||||
get => GetValue(IsClosedProperty);
|
||||
set => SetValue(IsClosedProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="IsClosed"/> property.
|
||||
/// </summary>
|
||||
public static readonly StyledProperty<bool> IsClosedProperty =
|
||||
AvaloniaProperty.Register<MessageCard, bool>(nameof(IsClosed));
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the type of the message
|
||||
/// </summary>
|
||||
public NotificationType NotificationType
|
||||
{
|
||||
get => GetValue(NotificationTypeProperty);
|
||||
set => SetValue(NotificationTypeProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="NotificationType" /> property
|
||||
/// </summary>
|
||||
public static readonly StyledProperty<NotificationType> NotificationTypeProperty =
|
||||
AvaloniaProperty.Register<MessageCard, NotificationType>(nameof(NotificationType));
|
||||
|
||||
public bool ShowClose
|
||||
{
|
||||
get => GetValue(ShowCloseProperty);
|
||||
set => SetValue(ShowCloseProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<bool> ShowCloseProperty =
|
||||
AvaloniaProperty.Register<MessageCard, bool>(nameof(ShowClose), true);
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="MessageClosed"/> event.
|
||||
/// </summary>
|
||||
public static readonly RoutedEvent<RoutedEventArgs> MessageClosedEvent =
|
||||
RoutedEvent.Register<MessageCard, RoutedEventArgs>(nameof(MessageClosed), RoutingStrategies.Bubble);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Raised when the <see cref="MessageCard"/> has closed.
|
||||
/// </summary>
|
||||
public event EventHandler<RoutedEventArgs>? MessageClosed
|
||||
{
|
||||
add => AddHandler(MessageClosedEvent, value);
|
||||
remove => RemoveHandler(MessageClosedEvent, value);
|
||||
}
|
||||
|
||||
public static bool GetCloseOnClick(Button obj)
|
||||
{
|
||||
_ = obj ?? throw new ArgumentNullException(nameof(obj));
|
||||
return obj.GetValue(CloseOnClickProperty);
|
||||
}
|
||||
|
||||
public static void SetCloseOnClick(Button obj, bool value)
|
||||
{
|
||||
_ = obj ?? throw new ArgumentNullException(nameof(obj));
|
||||
obj.SetValue(CloseOnClickProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines the CloseOnClick property.
|
||||
/// </summary>
|
||||
public static readonly AttachedProperty<bool> CloseOnClickProperty =
|
||||
AvaloniaProperty.RegisterAttached<MessageCard, Button, bool>("CloseOnClick", defaultValue: false);
|
||||
|
||||
private static void OnCloseOnClickPropertyChanged(AvaloniaObject d, AvaloniaPropertyChangedEventArgs e)
|
||||
{
|
||||
var button = (Button)d;
|
||||
var value = (bool)e.NewValue!;
|
||||
if (value)
|
||||
{
|
||||
button.Click += Button_Click;
|
||||
}
|
||||
else
|
||||
{
|
||||
button.Click -= Button_Click;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when a button inside the Message is clicked.
|
||||
/// </summary>
|
||||
private static void Button_Click(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
var btn = sender as ILogical;
|
||||
var message = btn?.GetLogicalAncestors().OfType<MessageCard>().FirstOrDefault();
|
||||
message?.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Closes the <see cref="MessageCard"/>.
|
||||
/// </summary>
|
||||
public void Close()
|
||||
{
|
||||
if (IsClosing)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
IsClosing = true;
|
||||
}
|
||||
|
||||
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs e)
|
||||
{
|
||||
base.OnPropertyChanged(e);
|
||||
|
||||
if (e.Property == ContentProperty && e.NewValue is IMessage message)
|
||||
{
|
||||
SetValue(NotificationTypeProperty, message.Type);
|
||||
}
|
||||
|
||||
if (e.Property == NotificationTypeProperty)
|
||||
{
|
||||
UpdateNotificationType();
|
||||
}
|
||||
|
||||
if (e.Property == IsClosedProperty)
|
||||
{
|
||||
if (!IsClosing && !IsClosed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
RaiseEvent(new RoutedEventArgs(MessageClosedEvent));
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateNotificationType()
|
||||
{
|
||||
switch (NotificationType)
|
||||
{
|
||||
case NotificationType.Error:
|
||||
PseudoClasses.Add(PC_Error);
|
||||
break;
|
||||
|
||||
case NotificationType.Information:
|
||||
PseudoClasses.Add(PC_Information);
|
||||
break;
|
||||
|
||||
case NotificationType.Success:
|
||||
PseudoClasses.Add(PC_Success);
|
||||
break;
|
||||
|
||||
case NotificationType.Warning:
|
||||
PseudoClasses.Add(PC_Warning);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
80
src/Ursa/Controls/NotificationShared/WindowMessageManager.cs
Normal file
80
src/Ursa/Controls/NotificationShared/WindowMessageManager.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using System.Collections;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Metadata;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Layout;
|
||||
using Avalonia.VisualTree;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="WindowMessageManager"/> that displays messages in a <see cref="Window"/>.
|
||||
/// </summary>
|
||||
[TemplatePart(PART_Items, typeof(Panel))]
|
||||
public abstract class WindowMessageManager : TemplatedControl
|
||||
{
|
||||
public const string PART_Items = "PART_Items";
|
||||
|
||||
protected IList? _items;
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="MaxItems"/> property.
|
||||
/// </summary>
|
||||
public static readonly StyledProperty<int> MaxItemsProperty =
|
||||
AvaloniaProperty.Register<WindowMessageManager, int>(nameof(MaxItems), 5);
|
||||
|
||||
/// <summary>
|
||||
/// Defines the maximum number of messages visible at once.
|
||||
/// </summary>
|
||||
public int MaxItems
|
||||
{
|
||||
get => GetValue(MaxItemsProperty);
|
||||
set => SetValue(MaxItemsProperty, value);
|
||||
}
|
||||
|
||||
static WindowMessageManager()
|
||||
{
|
||||
HorizontalAlignmentProperty.OverrideDefaultValue<WindowMessageManager>(HorizontalAlignment.Stretch);
|
||||
VerticalAlignmentProperty.OverrideDefaultValue<WindowMessageManager>(VerticalAlignment.Stretch);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
|
||||
var itemsControl = e.NameScope.Find<Panel>(PART_Items);
|
||||
_items = itemsControl?.Children;
|
||||
}
|
||||
|
||||
public abstract void Show(object content);
|
||||
|
||||
/// <summary>
|
||||
/// Installs the <see cref="WindowMessageManager"/> within the <see cref="AdornerLayer"/>
|
||||
/// </summary>
|
||||
protected void InstallFromTopLevel(TopLevel topLevel)
|
||||
{
|
||||
topLevel.TemplateApplied += TopLevelOnTemplateApplied;
|
||||
var adorner = topLevel.FindDescendantOfType<VisualLayerManager>()?.AdornerLayer;
|
||||
if (adorner is not null)
|
||||
{
|
||||
adorner.Children.Add(this);
|
||||
AdornerLayer.SetAdornedElement(this, adorner);
|
||||
}
|
||||
}
|
||||
|
||||
protected void TopLevelOnTemplateApplied(object? sender, TemplateAppliedEventArgs e)
|
||||
{
|
||||
if (Parent is AdornerLayer adornerLayer)
|
||||
{
|
||||
adornerLayer.Children.Remove(this);
|
||||
AdornerLayer.SetAdornedElement(this, null);
|
||||
}
|
||||
|
||||
// Reinstall message manager on template reapplied.
|
||||
var topLevel = (TopLevel)sender!;
|
||||
topLevel.TemplateApplied -= TopLevelOnTemplateApplied;
|
||||
InstallFromTopLevel(topLevel);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
using Avalonia.Metadata;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a toast manager that can show arbitrary content.
|
||||
/// Managed toast managers can show any content.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Because toast managers of this type are implemented purely in managed code, they
|
||||
/// can display arbitrary content, as opposed to toast managers which display toasts
|
||||
/// using the host operating system's toast mechanism.
|
||||
/// </remarks>
|
||||
[NotClientImplementable]
|
||||
public interface IManagedToastManager : IToastManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Shows a toast.
|
||||
/// </summary>
|
||||
/// <param name="content">The content to be displayed.</param>
|
||||
void Show(object content);
|
||||
}
|
||||
@@ -1,43 +1,12 @@
|
||||
using Avalonia.Controls.Notifications;
|
||||
using Avalonia.Metadata;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a toast that can be shown in a window or by the host operating system.
|
||||
/// </summary>
|
||||
[NotClientImplementable]
|
||||
public interface IToast
|
||||
public interface IToast : IMessage
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Gets the toast message.
|
||||
/// </summary>
|
||||
string? Content { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="NotificationType"/> of the toast.
|
||||
/// </summary>
|
||||
NotificationType Type { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the toast should show a close button.
|
||||
/// </summary>
|
||||
bool ShowClose { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the expiration time of the toast after which it will automatically close.
|
||||
/// If the value is <see cref="TimeSpan.Zero"/> then the toast will remain open until the user closes it.
|
||||
/// </summary>
|
||||
TimeSpan Expiration { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets an Action to be run when the toast is clicked.
|
||||
/// </summary>
|
||||
Action? OnClick { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets an Action to be run when the toast is closed.
|
||||
/// </summary>
|
||||
Action? OnClose { get; }
|
||||
}
|
||||
@@ -1,206 +1,6 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Metadata;
|
||||
using Avalonia.Controls.Notifications;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.LogicalTree;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Control that represents and displays a toast.
|
||||
/// </summary>
|
||||
[PseudoClasses(PC_Information, PC_Success, PC_Warning, PC_Error)]
|
||||
public class ToastCard : ContentControl
|
||||
{
|
||||
public const string PC_Information = ":information";
|
||||
public const string PC_Success = ":success";
|
||||
public const string PC_Warning = ":warning";
|
||||
public const string PC_Error = ":error";
|
||||
|
||||
private bool _isClosing;
|
||||
|
||||
static ToastCard()
|
||||
{
|
||||
CloseOnClickProperty.Changed.AddClassHandler<Button>(OnCloseOnClickPropertyChanged);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ToastCard"/> class.
|
||||
/// </summary>
|
||||
public ToastCard()
|
||||
{
|
||||
UpdateNotificationType();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the toast is already closing.
|
||||
/// </summary>
|
||||
public bool IsClosing
|
||||
{
|
||||
get => _isClosing;
|
||||
private set => SetAndRaise(IsClosingProperty, ref _isClosing, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="IsClosing"/> property.
|
||||
/// </summary>
|
||||
public static readonly DirectProperty<ToastCard, bool> IsClosingProperty =
|
||||
AvaloniaProperty.RegisterDirect<ToastCard, bool>(nameof(IsClosing), o => o.IsClosing);
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the toast is closed.
|
||||
/// </summary>
|
||||
public bool IsClosed
|
||||
{
|
||||
get => GetValue(IsClosedProperty);
|
||||
set => SetValue(IsClosedProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="IsClosed"/> property.
|
||||
/// </summary>
|
||||
public static readonly StyledProperty<bool> IsClosedProperty =
|
||||
AvaloniaProperty.Register<ToastCard, bool>(nameof(IsClosed));
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the type of the toast
|
||||
/// </summary>
|
||||
public NotificationType NotificationType
|
||||
{
|
||||
get => GetValue(NotificationTypeProperty);
|
||||
set => SetValue(NotificationTypeProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="NotificationType" /> property
|
||||
/// </summary>
|
||||
public static readonly StyledProperty<NotificationType> NotificationTypeProperty =
|
||||
AvaloniaProperty.Register<ToastCard, NotificationType>(nameof(NotificationType));
|
||||
public bool ShowClose
|
||||
{
|
||||
get => GetValue(ShowCloseProperty);
|
||||
set => SetValue(ShowCloseProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<bool> ShowCloseProperty =
|
||||
AvaloniaProperty.Register<ToastCard, bool>(nameof(ShowClose), true);
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="ToastClosed"/> event.
|
||||
/// </summary>
|
||||
public static readonly RoutedEvent<RoutedEventArgs> ToastClosedEvent =
|
||||
RoutedEvent.Register<ToastCard, RoutedEventArgs>(nameof(ToastClosed), RoutingStrategies.Bubble);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Raised when the <see cref="ToastCard"/> has closed.
|
||||
/// </summary>
|
||||
public event EventHandler<RoutedEventArgs>? ToastClosed
|
||||
{
|
||||
add => AddHandler(ToastClosedEvent, value);
|
||||
remove => RemoveHandler(ToastClosedEvent, value);
|
||||
}
|
||||
|
||||
public static bool GetCloseOnClick(Button obj)
|
||||
{
|
||||
_ = obj ?? throw new ArgumentNullException(nameof(obj));
|
||||
return obj.GetValue(CloseOnClickProperty);
|
||||
}
|
||||
|
||||
public static void SetCloseOnClick(Button obj, bool value)
|
||||
{
|
||||
_ = obj ?? throw new ArgumentNullException(nameof(obj));
|
||||
obj.SetValue(CloseOnClickProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines the CloseOnClick property.
|
||||
/// </summary>
|
||||
public static readonly AttachedProperty<bool> CloseOnClickProperty =
|
||||
AvaloniaProperty.RegisterAttached<ToastCard, Button, bool>("CloseOnClick", defaultValue: false);
|
||||
|
||||
private static void OnCloseOnClickPropertyChanged(AvaloniaObject d, AvaloniaPropertyChangedEventArgs e)
|
||||
{
|
||||
var button = (Button)d;
|
||||
var value = (bool)e.NewValue!;
|
||||
if (value)
|
||||
{
|
||||
button.Click += Button_Click;
|
||||
}
|
||||
else
|
||||
{
|
||||
button.Click -= Button_Click;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when a button inside the Toast is clicked.
|
||||
/// </summary>
|
||||
private static void Button_Click(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
var btn = sender as ILogical;
|
||||
var toast = btn?.GetLogicalAncestors().OfType<ToastCard>().FirstOrDefault();
|
||||
toast?.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Closes the <see cref="ToastCard"/>.
|
||||
/// </summary>
|
||||
public void Close()
|
||||
{
|
||||
if (IsClosing)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
IsClosing = true;
|
||||
}
|
||||
|
||||
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs e)
|
||||
{
|
||||
base.OnPropertyChanged(e);
|
||||
|
||||
if (e.Property == ContentProperty && e.NewValue is IToast toast)
|
||||
{
|
||||
SetValue(NotificationTypeProperty, toast.Type);
|
||||
}
|
||||
|
||||
if (e.Property == NotificationTypeProperty)
|
||||
{
|
||||
UpdateNotificationType();
|
||||
}
|
||||
|
||||
if (e.Property == IsClosedProperty)
|
||||
{
|
||||
if (!IsClosing && !IsClosed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
RaiseEvent(new RoutedEventArgs(ToastClosedEvent));
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateNotificationType()
|
||||
{
|
||||
switch (NotificationType)
|
||||
{
|
||||
case NotificationType.Error:
|
||||
PseudoClasses.Add(":error");
|
||||
break;
|
||||
|
||||
case NotificationType.Information:
|
||||
PseudoClasses.Add(":information");
|
||||
break;
|
||||
|
||||
case NotificationType.Success:
|
||||
PseudoClasses.Add(":success");
|
||||
break;
|
||||
|
||||
case NotificationType.Warning:
|
||||
PseudoClasses.Add(":warning");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
public class ToastCard : MessageCard;
|
||||
@@ -1,38 +1,17 @@
|
||||
using System.Collections;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Metadata;
|
||||
using Avalonia.Controls.Notifications;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Layout;
|
||||
using Avalonia.Threading;
|
||||
using Avalonia.VisualTree;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IToastManager"/> that displays toasts in a <see cref="Window"/>.
|
||||
/// </summary>
|
||||
[TemplatePart("PART_Items", typeof(Panel))]
|
||||
public class WindowToastManager : TemplatedControl, IManagedToastManager
|
||||
public class WindowToastManager : WindowMessageManager, IToastManager
|
||||
{
|
||||
private IList? _items;
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="MaxItems"/> property.
|
||||
/// </summary>
|
||||
public static readonly StyledProperty<int> MaxItemsProperty =
|
||||
AvaloniaProperty.Register<WindowToastManager, int>(nameof(MaxItems), 5);
|
||||
|
||||
/// <summary>
|
||||
/// Defines the maximum number of toasts visible at once.
|
||||
/// </summary>
|
||||
public int MaxItems
|
||||
{
|
||||
get => GetValue(MaxItemsProperty);
|
||||
set => SetValue(MaxItemsProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="WindowToastManager"/> class.
|
||||
/// </summary>
|
||||
@@ -52,21 +31,6 @@ public class WindowToastManager : TemplatedControl, IManagedToastManager
|
||||
{
|
||||
}
|
||||
|
||||
static WindowToastManager()
|
||||
{
|
||||
HorizontalAlignmentProperty.OverrideDefaultValue<WindowToastManager>(HorizontalAlignment.Stretch);
|
||||
VerticalAlignmentProperty.OverrideDefaultValue<WindowToastManager>(VerticalAlignment.Stretch);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
|
||||
var itemsControl = e.NameScope.Find<Panel>("PART_Items");
|
||||
_items = itemsControl?.Children;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Show(IToast content)
|
||||
{
|
||||
@@ -74,7 +38,7 @@ public class WindowToastManager : TemplatedControl, IManagedToastManager
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Show(object content)
|
||||
public override void Show(object content)
|
||||
{
|
||||
if (content is IToast toast)
|
||||
{
|
||||
@@ -96,7 +60,8 @@ public class WindowToastManager : TemplatedControl, IManagedToastManager
|
||||
/// <param name="onClick">an Action to be run when the toast is clicked</param>
|
||||
/// <param name="onClose">an Action to be run when the toast is closed</param>
|
||||
/// <param name="classes">style classes to apply</param>
|
||||
public async void Show(object content,
|
||||
public async void Show(
|
||||
object content,
|
||||
NotificationType type,
|
||||
TimeSpan? expiration = null,
|
||||
bool showClose = true,
|
||||
@@ -122,7 +87,7 @@ public class WindowToastManager : TemplatedControl, IManagedToastManager
|
||||
}
|
||||
}
|
||||
|
||||
toastControl.ToastClosed += (sender, _) =>
|
||||
toastControl.MessageClosed += (sender, _) =>
|
||||
{
|
||||
onClose?.Invoke();
|
||||
|
||||
@@ -150,32 +115,4 @@ public class WindowToastManager : TemplatedControl, IManagedToastManager
|
||||
|
||||
toastControl.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Installs the <see cref="WindowToastManager"/> within the <see cref="AdornerLayer"/>
|
||||
/// </summary>
|
||||
private void InstallFromTopLevel(TopLevel topLevel)
|
||||
{
|
||||
topLevel.TemplateApplied += TopLevelOnTemplateApplied;
|
||||
var adorner = topLevel.FindDescendantOfType<VisualLayerManager>()?.AdornerLayer;
|
||||
if (adorner is not null)
|
||||
{
|
||||
adorner.Children.Add(this);
|
||||
AdornerLayer.SetAdornedElement(this, adorner);
|
||||
}
|
||||
}
|
||||
|
||||
private void TopLevelOnTemplateApplied(object? sender, TemplateAppliedEventArgs e)
|
||||
{
|
||||
if (Parent is AdornerLayer adornerLayer)
|
||||
{
|
||||
adornerLayer.Children.Remove(this);
|
||||
AdornerLayer.SetAdornedElement(this, null);
|
||||
}
|
||||
|
||||
// Reinstall toast manager on template reapplied.
|
||||
var topLevel = (TopLevel)sender!;
|
||||
topLevel.TemplateApplied -= TopLevelOnTemplateApplied;
|
||||
InstallFromTopLevel(topLevel);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user