diff --git a/samples/ControlCatalog/MainWindow.xaml b/samples/ControlCatalog/MainWindow.xaml index 19908624cf..49d0ee72ff 100644 --- a/samples/ControlCatalog/MainWindow.xaml +++ b/samples/ControlCatalog/MainWindow.xaml @@ -5,10 +5,7 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vm="clr-namespace:ControlCatalog.ViewModels" xmlns:v="clr-namespace:ControlCatalog.Views" - x:Class="ControlCatalog.MainWindow" - LocalNotificationManager="{Binding NotificationManager, Mode=OneWayToSource}" - NotificationArea.Position="TopRight" - NotificationArea.MaxItems="3"> + x:Class="ControlCatalog.MainWindow"> diff --git a/samples/ControlCatalog/MainWindow.xaml.cs b/samples/ControlCatalog/MainWindow.xaml.cs index 02cc692949..1d54378f3a 100644 --- a/samples/ControlCatalog/MainWindow.xaml.cs +++ b/samples/ControlCatalog/MainWindow.xaml.cs @@ -1,6 +1,7 @@ using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Notifications; +using Avalonia.Controls.Primitives; using Avalonia.Markup.Xaml; using Avalonia.Threading; using ControlCatalog.ViewModels; @@ -11,6 +12,8 @@ namespace ControlCatalog { public class MainWindow : Window { + private NotificationArea _notificationArea; + public MainWindow() { this.InitializeComponent(); @@ -18,7 +21,13 @@ namespace ControlCatalog //Renderer.DrawFps = true; //Renderer.DrawDirtyRects = Renderer.DrawFps = true; - DataContext = new MainWindowViewModel(); + _notificationArea = new NotificationArea + { + Position = NotificationPosition.TopRight, + MaxItems = 3 + }; + + DataContext = new MainWindowViewModel(_notificationArea); } private void InitializeComponent() @@ -30,5 +39,12 @@ namespace ControlCatalog theme.TryGetResource("Button", out _); AvaloniaXamlLoader.Load(this); } + + protected override void OnTemplateApplied(TemplateAppliedEventArgs e) + { + base.OnTemplateApplied(e); + + _notificationArea.Install(this); + } } } diff --git a/samples/ControlCatalog/ViewModels/MainWindowViewModel.cs b/samples/ControlCatalog/ViewModels/MainWindowViewModel.cs index 4623149a88..5ba969345c 100644 --- a/samples/ControlCatalog/ViewModels/MainWindowViewModel.cs +++ b/samples/ControlCatalog/ViewModels/MainWindowViewModel.cs @@ -9,8 +9,10 @@ namespace ControlCatalog.ViewModels { class MainWindowViewModel : ViewModelBase { - public MainWindowViewModel() + public MainWindowViewModel(INotificationManager notificationManager) { + _notificationManager = notificationManager; + ShowCustomManagedNotificationCommand = ReactiveCommand.Create(() => { NotificationManager.Show(new NotificationViewModel(NotificationManager) { Title = "Hey There!", Message = "Did you know that Avalonia now supports Custom In-Window Notifications?" }); diff --git a/src/Avalonia.Controls/Notifications/NotificationArea.cs b/src/Avalonia.Controls/Notifications/NotificationArea.cs index 3fe9a4a190..01655915e3 100644 --- a/src/Avalonia.Controls/Notifications/NotificationArea.cs +++ b/src/Avalonia.Controls/Notifications/NotificationArea.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Threading.Tasks; using Avalonia.Controls.Primitives; using Avalonia.Interactivity; +using Avalonia.VisualTree; namespace Avalonia.Controls.Notifications { @@ -22,7 +23,7 @@ namespace Avalonia.Controls.Notifications } public static readonly AvaloniaProperty PositionProperty = - AvaloniaProperty.RegisterAttached("Position", defaultValue: NotificationPosition.TopLeft , inherits: true); + AvaloniaProperty.RegisterAttached("Position", defaultValue: NotificationPosition.TopLeft, inherits: true); public NotificationPosition Position { @@ -70,7 +71,7 @@ namespace Avalonia.Controls.Notifications public void Show(NotificationContent content, TimeSpan? expirationTime, Action onClick, Action onClose) { - Show(content as object, expirationTime, onClick, onClose); + Show(content as object, expirationTime, onClick, onClose); } public async void Show(object content, TimeSpan? expirationTime, Action onClick, Action onClose) @@ -116,6 +117,19 @@ namespace Avalonia.Controls.Notifications var notification = sender as Notification; _items.Remove(notification); } + + public void Install(TopLevel host) + { + var adornerLayer = host.GetVisualDescendants() + .OfType() + .FirstOrDefault() + ?.AdornerLayer; + + if (adornerLayer != null) + { + adornerLayer.Children.Add(this as IControl); + } + } } public enum NotificationPosition diff --git a/src/Avalonia.Controls/TopLevel.cs b/src/Avalonia.Controls/TopLevel.cs index 7038237dcb..b08d55ff18 100644 --- a/src/Avalonia.Controls/TopLevel.cs +++ b/src/Avalonia.Controls/TopLevel.cs @@ -48,26 +48,12 @@ namespace Avalonia.Controls public static readonly StyledProperty PointerOverElementProperty = AvaloniaProperty.Register(nameof(IInputRoot.PointerOverElement)); - /// - /// Defines the property. - /// - public static readonly DirectProperty LocalNotificationManagerProperty = - AvaloniaProperty.RegisterDirect(nameof(LocalNotificationManager), o => o.LocalNotificationManager, (o, v) => o.LocalNotificationManager = v); - - /// - /// Defines the property. - /// - public static readonly DirectProperty SystemNotificationManagerProperty = - AvaloniaProperty.RegisterDirect(nameof(SystemNotificationManager), o => o.SystemNotificationManager, (o, v) => o.SystemNotificationManager = v); - private readonly IInputManager _inputManager; private readonly IAccessKeyHandler _accessKeyHandler; private readonly IKeyboardNavigationHandler _keyboardNavigationHandler; private readonly IApplicationLifecycle _applicationLifecycle; private readonly IPlatformRenderInterface _renderInterface; private Size _clientSize; - private NotificationArea _localNotificationManager; - private INotificationManager _systemNotificationManager; private ILayoutManager _layoutManager; /// @@ -104,8 +90,6 @@ namespace Avalonia.Controls PlatformImpl = impl; - LocalNotificationManager = new NotificationArea(); - dependencyResolver = dependencyResolver ?? AvaloniaLocator.Current; var styler = TryGetService(dependencyResolver); @@ -115,13 +99,6 @@ namespace Avalonia.Controls _applicationLifecycle = TryGetService(dependencyResolver); _renderInterface = TryGetService(dependencyResolver); - _systemNotificationManager = TryGetService(dependencyResolver); - - if(_systemNotificationManager == null) - { - SystemNotificationManager = _localNotificationManager; - } - Renderer = impl.CreateRenderer(this); if (Renderer != null) @@ -181,25 +158,6 @@ namespace Avalonia.Controls protected set { SetAndRaise(ClientSizeProperty, ref _clientSize, value); } } - /// - /// Gets or sets the Local (in window managed) notification manager. - /// - public NotificationArea LocalNotificationManager - { - get => _localNotificationManager; - protected set => SetAndRaise(LocalNotificationManagerProperty, ref _localNotificationManager, value); - } - - /// - /// Gets or sets the System (native os) notification manager. If the system doesnt natively support notifications then this property - /// will match the . - /// - public INotificationManager SystemNotificationManager - { - get => _systemNotificationManager; - protected set => SetAndRaise(SystemNotificationManagerProperty, ref _systemNotificationManager, value); - } - public ILayoutManager LayoutManager { get @@ -415,20 +373,5 @@ 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); - } } }