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!"));
}
}