Merge pull request #398 from irihitech/toast

New Control: Notification & Toast
This commit is contained in:
Dong Bin
2024-09-12 18:55:40 +08:00
committed by GitHub
33 changed files with 2034 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
<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 Spacing="20">
<StackPanel Orientation="Horizontal">
<ToggleSwitch IsChecked="{Binding ShowIcon}" Content="ShowIcon" />
<ToggleSwitch IsChecked="{Binding ShowClose}" Content="ShowClose" />
</StackPanel>
<UniformGrid Rows="2" Columns="3" Width="500" HorizontalAlignment="Left">
<UniformGrid.Styles>
<Style Selector="RadioButton">
<Setter Property="Theme" Value="{DynamicResource PureCardRadioButton}" />
<Setter Property="Command" Value="{Binding ChangePosition}" />
<Setter Property="CommandParameter" Value="{Binding $self.Content}" />
</Style>
</UniformGrid.Styles>
<RadioButton Content="TopLeft" />
<RadioButton Content="TopCenter" />
<RadioButton Content="TopRight" IsChecked="True" />
<RadioButton Content="BottomLeft" />
<RadioButton Content="BottomCenter" />
<RadioButton Content="BottomRight" />
</UniformGrid>
<StackPanel Orientation="Horizontal" Spacing="20">
<StackPanel.Styles>
<Style Selector="Button">
<Setter Property="Command" Value="{Binding ShowNormal}" />
<Setter Property="CommandParameter" Value="{Binding $self.Content}" />
</Style>
</StackPanel.Styles>
<Button Content="Information" />
<Button Content="Success" Classes="Success" />
<Button Content="Warning" Classes="Warning" />
<Button Content="Error" Classes="Danger" />
</StackPanel>
<StackPanel Orientation="Horizontal" Spacing="20">
<StackPanel.Styles>
<Style Selector="Button">
<Setter Property="Theme" Value="{DynamicResource SolidButton}" />
<Setter Property="Command" Value="{Binding ShowLight}" />
<Setter Property="CommandParameter" Value="{Binding $self.Content}" />
</Style>
</StackPanel.Styles>
<Button Content="Information" />
<Button Content="Success" Classes="Success" />
<Button Content="Warning" Classes="Warning" />
<Button Content="Error" Classes="Danger" />
</StackPanel>
</StackPanel>
</UserControl>

View File

@@ -0,0 +1,32 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.LogicalTree;
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 };
}
protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
{
base.OnDetachedFromLogicalTree(e);
_viewModel.NotificationManager?.Uninstall();
}
}

View File

@@ -0,0 +1,47 @@
<UserControl x:Class="Ursa.Demo.Pages.ToastDemo"
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:ToastDemoViewModel"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<Design.DataContext>
<vm:ToastDemoViewModel />
</Design.DataContext>
<StackPanel Spacing="20">
<StackPanel Orientation="Horizontal">
<ToggleSwitch IsChecked="{Binding ShowIcon}" Content="ShowIcon" />
<ToggleSwitch IsChecked="{Binding ShowClose}" Content="ShowClose" />
</StackPanel>
<StackPanel Orientation="Horizontal" Spacing="20">
<StackPanel.Styles>
<Style Selector="Button">
<Setter Property="Command" Value="{Binding ShowNormal}" />
<Setter Property="CommandParameter" Value="{Binding $self.Content}" />
</Style>
</StackPanel.Styles>
<Button Content="Information" />
<Button Content="Success" Classes="Success" />
<Button Content="Warning" Classes="Warning" />
<Button Content="Error" Classes="Danger" />
</StackPanel>
<StackPanel Orientation="Horizontal" Spacing="20">
<StackPanel.Styles>
<Style Selector="Button">
<Setter Property="Theme" Value="{DynamicResource SolidButton}" />
<Setter Property="Command" Value="{Binding ShowLight}" />
<Setter Property="CommandParameter" Value="{Binding $self.Content}" />
</Style>
</StackPanel.Styles>
<Button Content="Information" />
<Button Content="Success" Classes="Success" />
<Button Content="Warning" Classes="Warning" />
<Button Content="Error" Classes="Danger" />
</StackPanel>
</StackPanel>
</UserControl>

View File

@@ -0,0 +1,31 @@
using Avalonia;
using Avalonia.Controls;
using Ursa.Controls;
using Ursa.Demo.ViewModels;
namespace Ursa.Demo.Pages;
public partial class ToastDemo : UserControl
{
private ToastDemoViewModel _viewModel;
public ToastDemo()
{
InitializeComponent();
_viewModel = new ToastDemoViewModel();
DataContext = _viewModel;
}
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
var topLevel = TopLevel.GetTopLevel(this);
_viewModel.ToastManager = new WindowToastManager(topLevel) { MaxItems = 3 };
}
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnDetachedFromVisualTree(e);
_viewModel.ToastManager?.Uninstall();
}
}

View File

@@ -53,6 +53,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(),
@@ -68,6 +69,7 @@ public class MainViewViewModel : ViewModelBase
MenuKeys.MenuKeyTreeComboBox => new TreeComboBoxDemoViewModel(),
MenuKeys.MenuKeyTwoTonePathIcon => new TwoTonePathIconDemoViewModel(),
MenuKeys.MenuKeyThemeToggler => new ThemeTogglerDemoViewModel(),
MenuKeys.MenuKeyToast => new ToastDemoViewModel(),
MenuKeys.MenuKeyToolBar => new ToolBarDemoViewModel(),
MenuKeys.MenuKeyTimeBox => new TimeBoxDemoViewModel(),
MenuKeys.MenuKeyPinCode => new PinCodeDemoViewModel(),

