feat: add ShowIcon property.

This commit is contained in:
Zhang Dian
2024-09-11 19:32:13 +08:00
parent c1e58d5e71
commit 621f55674c
12 changed files with 72 additions and 43 deletions

View File

@@ -12,7 +12,10 @@
<vm:NotificationDemoViewModel /> <vm:NotificationDemoViewModel />
</Design.DataContext> </Design.DataContext>
<StackPanel Spacing="20"> <StackPanel Spacing="20">
<StackPanel Orientation="Horizontal">
<ToggleSwitch IsChecked="{Binding ShowIcon}" Content="ShowIcon" />
<ToggleSwitch IsChecked="{Binding ShowClose}" Content="ShowClose" /> <ToggleSwitch IsChecked="{Binding ShowClose}" Content="ShowClose" />
</StackPanel>
<UniformGrid Rows="2" Columns="3" Width="500" HorizontalAlignment="Left"> <UniformGrid Rows="2" Columns="3" Width="500" HorizontalAlignment="Left">
<UniformGrid.Styles> <UniformGrid.Styles>

View File

@@ -12,7 +12,10 @@
<vm:ToastDemoViewModel /> <vm:ToastDemoViewModel />
</Design.DataContext> </Design.DataContext>
<StackPanel Spacing="20"> <StackPanel Spacing="20">
<StackPanel Orientation="Horizontal">
<ToggleSwitch IsChecked="{Binding ShowIcon}" Content="ShowIcon" />
<ToggleSwitch IsChecked="{Binding ShowClose}" Content="ShowClose" /> <ToggleSwitch IsChecked="{Binding ShowClose}" Content="ShowClose" />
</StackPanel>
<StackPanel Orientation="Horizontal" Spacing="20"> <StackPanel Orientation="Horizontal" Spacing="20">
<StackPanel.Styles> <StackPanel.Styles>

View File

@@ -11,6 +11,7 @@ public partial class NotificationDemoViewModel : ObservableObject
{ {
public WindowNotificationManager? NotificationManager { get; set; } public WindowNotificationManager? NotificationManager { get; set; }
[ObservableProperty] private bool _showIcon = true;
[ObservableProperty] private bool _showClose = true; [ObservableProperty] private bool _showClose = true;
[RelayCommand] [RelayCommand]
@@ -26,27 +27,25 @@ public partial class NotificationDemoViewModel : ObservableObject
[RelayCommand] [RelayCommand]
public void ShowNormal(object obj) public void ShowNormal(object obj)
{ {
if (obj is string s) if (obj is not string s) return;
{
Enum.TryParse<NotificationType>(s, out var notificationType); Enum.TryParse<NotificationType>(s, out var notificationType);
NotificationManager?.Show( NotificationManager?.Show(
new Notification("Welcome", "This is message"), new Notification("Welcome", "This is message"),
showIcon: ShowIcon,
showClose: ShowClose, showClose: ShowClose,
type: notificationType); type: notificationType);
} }
}
[RelayCommand] [RelayCommand]
public void ShowLight(object obj) public void ShowLight(object obj)
{ {
if (obj is string s) if (obj is not string s) return;
{
Enum.TryParse<NotificationType>(s, out var notificationType); Enum.TryParse<NotificationType>(s, out var notificationType);
NotificationManager?.Show( NotificationManager?.Show(
new Notification("Welcome", "This is message"), new Notification("Welcome", "This is message"),
showIcon: ShowIcon,
showClose: ShowClose, showClose: ShowClose,
type: notificationType, type: notificationType,
classes: ["Light"]); classes: ["Light"]);
} }
} }
}

View File

