From 7ce869475ed1aa3875ae6be4bad2db7885a799a1 Mon Sep 17 00:00:00 2001 From: Emmanuel Hansen Date: Tue, 25 Oct 2022 12:02:08 +0000 Subject: [PATCH] add notifcationmanager property to toplevel --- .../Pages/NotificationsPage.xaml.cs | 15 ++++++++++ .../ViewModels/MainWindowViewModel.cs | 30 ------------------- .../ViewModels/NotificationViewModel.cs | 29 ++++++++++++++++-- .../WindowNotificationManager.cs | 7 ++--- src/Avalonia.Controls/TopLevel.cs | 5 ++++ 5 files changed, 49 insertions(+), 37 deletions(-) diff --git a/samples/ControlCatalog/Pages/NotificationsPage.xaml.cs b/samples/ControlCatalog/Pages/NotificationsPage.xaml.cs index eadf92b602..be08af41e1 100644 --- a/samples/ControlCatalog/Pages/NotificationsPage.xaml.cs +++ b/samples/ControlCatalog/Pages/NotificationsPage.xaml.cs @@ -1,18 +1,33 @@ +using Avalonia; using Avalonia.Controls; using Avalonia.Markup.Xaml; +using ControlCatalog.ViewModels; namespace ControlCatalog.Pages { public class NotificationsPage : UserControl { + private NotificationViewModel _viewModel; + public NotificationsPage() { this.InitializeComponent(); + + _viewModel = new NotificationViewModel(); + + DataContext = _viewModel; } private void InitializeComponent() { AvaloniaXamlLoader.Load(this); } + + protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) + { + base.OnAttachedToVisualTree(e); + + _viewModel.NotificationManager = (VisualRoot as TopLevel)?.NotificationManager; + } } } diff --git a/samples/ControlCatalog/ViewModels/MainWindowViewModel.cs b/samples/ControlCatalog/ViewModels/MainWindowViewModel.cs index c2c2462246..72ba6cead0 100644 --- a/samples/ControlCatalog/ViewModels/MainWindowViewModel.cs +++ b/samples/ControlCatalog/ViewModels/MainWindowViewModel.cs @@ -26,23 +26,6 @@ namespace ControlCatalog.ViewModels public MainWindowViewModel(IManagedNotificationManager notificationManager) { - _notificationManager = notificationManager; - - ShowCustomManagedNotificationCommand = MiniCommand.Create(() => - { - NotificationManager.Show(new NotificationViewModel(NotificationManager) { Title = "Hey There!", Message = "Did you know that Avalonia now supports Custom In-Window Notifications?" }); - }); - - ShowManagedNotificationCommand = MiniCommand.Create(() => - { - NotificationManager.Show(new Avalonia.Controls.Notifications.Notification("Welcome", "Avalonia now supports Notifications.", NotificationType.Information)); - }); - - ShowNativeNotificationCommand = MiniCommand.Create(() => - { - NotificationManager.Show(new Avalonia.Controls.Notifications.Notification("Error", "Native Notifications are not quite ready. Coming soon.", NotificationType.Error)); - }); - AboutCommand = MiniCommand.CreateFromTask(async () => { var dialog = new AboutAvaloniaDialog(); @@ -52,7 +35,6 @@ namespace ControlCatalog.ViewModels await dialog.ShowDialog(mainWindow); } }); - ExitCommand = MiniCommand.Create(() => { (App.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.Shutdown(); @@ -143,24 +125,12 @@ namespace ControlCatalog.ViewModels set { this.RaiseAndSetIfChanged(ref _windowStates, value); } } - public IManagedNotificationManager NotificationManager - { - get { return _notificationManager; } - set { this.RaiseAndSetIfChanged(ref _notificationManager, value); } - } - public bool IsMenuItemChecked { get { return _isMenuItemChecked; } set { this.RaiseAndSetIfChanged(ref _isMenuItemChecked, value); } } - public MiniCommand ShowCustomManagedNotificationCommand { get; } - - public MiniCommand ShowManagedNotificationCommand { get; } - - public MiniCommand ShowNativeNotificationCommand { get; } - public MiniCommand AboutCommand { get; } public MiniCommand ExitCommand { get; } diff --git a/samples/ControlCatalog/ViewModels/NotificationViewModel.cs b/samples/ControlCatalog/ViewModels/NotificationViewModel.cs index b714c319a6..a31f164a2a 100644 --- a/samples/ControlCatalog/ViewModels/NotificationViewModel.cs +++ b/samples/ControlCatalog/ViewModels/NotificationViewModel.cs @@ -6,16 +6,33 @@ namespace ControlCatalog.ViewModels { public class NotificationViewModel { - public NotificationViewModel(INotificationManager manager) + public WindowNotificationManager? NotificationManager { get; set; } + + public NotificationViewModel() { + ShowCustomManagedNotificationCommand = MiniCommand.Create(() => + { + NotificationManager?.Show(new NotificationViewModel() { Title = "Hey There!", Message = "Did you know that Avalonia now supports Custom In-Window Notifications?" , NotificationManager = NotificationManager}); + }); + + ShowManagedNotificationCommand = MiniCommand.Create(() => + { + NotificationManager?.Show(new Avalonia.Controls.Notifications.Notification("Welcome", "Avalonia now supports Notifications.", NotificationType.Information)); + }); + + ShowNativeNotificationCommand = MiniCommand.Create(() => + { + NotificationManager?.Show(new Avalonia.Controls.Notifications.Notification("Error", "Native Notifications are not quite ready. Coming soon.", NotificationType.Error)); + }); + YesCommand = MiniCommand.Create(() => { - manager.Show(new Avalonia.Controls.Notifications.Notification("Avalonia Notifications", "Start adding notifications to your app today.")); + NotificationManager?.Show(new Avalonia.Controls.Notifications.Notification("Avalonia Notifications", "Start adding notifications to your app today.")); }); NoCommand = MiniCommand.Create(() => { - manager.Show(new Avalonia.Controls.Notifications.Notification("Avalonia Notifications", "Start adding notifications to your app today. To find out more visit...")); + NotificationManager?.Show(new Avalonia.Controls.Notifications.Notification("Avalonia Notifications", "Start adding notifications to your app today. To find out more visit...")); }); } @@ -26,5 +43,11 @@ namespace ControlCatalog.ViewModels public MiniCommand NoCommand { get; } + public MiniCommand ShowCustomManagedNotificationCommand { get; } + + public MiniCommand ShowManagedNotificationCommand { get; } + + public MiniCommand ShowNativeNotificationCommand { get; } + } } diff --git a/src/Avalonia.Controls/Notifications/WindowNotificationManager.cs b/src/Avalonia.Controls/Notifications/WindowNotificationManager.cs index 630276767a..969b0a38b1 100644 --- a/src/Avalonia.Controls/Notifications/WindowNotificationManager.cs +++ b/src/Avalonia.Controls/Notifications/WindowNotificationManager.cs @@ -3,11 +3,10 @@ using System.Collections; using System.Linq; using System.Reactive.Linq; using System.Threading.Tasks; +using Avalonia.Controls.Metadata; using Avalonia.Controls.Primitives; using Avalonia.Rendering; -using Avalonia.Data; using Avalonia.VisualTree; -using Avalonia.Controls.Metadata; namespace Avalonia.Controls.Notifications { @@ -55,7 +54,7 @@ namespace Avalonia.Controls.Notifications /// Initializes a new instance of the class. /// /// The window that will host the control. - public WindowNotificationManager(Window host) + public WindowNotificationManager(TopLevel host) { if (VisualChildren.Count != 0) { @@ -151,7 +150,7 @@ namespace Avalonia.Controls.Notifications /// of the host . /// /// The that will be the host. - private void Install(Window host) + private void Install(TemplatedControl host) { var adornerLayer = host.FindDescendantOfType()?.AdornerLayer; diff --git a/src/Avalonia.Controls/TopLevel.cs b/src/Avalonia.Controls/TopLevel.cs index 47fc9d7988..f91d58ea13 100644 --- a/src/Avalonia.Controls/TopLevel.cs +++ b/src/Avalonia.Controls/TopLevel.cs @@ -1,6 +1,7 @@ using System; using System.Reactive.Linq; using Avalonia.Controls.Metadata; +using Avalonia.Controls.Notifications; using Avalonia.Controls.Platform; using Avalonia.Controls.Primitives; using Avalonia.Input; @@ -95,6 +96,7 @@ namespace Avalonia.Controls private Border? _transparencyFallbackBorder; private TargetWeakEventSubscriber? _resourcesChangesSubscriber; private IStorageProvider? _storageProvider; + private WindowNotificationManager _notificationManager; /// /// Initializes static members of the class. @@ -151,6 +153,7 @@ namespace Avalonia.Controls _keyboardNavigationHandler = TryGetService(dependencyResolver); _renderInterface = TryGetService(dependencyResolver); _globalStyles = TryGetService(dependencyResolver); + _notificationManager = new WindowNotificationManager(this); Renderer = impl.CreateRenderer(this); @@ -328,6 +331,8 @@ namespace Avalonia.Controls IRenderTarget IRenderRoot.CreateRenderTarget() => CreateRenderTarget(); + public WindowNotificationManager NotificationManager => _notificationManager; + /// protected virtual IRenderTarget CreateRenderTarget() {