View File

@@ -38,6 +38,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, Status = "New"},
new() { MenuHeader = "Number Displayer", Key = MenuKeys.MenuKeyNumberDisplayer, Status = "New" },
new() { MenuHeader = "Numeric UpDown", Key = MenuKeys.MenuKeyNumericUpDown },
new() { MenuHeader = "NumPad", Key = MenuKeys.MenuKeyNumPad },
@@ -54,6 +55,7 @@ public class MenuViewModel: ViewModelBase
new() { MenuHeader = "Timeline", Key = MenuKeys.MenuKeyTimeline },
new() { MenuHeader = "TreeComboBox", Key = MenuKeys.MenuKeyTreeComboBox },
new() { MenuHeader = "TwoTonePathIcon", Key = MenuKeys.MenuKeyTwoTonePathIcon},
new() { MenuHeader = "Toast", Key = MenuKeys.MenuKeyToast, Status = "New"},
new() { MenuHeader = "ToolBar", Key = MenuKeys.MenuKeyToolBar },
new() { MenuHeader = "Time Box", Key = MenuKeys.MenuKeyTimeBox },
};
@@ -89,6 +91,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";
@@ -104,6 +107,7 @@ public static class MenuKeys
public const string MenuKeyTwoTonePathIcon = "TwoTonePathIcon";
public const string MenuKeyThemeToggler = "ThemeToggler";
public const string MenuKeyTreeComboBox = "TreeComboBox";
public const string MenuKeyToast = "Toast";
public const string MenuKeyToolBar = "ToolBar";
public const string MenuKeyPinCode = "PinCode";
public const string MenuKeyTimeBox = "TimeBox";

View File

@@ -0,0 +1,51 @@
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 _showIcon = true;
[ObservableProperty] private bool _showClose = true;
[RelayCommand]
public void ChangePosition(object obj)
{
if (obj is string s && NotificationManager is not null)
{
Enum.TryParse<NotificationPosition>(s, out var notificationPosition);
NotificationManager.Position = notificationPosition;
}
}
[RelayCommand]
public void ShowNormal(object obj)
{
if (obj is not string s) return;
Enum.TryParse<NotificationType>(s, out var notificationType);
NotificationManager?.Show(
new Notification("Welcome", "This is message"),
showIcon: ShowIcon,
showClose: ShowClose,
type: notificationType);
}
[RelayCommand]
public void ShowLight(object obj)
{
if (obj is not string s) return;
Enum.TryParse<NotificationType>(s, out var notificationType);
NotificationManager?.Show(
new Notification("Welcome", "This is message"),
showIcon: ShowIcon,
showClose: ShowClose,
type: notificationType,
classes: ["Light"]);
}
}

View File

@@ -0,0 +1,64 @@
using System;
using Avalonia.Controls.Notifications;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Ursa.Controls;
namespace Ursa.Demo.ViewModels;
public partial class ToastDemoViewModel : ObservableObject
{
public WindowToastManager? ToastManager { get; set; }
[ObservableProperty] private bool _showIcon = true;
[ObservableProperty] private bool _showClose = true;
[RelayCommand]
public void ShowNormal(object obj)
{
if (obj is string s)
{
Enum.TryParse<NotificationType>(s, out var notificationType);
ToastManager?.Show(
new Toast("This is message"),
showIcon: ShowIcon,
showClose: ShowClose,
type: notificationType);
}
// ToastManager?.Show(new ToastDemoViewModel
// {
// Content = "This is message",
// ToastManager = ToastManager
// });
}
[RelayCommand]
public void ShowLight(object obj)
{
if (obj is string s)
{
Enum.TryParse<NotificationType>(s, out var notificationType);
ToastManager?.Show(
new Toast("This is message"),
showIcon: ShowIcon,
showClose: ShowClose,
type: notificationType,
classes: ["Light"]);
}
}
public string? Content { get; set; }
[RelayCommand]
public void YesCommand()
{
ToastManager?.Show(new Toast("Yes!"));
}
[RelayCommand]
public void NoCommand()
{
ToastManager?.Show(new Toast("No!"));
}
}

View File