@@ -10,8 +10,8 @@ public partial class ToastDemoViewModel : ObservableObject
{ {
public WindowToastManager? ToastManager { get; set; } public WindowToastManager? ToastManager { get; set; }
[ObservableProperty] [ObservableProperty] private bool _showIcon = true;
private bool _showClose = true; [ObservableProperty] private bool _showClose = true;
[RelayCommand] [RelayCommand]
public void ShowNormal(object obj) public void ShowNormal(object obj)
@@ -21,6 +21,7 @@ public partial class ToastDemoViewModel : ObservableObject
Enum.TryParse<NotificationType>(s, out var notificationType); Enum.TryParse<NotificationType>(s, out var notificationType);
ToastManager?.Show( ToastManager?.Show(
new Toast("This is message"), new Toast("This is message"),
showIcon: ShowIcon,
showClose: ShowClose, showClose: ShowClose,
type: notificationType); type: notificationType);
} }
@@ -40,6 +41,7 @@ public partial class ToastDemoViewModel : ObservableObject
Enum.TryParse<NotificationType>(s, out var notificationType); Enum.TryParse<NotificationType>(s, out var notificationType);
ToastManager?.Show( ToastManager?.Show(
new Toast("This is message"), new Toast("This is message"),
showIcon: ShowIcon,
showClose: ShowClose, showClose: ShowClose,
type: notificationType, type: notificationType,
classes: ["Light"]); classes: ["Light"]);

View File

@@ -92,7 +92,7 @@
Height="{DynamicResource NotificationCardIconHeight}" Height="{DynamicResource NotificationCardIconHeight}"
Margin="{DynamicResource NotificationCardIconMargin}" Margin="{DynamicResource NotificationCardIconMargin}"
VerticalAlignment="Top" VerticalAlignment="Top"
IsVisible="False" IsVisible="{TemplateBinding ShowIcon}"
Data="{DynamicResource NotificationCardInformationIconPathData}" /> Data="{DynamicResource NotificationCardInformationIconPathData}" />
<ContentControl <ContentControl
x:Name="PART_Content" x:Name="PART_Content"
@@ -366,22 +366,18 @@
</Style> </Style>
<Style Selector="^:information /template/ PathIcon#NotificationIcon"> <Style Selector="^:information /template/ PathIcon#NotificationIcon">
<Setter Property="IsVisible" Value="True" />
<Setter Property="Foreground" Value="{DynamicResource NotificationCardInformationIconForeground}" /> <Setter Property="Foreground" Value="{DynamicResource NotificationCardInformationIconForeground}" />
<Setter Property="Data" Value="{DynamicResource NotificationCardInformationIconPathData}" /> <Setter Property="Data" Value="{DynamicResource NotificationCardInformationIconPathData}" />
</Style> </Style>
<Style Selector="^:success /template/ PathIcon#NotificationIcon"> <Style Selector="^:success /template/ PathIcon#NotificationIcon">
<Setter Property="IsVisible" Value="True" />
<Setter Property="Foreground" Value="{DynamicResource NotificationCardSuccessIconForeground}" /> <Setter Property="Foreground" Value="{DynamicResource NotificationCardSuccessIconForeground}" />
<Setter Property="Data" Value="{DynamicResource NotificationCardSuccessIconPathData}" /> <Setter Property="Data" Value="{DynamicResource NotificationCardSuccessIconPathData}" />
</Style> </Style>
<Style Selector="^:warning /template/ PathIcon#NotificationIcon"> <Style Selector="^:warning /template/ PathIcon#NotificationIcon">
<Setter Property="IsVisible" Value="True" />
<Setter Property="Foreground" Value="{DynamicResource NotificationCardWarningIconForeground}" /> <Setter Property="Foreground" Value="{DynamicResource NotificationCardWarningIconForeground}" />
<Setter Property="Data" Value="{DynamicResource NotificationCardWarningIconPathData}" /> <Setter Property="Data" Value="{DynamicResource NotificationCardWarningIconPathData}" />
</Style> </Style>
<Style Selector="^:error /template/ PathIcon#NotificationIcon"> <Style Selector="^:error /template/ PathIcon#NotificationIcon">
<Setter Property="IsVisible" Value="True" />
<Setter Property="Foreground" Value="{DynamicResource NotificationCardErrorIconForeground}" /> <Setter Property="Foreground" Value="{DynamicResource NotificationCardErrorIconForeground}" />
<Setter Property="Data" Value="{DynamicResource NotificationCardErrorIconPathData}" /> <Setter Property="Data" Value="{DynamicResource NotificationCardErrorIconPathData}" />
</Style> </Style>

