From f52d62f9f532e343c1f0db8f95f7242321562b93 Mon Sep 17 00:00:00 2001
From: Zhang Dian <54255897+zdpcdt@users.noreply.github.com>
Date: Thu, 5 Sep 2024 03:30:02 +0800
Subject: [PATCH] feat: add close button.
---
demo/Ursa.Demo/Pages/ToastDemo.axaml | 1 +
.../ViewModels/ToastDemoViewModel.cs | 5 ++
src/Ursa.Themes.Semi/Controls/Toast.axaml | 61 +++++++++++++------
src/Ursa/Controls/Toast/IToast.cs | 5 ++
src/Ursa/Controls/Toast/Toast.cs | 5 ++
src/Ursa/Controls/Toast/ToastCard.cs | 8 +++
src/Ursa/Controls/Toast/WindowToastManager.cs | 25 +++-----
7 files changed, 76 insertions(+), 34 deletions(-)
diff --git a/demo/Ursa.Demo/Pages/ToastDemo.axaml b/demo/Ursa.Demo/Pages/ToastDemo.axaml
index 4085f09..1a4a1db 100644
--- a/demo/Ursa.Demo/Pages/ToastDemo.axaml
+++ b/demo/Ursa.Demo/Pages/ToastDemo.axaml
@@ -12,6 +12,7 @@
+
diff --git a/demo/Ursa.Demo/ViewModels/ToastDemoViewModel.cs b/demo/Ursa.Demo/ViewModels/ToastDemoViewModel.cs
index 97be1d8..9b97e19 100644
--- a/demo/Ursa.Demo/ViewModels/ToastDemoViewModel.cs
+++ b/demo/Ursa.Demo/ViewModels/ToastDemoViewModel.cs
@@ -10,6 +10,9 @@ public partial class ToastDemoViewModel : ObservableObject
{
public WindowToastManager? ToastManager { get; set; }
+ [ObservableProperty]
+ private bool _showClose = true;
+
[RelayCommand]
public void ShowNormal(object obj)
{
@@ -18,6 +21,7 @@ public partial class ToastDemoViewModel : ObservableObject
Enum.TryParse(s, out var notificationType);
ToastManager?.Show(
new Toast("This is message"),
+ showClose: ShowClose,
type: notificationType);
}
@@ -36,6 +40,7 @@ public partial class ToastDemoViewModel : ObservableObject
Enum.TryParse(s, out var notificationType);
ToastManager?.Show(
new Toast("This is message"),
+ showClose: ShowClose,
type: notificationType,
classes: ["Light"]);
}
diff --git a/src/Ursa.Themes.Semi/Controls/Toast.axaml b/src/Ursa.Themes.Semi/Controls/Toast.axaml
index 12af584..87a2958 100644
--- a/src/Ursa.Themes.Semi/Controls/Toast.axaml
+++ b/src/Ursa.Themes.Semi/Controls/Toast.axaml
@@ -3,17 +3,12 @@
xmlns:u="https://irihi.tech/ursa">
-
+
+
Hello, Semi.Avalonia!
-
-
-
-
-
-
-
+
@@ -35,7 +30,6 @@
-
@@ -53,40 +47,44 @@
Padding="{DynamicResource NotificationCardPadding}"
BoxShadow="{DynamicResource NotificationCardBoxShadow}"
BorderBrush="{TemplateBinding BorderBrush}"
+ VerticalAlignment="Stretch"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}">
-
+
-
+
+
@@ -193,4 +191,31 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/Ursa/Controls/Toast/IToast.cs b/src/Ursa/Controls/Toast/IToast.cs
index 1a9f7eb..107a6d0 100644
--- a/src/Ursa/Controls/Toast/IToast.cs
+++ b/src/Ursa/Controls/Toast/IToast.cs
@@ -20,6 +20,11 @@ public interface IToast
///
NotificationType Type { get; }
+ ///
+ /// Gets a value indicating whether the toast should show a close button.
+ ///
+ bool ShowClose { get; }
+
///
/// Gets the expiration time of the toast after which it will automatically close.
/// If the value is then the toast will remain open until the user closes it.
diff --git a/src/Ursa/Controls/Toast/Toast.cs b/src/Ursa/Controls/Toast/Toast.cs
index b4267fd..1ec0acd 100644
--- a/src/Ursa/Controls/Toast/Toast.cs
+++ b/src/Ursa/Controls/Toast/Toast.cs
@@ -22,12 +22,14 @@ public class Toast : IToast, INotifyPropertyChanged
/// The of the toast.
/// The expiry time at which the toast will close.
/// Use for toasts that will remain open.
+ /// A value indicating whether the toast should show a close button.
/// An Action to call when the toast is clicked.
/// An Action to call when the toast is closed.
public Toast(
string? content,
NotificationType type = NotificationType.Information,
TimeSpan? expiration = null,
+ bool showClose = true,
Action? onClick = null,
Action? onClose = null)
{
@@ -62,6 +64,9 @@ public class Toast : IToast, INotifyPropertyChanged
///
public NotificationType Type { get; set; }
+ ///
+ public bool ShowClose { get; set; }
+
///
public TimeSpan Expiration { get; set; }
diff --git a/src/Ursa/Controls/Toast/ToastCard.cs b/src/Ursa/Controls/Toast/ToastCard.cs
index 2542d33..b9084a3 100644
--- a/src/Ursa/Controls/Toast/ToastCard.cs
+++ b/src/Ursa/Controls/Toast/ToastCard.cs
@@ -77,6 +77,14 @@ public class ToastCard : ContentControl
///
public static readonly StyledProperty NotificationTypeProperty =
AvaloniaProperty.Register(nameof(NotificationType));
+ public bool ShowClose
+ {
+ get => GetValue(ShowCloseProperty);
+ set => SetValue(ShowCloseProperty, value);
+ }
+
+ public static readonly StyledProperty ShowCloseProperty =
+ AvaloniaProperty.Register(nameof(ShowClose), true);
///
/// Defines the event.
diff --git a/src/Ursa/Controls/Toast/WindowToastManager.cs b/src/Ursa/Controls/Toast/WindowToastManager.cs
index a07d952..5d803a6 100644
--- a/src/Ursa/Controls/Toast/WindowToastManager.cs
+++ b/src/Ursa/Controls/Toast/WindowToastManager.cs
@@ -70,7 +70,7 @@ public class WindowToastManager : TemplatedControl, IManagedToastManager
///
public void Show(IToast content)
{
- Show(content, content.Type, content.Expiration, content.OnClick, content.OnClose);
+ Show(content, content.Type, content.Expiration, content.ShowClose, content.OnClick, content.OnClose);
}
///
@@ -78,7 +78,7 @@ public class WindowToastManager : TemplatedControl, IManagedToastManager
{
if (content is IToast toast)
{
- Show(toast, toast.Type, toast.Expiration, toast.OnClick, toast.OnClose);
+ Show(toast, toast.Type, toast.Expiration, toast.ShowClose, toast.OnClick, toast.OnClose);
}
else
{
@@ -92,12 +92,14 @@ public class WindowToastManager : TemplatedControl, IManagedToastManager
/// the content of the toast
/// the type of the toast
/// 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
+ /// whether to show the close button
/// an Action to be run when the toast is clicked
/// an Action to be run when the toast is closed
/// style classes to apply
public async void Show(object content,
NotificationType type,
TimeSpan? expiration = null,
+ bool showClose = true,
Action? onClick = null,
Action? onClose = null,
string[]? classes = null)
@@ -107,11 +109,12 @@ public class WindowToastManager : TemplatedControl, IManagedToastManager
var toastControl = new ToastCard
{
Content = content,
- NotificationType = type
+ NotificationType = type,
+ ShowClose = showClose
};
// Add style classes if any
- if (classes != null)
+ if (classes is not null)
{
foreach (var @class in classes)
{
@@ -119,19 +122,14 @@ public class WindowToastManager : TemplatedControl, IManagedToastManager
}
}
- toastControl.ToastClosed += (sender, args) =>
+ toastControl.ToastClosed += (sender, _) =>
{
onClose?.Invoke();
_items?.Remove(sender);
};
- toastControl.PointerPressed += (sender, args) =>
- {
- onClick?.Invoke();
-
- (sender as ToastCard)?.Close();
- };
+ toastControl.PointerPressed += (_, _) => { onClick?.Invoke(); };
Dispatcher.UIThread.Post(() =>
{
@@ -153,11 +151,6 @@ public class WindowToastManager : TemplatedControl, IManagedToastManager
toastControl.Close();
}
- protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
- {
- base.OnPropertyChanged(change);
- }
-
///
/// Installs the within the
///