@@ -0,0 +1,463 @@
<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">
<ReversibleStackPanel Orientation="Vertical" ReverseOrder="True">
<u:NotificationCard ShowIcon="False" ShowClose="False" />
<u:NotificationCard ShowIcon="False" />
<u:NotificationCard NotificationType="Information">
Hello, Ursa!
</u:NotificationCard>
<u:NotificationCard NotificationType="Success">
<u:Notification Title="Welcome" />
</u:NotificationCard>
<u:NotificationCard NotificationType="Warning">
<u:Notification Content="Hello, Ursa!" />
</u:NotificationCard>
<u:NotificationCard NotificationType="Error" Classes="Light">
<u:Notification Title="Welcome">
Hello, Ursa!
</u:Notification>
</u:NotificationCard>
</ReversibleStackPanel>
</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" ReverseOrder="True" />
</ControlTemplate>
</Setter>
<Style Selector="^:topleft /template/ ReversibleStackPanel#PART_Items">
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="HorizontalAlignment" Value="Left" />
</Style>
<Style Selector="^:topright /template/ ReversibleStackPanel#PART_Items">
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="HorizontalAlignment" Value="Right" />
</Style>
<Style Selector="^:topcenter /template/ ReversibleStackPanel#PART_Items">
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
<Style Selector="^:bottomleft /template/ ReversibleStackPanel#PART_Items">
<Setter Property="VerticalAlignment" Value="Bottom" />
<Setter Property="HorizontalAlignment" Value="Left" />
</Style>
<Style Selector="^:bottomright /template/ ReversibleStackPanel#PART_Items">
<Setter Property="VerticalAlignment" Value="Bottom" />
<Setter Property="HorizontalAlignment" Value="Right" />
</Style>
<Style Selector="^:bottomcenter /template/ ReversibleStackPanel#PART_Items">
<Setter Property="VerticalAlignment" Value="Bottom" />
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</ControlTheme>
<ControlTheme x:Key="{x:Type u:NotificationCard}" TargetType="u:NotificationCard">
<Setter Property="UseLayoutRounding" Value="True" />
<Setter Property="BorderThickness" Value="{DynamicResource NotificationCardBorderThickness}" />
<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 NotificationCardBoxShadows}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}">
<DockPanel MinWidth="{DynamicResource NotificationCardMinWidth}">
<PathIcon
x:Name="NotificationIcon"
Width="{DynamicResource NotificationCardIconWidth}"
Height="{DynamicResource NotificationCardIconHeight}"
Margin="{DynamicResource NotificationCardIconMargin}"
VerticalAlignment="Top"
IsVisible="{TemplateBinding ShowIcon}"
Data="{DynamicResource NotificationCardInformationIconPathData}" />
<ContentControl
x:Name="PART_Content"
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}"
IsVisible="{Binding Content, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
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"
VerticalAlignment="Top"
HorizontalAlignment="Right"
Theme="{StaticResource NotificationCloseButton}"
IsVisible="{TemplateBinding ShowClose}"
u:MessageCard.CloseOnClick="True" />
</DockPanel>
</Border>
</Border>
</LayoutTransformControl>
</ControlTemplate>
</Setter>
<Style Selector="^">
<Style Selector="^ /template/ LayoutTransformControl#PART_LayoutTransformControl">
<Style.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>
</Animation>
<Animation
Easing="QuadraticEaseIn"
FillMode="Forward"
Duration="0:0:0.15">
<KeyFrame Cue="0%">
<Setter Property="ScaleTransform.ScaleY" Value="0" />
</KeyFrame>
<KeyFrame Cue="70%">
<Setter Property="ScaleTransform.ScaleY" Value="0" />
</KeyFrame>
<KeyFrame Cue="100%">
<Setter Property="ScaleTransform.ScaleY" Value="1" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
<Style Selector="^:topleft">
<Style.Animations>
<Animation
Easing="QuadraticEaseIn"
FillMode="Forward"
Duration="0:0:0.3">
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="0%">
<Setter Property="TranslateTransform.X" Value="-500" />
</KeyFrame>
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="100%">
<Setter Property="TranslateTransform.X" Value="0" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
<Style Selector="^:topright">
<Style.Animations>
<Animation
Easing="QuadraticEaseIn"
FillMode="Forward"
Duration="0:0:0.3">
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="0%">
<Setter Property="TranslateTransform.X" Value="500" />
</KeyFrame>
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="100%">
<Setter Property="TranslateTransform.X" Value="0" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
<Style Selector="^:bottomleft">
<Style.Animations>
<Animation
Easing="QuadraticEaseIn"
FillMode="Forward"
Duration="0:0:0.3">
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="0%">
<Setter Property="TranslateTransform.X" Value="-500" />
</KeyFrame>
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="100%">
<Setter Property="TranslateTransform.X" Value="0" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
<Style Selector="^:bottomright">
<Style.Animations>
<Animation
Easing="QuadraticEaseIn"
FillMode="Forward"
Duration="0:0:0.3">
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="0%">
<Setter Property="TranslateTransform.X" Value="500" />
</KeyFrame>
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="100%">
<Setter Property="TranslateTransform.X" Value="0" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
<Style Selector="^:topcenter">
<Style.Animations>
<Animation
Easing="QuadraticEaseIn"
FillMode="Forward"
Duration="0:0:0.3">
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="0%">
<Setter Property="TranslateTransform.Y" Value="-100" />
</KeyFrame>
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="100%">
<Setter Property="TranslateTransform.Y" Value="0" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
<Style Selector="^:bottomcenter">
<Style.Animations>
<Animation
Easing="QuadraticEaseIn"
FillMode="Forward"
Duration="0:0:0.3">
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="0%">
<Setter Property="TranslateTransform.Y" Value="100" />
</KeyFrame>
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="100%">
<Setter Property="TranslateTransform.Y" Value="0" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
</Style>
<Style Selector="^[IsClosing=true]">
<Style Selector="^ /template/ LayoutTransformControl#PART_LayoutTransformControl">
<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>
</Animation>
<Animation
Easing="QuadraticEaseOut"
FillMode="Forward"
Duration="0:0:0.15">
<KeyFrame Cue="0%">
<Setter Property="ScaleTransform.ScaleY" Value="1" />
</KeyFrame>
<KeyFrame Cue="70%">
<Setter Property="ScaleTransform.ScaleY" Value="1" />
</KeyFrame>
<KeyFrame Cue="100%">
<Setter Property="ScaleTransform.ScaleY" Value="0" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
<Style Selector="^:topleft">
<Style.Animations>
<Animation
Easing="QuadraticEaseOut"
FillMode="Forward"
Duration="0:0:0.3">
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="0%">
<Setter Property="TranslateTransform.X" Value="0" />
</KeyFrame>
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="100%">
<Setter Property="TranslateTransform.X" Value="-500" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
<Style Selector="^:topright">
<Style.Animations>
<Animation
Easing="QuadraticEaseOut"
FillMode="Forward"
Duration="0:0:0.3">
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="0%">
<Setter Property="TranslateTransform.X" Value="0" />
</KeyFrame>
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="100%">
<Setter Property="TranslateTransform.X" Value="500" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
<Style Selector="^:bottomleft">
<Style.Animations>
<Animation
Easing="QuadraticEaseOut"
FillMode="Forward"
Duration="0:0:0.3">
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="0%">
<Setter Property="TranslateTransform.X" Value="0" />
</KeyFrame>
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="100%">
<Setter Property="TranslateTransform.X" Value="-500" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
<Style Selector="^:bottomright">
<Style.Animations>
<Animation
Easing="QuadraticEaseOut"
FillMode="Forward"
Duration="0:0:0.3">
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="0%">
<Setter Property="TranslateTransform.X" Value="0" />
</KeyFrame>
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="100%">
<Setter Property="TranslateTransform.X" Value="500" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
<Style Selector="^:topcenter">
<Style.Animations>
<Animation
Easing="QuadraticEaseOut"
FillMode="Forward"
Duration="0:0:0.3">
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="0%">
<Setter Property="TranslateTransform.Y" Value="0" />
</KeyFrame>
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="100%">
<Setter Property="TranslateTransform.Y" Value="-100" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
<Style Selector="^:bottomcenter">
<Style.Animations>
<Animation
Easing="QuadraticEaseOut"
FillMode="Forward"
Duration="0:0:0.3">
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="0%">
<Setter Property="TranslateTransform.Y" Value="0" />
</KeyFrame>
<KeyFrame KeySpline="0.62,0.63,0,1.13" Cue="100%">
<Setter Property="TranslateTransform.Y" Value="100" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
</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="Foreground" Value="{DynamicResource NotificationCardInformationIconForeground}" />
<Setter Property="Data" Value="{DynamicResource NotificationCardInformationIconPathData}" />
</Style>
<Style Selector="^:success /template/ PathIcon#NotificationIcon">
<Setter Property="Foreground" Value="{DynamicResource NotificationCardSuccessIconForeground}" />
<Setter Property="Data" Value="{DynamicResource NotificationCardSuccessIconPathData}" />
</Style>
<Style Selector="^:warning /template/ PathIcon#NotificationIcon">
<Setter Property="Foreground" Value="{DynamicResource NotificationCardWarningIconForeground}" />
<Setter Property="Data" Value="{DynamicResource NotificationCardWarningIconPathData}" />
</Style>
<Style Selector="^:error /template/ PathIcon#NotificationIcon">
<Setter Property="Foreground" Value="{DynamicResource NotificationCardErrorIconForeground}" />
<Setter Property="Data" Value="{DynamicResource NotificationCardErrorIconPathData}" />
</Style>
<Style Selector="^.Light">
<Setter Property="Background" Value="{DynamicResource NotificationCardLightBackground}" />
<Style Selector="^:information /template/ Border#PART_RootBorder">
<Setter Property="BorderBrush" Value="{DynamicResource NotificationCardLightInformationBorderBrush}" />
<Setter Property="Background" Value="{DynamicResource NotificationCardLightInformationBackground}" />
</Style>
<Style Selector="^:success /template/ Border#PART_RootBorder">
<Setter Property="BorderBrush" Value="{DynamicResource NotificationCardLightSuccessBorderBrush}" />
<Setter Property="Background" Value="{DynamicResource NotificationCardLightSuccessBackground}" />
</Style>
<Style Selector="^:warning /template/ Border#PART_RootBorder">
<Setter Property="BorderBrush" Value="{DynamicResource NotificationCardLightWarningBorderBrush}" />
<Setter Property="Background" Value="{DynamicResource NotificationCardLightWarningBackground}" />
</Style>
<Style Selector="^:error /template/ Border#PART_RootBorder">
<Setter Property="BorderBrush" Value="{DynamicResource NotificationCardLightErrorBorderBrush}" />
<Setter Property="Background" Value="{DynamicResource NotificationCardLightErrorBackground}" />
</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="Foreground" Value="{DynamicResource NotificationCardCloseButtonForeground}" />
<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>