View File

@@ -57,7 +57,7 @@
Height="{DynamicResource NotificationCardIconHeight}" Height="{DynamicResource NotificationCardIconHeight}"
Margin="{DynamicResource ToastCardIconMargin}" Margin="{DynamicResource ToastCardIconMargin}"
VerticalAlignment="Top" VerticalAlignment="Top"
IsVisible="False" IsVisible="{TemplateBinding ShowIcon}"
Data="{DynamicResource NotificationCardInformationIconPathData}" /> Data="{DynamicResource NotificationCardInformationIconPathData}" />
<ContentControl <ContentControl
x:Name="PART_Content" x:Name="PART_Content"
@@ -154,22 +154,18 @@
</Style> </Style>
<Style Selector="^:information /template/ PathIcon#ToastIcon"> <Style Selector="^:information /template/ PathIcon#ToastIcon">
<Setter Property="IsVisible" Value="True" />
<Setter Property="Foreground" Value="{DynamicResource NotificationCardInformationIconForeground}" /> <Setter Property="Foreground" Value="{DynamicResource NotificationCardInformationIconForeground}" />
<Setter Property="Data" Value="{DynamicResource NotificationCardInformationIconPathData}" /> <Setter Property="Data" Value="{DynamicResource NotificationCardInformationIconPathData}" />
</Style> </Style>
<Style Selector="^:success /template/ PathIcon#ToastIcon"> <Style Selector="^:success /template/ PathIcon#ToastIcon">
<Setter Property="IsVisible" Value="True" />
<Setter Property="Foreground" Value="{DynamicResource NotificationCardSuccessIconForeground}" /> <Setter Property="Foreground" Value="{DynamicResource NotificationCardSuccessIconForeground}" />
<Setter Property="Data" Value="{DynamicResource NotificationCardSuccessIconPathData}" /> <Setter Property="Data" Value="{DynamicResource NotificationCardSuccessIconPathData}" />
</Style> </Style>
<Style Selector="^:warning /template/ PathIcon#ToastIcon"> <Style Selector="^:warning /template/ PathIcon#ToastIcon">
<Setter Property="IsVisible" Value="True" />
<Setter Property="Foreground" Value="{DynamicResource NotificationCardWarningIconForeground}" /> <Setter Property="Foreground" Value="{DynamicResource NotificationCardWarningIconForeground}" />
<Setter Property="Data" Value="{DynamicResource NotificationCardWarningIconPathData}" /> <Setter Property="Data" Value="{DynamicResource NotificationCardWarningIconPathData}" />
</Style> </Style>
<Style Selector="^:error /template/ PathIcon#ToastIcon"> <Style Selector="^:error /template/ PathIcon#ToastIcon">
<Setter Property="IsVisible" Value="True" />
<Setter Property="Foreground" Value="{DynamicResource NotificationCardErrorIconForeground}" /> <Setter Property="Foreground" Value="{DynamicResource NotificationCardErrorIconForeground}" />
<Setter Property="Data" Value="{DynamicResource NotificationCardErrorIconPathData}" /> <Setter Property="Data" Value="{DynamicResource NotificationCardErrorIconPathData}" />
</Style> </Style>

View File

@@ -85,6 +85,9 @@ public class Notification : INotification, INotifyPropertyChanged
/// <inheritdoc/> /// <inheritdoc/>
public TimeSpan Expiration { get; set; } public TimeSpan Expiration { get; set; }
/// <inheritdoc/>
public bool ShowIcon { get; set; }
/// <inheritdoc/> /// <inheritdoc/>
public bool ShowClose { get; } public bool ShowClose { get; }

View File

