diff --git a/samples/ControlCatalog/MainWindow.xaml.cs b/samples/ControlCatalog/MainWindow.xaml.cs index c61296ac8f..c589f41442 100644 --- a/samples/ControlCatalog/MainWindow.xaml.cs +++ b/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; } diff --git a/samples/ControlCatalog/Pages/NotificationsPage.xaml.cs b/samples/ControlCatalog/Pages/NotificationsPage.xaml.cs index eadf92b602..bfd49a2c00 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 = new Avalonia.Controls.Notifications.WindowNotificationManager(VisualRoot as TopLevel); + } } } diff --git a/samples/ControlCatalog/ViewModels/MainWindowViewModel.cs b/samples/ControlCatalog/ViewModels/MainWindowViewModel.cs index c2c2462246..b79eff780c 100644 --- a/samples/ControlCatalog/ViewModels/MainWindowViewModel.cs +++ b/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; } 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..45beaa0b2f 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,20 +54,12 @@ 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) + if (host != null) { Install(host); } - else - { - Observable.FromEventPattern(host, nameof(host.TemplateApplied)).Take(1) - .Subscribe(_ => - { - Install(host); - }); - } UpdatePseudoClasses(Position); } @@ -151,7 +142,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..6804c9ecb9 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;