View File

@@ -0,0 +1,250 @@
<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">
<ReversibleStackPanel>
<u:ToastCard ShowIcon="False" ShowClose="False" />
<u:ToastCard />
<u:ToastCard>
Hello, Ursa!
</u:ToastCard>
<u:ToastCard NotificationType="Success" Classes="Light">
<u:Toast>
Hello, Ursa!
</u:Toast>
</u:ToastCard>
</ReversibleStackPanel>
</ThemeVariantScope>
</Design.PreviewWith>
<ControlTheme x:Key="{x:Type u:WindowToastManager}" TargetType="u:WindowToastManager">
<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:ToastCard}" TargetType="u:ToastCard">
<Setter Property="UseLayoutRounding" Value="True" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="BorderThickness" Value="{DynamicResource ToastCardBorderThickness}" />
<Setter Property="Background" Value="{DynamicResource ToastCardBackground}" />
<Setter Property="CornerRadius" Value="{DynamicResource ToastCardCornerRadius}" />
<Setter Property="Template">
<ControlTemplate TargetType="u:ToastCard">
<LayoutTransformControl x:Name="PART_LayoutTransformControl" UseRenderTransform="True">
<Border
Margin="{DynamicResource ToastCardMargin}"
Background="{TemplateBinding Background}"
CornerRadius="{TemplateBinding CornerRadius}">
<Border
x:Name="PART_RootBorder"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
MinHeight="{DynamicResource ToastCardMinHeight}"
Padding="{DynamicResource ToastCardPadding}"
VerticalAlignment="Top"
BoxShadow="{DynamicResource NotificationCardBoxShadows}"
CornerRadius="{TemplateBinding CornerRadius}">
<DockPanel>
<PathIcon
x:Name="ToastIcon"
Width="{DynamicResource ToastCardIconWidth}"
Height="{DynamicResource ToastCardIconHeight}"
Margin="{DynamicResource ToastCardIconMargin}"
VerticalAlignment="Top"
IsVisible="{TemplateBinding ShowIcon}"
Data="{DynamicResource NotificationCardInformationIconPathData}" />
<ContentControl
x:Name="PART_Content"
Margin="{DynamicResource ToastCardContentMargin}"
VerticalContentAlignment="Center"
MaxWidth="{DynamicResource ToastCardContentMaxWidth}"
Content="{TemplateBinding Content}">
<ContentControl.DataTemplates>
<DataTemplate DataType="u:IToast">
<SelectableTextBlock
Foreground="{DynamicResource ToastCardContentForeground}"
FontWeight="{DynamicResource ToastCardContentFontWeight}"
Text="{Binding Content}"
TextWrapping="Wrap" />
</DataTemplate>
<DataTemplate DataType="x:String">
<SelectableTextBlock
Foreground="{DynamicResource ToastCardContentForeground}"
FontWeight="{DynamicResource ToastCardContentFontWeight}"
Text="{Binding}"
TextWrapping="Wrap" />
</DataTemplate>
</ContentControl.DataTemplates>
</ContentControl>
<Button
x:Name="PART_CloseButton"
Theme="{StaticResource ToastCloseButton}"
VerticalAlignment="Top"
IsVisible="{TemplateBinding ShowClose}"
u:MessageCard.CloseOnClick="True" />
</DockPanel>
</Border>
</Border>
</LayoutTransformControl>
</ControlTemplate>
</Setter>
<Style Selector="^ /template/ LayoutTransformControl#PART_LayoutTransformControl">
<Style.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>
<Animation
Easing="QuadraticEaseIn"
FillMode="Forward"
Duration="0:0:0.15">
<KeyFrame Cue="0%">
<Setter Property="ScaleTransform.ScaleY" Value="0" />
</KeyFrame>
<KeyFrame Cue="70%">
<Setter Property="ScaleTransform.ScaleY" Value="0" />
</KeyFrame>
<KeyFrame Cue="100%">
<Setter Property="ScaleTransform.ScaleY" Value="1" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
<Style Selector="^[IsClosing=true] /template/ LayoutTransformControl#PART_LayoutTransformControl">
<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>
<Animation
Easing="QuadraticEaseOut"
FillMode="Forward"
Duration="0:0:0.15">
<KeyFrame Cue="0%">
<Setter Property="ScaleTransform.ScaleY" Value="1" />
</KeyFrame>
<KeyFrame Cue="70%">
<Setter Property="ScaleTransform.ScaleY" Value="1" />
</KeyFrame>
<KeyFrame Cue="100%">
<Setter Property="ScaleTransform.ScaleY" Value="0" />
</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#ToastIcon">
<Setter Property="Foreground" Value="{DynamicResource NotificationCardInformationIconForeground}" />
<Setter Property="Data" Value="{DynamicResource NotificationCardInformationIconPathData}" />
</Style>
<Style Selector="^:success /template/ PathIcon#ToastIcon">
<Setter Property="Foreground" Value="{DynamicResource NotificationCardSuccessIconForeground}" />
<Setter Property="Data" Value="{DynamicResource NotificationCardSuccessIconPathData}" />
</Style>
<Style Selector="^:warning /template/ PathIcon#ToastIcon">
<Setter Property="Foreground" Value="{DynamicResource NotificationCardWarningIconForeground}" />
<Setter Property="Data" Value="{DynamicResource NotificationCardWarningIconPathData}" />
</Style>
<Style Selector="^:error /template/ PathIcon#ToastIcon">
<Setter Property="Foreground" Value="{DynamicResource NotificationCardErrorIconForeground}" />
<Setter Property="Data" Value="{DynamicResource NotificationCardErrorIconPathData}" />
</Style>
<Style Selector="^.Light">
<Setter Property="Background" Value="{DynamicResource NotificationCardLightBackground}" />
<Style Selector="^:information /template/ Border#PART_RootBorder">
<Setter Property="BorderBrush" Value="{DynamicResource NotificationCardLightInformationBorderBrush}" />
<Setter Property="Background" Value="{DynamicResource NotificationCardLightInformationBackground}" />
</Style>
<Style Selector="^:success /template/ Border#PART_RootBorder">
<Setter Property="BorderBrush" Value="{DynamicResource NotificationCardLightSuccessBorderBrush}" />
<Setter Property="Background" Value="{DynamicResource NotificationCardLightSuccessBackground}" />
</Style>
<Style Selector="^:warning /template/ Border#PART_RootBorder">
<Setter Property="BorderBrush" Value="{DynamicResource NotificationCardLightWarningBorderBrush}" />
<Setter Property="Background" Value="{DynamicResource NotificationCardLightWarningBackground}" />
</Style>
<Style Selector="^:error /template/ Border#PART_RootBorder">
<Setter Property="BorderBrush" Value="{DynamicResource NotificationCardLightErrorBorderBrush}" />
<Setter Property="Background" Value="{DynamicResource NotificationCardLightErrorBackground}" />
</Style>
</Style>
</ControlTheme>
<ControlTheme x:Key="ToastCloseButton" 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>

