Sfoglia il codice sorgente

Merge pull request #9277 from emmauss/notification

Allow NotifcationManager to be created on demand
Max Katz 3 anni fa
parent
commit
379e2b56e5

+ 1 - 11
samples/ControlCatalog/MainWindow.xaml.cs

@@ -11,23 +11,13 @@ namespace ControlCatalog
 {
     public class MainWindow : Window
     {
-        private WindowNotificationManager _notificationArea;
         private NativeMenu? _recentMenu;
 
         public MainWindow()
         {
             this.InitializeComponent();
 
-            //Renderer.DrawFps = true;
-            //Renderer.DrawDirtyRects = Renderer.DrawFps = true;
-
-            _notificationArea = new WindowNotificationManager(this)
-            {
-                Position = NotificationPosition.TopRight,
-                MaxItems = 3
-            };
-
-            DataContext = new MainWindowViewModel(_notificationArea);
+            DataContext = new MainWindowViewModel();
             _recentMenu = ((NativeMenu.GetMenu(this)?.Items[0] as NativeMenuItem)?.Menu?.Items[2] as NativeMenuItem)?.Menu;
         }
 

+ 15 - 0
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 = new Avalonia.Controls.Notifications.WindowNotificationManager(VisualRoot as TopLevel);
+        }
     }
 }

+ 1 - 31
samples/ControlCatalog/ViewModels/MainWindowViewModel.cs

@@ -24,25 +24,8 @@ namespace ControlCatalog.ViewModels
         private bool _preferSystemChromeEnabled;
         private double _titleBarHeight;
 
-        public MainWindowViewModel(IManagedNotificationManager notificationManager)
+        public MainWindowViewModel()
         {
-            _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; }

+ 26 - 3
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; }
+
     }
 }

+ 4 - 13
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,20 +54,12 @@ namespace Avalonia.Controls.Notifications
         /// Initializes a new instance of the <see cref="WindowNotificationManager"/> class.
         /// </summary>
         /// <param name="host">The window that will host the control.</param>
-        public WindowNotificationManager(Window host)
+        public WindowNotificationManager(TopLevel? host)
         {
-            if (VisualChildren.Count != 0)
+            if (host != null)
             {
                 Install(host);
             }
-            else
-            {
-                Observable.FromEventPattern<TemplateAppliedEventArgs>(host, nameof(host.TemplateApplied)).Take(1)
-                    .Subscribe(_ =>
-                    {
-                        Install(host);
-                    });
-            }
 
             UpdatePseudoClasses(Position);
         }
@@ -151,7 +142,7 @@ namespace Avalonia.Controls.Notifications
         /// of the host <see cref="Window"/>.
         /// </summary>
         /// <param name="host">The <see cref="Window"/> that will be the host.</param>
-        private void Install(Window host)
+        private void Install(TemplatedControl host)
         {
             var adornerLayer = host.FindDescendantOfType<VisualLayerManager>()?.AdornerLayer;
 

+ 1 - 0
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;