feat: add new Notification.
This commit is contained in:
34
demo/Ursa.Demo/Pages/NotificationDemo.axaml
Normal file
34
demo/Ursa.Demo/Pages/NotificationDemo.axaml
Normal file
@@ -0,0 +1,34 @@
|
||||
<UserControl x:Class="Ursa.Demo.Pages.NotificationDemo"
|
||||
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:Ursa.Demo.ViewModels"
|
||||
x:DataType="vm:NotificationDemoViewModel"
|
||||
d:DesignHeight="450"
|
||||
d:DesignWidth="800"
|
||||
mc:Ignorable="d">
|
||||
<Design.DataContext>
|
||||
<vm:NotificationDemoViewModel />
|
||||
</Design.DataContext>
|
||||
<StackPanel>
|
||||
<ToggleSwitch IsChecked="{Binding ShowClose}" Content="ShowClose"/>
|
||||
<StackPanel Orientation="Horizontal" Spacing="20">
|
||||
<Button Command="{Binding ShowNormal}" CommandParameter="{Binding $self.Content}" Content="Information" />
|
||||
<Button Command="{Binding ShowNormal}" CommandParameter="{Binding $self.Content}" Content="Success" Classes="Success" />
|
||||
<Button Command="{Binding ShowNormal}" CommandParameter="{Binding $self.Content}" Content="Warning" Classes="Warning" />
|
||||
<Button Command="{Binding ShowNormal}" CommandParameter="{Binding $self.Content}" Content="Error" Classes="Danger" />
|
||||
</StackPanel>
|
||||
<StackPanel Orientation="Horizontal" Spacing="20">
|
||||
<StackPanel.Styles>
|
||||
<Style Selector="Button">
|
||||
<Setter Property="Theme" Value="{DynamicResource SolidButton}" />
|
||||
</Style>
|
||||
</StackPanel.Styles>
|
||||
<Button Command="{Binding ShowLight}" CommandParameter="{Binding $self.Content}" Content="Information" />
|
||||
<Button Command="{Binding ShowLight}" CommandParameter="{Binding $self.Content}" Content="Success" Classes="Success" />
|
||||
<Button Command="{Binding ShowLight}" CommandParameter="{Binding $self.Content}" Content="Warning" Classes="Warning" />
|
||||
<Button Command="{Binding ShowLight}" CommandParameter="{Binding $self.Content}" Content="Error" Classes="Danger" />
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
</UserControl>
|
||||
25
demo/Ursa.Demo/Pages/NotificationDemo.axaml.cs
Normal file
25
demo/Ursa.Demo/Pages/NotificationDemo.axaml.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Ursa.Controls;
|
||||
using Ursa.Demo.ViewModels;
|
||||
|
||||
namespace Ursa.Demo.Pages;
|
||||
|
||||
public partial class NotificationDemo : UserControl
|
||||
{
|
||||
private NotificationDemoViewModel _viewModel;
|
||||
|
||||
public NotificationDemo()
|
||||
{
|
||||
InitializeComponent();
|
||||
_viewModel = new NotificationDemoViewModel();
|
||||
DataContext = _viewModel;
|
||||
}
|
||||
|
||||
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
base.OnAttachedToVisualTree(e);
|
||||
var topLevel = TopLevel.GetTopLevel(this);
|
||||
_viewModel.NotificationManager = new WindowNotificationManager(topLevel) { MaxItems = 3 };
|
||||
}
|
||||
}
|
||||
@@ -52,6 +52,7 @@ public class MainViewViewModel : ViewModelBase
|
||||
MenuKeys.MenuKeyMessageBox => new MessageBoxDemoViewModel(),
|
||||
MenuKeys.MenuKeyMultiComboBox => new MultiComboBoxDemoViewModel(),
|
||||
MenuKeys.MenuKeyNavMenu => new NavMenuDemoViewModel(),
|
||||
MenuKeys.MenuKeyNotification => new NotificationDemoViewModel(),
|
||||
MenuKeys.MenuKeyNumberDisplayer => new NumberDisplayerDemoViewModel(),
|
||||
MenuKeys.MenuKeyNumericUpDown => new NumericUpDownDemoViewModel(),
|
||||
MenuKeys.MenuKeyNumPad => new NumPadDemoViewModel(),
|
||||
|
||||
@@ -37,6 +37,7 @@ public class MenuViewModel: ViewModelBase
|
||||
new() { MenuHeader = "Message Box", Key = MenuKeys.MenuKeyMessageBox },
|
||||
new() { MenuHeader = "MultiComboBox", Key = MenuKeys.MenuKeyMultiComboBox, Status = "Updated" },
|
||||
new() { MenuHeader = "Nav Menu", Key = MenuKeys.MenuKeyNavMenu },
|
||||
new() { MenuHeader = "Notification", Key = MenuKeys.MenuKeyNotification },
|
||||
new() { MenuHeader = "Number Displayer", Key = MenuKeys.MenuKeyNumberDisplayer, Status = "New" },
|
||||
new() { MenuHeader = "Numeric UpDown", Key = MenuKeys.MenuKeyNumericUpDown },
|
||||
new() { MenuHeader = "NumPad", Key = MenuKeys.MenuKeyNumPad },
|
||||
@@ -88,6 +89,7 @@ public static class MenuKeys
|
||||
public const string MenuKeyMessageBox = "MessageBox";
|
||||
public const string MenuKeyMultiComboBox = "MultiComboBox";
|
||||
public const string MenuKeyNavMenu = "NavMenu";
|
||||
public const string MenuKeyNotification = "Notification";
|
||||
public const string MenuKeyNumberDisplayer = "NumberDisplayer";
|
||||
public const string MenuKeyNumericUpDown = "NumericUpDown";
|
||||
public const string MenuKeyNumPad = "NumPad";
|
||||
|
||||
42
demo/Ursa.Demo/ViewModels/NotificationDemoViewModel.cs
Normal file
42
demo/Ursa.Demo/ViewModels/NotificationDemoViewModel.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using Avalonia.Controls.Notifications;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Notification = Ursa.Controls.Notification;
|
||||
using WindowNotificationManager = Ursa.Controls.WindowNotificationManager;
|
||||
|
||||
namespace Ursa.Demo.ViewModels;
|
||||
|
||||
public partial class NotificationDemoViewModel : ObservableObject
|
||||
{
|
||||
public WindowNotificationManager? NotificationManager { get; set; }
|
||||
|
||||
[ObservableProperty] private bool _showClose = true;
|
||||
|
||||
[RelayCommand]
|
||||
public void ShowNormal(object obj)
|
||||
{
|
||||
if (obj is string s)
|
||||
{
|
||||
Enum.TryParse<NotificationType>(s, out var notificationType);
|
||||
NotificationManager?.Show(
|
||||
new Notification("Welcome", "This is message"),
|
||||
showClose: ShowClose,
|
||||
type: notificationType);
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
public void ShowLight(object obj)
|
||||
{
|
||||
if (obj is string s)
|
||||
{
|
||||
Enum.TryParse<NotificationType>(s, out var notificationType);
|
||||
NotificationManager?.Show(
|
||||
new Notification("Welcome", "This is message"),
|
||||
showClose: ShowClose,
|
||||
type: notificationType,
|
||||
classes: ["Light"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
233
src/Ursa.Themes.Semi/Controls/Notification.axaml
Normal file
233
src/Ursa.Themes.Semi/Controls/Notification.axaml
Normal file
@@ -0,0 +1,233 @@
|
||||
<ResourceDictionary xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:u="https://irihi.tech/ursa">
|
||||
<Design.PreviewWith>
|
||||
<ThemeVariantScope RequestedThemeVariant="Dark">
|
||||
<StackPanel Orientation="Vertical">
|
||||
<u:NotificationCard ShowClose="False" />
|
||||
<u:NotificationCard>
|
||||
Hello, Ursa!
|
||||
</u:NotificationCard>
|
||||
<u:NotificationCard NotificationType="Success">
|
||||
<u:Notification Title="Welcome" Content="Hello, Ursa!" />
|
||||
</u:NotificationCard>
|
||||
</StackPanel>
|
||||
</ThemeVariantScope>
|
||||
</Design.PreviewWith>
|
||||
|
||||
<ControlTheme x:Key="{x:Type u:WindowNotificationManager}" TargetType="u:WindowNotificationManager">
|
||||
<Setter Property="Margin" Value="0" />
|
||||
<Setter Property="Template">
|
||||
<ControlTemplate>
|
||||
<ReversibleStackPanel Name="PART_Items" />
|
||||
</ControlTemplate>
|
||||
</Setter>
|
||||
<Style Selector="^ /template/ ReversibleStackPanel#PART_Items">
|
||||
<Setter Property="VerticalAlignment" Value="Top" />
|
||||
<Setter Property="HorizontalAlignment" Value="Center" />
|
||||
</Style>
|
||||
</ControlTheme>
|
||||
|
||||
<ControlTheme x:Key="{x:Type u:NotificationCard}" TargetType="u:NotificationCard">
|
||||
<Setter Property="UseLayoutRounding" Value="True" />
|
||||
<Setter Property="RenderTransformOrigin" Value="50%,75%" />
|
||||
<Setter Property="BorderThickness" Value="{DynamicResource NotificationCardBorderThickness}" />
|
||||
<Setter Property="BorderBrush" Value="{DynamicResource NotificationCardBorderBrush}" />
|
||||
<Setter Property="Background" Value="{DynamicResource NotificationCardBackground}" />
|
||||
<Setter Property="CornerRadius" Value="{DynamicResource NotificationCardCornerRadius}" />
|
||||
<Setter Property="Template">
|
||||
<ControlTemplate TargetType="u:NotificationCard">
|
||||
<LayoutTransformControl x:Name="PART_LayoutTransformControl" UseRenderTransform="True">
|
||||
<Border
|
||||
Margin="{DynamicResource NotificationCardMargin}"
|
||||
Background="{TemplateBinding Background}"
|
||||
CornerRadius="{TemplateBinding CornerRadius}">
|
||||
<Border
|
||||
x:Name="PART_RootBorder"
|
||||
Padding="{DynamicResource NotificationCardPadding}"
|
||||
BoxShadow="{DynamicResource NotificationCardBoxShadow}"
|
||||
BorderBrush="{TemplateBinding BorderBrush}"
|
||||
BorderThickness="{TemplateBinding BorderThickness}"
|
||||
CornerRadius="{TemplateBinding CornerRadius}">
|
||||
<Grid ColumnDefinitions="Auto,*,Auto">
|
||||
<PathIcon
|
||||
x:Name="NotificationIcon"
|
||||
Grid.Column="0"
|
||||
Width="{DynamicResource NotificationCardIconWidth}"
|
||||
Height="{DynamicResource NotificationCardIconHeight}"
|
||||
Margin="{DynamicResource NotificationCardIconMargin}"
|
||||
VerticalAlignment="Top"
|
||||
IsVisible="False"
|
||||
Data="{DynamicResource NotificationCardInformationIconPathData}" />
|
||||
<ContentControl
|
||||
x:Name="PART_Content"
|
||||
Grid.Column="1"
|
||||
VerticalContentAlignment="Center"
|
||||
Content="{TemplateBinding Content}">
|
||||
<ContentControl.DataTemplates>
|
||||
<DataTemplate DataType="u:INotification">
|
||||
<StackPanel Spacing="{DynamicResource NotificationCardTitleSpacing}">
|
||||
<SelectableTextBlock
|
||||
Foreground="{DynamicResource NotificationCardTitleForeground}"
|
||||
FontSize="{DynamicResource NotificationCardTitleFontSize}"
|
||||
FontWeight="{DynamicResource NotificationCardTitleFontWeight}"
|
||||
IsVisible="{Binding Title, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
|
||||
Text="{Binding Title}" />
|
||||
<SelectableTextBlock
|
||||
Foreground="{DynamicResource NotificationCardMessageForeground}"
|
||||
FontSize="{DynamicResource NotificationCardMessageFontSize}"
|
||||
FontWeight="{DynamicResource NotificationCardMessageFontWeight}"
|
||||
Text="{Binding Content}"
|
||||
TextWrapping="Wrap" />
|
||||
</StackPanel>
|
||||
</DataTemplate>
|
||||
<DataTemplate DataType="x:String">
|
||||
<SelectableTextBlock
|
||||
Foreground="{DynamicResource NotificationCardMessageForeground}"
|
||||
FontSize="{DynamicResource NotificationCardMessageFontSize}"
|
||||
FontWeight="{DynamicResource NotificationCardMessageFontWeight}"
|
||||
Text="{Binding}"
|
||||
TextWrapping="Wrap" />
|
||||
</DataTemplate>
|
||||
</ContentControl.DataTemplates>
|
||||
</ContentControl>
|
||||
<Button
|
||||
x:Name="PART_CloseButton"
|
||||
Grid.Column="2"
|
||||
VerticalAlignment="Top"
|
||||
HorizontalAlignment="Right"
|
||||
Theme="{StaticResource NotificationCloseButton}"
|
||||
IsVisible="{TemplateBinding ShowClose}"
|
||||
u:MessageCard.CloseOnClick="True" />
|
||||
</Grid>
|
||||
</Border>
|
||||
</Border>
|
||||
</LayoutTransformControl>
|
||||
</ControlTemplate>
|
||||
</Setter>
|
||||
|
||||
<ControlTheme.Animations>
|
||||
<Animation
|
||||
Easing="QuadraticEaseIn"
|
||||
FillMode="Forward"
|
||||
Duration="0:0:0.3">
|
||||
<KeyFrame Cue="0%">
|
||||
<Setter Property="Opacity" Value="0" />
|
||||
</KeyFrame>
|
||||
<KeyFrame Cue="100%">
|
||||
<Setter Property="Opacity" Value="1" />
|
||||
</KeyFrame>
|
||||
<KeyFrame KeySpline="0.22,0.57,0.02,1.2" Cue="0%">
|
||||
<Setter Property="TranslateTransform.Y" Value="-100" />
|
||||
</KeyFrame>
|
||||
<KeyFrame KeySpline="0.22,0.57,0.02,1.2" Cue="100%">
|
||||
<Setter Property="TranslateTransform.Y" Value="0" />
|
||||
</KeyFrame>
|
||||
</Animation>
|
||||
</ControlTheme.Animations>
|
||||
|
||||
<Style Selector="^[IsClosing=true] /template/ LayoutTransformControl#PART_LayoutTransformControl">
|
||||
<Setter Property="RenderTransformOrigin" Value="50%,0%" />
|
||||
<Style.Animations>
|
||||
<Animation
|
||||
Easing="QuadraticEaseOut"
|
||||
FillMode="Forward"
|
||||
Duration="0:0:0.3">
|
||||
<KeyFrame Cue="0%">
|
||||
<Setter Property="Opacity" Value="1" />
|
||||
</KeyFrame>
|
||||
<KeyFrame Cue="100%">
|
||||
<Setter Property="Opacity" Value="0" />
|
||||
</KeyFrame>
|
||||
<KeyFrame KeySpline="0.22,0.57,0.02,1.2" Cue="0%">
|
||||
<Setter Property="TranslateTransform.Y" Value="0" />
|
||||
</KeyFrame>
|
||||
<KeyFrame KeySpline="0.22,0.57,0.02,1.2" Cue="100%">
|
||||
<Setter Property="TranslateTransform.Y" Value="-100" />
|
||||
</KeyFrame>
|
||||
</Animation>
|
||||
</Style.Animations>
|
||||
</Style>
|
||||
|
||||
<Style Selector="^[IsClosing=true]">
|
||||
<Style.Animations>
|
||||
<Animation
|
||||
Easing="QuadraticEaseOut"
|
||||
FillMode="Forward"
|
||||
Duration="0:0:0.3">
|
||||
<KeyFrame Cue="100%">
|
||||
<Setter Property="IsClosed" Value="True" />
|
||||
</KeyFrame>
|
||||
</Animation>
|
||||
</Style.Animations>
|
||||
</Style>
|
||||
|
||||
<Style Selector="^:information /template/ PathIcon#NotificationIcon">
|
||||
<Setter Property="IsVisible" Value="True" />
|
||||
<Setter Property="Foreground" Value="{DynamicResource NotificationCardInformationIconForeground}" />
|
||||
<Setter Property="Data" Value="{DynamicResource NotificationCardInformationIconPathData}" />
|
||||
</Style>
|
||||
<Style Selector="^:success /template/ PathIcon#NotificationIcon">
|
||||
<Setter Property="IsVisible" Value="True" />
|
||||
<Setter Property="Foreground" Value="{DynamicResource NotificationCardSuccessIconForeground}" />
|
||||
<Setter Property="Data" Value="{DynamicResource NotificationCardSuccessIconPathData}" />
|
||||
</Style>
|
||||
<Style Selector="^:warning /template/ PathIcon#NotificationIcon">
|
||||
<Setter Property="IsVisible" Value="True" />
|
||||
<Setter Property="Foreground" Value="{DynamicResource NotificationCardWarningIconForeground}" />
|
||||
<Setter Property="Data" Value="{DynamicResource NotificationCardWarningIconPathData}" />
|
||||
</Style>
|
||||
<Style Selector="^:error /template/ PathIcon#NotificationIcon">
|
||||
<Setter Property="IsVisible" Value="True" />
|
||||
<Setter Property="Foreground" Value="{DynamicResource NotificationCardErrorIconForeground}" />
|
||||
<Setter Property="Data" Value="{DynamicResource NotificationCardErrorIconPathData}" />
|
||||
</Style>
|
||||
|
||||
<Style Selector="^.Light">
|
||||
<Setter Property="Background" Value="{DynamicResource SemiColorBackground0}" />
|
||||
<Style Selector="^:information /template/ Border#PART_RootBorder">
|
||||
<Setter Property="BorderBrush" Value="{DynamicResource SemiColorInformation}" />
|
||||
<Setter Property="Background" Value="{DynamicResource SemiColorInformationLight}" />
|
||||
</Style>
|
||||
<Style Selector="^:success /template/ Border#PART_RootBorder">
|
||||
<Setter Property="BorderBrush" Value="{DynamicResource SemiColorSuccess}" />
|
||||
<Setter Property="Background" Value="{DynamicResource SemiColorSuccessLight}" />
|
||||
</Style>
|
||||
<Style Selector="^:warning /template/ Border#PART_RootBorder">
|
||||
<Setter Property="BorderBrush" Value="{DynamicResource SemiColorWarning}" />
|
||||
<Setter Property="Background" Value="{DynamicResource SemiColorWarningLight}" />
|
||||
</Style>
|
||||
<Style Selector="^:error /template/ Border#PART_RootBorder">
|
||||
<Setter Property="BorderBrush" Value="{DynamicResource SemiColorDanger}" />
|
||||
<Setter Property="Background" Value="{DynamicResource SemiColorDangerLight}" />
|
||||
</Style>
|
||||
</Style>
|
||||
</ControlTheme>
|
||||
|
||||
<ControlTheme x:Key="NotificationCloseButton" TargetType="Button">
|
||||
<Setter Property="CornerRadius" Value="6" />
|
||||
<Setter Property="Height" Value="24" />
|
||||
<Setter Property="Width" Value="24" />
|
||||
<Setter Property="Cursor" Value="Hand" />
|
||||
<Setter Property="Template">
|
||||
<ControlTemplate TargetType="Button">
|
||||
<Border
|
||||
Name="PART_Border"
|
||||
Padding="{TemplateBinding Padding}"
|
||||
Background="Transparent"
|
||||
CornerRadius="{TemplateBinding CornerRadius}">
|
||||
<PathIcon
|
||||
Width="10"
|
||||
Height="10"
|
||||
Data="{DynamicResource WindowCloseIconGlyph}" />
|
||||
</Border>
|
||||
</ControlTemplate>
|
||||
</Setter>
|
||||
<Style Selector="^:pointerover /template/ Border">
|
||||
<Setter Property="Background" Value="{DynamicResource ButtonDefaultPointeroverBackground}" />
|
||||
</Style>
|
||||
<Style Selector="^:pressed /template/ Border">
|
||||
<Setter Property="Background" Value="{DynamicResource ButtonDefaultPressedBackground}" />
|
||||
</Style>
|
||||
</ControlTheme>
|
||||
</ResourceDictionary>
|
||||
@@ -29,6 +29,7 @@
|
||||
<ResourceInclude Source="MessageBox.axaml" />
|
||||
<ResourceInclude Source="MultiComboBox.axaml" />
|
||||
<ResourceInclude Source="NavMenu.axaml" />
|
||||
<ResourceInclude Source="Notification.axaml" />
|
||||
<ResourceInclude Source="NumericUpDown.axaml" />
|
||||
<ResourceInclude Source="NumPad.axaml" />
|
||||
<ResourceInclude Source="NumberDisplayer.axaml" />
|
||||
|
||||
17
src/Ursa/Controls/Notification/INotification.cs
Normal file
17
src/Ursa/Controls/Notification/INotification.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a notification that can be shown in a window or by the host operating system.
|
||||
/// </summary>
|
||||
public interface INotification : IMessage
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the Title of the notification.
|
||||
/// </summary>
|
||||
string? Title { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Content of the notification.
|
||||
/// </summary>
|
||||
string? Content { get; }
|
||||
}
|
||||
14
src/Ursa/Controls/Notification/INotificationManager.cs
Normal file
14
src/Ursa/Controls/Notification/INotificationManager.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a notification manager that can be used to show notifications in a window or using
|
||||
/// the host operating system.
|
||||
/// </summary>
|
||||
public interface INotificationManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Show a notification.
|
||||
/// </summary>
|
||||
/// <param name="notification">The notification to be displayed.</param>
|
||||
void Show(INotification notification);
|
||||
}
|
||||
103
src/Ursa/Controls/Notification/Notification.cs
Normal file
103
src/Ursa/Controls/Notification/Notification.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Avalonia.Controls.Notifications;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// A notification that can be shown in a window or by the host operating system.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This class represents a notification that can be displayed either in a window using
|
||||
/// <see cref="WindowNotificationManager"/> or by the host operating system (to be implemented).
|
||||
/// </remarks>
|
||||
public class Notification : INotification, INotifyPropertyChanged
|
||||
{
|
||||
private string? _title, _content;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Notification"/> class.
|
||||
/// </summary>
|
||||
/// <param name="title">The title of the notification.</param>
|
||||
/// <param name="content">The content to be displayed in the notification.</param>
|
||||
/// <param name="type">The <see cref="NotificationType"/> of the notification.</param>
|
||||
/// <param name="expiration">The expiry time at which the notification will close.
|
||||
/// Use <see cref="TimeSpan.Zero"/> for notifications that will remain open.</param>
|
||||
/// <param name="showClose">A value indicating whether the notification should show a close button.</param>
|
||||
/// <param name="onClick">An Action to call when the notification is clicked.</param>
|
||||
/// <param name="onClose">An Action to call when the notification is closed.</param>
|
||||
public Notification(
|
||||
string? title,
|
||||
string? content,
|
||||
NotificationType type = NotificationType.Information,
|
||||
TimeSpan? expiration = null,
|
||||
bool showClose = true,
|
||||
Action? onClick = null,
|
||||
Action? onClose = null)
|
||||
{
|
||||
Title = title;
|
||||
Content = content;
|
||||
Type = type;
|
||||
Expiration = expiration ?? TimeSpan.FromSeconds(3);
|
||||
ShowClose = showClose;
|
||||
OnClick = onClick;
|
||||
OnClose = onClose;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Notification"/> class.
|
||||
/// </summary>
|
||||
public Notification() : this(null, null)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string? Title
|
||||
{
|
||||
get => _title;
|
||||
set
|
||||
{
|
||||
if (_title != value)
|
||||
{
|
||||
_title = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string? Content
|
||||
{
|
||||
get => _content;
|
||||
set
|
||||
{
|
||||
if (_content != value)
|
||||
{
|
||||
_content = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public NotificationType Type { get; set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public TimeSpan Expiration { get; set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool ShowClose { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Action? OnClick { get; set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Action? OnClose { get; set; }
|
||||
|
||||
public event PropertyChangedEventHandler? PropertyChanged;
|
||||
|
||||
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
6
src/Ursa/Controls/Notification/NotificationCard.cs
Normal file
6
src/Ursa/Controls/Notification/NotificationCard.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Control that represents and displays a notification.
|
||||
/// </summary>
|
||||
public class NotificationCard : MessageCard;
|
||||
171
src/Ursa/Controls/Notification/WindowNotificationManager.cs
Normal file
171
src/Ursa/Controls/Notification/WindowNotificationManager.cs
Normal file
@@ -0,0 +1,171 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Metadata;
|
||||
using Avalonia.Controls.Notifications;
|
||||
using Avalonia.Layout;
|
||||
using Avalonia.Threading;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="INotificationManager"/> that displays notifications in a <see cref="Window"/>.
|
||||
/// </summary>
|
||||
[PseudoClasses(PC_TopLeft, PC_TopRight, PC_BottomLeft, PC_BottomRight, PC_TopCenter, PC_BottomCenter)]
|
||||
public class WindowNotificationManager : WindowMessageManager, INotificationManager
|
||||
{
|
||||
public const string PC_TopLeft = ":topleft";
|
||||
public const string PC_TopRight = ":topright";
|
||||
public const string PC_BottomLeft = ":bottomleft";
|
||||
public const string PC_BottomRight = ":bottomright";
|
||||
public const string PC_TopCenter = ":topcenter";
|
||||
public const string PC_BottomCenter = ":bottomcenter";
|
||||
|
||||
/// <summary>
|
||||
/// Defines the <see cref="Position"/> property.
|
||||
/// </summary>
|
||||
public static readonly StyledProperty<NotificationPosition> PositionProperty =
|
||||
AvaloniaProperty.Register<WindowNotificationManager, NotificationPosition>(nameof(Position),
|
||||
NotificationPosition.TopRight);
|
||||
|
||||
/// <summary>
|
||||
/// Defines which corner of the screen notifications can be displayed in.
|
||||
/// </summary>
|
||||
/// <seealso cref="NotificationPosition"/>
|
||||
public NotificationPosition Position
|
||||
{
|
||||
get => GetValue(PositionProperty);
|
||||
set => SetValue(PositionProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="WindowNotificationManager"/> class.
|
||||
/// </summary>
|
||||
/// <param name="host">The TopLevel that will host the control.</param>
|
||||
public WindowNotificationManager(TopLevel? host) : this()
|
||||
{
|
||||
if (host is not null)
|
||||
{
|
||||
InstallFromTopLevel(host);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="WindowNotificationManager"/> class.
|
||||
/// </summary>
|
||||
public WindowNotificationManager()
|
||||
{
|
||||
UpdatePseudoClasses(Position);
|
||||
}
|
||||
|
||||
static WindowNotificationManager()
|
||||
{
|
||||
HorizontalAlignmentProperty.OverrideDefaultValue<WindowNotificationManager>(HorizontalAlignment.Stretch);
|
||||
VerticalAlignmentProperty.OverrideDefaultValue<WindowNotificationManager>(VerticalAlignment.Stretch);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Show(INotification content)
|
||||
{
|
||||
Show(content, content.Type, content.Expiration, content.ShowClose, content.OnClick, content.OnClose);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Show(object content)
|
||||
{
|
||||
if (content is INotification notification)
|
||||
{
|
||||
Show(notification, notification.Type, notification.Expiration, notification.ShowClose, notification.OnClick,
|
||||
notification.OnClose);
|
||||
}
|
||||
else
|
||||
{
|
||||
Show(content, NotificationType.Information);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows a Notification
|
||||
/// </summary>
|
||||
/// <param name="content">the content 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="showClose">whether to show the close button</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="classes">style classes to apply</param>
|
||||
public async void Show(
|
||||
object content,
|
||||
NotificationType type,
|
||||
TimeSpan? expiration = null,
|
||||
bool showClose = true,
|
||||
Action? onClick = null,
|
||||
Action? onClose = null,
|
||||
string[]? classes = null)
|
||||
{
|
||||
Dispatcher.UIThread.VerifyAccess();
|
||||
|
||||
var notificationControl = new NotificationCard
|
||||
{
|
||||
Content = content,
|
||||
NotificationType = type,
|
||||
ShowClose = showClose
|
||||
};
|
||||
|
||||
// Add style classes if any
|
||||
if (classes is not null)
|
||||
{
|
||||
foreach (var @class in classes)
|
||||
{
|
||||
notificationControl.Classes.Add(@class);
|
||||
}
|
||||
}
|
||||
|
||||
notificationControl.MessageClosed += (sender, _) =>
|
||||
{
|
||||
onClose?.Invoke();
|
||||
|
||||
_items?.Remove(sender);
|
||||
};
|
||||
|
||||
notificationControl.PointerPressed += (_, _) => { onClick?.Invoke(); };
|
||||
|
||||
Dispatcher.UIThread.Post(() =>
|
||||
{
|
||||
_items?.Add(notificationControl);
|
||||
|
||||
if (_items?.OfType<NotificationCard>().Count(i => !i.IsClosing) > MaxItems)
|
||||
{
|
||||
_items.OfType<NotificationCard>().First(i => !i.IsClosing).Close();
|
||||
}
|
||||
});
|
||||
|
||||
if (expiration == TimeSpan.Zero)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await Task.Delay(expiration ?? TimeSpan.FromSeconds(3));
|
||||
|
||||
notificationControl.Close();
|
||||
}
|
||||
|
||||
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
|
||||
{
|
||||
base.OnPropertyChanged(change);
|
||||
|
||||
if (change.Property == PositionProperty)
|
||||
{
|
||||
UpdatePseudoClasses(change.GetNewValue<NotificationPosition>());
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdatePseudoClasses(NotificationPosition position)
|
||||
{
|
||||
PseudoClasses.Set(PC_TopLeft, position == NotificationPosition.TopLeft);
|
||||
PseudoClasses.Set(PC_TopRight, position == NotificationPosition.TopRight);
|
||||
PseudoClasses.Set(PC_BottomLeft, position == NotificationPosition.BottomLeft);
|
||||
PseudoClasses.Set(PC_BottomRight, position == NotificationPosition.BottomRight);
|
||||
PseudoClasses.Set(PC_TopCenter, position == NotificationPosition.TopCenter);
|
||||
PseudoClasses.Set(PC_BottomCenter, position == NotificationPosition.BottomCenter);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user