View File

@@ -30,6 +30,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" />
@@ -47,6 +48,7 @@
<ResourceInclude Source="TreeComboBox.axaml"/>
<ResourceInclude Source="Skeleton.axaml" />
<ResourceInclude Source="TwoTonePathIcon.axaml" />
<ResourceInclude Source="Toast.axaml" />
<ResourceInclude Source="ToolBar.axaml" />
<ResourceInclude Source="TimeBox.axaml"/>
<ResourceInclude Source="UrsaView.axaml" />

View File

@@ -0,0 +1,15 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<BoxShadows x:Key="NotificationCardBoxShadows">inset 0 0 0 1 #1AFFFFFF, 0 4 14 0 #40000000</BoxShadows>
<SolidColorBrush x:Key="NotificationCardLightBackground" Color="#16161A" />
<SolidColorBrush x:Key="NotificationCardLightInformationBorderBrush" Color="#54A9FF" />
<SolidColorBrush x:Key="NotificationCardLightInformationBackground" Opacity="0.2" Color="#54A9FF" />
<SolidColorBrush x:Key="NotificationCardLightSuccessBorderBrush" Color="#5DC264" />
<SolidColorBrush x:Key="NotificationCardLightSuccessBackground" Opacity="0.2" Color="#5DC264" />
<SolidColorBrush x:Key="NotificationCardLightWarningBorderBrush" Color="#FFAE43" />
<SolidColorBrush x:Key="NotificationCardLightWarningBackground" Opacity="0.2" Color="#FFAE43" />
<SolidColorBrush x:Key="NotificationCardLightErrorBorderBrush" Color="#FC725A" />
<SolidColorBrush x:Key="NotificationCardLightErrorBackground" Opacity="0.2" Color="#FC725A" />
<SolidColorBrush x:Key="NotificationCardCloseButtonForeground" Opacity="0.8" Color="#F9F9F9" />
</ResourceDictionary>

