Explorar el Código

dont use properties on toplevel

Dan Walmsley hace 6 años
padre
commit
e14cc277d7

+ 1 - 4
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">
     <Window.DataTemplates>
         <DataTemplate DataType="vm:NotificationViewModel">
             <v:NotificationView />

+ 17 - 1
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);
+        }
     }
 }

+ 3 - 1
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?" });

+ 16 - 2
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<NotificationPosition> PositionProperty =
-          AvaloniaProperty.RegisterAttached<NotificationArea, TopLevel, NotificationPosition>("Position", defaultValue: NotificationPosition.TopLeft , inherits: true);
+          AvaloniaProperty.RegisterAttached<NotificationArea, TopLevel, NotificationPosition>("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<AdornerDecorator>()
+                .FirstOrDefault()
+                ?.AdornerLayer;
+
+            if (adornerLayer != null)
+            {
+                adornerLayer.Children.Add(this as IControl);
+            }
+        }
     }
 
     public enum NotificationPosition

+ 0 - 57
src/Avalonia.Controls/TopLevel.cs

@@ -48,26 +48,12 @@ namespace Avalonia.Controls
         public static readonly StyledProperty<IInputElement> PointerOverElementProperty =
             AvaloniaProperty.Register<TopLevel, IInputElement>(nameof(IInputRoot.PointerOverElement));
 
-        /// <summary>
-        /// Defines the <see cref="LocalNotificationManager"/> property.
-        /// </summary>
-        public static readonly DirectProperty<TopLevel, NotificationArea> LocalNotificationManagerProperty =
-            AvaloniaProperty.RegisterDirect<TopLevel, NotificationArea>(nameof(LocalNotificationManager), o => o.LocalNotificationManager, (o, v) => o.LocalNotificationManager = v);
-
-        /// <summary>
-        /// Defines the <see cref="SystemNotificationManager"/> property.
-        /// </summary>
-        public static readonly DirectProperty<TopLevel, INotificationManager> SystemNotificationManagerProperty =
-            AvaloniaProperty.RegisterDirect<TopLevel, INotificationManager>(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;
 
         /// <summary>
@@ -104,8 +90,6 @@ namespace Avalonia.Controls
 
             PlatformImpl = impl;
 
-            LocalNotificationManager = new NotificationArea();
-
             dependencyResolver = dependencyResolver ?? AvaloniaLocator.Current;
             var styler = TryGetService<IStyler>(dependencyResolver);
 
@@ -115,13 +99,6 @@ namespace Avalonia.Controls
             _applicationLifecycle = TryGetService<IApplicationLifecycle>(dependencyResolver);
             _renderInterface = TryGetService<IPlatformRenderInterface>(dependencyResolver);
 
-            _systemNotificationManager = TryGetService<INotificationManager>(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); }
         }
 
-        /// <summary>
-        /// Gets or sets the Local (in window managed) notification manager.
-        /// </summary>
-        public NotificationArea LocalNotificationManager
-        {
-            get => _localNotificationManager;
-            protected set => SetAndRaise(LocalNotificationManagerProperty, ref _localNotificationManager, value);
-        }
-
-        /// <summary>
-        /// Gets or sets the System (native os) notification manager. If the system doesnt natively support notifications then this property
-        /// will match the <see cref="LocalNotificationManager"/>.
-        /// </summary>
-        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<AdornerDecorator>()
-                .FirstOrDefault()
-                ?.AdornerLayer;
-
-            if (adornerLayer != null)
-            {
-                adornerLayer.Children.Add(LocalNotificationManager as IControl);
-            }
-
-            base.OnTemplateApplied(e);
-        }
     }
 }