@@ -66,7 +66,9 @@ public class WindowNotificationManager : WindowMessageManager, INotificationMana
/// <inheritdoc/> /// <inheritdoc/>
public void Show(INotification content) public void Show(INotification content)
{ {
Show(content, content.Type, content.Expiration, content.ShowClose, content.OnClick, content.OnClose); Show(content, content.Type, content.Expiration,
content.ShowIcon, content.ShowClose,
content.OnClick, content.OnClose);
} }
/// <inheritdoc/> /// <inheritdoc/>
@@ -74,8 +76,9 @@ public class WindowNotificationManager : WindowMessageManager, INotificationMana
{ {
if (content is INotification notification) if (content is INotification notification)
{ {
Show(notification, notification.Type, notification.Expiration, notification.ShowClose, notification.OnClick, Show(notification, notification.Type, notification.Expiration,
notification.OnClose); notification.ShowIcon, notification.ShowClose,
notification.OnClick, notification.OnClose);
} }
else else
{ {
@@ -89,6 +92,7 @@ public class WindowNotificationManager : WindowMessageManager, INotificationMana
/// <param name="content">the content of the notification</param> /// <param name="content">the content of the notification</param>
/// <param name="type">the type of the notification</param> /// <param name="type">the type of the notification</param>
/// <param name="expiration">the expiration time of the notification after which it will automatically close. If the value is Zero then the notification will remain open until the user closes it</param> /// <param name="expiration">the expiration time of the notification after which it will automatically close. If the value is Zero then the notification will remain open until the user closes it</param>
/// <param name="showIcon">whether to show the icon</param>
/// <param name="showClose">whether to show the close button</param> /// <param name="showClose">whether to show the close button</param>
/// <param name="onClick">an Action to be run when the notification is clicked</param> /// <param name="onClick">an Action to be run when the notification is clicked</param>
/// <param name="onClose">an Action to be run when the notification is closed</param> /// <param name="onClose">an Action to be run when the notification is closed</param>
@@ -97,6 +101,7 @@ public class WindowNotificationManager : WindowMessageManager, INotificationMana
object content, object content,
NotificationType type, NotificationType type,
TimeSpan? expiration = null, TimeSpan? expiration = null,
bool showIcon = true,
bool showClose = true, bool showClose = true,
Action? onClick = null, Action? onClick = null,
Action? onClose = null, Action? onClose = null,
@@ -108,6 +113,7 @@ public class WindowNotificationManager : WindowMessageManager, INotificationMana
{ {
Content = content, Content = content,
NotificationType = type, NotificationType = type,
ShowIcon = showIcon,
ShowClose = showClose, ShowClose = showClose,
[!NotificationCard.PositionProperty] = this[!PositionProperty] [!NotificationCard.PositionProperty] = this[!PositionProperty]
}; };

View File

@@ -12,6 +12,11 @@ public interface IMessage
/// </summary> /// </summary>
NotificationType Type { get; } NotificationType Type { get; }
/// <summary>
/// Gets a value indicating whether the message should show an icon.
/// </summary>
bool ShowIcon { get; }
/// <summary> /// <summary>
/// Gets a value indicating whether the message should show a close button. /// Gets a value indicating whether the message should show a close button.
/// </summary> /// </summary>

View File

@@ -78,6 +78,15 @@ public abstract class MessageCard : ContentControl
public static readonly StyledProperty<NotificationType> NotificationTypeProperty = public static readonly StyledProperty<NotificationType> NotificationTypeProperty =
AvaloniaProperty.Register<MessageCard, NotificationType>(nameof(NotificationType)); AvaloniaProperty.Register<MessageCard, NotificationType>(nameof(NotificationType));
public bool ShowIcon
{
get => GetValue(ShowIconProperty);
set => SetValue(ShowIconProperty, value);
}
public static readonly StyledProperty<bool> ShowIconProperty =
AvaloniaProperty.Register<MessageCard, bool>(nameof(ShowIcon), true);
public bool ShowClose public bool ShowClose
{ {
get => GetValue(ShowCloseProperty); get => GetValue(ShowCloseProperty);
@@ -187,19 +196,19 @@ public abstract class MessageCard : ContentControl
{ {
switch (NotificationType) switch (NotificationType)
{ {
case NotificationType.Error: case Avalonia.Controls.Notifications.NotificationType.Error:
PseudoClasses.Add(PC_Error); PseudoClasses.Add(PC_Error);
break; break;
case NotificationType.Information: case Avalonia.Controls.Notifications.NotificationType.Information:
PseudoClasses.Add(PC_Information); PseudoClasses.Add(PC_Information);
break; break;
case NotificationType.Success: case Avalonia.Controls.Notifications.NotificationType.Success:
PseudoClasses.Add(PC_Success); PseudoClasses.Add(PC_Success);
break; break;
case NotificationType.Warning: case Avalonia.Controls.Notifications.NotificationType.Warning:
PseudoClasses.Add(PC_Warning); PseudoClasses.Add(PC_Warning);
break; break;
} }

View File

@@ -65,6 +65,9 @@ public class Toast : IToast, INotifyPropertyChanged
/// <inheritdoc/> /// <inheritdoc/>
public NotificationType Type { get; set; } public NotificationType Type { get; set; }
/// <inheritdoc/>
public bool ShowIcon { get; set; }
/// <inheritdoc/> /// <inheritdoc/>
public bool ShowClose { get; set; } public bool ShowClose { get; set; }

View File

@@ -1,8 +1,5 @@
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Notifications; using Avalonia.Controls.Notifications;
using Avalonia.Controls.Primitives;
using Avalonia.Layout;
using Avalonia.Threading; using Avalonia.Threading;
namespace Ursa.Controls; namespace Ursa.Controls;
@@ -34,7 +31,9 @@ public class WindowToastManager : WindowMessageManager, IToastManager
/// <inheritdoc/> /// <inheritdoc/>
public void Show(IToast content) public void Show(IToast content)
{ {
Show(content, content.Type, content.Expiration, content.ShowClose, content.OnClick, content.OnClose); Show(content, content.Type, content.Expiration,
content.ShowIcon, content.ShowClose,
content.OnClick, content.OnClose);
} }
/// <inheritdoc/> /// <inheritdoc/>
@@ -42,7 +41,9 @@ public class WindowToastManager : WindowMessageManager, IToastManager
{ {
if (content is IToast toast) if (content is IToast toast)
{ {
Show(toast, toast.Type, toast.Expiration, toast.ShowClose, toast.OnClick, toast.OnClose); Show(toast, toast.Type, toast.Expiration,
toast.ShowIcon, toast.ShowClose,
toast.OnClick, toast.OnClose);
} }
else else
{ {
@@ -56,6 +57,7 @@ public class WindowToastManager : WindowMessageManager, IToastManager
/// <param name="content">the content of the toast</param> /// <param name="content">the content of the toast</param>
/// <param name="type">the type of the toast</param> /// <param name="type">the type of the toast</param>
/// <param name="expiration">the expiration time of the toast after which it will automatically close. If the value is Zero then the toast will remain open until the user closes it</param> /// <param name="expiration">the expiration time of the toast after which it will automatically close. If the value is Zero then the toast will remain open until the user closes it</param>
/// <param name="showIcon">whether to show the icon</param>
/// <param name="showClose">whether to show the close button</param> /// <param name="showClose">whether to show the close button</param>
/// <param name="onClick">an Action to be run when the toast is clicked</param> /// <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="onClose">an Action to be run when the toast is closed</param>
@@ -64,6 +66,7 @@ public class WindowToastManager : WindowMessageManager, IToastManager
object content, object content,
NotificationType type, NotificationType type,
TimeSpan? expiration = null, TimeSpan? expiration = null,
bool showIcon = true,
bool showClose = true, bool showClose = true,
Action? onClick = null, Action? onClick = null,
Action? onClose = null, Action? onClose = null,
@@ -75,6 +78,7 @@ public class WindowToastManager : WindowMessageManager, IToastManager
{ {
Content = content, Content = content,
NotificationType = type, NotificationType = type,
ShowIcon = showIcon,
ShowClose = showClose ShowClose = showClose
}; };