View File

@@ -0,0 +1,5 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ToastCardContentForeground" Color="#F9F9F9" />
<SolidColorBrush x:Key="ToastCardBackground" Color="#43444A" />
</ResourceDictionary>

View File

@@ -12,10 +12,12 @@
<MergeResourceInclude Source="KeyGestureInput.axaml" />
<MergeResourceInclude Source="Loading.axaml" />
<MergeResourceInclude Source="NavigationMenu.axaml" />
<MergeResourceInclude Source="NotificationShared.axaml" />
<MergeResourceInclude Source="Pagination.axaml" />
<MergeResourceInclude Source="Rating.axaml" />
<MergeResourceInclude Source="TagInput.axaml" />
<MergeResourceInclude Source="Timeline.axaml" />
<MergeResourceInclude Source="Toast.axaml" />
<MergeResourceInclude Source="Skeleton.axaml" />
<MergeResourceInclude Source="TimeBox.axaml" />
</ResourceDictionary.MergedDictionaries>

View File

@@ -0,0 +1,15 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<BoxShadows x:Key="NotificationCardBoxShadows">0 0 1 0 #4A000000, 0 4 14 0 #1A000000</BoxShadows>
<SolidColorBrush x:Key="NotificationCardLightBackground" Color="White" />
<SolidColorBrush x:Key="NotificationCardLightInformationBorderBrush" Color="#0077FA" />
<SolidColorBrush x:Key="NotificationCardLightInformationBackground" Color="#EAF5FF" />
<SolidColorBrush x:Key="NotificationCardLightSuccessBorderBrush" Color="#3BB346" />
<SolidColorBrush x:Key="NotificationCardLightSuccessBackground" Color="#ECF7EC" />
<SolidColorBrush x:Key="NotificationCardLightWarningBorderBrush" Color="#FC8800" />
<SolidColorBrush x:Key="NotificationCardLightWarningBackground" Color="#FFF8EA" />
<SolidColorBrush x:Key="NotificationCardLightErrorBorderBrush" Color="#F93920" />
<SolidColorBrush x:Key="NotificationCardLightErrorBackground" Color="#FEF2ED" />
<SolidColorBrush x:Key="NotificationCardCloseButtonForeground" Opacity="0.8" Color="#1C1F23" />
</ResourceDictionary>

View File

@@ -0,0 +1,5 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ToastCardContentForeground" Color="#1C1F23" />
<SolidColorBrush x:Key="ToastCardBackground" Color="White" />
</ResourceDictionary>

View File

@@ -12,10 +12,12 @@
<MergeResourceInclude Source="KeyGestureInput.axaml" />
<MergeResourceInclude Source="Loading.axaml" />
<MergeResourceInclude Source="NavigationMenu.axaml" />
<MergeResourceInclude Source="NotificationShared.axaml" />
<MergeResourceInclude Source="Pagination.axaml" />
<MergeResourceInclude Source="Rating.axaml" />
<MergeResourceInclude Source="TagInput.axaml" />
<MergeResourceInclude Source="Timeline.axaml" />
<MergeResourceInclude Source="Toast.axaml" />
<MergeResourceInclude Source="Skeleton.axaml" />
<MergeResourceInclude Source="TimeBox.axaml" />
</ResourceDictionary.MergedDictionaries>

