From 962ed29bb6f68f7c30dc21f9d43468b3059a330d Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 25 Apr 2019 21:48:26 +0100 Subject: [PATCH] remove need for seperate NotificationManager class. --- .../ViewModels/MainWindowViewModel.cs | 12 +++-- .../ViewModels/NotificationViewModel.cs | 3 +- .../Notifications/INotificationManager.cs | 2 +- .../Notifications/NotificationArea.cs | 8 +-- .../Notifications/NotificationManager.cs | 53 ------------------- src/Avalonia.Controls/TopLevel.cs | 18 ++++++- 6 files changed, 31 insertions(+), 65 deletions(-) delete mode 100644 src/Avalonia.Controls/Notifications/NotificationManager.cs diff --git a/samples/ControlCatalog/ViewModels/MainWindowViewModel.cs b/samples/ControlCatalog/ViewModels/MainWindowViewModel.cs index 5335d1cacc..a514b4d621 100644 --- a/samples/ControlCatalog/ViewModels/MainWindowViewModel.cs +++ b/samples/ControlCatalog/ViewModels/MainWindowViewModel.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using System.Threading.Tasks; +using Avalonia; using Avalonia.Controls.Notifications; using Avalonia.Threading; @@ -15,19 +16,20 @@ namespace ControlCatalog.ViewModels { await Task.Delay(5000); - NotificationManager.Instance.Show(new NotificationViewModel { Title = "Warning", Message = "Please save your work before closing." }, "Main"); + + Application.Current.MainWindow.LocalNotificationManager.Show(new NotificationViewModel { Title = "Warning", Message = "Please save your work before closing." }); await Task.Delay(1500); - NotificationManager.Instance.Show(new NotificationContent { Message = "Test2", Type = NotificationType.Error }, "Main"); + Application.Current.MainWindow.LocalNotificationManager.Show(new NotificationContent { Message = "Test2", Type = NotificationType.Error }); await Task.Delay(2000); - NotificationManager.Instance.Show(new NotificationContent { Message = "Test3", Type = NotificationType.Warning }, "Main"); + Application.Current.MainWindow.LocalNotificationManager.Show(new NotificationContent { Message = "Test3", Type = NotificationType.Warning }); await Task.Delay(2500); - NotificationManager.Instance.Show(new NotificationContent { Message = "Test4", Type = NotificationType.Success }, "Main"); + Application.Current.MainWindow.LocalNotificationManager.Show(new NotificationContent { Message = "Test4", Type = NotificationType.Success }); await Task.Delay(500); - NotificationManager.Instance.Show("Test5", "Main"); + Application.Current.MainWindow.LocalNotificationManager.Show("Test5"); }); } diff --git a/samples/ControlCatalog/ViewModels/NotificationViewModel.cs b/samples/ControlCatalog/ViewModels/NotificationViewModel.cs index c444147d1f..8a82a82447 100644 --- a/samples/ControlCatalog/ViewModels/NotificationViewModel.cs +++ b/samples/ControlCatalog/ViewModels/NotificationViewModel.cs @@ -1,4 +1,5 @@ using System.Reactive; +using Avalonia; using Avalonia.Controls.Notifications; using ReactiveUI; @@ -10,7 +11,7 @@ namespace ControlCatalog.ViewModels { OKCommand = ReactiveCommand.Create(() => { - NotificationManager.Instance.Show("Notification Accepted", "Main"); + Application.Current.MainWindow.LocalNotificationManager.Show("Notification Accepted"); }); } diff --git a/src/Avalonia.Controls/Notifications/INotificationManager.cs b/src/Avalonia.Controls/Notifications/INotificationManager.cs index 3ea690388d..4de8cb8067 100644 --- a/src/Avalonia.Controls/Notifications/INotificationManager.cs +++ b/src/Avalonia.Controls/Notifications/INotificationManager.cs @@ -4,6 +4,6 @@ namespace Avalonia.Controls.Notifications { public interface INotificationManager { - void Show(object content, string areaName = "", TimeSpan? expirationTime = null, Action onClick = null, Action onClose = null); + void Show(object content, TimeSpan? expirationTime = null, Action onClick = null, Action onClose = null); } } diff --git a/src/Avalonia.Controls/Notifications/NotificationArea.cs b/src/Avalonia.Controls/Notifications/NotificationArea.cs index eb71f840bc..c641e09b02 100644 --- a/src/Avalonia.Controls/Notifications/NotificationArea.cs +++ b/src/Avalonia.Controls/Notifications/NotificationArea.cs @@ -7,7 +7,7 @@ using Avalonia.Interactivity; namespace Avalonia.Controls.Notifications { - public class NotificationArea : TemplatedControl + public class NotificationArea : TemplatedControl, INotificationManager { private IList _items; @@ -31,7 +31,6 @@ namespace Avalonia.Controls.Notifications public NotificationArea() { - NotificationManager.AddArea(this); } static NotificationArea() @@ -50,7 +49,7 @@ namespace Avalonia.Controls.Notifications _items = itemsControl?.Children; } - public async void Show(object content, TimeSpan expirationTime, Action onClick, Action onClose) + public async void Show(object content, TimeSpan? expirationTime, Action onClick, Action onClose) { var notification = new Notification { @@ -82,7 +81,8 @@ namespace Avalonia.Controls.Notifications { return; } - await Task.Delay(expirationTime); + + await Task.Delay(expirationTime ?? TimeSpan.FromSeconds(5)); notification.Close(); } diff --git a/src/Avalonia.Controls/Notifications/NotificationManager.cs b/src/Avalonia.Controls/Notifications/NotificationManager.cs deleted file mode 100644 index 6de3149e5b..0000000000 --- a/src/Avalonia.Controls/Notifications/NotificationManager.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Avalonia.Threading; - -namespace Avalonia.Controls.Notifications -{ - public class NotificationManager : INotificationManager - { - private static readonly List Areas = new List(); - private static Window _window; - - public static NotificationManager Instance = new NotificationManager(); - - public void Show(object content, string areaName = "", TimeSpan? expirationTime = null, Action onClick = null, - Action onClose = null) - { - if (!Dispatcher.UIThread.CheckAccess()) - { - Dispatcher.UIThread.Post( - new Action(() => Show(content, areaName, expirationTime, onClick, onClose))); - return; - } - - if (expirationTime == null) - expirationTime = TimeSpan.FromSeconds(5); - - if (areaName == string.Empty && _window == null) - { - var workArea = Application.Current.MainWindow.Screens.Primary.WorkingArea; - - _window = new Window - { - Position = workArea.Position, - Width = workArea.Width, - Height = workArea.Height - }; - - _window.Show(); - } - - foreach (var area in Areas.Where(a => a.Name == areaName)) - { - area.Show(content, (TimeSpan)expirationTime, onClick, onClose); - } - } - - internal static void AddArea(NotificationArea area) - { - Areas.Add(area); - } - } -} diff --git a/src/Avalonia.Controls/TopLevel.cs b/src/Avalonia.Controls/TopLevel.cs index 732363466a..475fc5a927 100644 --- a/src/Avalonia.Controls/TopLevel.cs +++ b/src/Avalonia.Controls/TopLevel.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; +using System.Linq; using System.Reactive.Linq; using Avalonia.Controls.Notifications; using Avalonia.Controls.Primitives; @@ -103,7 +104,7 @@ namespace Avalonia.Controls PlatformImpl = impl; - LocalNotificationManager = new NotificationManager(); + LocalNotificationManager = new NotificationArea(); dependencyResolver = dependencyResolver ?? AvaloniaLocator.Current; var styler = TryGetService(dependencyResolver); @@ -414,5 +415,20 @@ namespace Avalonia.Controls { (this as IInputRoot).MouseDevice.SceneInvalidated(this, e.DirtyRect); } + + protected override void OnTemplateApplied(TemplateAppliedEventArgs e) + { + var adornerLayer = this.GetVisualDescendants() + .OfType() + .FirstOrDefault() + ?.AdornerLayer; + + if (adornerLayer != null) + { + adornerLayer.Children.Add(LocalNotificationManager as IControl); + } + + base.OnTemplateApplied(e); + } } }