View File

@@ -0,0 +1,3 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Thickness x:Key="NotificationCardPadding">20 16 12 16</Thickness>
</ResourceDictionary>

View File

@@ -0,0 +1,17 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<x:Double x:Key="ToastCardMinHeight">40</x:Double>
<Thickness x:Key="ToastCardBorderThickness">1</Thickness>
<CornerRadius x:Key="ToastCardCornerRadius">6</CornerRadius>
<Thickness x:Key="ToastCardMargin">12</Thickness>
<Thickness x:Key="ToastCardPadding">12 8</Thickness>
<x:Double x:Key="ToastCardIconWidth">18</x:Double>
<x:Double x:Key="ToastCardIconHeight">18</x:Double>
<Thickness x:Key="ToastCardIconMargin">0 2 0 0</Thickness>
<FontWeight x:Key="ToastCardContentFontWeight">600</FontWeight>
<Thickness x:Key="ToastCardContentMargin">12 0</Thickness>
<x:Double x:Key="ToastCardContentMaxWidth">450</x:Double>
</ResourceDictionary>

View File

@@ -15,12 +15,14 @@
<MergeResourceInclude Source="KeyGestureInput.axaml" />
<MergeResourceInclude Source="MessageBox.axaml" />
<MergeResourceInclude Source="NavigationMenu.axaml" />
<MergeResourceInclude Source="Notification.axaml" />
<MergeResourceInclude Source="Pagination.axaml" />
<MergeResourceInclude Source="Rating.axaml" />
<MergeResourceInclude Source="ScrollToButton.axaml" />
<MergeResourceInclude Source="TagInput.axaml" />
<MergeResourceInclude Source="Skeleton.axaml" />
<MergeResourceInclude Source="ThemeSelector.axaml" />
<MergeResourceInclude Source="Toast.axaml" />
<MergeResourceInclude Source="ToolBar.axaml" />
<MergeResourceInclude Source="TimeBox.axaml" />
</ResourceDictionary.MergedDictionaries>

View 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; }
}

View 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);
}

View File

@@ -0,0 +1,108 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Avalonia.Controls.Notifications;
using Avalonia.Metadata;
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/>
[Content]
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 ShowIcon { 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));
}
}

View File

@@ -0,0 +1,49 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Notifications;
using Avalonia.LogicalTree;
namespace Ursa.Controls;
/// <summary>
/// Control that represents and displays a notification.
/// </summary>
[PseudoClasses(
WindowNotificationManager.PC_TopLeft,
WindowNotificationManager.PC_TopRight,
WindowNotificationManager.PC_BottomLeft,
WindowNotificationManager.PC_BottomRight,
WindowNotificationManager.PC_TopCenter,
WindowNotificationManager.PC_BottomCenter
)]
public class NotificationCard : MessageCard
{
private NotificationPosition _position;
public NotificationPosition Position
{
get => _position;
set => SetAndRaise(PositionProperty, ref _position, value);
}
public static readonly DirectProperty<NotificationCard, NotificationPosition> PositionProperty =
AvaloniaProperty.RegisterDirect<NotificationCard, NotificationPosition>(nameof(Position),
o => o.Position, (o, v) => o.Position = v);
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
{
base.OnAttachedToLogicalTree(e);
UpdatePseudoClasses(Position);
}
private void UpdatePseudoClasses(NotificationPosition position)
{
PseudoClasses.Set(WindowNotificationManager.PC_TopLeft, position == NotificationPosition.TopLeft);
PseudoClasses.Set(WindowNotificationManager.PC_TopRight, position == NotificationPosition.TopRight);
PseudoClasses.Set(WindowNotificationManager.PC_BottomLeft, position == NotificationPosition.BottomLeft);
PseudoClasses.Set(WindowNotificationManager.PC_BottomRight, position == NotificationPosition.BottomRight);
PseudoClasses.Set(WindowNotificationManager.PC_TopCenter, position == NotificationPosition.TopCenter);
PseudoClasses.Set(WindowNotificationManager.PC_BottomCenter, position == NotificationPosition.BottomCenter);
}
}

View File

@@ -0,0 +1,178 @@
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.ShowIcon, 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.ShowIcon, 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="showIcon">whether to show the icon</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 showIcon = true,
bool showClose = true,
Action? onClick = null,
Action? onClose = null,
string[]? classes = null)
{
Dispatcher.UIThread.VerifyAccess();
var notificationControl = new NotificationCard
{
Content = content,
NotificationType = type,
ShowIcon = showIcon,
ShowClose = showClose,
[!NotificationCard.PositionProperty] = this[!PositionProperty]
};
// 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);
}
}

View File

@@ -0,0 +1,40 @@
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 an icon.
/// </summary>
bool ShowIcon { 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; }
}

View File

@@ -0,0 +1,216 @@
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 ShowIcon
{
get => GetValue(ShowIconProperty);
set => SetValue(ShowIconProperty, value);
}
public static readonly StyledProperty<bool> ShowIconProperty =
AvaloniaProperty.Register<MessageCard, bool>(nameof(ShowIcon), true);
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;
}
}
}

View File

@@ -0,0 +1,89 @@
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);
}
}
public virtual void Uninstall()
{
if (Parent is AdornerLayer adornerLayer)
{
adornerLayer.Children.Remove(this);
AdornerLayer.SetAdornedElement(this, null);
}
}
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);
}
}

View File

@@ -0,0 +1,12 @@
namespace Ursa.Controls;
/// <summary>
/// Represents a toast that can be shown in a window or by the host operating system.
/// </summary>
public interface IToast : IMessage
{
/// <summary>
/// Gets the toast message.
/// </summary>
string? Content { get; }
}

View File

@@ -0,0 +1,17 @@
using Avalonia.Metadata;
namespace Ursa.Controls;
/// <summary>
/// Represents a toast manager that can be used to show toasts in a window or using
/// the host operating system.
/// </summary>
[NotClientImplementable]
public interface IToastManager
{
/// <summary>
/// Show a toast.
/// </summary>
/// <param name="toast">The toast to be displayed.</param>
void Show(IToast toast);
}

View File

@@ -0,0 +1,91 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Avalonia.Controls.Notifications;
using Avalonia.Metadata;
namespace Ursa.Controls;
/// <summary>
/// A toast that can be shown in a window or by the host operating system.
/// </summary>
/// <remarks>
/// This class represents a toast that can be displayed either in a window using
/// <see cref="WindowToastManager"/> or by the host operating system (to be implemented).
/// </remarks>
public class Toast : IToast, INotifyPropertyChanged
{
private string? _content;
/// <summary>
/// Initializes a new instance of the <see cref="Toast"/> class.
/// </summary>
/// <param name="content">The content to be displayed in the toast.</param>
/// <param name="type">The <see cref="NotificationType"/> of the toast.</param>
/// <param name="expiration">The expiry time at which the toast will close.
/// Use <see cref="TimeSpan.Zero"/> for toasts that will remain open.</param>
/// <param name="showClose">A value indicating whether the toast should show a close button.</param>
/// <param name="onClick">An Action to call when the toast is clicked.</param>
/// <param name="onClose">An Action to call when the toast is closed.</param>
public Toast(
string? content,
NotificationType type = NotificationType.Information,
TimeSpan? expiration = null,
bool showClose = true,
Action? onClick = null,
Action? onClose = null)
{
Content = content;
Type = type;
Expiration = expiration ?? TimeSpan.FromSeconds(3);
ShowClose = showClose;
OnClick = onClick;
OnClose = onClose;
}
/// <summary>
/// Initializes a new instance of the <see cref="Toast"/> class.
/// </summary>
public Toast() : this(null)
{
}
/// <inheritdoc/>
[Content]
public string? Content
{
get => _content;
set
{
if (_content != value)
{
_content = value;
OnPropertyChanged();
}
}
}
/// <inheritdoc/>
public NotificationType Type { get; set; }
/// <inheritdoc/>
public bool ShowIcon { get; set; }
/// <inheritdoc/>
public bool ShowClose { get; set; }
/// <inheritdoc/>
public TimeSpan Expiration { get; set; }
/// <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));
}
}

View File

@@ -0,0 +1,6 @@
namespace Ursa.Controls;
/// <summary>
/// Control that represents and displays a toast.
/// </summary>
public class ToastCard : MessageCard;

View File

@@ -0,0 +1,122 @@
using Avalonia.Controls;
using Avalonia.Controls.Notifications;
using Avalonia.Threading;
namespace Ursa.Controls;
/// <summary>
/// An <see cref="IToastManager"/> that displays toasts in a <see cref="Window"/>.
/// </summary>
public class WindowToastManager : WindowMessageManager, IToastManager
{
/// <summary>
/// Initializes a new instance of the <see cref="WindowToastManager"/> class.
/// </summary>
/// <param name="host">The TopLevel that will host the control.</param>
public WindowToastManager(TopLevel? host) : this()
{
if (host is not null)
{
InstallFromTopLevel(host);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="WindowToastManager"/> class.
/// </summary>
public WindowToastManager()
{
}
/// <inheritdoc/>
public void Show(IToast content)
{
Show(content, content.Type, content.Expiration,
content.ShowIcon, content.ShowClose,
content.OnClick, content.OnClose);
}
/// <inheritdoc/>
public override void Show(object content)
{
if (content is IToast toast)
{
Show(toast, toast.Type, toast.Expiration,
toast.ShowIcon, toast.ShowClose,
toast.OnClick, toast.OnClose);
}
else
{
Show(content, NotificationType.Information);
}
}
/// <summary>
/// Shows a Toast
/// </summary>
/// <param name="content">the content 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="showIcon">whether to show the icon</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="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,
NotificationType type,
TimeSpan? expiration = null,
bool showIcon = true,
bool showClose = true,
Action? onClick = null,
Action? onClose = null,
string[]? classes = null)
{
Dispatcher.UIThread.VerifyAccess();
var toastControl = new ToastCard
{
Content = content,
NotificationType = type,
ShowIcon = showIcon,
ShowClose = showClose
};
// Add style classes if any
if (classes is not null)
{
foreach (var @class in classes)
{
toastControl.Classes.Add(@class);
}
}
toastControl.MessageClosed += (sender, _) =>
{
onClose?.Invoke();
_items?.Remove(sender);
};
toastControl.PointerPressed += (_, _) => { onClick?.Invoke(); };
Dispatcher.UIThread.Post(() =>
{
_items?.Add(toastControl);
if (_items?.OfType<ToastCard>().Count(i => !i.IsClosing) > MaxItems)
{
_items.OfType<ToastCard>().First(i => !i.IsClosing).Close();
}
});
if (expiration == TimeSpan.Zero)
{
return;
}
await Task.Delay(expiration ?? TimeSpan.FromSeconds(3));
toastControl.Close();
}
}